@@ -25,11 +25,14 @@ import (
2525 sdkmetric "go.opentelemetry.io/otel/sdk/metric"
2626 "go.opentelemetry.io/otel/sdk/metric/metricdata"
2727 "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
28+ "go.opentelemetry.io/otel/sdk/trace/internal/observ"
2829 semconv "go.opentelemetry.io/otel/semconv/v1.37.0"
2930 "go.opentelemetry.io/otel/semconv/v1.37.0/otelconv"
3031 "go.opentelemetry.io/otel/trace"
3132)
3233
34+ const componentID = 0
35+
3336type testBatchExporter struct {
3437 mu sync.Mutex
3538 spans []ReadOnlySpan
@@ -693,6 +696,9 @@ func TestBatchSpanProcessorMetricsDisabled(t *testing.T) {
693696}
694697
695698func TestBatchSpanProcessorMetrics (t * testing.T ) {
699+ // Reset for deterministic component ID.
700+ processorIDCounter .Store (componentID )
701+
696702 t .Setenv ("OTEL_GO_X_OBSERVABILITY" , "true" )
697703 tp := basicTracerProvider (t )
698704 reader := sdkmetric .NewManualReader ()
@@ -710,7 +716,6 @@ func TestBatchSpanProcessorMetrics(t *testing.T) {
710716 WithMaxQueueSize (2 ),
711717 WithMaxExportBatchSize (2 ),
712718 )
713- internalBsp := bsp .(* batchSpanProcessor )
714719 tp .RegisterSpanProcessor (bsp )
715720
716721 tr := tp .Tracer ("TestBatchSpanProcessorMetrics" )
@@ -719,15 +724,25 @@ func TestBatchSpanProcessorMetrics(t *testing.T) {
719724 ctx , cancel := context .WithTimeout (context .Background (), time .Second )
720725 defer cancel ()
721726 assert .NoError (t , me .waitForSpans (ctx , 2 ))
722- assertObsScopeMetrics (t , internalBsp .componentNameAttr , reader ,
723- expectMetrics {queueCapacity : 2 , queueSize : 0 , successProcessed : 2 })
727+ assertObsScopeMetrics (t , reader , expectMetrics {
728+ queueCapacity : 2 ,
729+ queueSize : 0 ,
730+ successProcessed : 2 ,
731+ })
724732 // Generate 3 spans. 2 fill the queue, and 1 is dropped because the queue is full.
725733 generateSpan (t , tr , testOption {genNumSpans : 3 })
726- assertObsScopeMetrics (t , internalBsp .componentNameAttr , reader ,
727- expectMetrics {queueCapacity : 2 , queueSize : 2 , queueFullProcessed : 1 , successProcessed : 2 })
734+ assertObsScopeMetrics (t , reader , expectMetrics {
735+ queueCapacity : 2 ,
736+ queueSize : 2 ,
737+ queueFullProcessed : 1 ,
738+ successProcessed : 2 ,
739+ })
728740}
729741
730742func TestBatchSpanProcessorBlockingMetrics (t * testing.T ) {
743+ // Reset for deterministic component ID.
744+ processorIDCounter .Store (componentID )
745+
731746 t .Setenv ("OTEL_GO_X_OBSERVABILITY" , "true" )
732747 tp := basicTracerProvider (t )
733748 reader := sdkmetric .NewManualReader ()
@@ -747,7 +762,6 @@ func TestBatchSpanProcessorBlockingMetrics(t *testing.T) {
747762 WithMaxQueueSize (2 ),
748763 WithMaxExportBatchSize (2 ),
749764 )
750- internalBsp := bsp .(* batchSpanProcessor )
751765 tp .RegisterSpanProcessor (bsp )
752766
753767 tr := tp .Tracer ("TestBatchSpanProcessorBlockingMetrics" )
@@ -756,23 +770,33 @@ func TestBatchSpanProcessorBlockingMetrics(t *testing.T) {
756770 ctx , cancel := context .WithTimeout (context .Background (), time .Second )
757771 defer cancel ()
758772 assert .NoError (t , me .waitForSpans (ctx , 2 ))
759- assertObsScopeMetrics (t , internalBsp .componentNameAttr , reader ,
760- expectMetrics {queueCapacity : 2 , queueSize : 0 , successProcessed : 2 })
773+ assertObsScopeMetrics (t , reader , expectMetrics {
774+ queueCapacity : 2 ,
775+ queueSize : 0 ,
776+ successProcessed : 2 ,
777+ })
761778 // Generate 2 spans to fill the queue.
762779 generateSpan (t , tr , testOption {genNumSpans : 2 })
763780 go func () {
764781 // Generate a span which blocks because the queue is full.
765782 generateSpan (t , tr , testOption {genNumSpans : 1 })
766783 }()
767- assertObsScopeMetrics (t , internalBsp .componentNameAttr , reader ,
768- expectMetrics {queueCapacity : 2 , queueSize : 2 , successProcessed : 2 })
784+ assertObsScopeMetrics (t , reader , expectMetrics {
785+ queueCapacity : 2 ,
786+ queueSize : 2 ,
787+ successProcessed : 2 ,
788+ })
769789
770790 // Use ForceFlush to force the span that is blocking on the full queue to be dropped.
771791 ctx , cancel = context .WithTimeout (context .Background (), 10 * time .Millisecond )
772792 defer cancel ()
773793 assert .Error (t , tp .ForceFlush (ctx ))
774- assertObsScopeMetrics (t , internalBsp .componentNameAttr , reader ,
775- expectMetrics {queueCapacity : 2 , queueSize : 2 , queueFullProcessed : 1 , successProcessed : 2 })
794+ assertObsScopeMetrics (t , reader , expectMetrics {
795+ queueCapacity : 2 ,
796+ queueSize : 2 ,
797+ queueFullProcessed : 1 ,
798+ successProcessed : 2 ,
799+ })
776800}
777801
778802type expectMetrics struct {
@@ -782,13 +806,16 @@ type expectMetrics struct {
782806 queueFullProcessed int64
783807}
784808
785- func assertObsScopeMetrics (t * testing.T , componentNameAttr attribute.KeyValue , reader sdkmetric.Reader ,
809+ func assertObsScopeMetrics (
810+ t * testing.T ,
811+ reader sdkmetric.Reader ,
786812 expectation expectMetrics ,
787813) {
788814 t .Helper ()
789815 gotResourceMetrics := new (metricdata.ResourceMetrics )
790816 assert .NoError (t , reader .Collect (context .Background (), gotResourceMetrics ))
791817
818+ componentNameAttr := observ .BSPComponentName (componentID )
792819 baseAttrs := attribute .NewSet (
793820 semconv .OTelComponentTypeBatchingSpanProcessor ,
794821 componentNameAttr ,
@@ -832,7 +859,7 @@ func assertObsScopeMetrics(t *testing.T, componentNameAttr attribute.KeyValue, r
832859 Attributes : attribute .NewSet (
833860 semconv .OTelComponentTypeBatchingSpanProcessor ,
834861 componentNameAttr ,
835- semconv . ErrorTypeKey . String ( string ( queueFull )) ,
862+ observ . ErrQueueFull ,
836863 ),
837864 })
838865 }
@@ -854,9 +881,9 @@ func assertObsScopeMetrics(t *testing.T, componentNameAttr attribute.KeyValue, r
854881
855882 wantScopeMetric := metricdata.ScopeMetrics {
856883 Scope : instrumentation.Scope {
857- Name : "go.opentelemetry.io/otel/sdk/trace" ,
884+ Name : observ . ScopeName ,
858885 Version : sdk .Version (),
859- SchemaURL : semconv .SchemaURL ,
886+ SchemaURL : observ .SchemaURL ,
860887 },
861888 Metrics : wantMetrics ,
862889 }
0 commit comments