From 3e17ef99d67f43ac6f344d45a3dbfc604fa8d4b2 Mon Sep 17 00:00:00 2001 From: Damien Mathieu <42@dmathieu.com> Date: Wed, 24 Jul 2024 09:33:07 +0200 Subject: [PATCH] Allow relying on InstrumentationScope in SpanStub and fix remaining deprecation issues (#5627) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rather than the deprecated InstrumentationLibrary This is a replacement for https://github.com/open-telemetry/opentelemetry-go/pull/3104, as after this, there is no usage of `instrumentation.Library` within the SDK anymore. --------- Co-authored-by: Robert PajÄ…k Co-authored-by: Sam Xie --- CHANGELOG.md | 1 + bridge/opencensus/trace_test.go | 4 +- .../internal/tracetransform/span_test.go | 6 +-- .../internal/otlptracetest/data.go | 2 +- .../internal/otlptracetest/data.go | 2 +- exporters/stdout/stdouttrace/trace_test.go | 5 +++ exporters/zipkin/model_test.go | 6 +-- .../otlp/otlptrace/otlptracetest/data.go.tmpl | 2 +- sdk/instrumentation/library.go | 3 +- sdk/trace/snapshot.go | 2 +- sdk/trace/span.go | 4 +- sdk/trace/tracetest/span.go | 45 +++++++++++-------- 12 files changed, 49 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64357fd3148..a9dd5037653 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm This new module contains an OTLP exporter that transmits log telemetry using gRPC. This module is unstable and breaking changes may be introduced. See our [versioning policy](VERSIONING.md) for more information about these stability guarantees. (#5629) +- Add `InstrumentationScope` field to `SpanStub` in `go.opentelemetry.io/otel/sdk/trace/tracetest`, as a replacement for the deprecated `InstrumentationLibrary`. (#5627) ### Fixed diff --git a/bridge/opencensus/trace_test.go b/bridge/opencensus/trace_test.go index d00fff5414f..de507442adc 100644 --- a/bridge/opencensus/trace_test.go +++ b/bridge/opencensus/trace_test.go @@ -23,6 +23,6 @@ func TestNewTraceBridge(t *testing.T) { gotSpans := exporter.GetSpans() require.Len(t, gotSpans, 1) gotSpan := gotSpans[0] - assert.Equal(t, gotSpan.InstrumentationLibrary.Name, scopeName) - assert.Equal(t, gotSpan.InstrumentationLibrary.Version, Version()) + assert.Equal(t, gotSpan.InstrumentationScope.Name, scopeName) + assert.Equal(t, gotSpan.InstrumentationScope.Version, Version()) } diff --git a/exporters/otlp/otlptrace/internal/tracetransform/span_test.go b/exporters/otlp/otlptrace/internal/tracetransform/span_test.go index 152ef383ad7..50cb9e123df 100644 --- a/exporters/otlp/otlptrace/internal/tracetransform/span_test.go +++ b/exporters/otlp/otlptrace/internal/tracetransform/span_test.go @@ -280,7 +280,7 @@ func TestSpanData(t *testing.T) { attribute.Int64("rk2", 5), attribute.StringSlice("rk3", []string{"sv1", "sv2"}), ), - InstrumentationLibrary: instrumentation.Scope{ + InstrumentationScope: instrumentation.Scope{ Name: "go.opentelemetry.io/test/otel", Version: "v0.0.1", SchemaURL: semconv.SchemaURL, @@ -316,8 +316,8 @@ func TestSpanData(t *testing.T) { assert.Equal(t, got[0].SchemaUrl, spanData.Resource.SchemaURL()) scopeSpans := got[0].GetScopeSpans() require.Len(t, scopeSpans, 1) - assert.Equal(t, scopeSpans[0].SchemaUrl, spanData.InstrumentationLibrary.SchemaURL) - assert.Equal(t, scopeSpans[0].GetScope(), InstrumentationScope(spanData.InstrumentationLibrary)) + assert.Equal(t, scopeSpans[0].SchemaUrl, spanData.InstrumentationScope.SchemaURL) + assert.Equal(t, scopeSpans[0].GetScope(), InstrumentationScope(spanData.InstrumentationScope)) require.Len(t, scopeSpans[0].Spans, 1) actualSpan := scopeSpans[0].Spans[0] diff --git a/exporters/otlp/otlptrace/otlptracegrpc/internal/otlptracetest/data.go b/exporters/otlp/otlptrace/otlptracegrpc/internal/otlptracetest/data.go index 75837732cc9..59f7b78e62b 100644 --- a/exporters/otlp/otlptrace/otlptracegrpc/internal/otlptracetest/data.go +++ b/exporters/otlp/otlptrace/otlptracegrpc/internal/otlptracetest/data.go @@ -46,7 +46,7 @@ func SingleReadOnlySpan() []tracesdk.ReadOnlySpan { DroppedLinks: 0, ChildSpanCount: 0, Resource: resource.NewSchemaless(attribute.String("a", "b")), - InstrumentationLibrary: instrumentation.Library{ + InstrumentationScope: instrumentation.Scope{ Name: "bar", Version: "0.0.0", }, diff --git a/exporters/otlp/otlptrace/otlptracehttp/internal/otlptracetest/data.go b/exporters/otlp/otlptrace/otlptracehttp/internal/otlptracetest/data.go index dd296c75c0e..e4154f82d00 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/internal/otlptracetest/data.go +++ b/exporters/otlp/otlptrace/otlptracehttp/internal/otlptracetest/data.go @@ -46,7 +46,7 @@ func SingleReadOnlySpan() []tracesdk.ReadOnlySpan { DroppedLinks: 0, ChildSpanCount: 0, Resource: resource.NewSchemaless(attribute.String("a", "b")), - InstrumentationLibrary: instrumentation.Library{ + InstrumentationScope: instrumentation.Scope{ Name: "bar", Version: "0.0.0", }, diff --git a/exporters/stdout/stdouttrace/trace_test.go b/exporters/stdout/stdouttrace/trace_test.go index a1fc44e559d..d598ca4cd85 100644 --- a/exporters/stdout/stdouttrace/trace_test.go +++ b/exporters/stdout/stdouttrace/trace_test.go @@ -186,6 +186,11 @@ func expectedJSON(now time.Time) string { } } ], + "InstrumentationScope": { + "Name": "", + "Version": "", + "SchemaURL": "" + }, "InstrumentationLibrary": { "Name": "", "Version": "", diff --git a/exporters/zipkin/model_test.go b/exporters/zipkin/model_test.go index 90d38c302be..dc00f9a6791 100644 --- a/exporters/zipkin/model_test.go +++ b/exporters/zipkin/model_test.go @@ -1015,7 +1015,7 @@ func TestTagsTransformation(t *testing.T) { { name: "instrLib-empty", data: tracetest.SpanStub{ - InstrumentationLibrary: instrumentation.Library{}, + InstrumentationScope: instrumentation.Scope{}, }, want: nil, }, @@ -1023,7 +1023,7 @@ func TestTagsTransformation(t *testing.T) { name: "instrLib-noversion", data: tracetest.SpanStub{ Attributes: []attribute.KeyValue{}, - InstrumentationLibrary: instrumentation.Library{ + InstrumentationScope: instrumentation.Scope{ Name: instrLibName, }, }, @@ -1035,7 +1035,7 @@ func TestTagsTransformation(t *testing.T) { name: "instrLib-with-version", data: tracetest.SpanStub{ Attributes: []attribute.KeyValue{}, - InstrumentationLibrary: instrumentation.Library{ + InstrumentationScope: instrumentation.Scope{ Name: instrLibName, Version: instrLibVersion, }, diff --git a/internal/shared/otlp/otlptrace/otlptracetest/data.go.tmpl b/internal/shared/otlp/otlptrace/otlptracetest/data.go.tmpl index f9b020b71cc..9efc6038b0e 100644 --- a/internal/shared/otlp/otlptrace/otlptracetest/data.go.tmpl +++ b/internal/shared/otlp/otlptrace/otlptracetest/data.go.tmpl @@ -46,7 +46,7 @@ func SingleReadOnlySpan() []tracesdk.ReadOnlySpan { DroppedLinks: 0, ChildSpanCount: 0, Resource: resource.NewSchemaless(attribute.String("a", "b")), - InstrumentationLibrary: instrumentation.Library{ + InstrumentationScope: instrumentation.Scope{ Name: "bar", Version: "0.0.0", }, diff --git a/sdk/instrumentation/library.go b/sdk/instrumentation/library.go index f4d1857c4f4..f2cdf3c6518 100644 --- a/sdk/instrumentation/library.go +++ b/sdk/instrumentation/library.go @@ -4,5 +4,6 @@ package instrumentation // import "go.opentelemetry.io/otel/sdk/instrumentation" // Library represents the instrumentation library. -// Deprecated: please use Scope instead. +// +// Deprecated: use [Scope] instead. type Library = Scope diff --git a/sdk/trace/snapshot.go b/sdk/trace/snapshot.go index 32f862790c7..d511d0f271f 100644 --- a/sdk/trace/snapshot.go +++ b/sdk/trace/snapshot.go @@ -99,7 +99,7 @@ func (s snapshot) InstrumentationScope() instrumentation.Scope { // InstrumentationLibrary returns information about the instrumentation // library that created the span. -func (s snapshot) InstrumentationLibrary() instrumentation.Library { +func (s snapshot) InstrumentationLibrary() instrumentation.Library { //nolint:staticcheck // This method needs to be define for backwards compatibility return s.instrumentationScope } diff --git a/sdk/trace/span.go b/sdk/trace/span.go index ac90f1a2600..4945f508303 100644 --- a/sdk/trace/span.go +++ b/sdk/trace/span.go @@ -62,7 +62,7 @@ type ReadOnlySpan interface { // InstrumentationLibrary returns information about the instrumentation // library that created the span. // Deprecated: please use InstrumentationScope instead. - InstrumentationLibrary() instrumentation.Library + InstrumentationLibrary() instrumentation.Library //nolint:staticcheck // This method needs to be define for backwards compatibility // Resource returns information about the entity that produced the span. Resource() *resource.Resource // DroppedAttributes returns the number of attributes dropped by the span @@ -642,7 +642,7 @@ func (s *recordingSpan) InstrumentationScope() instrumentation.Scope { // InstrumentationLibrary returns the instrumentation.Library associated with // the Tracer that created this span. -func (s *recordingSpan) InstrumentationLibrary() instrumentation.Library { +func (s *recordingSpan) InstrumentationLibrary() instrumentation.Library { //nolint:staticcheck // This method needs to be define for backwards compatibility s.mu.Lock() defer s.mu.Unlock() return s.tracer.instrumentationScope diff --git a/sdk/trace/tracetest/span.go b/sdk/trace/tracetest/span.go index 0a641f94889..cd2cc30ca2d 100644 --- a/sdk/trace/tracetest/span.go +++ b/sdk/trace/tracetest/span.go @@ -45,22 +45,25 @@ func (s SpanStubs) Snapshots() []tracesdk.ReadOnlySpan { // SpanStub is a stand-in for a Span. type SpanStub struct { - Name string - SpanContext trace.SpanContext - Parent trace.SpanContext - SpanKind trace.SpanKind - StartTime time.Time - EndTime time.Time - Attributes []attribute.KeyValue - Events []tracesdk.Event - Links []tracesdk.Link - Status tracesdk.Status - DroppedAttributes int - DroppedEvents int - DroppedLinks int - ChildSpanCount int - Resource *resource.Resource - InstrumentationLibrary instrumentation.Library + Name string + SpanContext trace.SpanContext + Parent trace.SpanContext + SpanKind trace.SpanKind + StartTime time.Time + EndTime time.Time + Attributes []attribute.KeyValue + Events []tracesdk.Event + Links []tracesdk.Link + Status tracesdk.Status + DroppedAttributes int + DroppedEvents int + DroppedLinks int + ChildSpanCount int + Resource *resource.Resource + InstrumentationScope instrumentation.Scope + + // Deprecated: use InstrumentationScope instead. + InstrumentationLibrary instrumentation.Library //nolint:staticcheck // This method needs to be define for backwards compatibility } // SpanStubFromReadOnlySpan returns a SpanStub populated from ro. @@ -85,12 +88,18 @@ func SpanStubFromReadOnlySpan(ro tracesdk.ReadOnlySpan) SpanStub { DroppedLinks: ro.DroppedLinks(), ChildSpanCount: ro.ChildSpanCount(), Resource: ro.Resource(), + InstrumentationScope: ro.InstrumentationScope(), InstrumentationLibrary: ro.InstrumentationScope(), } } // Snapshot returns a read-only copy of the SpanStub. func (s SpanStub) Snapshot() tracesdk.ReadOnlySpan { + scopeOrLibrary := s.InstrumentationScope + if scopeOrLibrary.Name == "" && scopeOrLibrary.Version == "" && scopeOrLibrary.SchemaURL == "" { + scopeOrLibrary = s.InstrumentationLibrary + } + return spanSnapshot{ name: s.Name, spanContext: s.SpanContext, @@ -107,7 +116,7 @@ func (s SpanStub) Snapshot() tracesdk.ReadOnlySpan { droppedLinks: s.DroppedLinks, childSpanCount: s.ChildSpanCount, resource: s.Resource, - instrumentationScope: s.InstrumentationLibrary, + instrumentationScope: scopeOrLibrary, } } @@ -152,6 +161,6 @@ func (s spanSnapshot) InstrumentationScope() instrumentation.Scope { return s.instrumentationScope } -func (s spanSnapshot) InstrumentationLibrary() instrumentation.Library { +func (s spanSnapshot) InstrumentationLibrary() instrumentation.Library { //nolint:staticcheck // This method needs to be define for backwards compatibility return s.instrumentationScope }