Repository: grpc/grpc-go
File: stats/opentelemetry/opentelemetry.go (Go)

Code before:
	return ret
}

func optionFromLabels(labelKeys []string, optionalLabelKeys []string, optionalLabels []string, labelVals ...string) otelmetric.MeasurementOption {
	var attributes []otelattribute.KeyValue

	// Once it hits here lower level has guaranteed length of labelVals matches
	// labelKeys + optionalLabelKeys.
	for i, label := range labelKeys {
		attributes = append(attributes, otelattribute.String(label, labelVals[i]))
	}

	for i, label := range optionalLabelKeys {
		for _, optLabel := range optionalLabels { // o(n) could build out a set but n is currently capped at < 5
			if label == optLabel {
				attributes = append(attributes, otelattribute.String(label, labelVals[i+len(labelKeys)]))
			}
		}
	}
	return otelmetric.WithAttributeSet(otelattribute.NewSet(attributes...))
}

// registryMetrics implements MetricsRecorder for the client and server stats
// handlers.
type registryMetrics struct {
	estats.UnimplementedMetricsRecorder
	intCounts       map[*estats.MetricDescriptor]otelmetric.Int64Counter
	floatCounts     map[*estats.MetricDescriptor]otelmetric.Float64Counter
	intHistos       map[*estats.MetricDescriptor]otelmetric.Int64Histogram
	floatHistos     map[*estats.MetricDescriptor]otelmetric.Float64Histogram
	intGauges       map[*estats.MetricDescriptor]otelmetric.Int64Gauge
	intUpDownCounts map[*estats.MetricDescriptor]otelmetric.Int64UpDownCounter

	// Asynchronous (Observable) Instruments
	intObservableGauges map[*estats.MetricDescriptor]otelmetric.Int64ObservableGauge

	meter          otelmetric.Meter
	optionalLabels []string
}

func (rm *registryMetrics) registerMetrics(metrics *stats.MetricSet, meter otelmetric.Meter) {
	rm.meter = meter
	rm.intCounts = make(map[*estats.MetricDescriptor]otelmetric.Int64Counter)
	rm.floatCounts = make(map[*estats.MetricDescriptor]otelmetric.Float64Counter)
	rm.intHistos = make(map[*estats.MetricDescriptor]otelmetric.Int64Histogram)
	rm.floatHistos = make(map[*estats.MetricDescriptor]otelmetric.Float64Histogram)
	rm.intGauges = make(map[*estats.MetricDescriptor]otelmetric.Int64Gauge)
	rm.intUpDownCounts = make(map[*estats.MetricDescriptor]otelmetric.Int64UpDownCounter)
	rm.intObservableGauges = make(map[*estats.MetricDescriptor]otelmetric.Int64ObservableGauge)
	for metric := range metrics.Metrics() {
		desc := estats.DescriptorForMetric(metric)

Review comment (suggestion):
I think we should avoid embedding the `UnimplementedMetricsRecorder` in non-test implementations and let the build break. This would help us avoid missing recorder implementations to update when adding new methods.

Code after:
	return ret
}

func optionFromLabels(labelKeys []string, optionalLabelKeys []string, optionalLabels []string, labelVals ...string) otelmetric.MeasurementOption {
	var attributes []otelattribute.KeyValue

	// Once it hits here lower level has guaranteed length of labelVals matches
	// labelKeys + optionalLabelKeys.
	for i, label := range labelKeys {
		attributes = append(attributes, otelattribute.String(label, labelVals[i]))
	}

	for i, label := range optionalLabelKeys {
		for _, optLabel := range optionalLabels { // o(n) could build out a set but n is currently capped at < 5
			if label == optLabel {
				attributes = append(attributes, otelattribute.String(label, labelVals[i+len(labelKeys)]))
			}
		}
	}
	return otelmetric.WithAttributeSet(otelattribute.NewSet(attributes...))
}

// registryMetrics implements MetricsRecorder for the client and server stats
// handlers.
type registryMetrics struct {
	internal.EnforceMetricsRecorderEmbedding
	intCounts       map[*estats.MetricDescriptor]otelmetric.Int64Counter
	floatCounts     map[*estats.MetricDescriptor]otelmetric.Float64Counter
	intHistos       map[*estats.MetricDescriptor]otelmetric.Int64Histogram
	floatHistos     map[*estats.MetricDescriptor]otelmetric.Float64Histogram
	intGauges       map[*estats.MetricDescriptor]otelmetric.Int64Gauge
	intUpDownCounts map[*estats.MetricDescriptor]otelmetric.Int64UpDownCounter

	// Asynchronous (Observable) Instruments
	intObservableGauges map[*estats.MetricDescriptor]otelmetric.Int64ObservableGauge

	meter          otelmetric.Meter
	optionalLabels []string
}

func (rm *registryMetrics) registerMetrics(metrics *stats.MetricSet, meter otelmetric.Meter) {
	rm.meter = meter
	rm.intCounts = make(map[*estats.MetricDescriptor]otelmetric.Int64Counter)
	rm.floatCounts = make(map[*estats.MetricDescriptor]otelmetric.Float64Counter)
	rm.intHistos = make(map[*estats.MetricDescriptor]otelmetric.Int64Histogram)
	rm.floatHistos = make(map[*estats.MetricDescriptor]otelmetric.Float64Histogram)
	rm.intGauges = make(map[*estats.MetricDescriptor]otelmetric.Int64Gauge)
	rm.intUpDownCounts = make(map[*estats.MetricDescriptor]otelmetric.Int64UpDownCounter)
	rm.intObservableGauges = make(map[*estats.MetricDescriptor]otelmetric.Int64ObservableGauge)

	for metric := range metrics.Metrics() {