/*
 *
 * Copyright 2024 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

// Package stats provides internal stats related functionality.
package stats import ( ) // LabelCallback is a function that is executed when telemetry // label keys are updated. type LabelCallback func(map[string]string) type telemetryLabelCallbackKey struct{} // UpdateLabels executes registered telemetry callbacks with the update labels. Labels // are copied before being processed by any callbacks to ensure mutations are not // shared among derived contexts. // // It is the responsibility of the registrant to handle conflicts or label resets. func ( context.Context, map[string]string) { executeTelemetryLabelCallbacks(, ) } // RegisterTelemetryLabelCallback registers a callback function that is executed whenever // telemetry labels are updated. func ( context.Context, LabelCallback) context.Context { if == nil { return } , := .Value(telemetryLabelCallbackKey{}).([]LabelCallback) if ! { return context.WithValue(, telemetryLabelCallbackKey{}, []LabelCallback{}) } return context.WithValue(, telemetryLabelCallbackKey{}, append(append([]LabelCallback(nil), ...), )) } // executeTelemetryLabelCallback runs the registered callbacks in the order they were // registered on the context with the provided labels. If no callbacks are registered // it does nothing. // // To ensure callbacks do not mutate the state of the provided label map it is copied // before execution. func ( context.Context, map[string]string) { , := .Value(telemetryLabelCallbackKey{}).([]LabelCallback) if ! { return } := map[string]string{} maps.Copy(, ) for , := range { () } }