@@ -66,6 +66,7 @@ const (
6666 // Metric units
6767 metricUnitMS = "ms"
6868 metricUnitCount = "1"
69+ maxAttrsLen = 12 // Monitored resource labels + Metric labels
6970)
7071
7172// These are effectively constant, but for testing purposes they are mutable
@@ -402,9 +403,10 @@ func (tf *builtinMetricsTracerFactory) createBuiltinMetricsTracer(ctx context.Co
402403// - converts metric attributes values captured throughout the operation / attempt
403404// to OpenTelemetry attributes format,
404405// - combines these with common client attributes and returns
405- func (mt * builtinMetricsTracer ) toOtelMetricAttrs (metricName string ) ([]attribute.KeyValue , error ) {
406+ func (mt * builtinMetricsTracer ) toOtelMetricAttrs (metricName string ) (attribute.Set , error ) {
407+ attrKeyValues := make ([]attribute.KeyValue , 0 , maxAttrsLen )
406408 // Create attribute key value pairs for attributes common to all metricss
407- attrKeyValues := []attribute. KeyValue {
409+ attrKeyValues = append ( attrKeyValues ,
408410 attribute .String (metricLabelKeyMethod , mt .method ),
409411
410412 // Add resource labels to otel metric labels.
@@ -416,13 +418,13 @@ func (mt *builtinMetricsTracer) toOtelMetricAttrs(metricName string) ([]attribut
416418 // use last attempt's cluster and zone
417419 attribute .String (monitoredResLabelKeyCluster , mt .currOp .currAttempt .clusterID ),
418420 attribute .String (monitoredResLabelKeyZone , mt .currOp .currAttempt .zoneID ),
419- }
421+ )
420422 attrKeyValues = append (attrKeyValues , mt .clientAttributes ... )
421423
422424 // Get metric details
423425 mDetails , found := metricsDetails [metricName ]
424426 if ! found {
425- return attrKeyValues , fmt .Errorf ("unable to create attributes list for unknown metric: %v" , metricName )
427+ return attribute. Set {} , fmt .Errorf ("unable to create attributes list for unknown metric: %v" , metricName )
426428 }
427429
428430 status := mt .currOp .status
@@ -438,9 +440,10 @@ func (mt *builtinMetricsTracer) toOtelMetricAttrs(metricName string) ([]attribut
438440 case metricLabelKeyStreamingOperation :
439441 attrKeyValues = append (attrKeyValues , attribute .Bool (metricLabelKeyStreamingOperation , mt .isStreaming ))
440442 default :
441- return attrKeyValues , fmt .Errorf ("unknown additional attribute: %v" , attrKey )
443+ return attribute. Set {} , fmt .Errorf ("unknown additional attribute: %v" , attrKey )
442444 }
443445 }
444446
445- return attrKeyValues , nil
447+ attrSet := attribute .NewSet (attrKeyValues ... )
448+ return attrSet , nil
446449}
0 commit comments