Skip to content

Commit

Permalink
Add schema URL support to Tracer
Browse files Browse the repository at this point in the history
This adds support for schema URL to the Tracer according to the specification:
open-telemetry/opentelemetry-specification#1666
(Link to replaced by the link to the spec after that PR is merged)

For the future: once the proto is updated we will need to populate the
schema_url field in the messages.
  • Loading branch information
tigrannajaryan committed May 27, 2021
1 parent c1f460e commit faa7c69
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- The `SpanStub` type and its associated functions were added to the `go.opentelemetry.io/otel/sdk/trace/tracetest` package.
This type can be used as a testing replacement for the `SpanSnapshot` that was removed from the `go.opentelemetry.io/otel/sdk/trace` package. (#1873)
- Adds support for scheme in `OTEL_EXPORTER_OTLP_ENDPOINT` according to the spec. (#1886)
- Adds `trace.WithSchemaURL` option for configuring the tracer with a Schema URL. (#1889)
- An example of using OpenTelemetry Go as a trace context forwarder. (#1912)
- `ParseTraceState` is added to the `go.opentelemetry.io/otel/trace` package.
It can be used to decode a `TraceState` from a `tracestate` header string value. (#1937)
Expand Down
2 changes: 2 additions & 0 deletions exporters/otlp/internal/transform/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func Spans(sdl []tracesdk.ReadOnlySpan) []*tracepb.ResourceSpans {
InstrumentationLibrary: instrumentationLibrary(sd.InstrumentationLibrary()),
Spans: []*tracepb.Span{},
}
// TODO: set schema_url field of ils when it is available in the proto.
_ = sd.InstrumentationLibrary().SchemaURL
}
ils.Spans = append(ils.Spans, span(sd))
ilsm[iKey] = ils
Expand Down
6 changes: 4 additions & 2 deletions exporters/otlp/internal/transform/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,9 @@ func TestSpanData(t *testing.T) {
DroppedLinks: 3,
Resource: resource.NewWithAttributes(attribute.String("rk1", "rv1"), attribute.Int64("rk2", 5)),
InstrumentationLibrary: instrumentation.Library{
Name: "go.opentelemetry.io/test/otel",
Version: "v0.0.1",
Name: "go.opentelemetry.io/test/otel",
Version: "v0.0.1",
SchemaURL: "https://opentelemetry.io/schemas/1.2.0",
},
}

Expand Down Expand Up @@ -295,6 +296,7 @@ func TestSpanData(t *testing.T) {
assert.Equal(t, got[0].GetResource(), Resource(spanData.Resource))
ilSpans := got[0].GetInstrumentationLibrarySpans()
require.Len(t, ilSpans, 1)
// TODO: Add SchemaURL field checking once the field is added to the proto.
assert.Equal(t, ilSpans[0].GetInstrumentationLibrary(), instrumentationLibrary(spanData.InstrumentationLibrary))
require.Len(t, ilSpans[0].Spans, 1)
actualSpan := ilSpans[0].Spans[0]
Expand Down
1 change: 1 addition & 0 deletions exporters/stdout/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
tracer = otel.GetTracerProvider().Tracer(
instrumentationName,
trace.WithInstrumentationVersion(instrumentationVersion),
trace.WithSchemaURL("https://opentelemetry.io/schemas/1.2.0"),
)

meter = global.GetMeterProvider().Meter(
Expand Down
3 changes: 2 additions & 1 deletion exporters/stdout/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ func TestExporter_ExportSpan(t *testing.T) {
],
"InstrumentationLibrary": {
"Name": "",
"Version": ""
"Version": "",
"SchemaURL": ""
}
}
]
Expand Down
2 changes: 2 additions & 0 deletions sdk/instrumentation/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ type Library struct {
Name string
// Version is the version of the instrumentation library.
Version string
// SchemaURL of the telemetry emitted by the library.
SchemaURL string
}
5 changes: 3 additions & 2 deletions sdk/trace/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ func (p *TracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.T
name = defaultTracerName
}
il := instrumentation.Library{
Name: name,
Version: c.InstrumentationVersion(),
Name: name,
Version: c.InstrumentationVersion(),
SchemaURL: c.SchemaURL(),
}
t, ok := p.namedTracer[il]
if !ok {
Expand Down
12 changes: 12 additions & 0 deletions sdk/trace/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/trace"
)

type basicSpanProcesor struct {
Expand Down Expand Up @@ -82,3 +84,13 @@ func TestFailedProcessorShutdownInUnregister(t *testing.T) {
err := stp.Shutdown(context.Background())
assert.NoError(t, err)
}

func TestSchemaURL(t *testing.T) {
stp := NewTracerProvider()
schemaURL := "https://opentelemetry.io/schemas/1.2.0"
tracerIface := stp.Tracer("tracername", trace.WithSchemaURL(schemaURL))

// Verify that the SchemaURL of the constructed Tracer is correctly populated.
tracerStruct := tracerIface.(*tracer)
assert.EqualValues(t, schemaURL, tracerStruct.instrumentationLibrary.SchemaURL)
}
8 changes: 5 additions & 3 deletions sdk/trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@ func TestWithResource(t *testing.T) {
}
}

func TestWithInstrumentationVersion(t *testing.T) {
func TestWithInstrumentationVersionAndSchema(t *testing.T) {
te := NewTestExporter()
tp := NewTracerProvider(WithSyncer(te), WithResource(resource.Empty()))

Expand All @@ -1293,6 +1293,7 @@ func TestWithInstrumentationVersion(t *testing.T) {
_, span := tp.Tracer(
"WithInstrumentationVersion",
trace.WithInstrumentationVersion("v0.1.0"),
trace.WithSchemaURL("https://opentelemetry.io/schemas/1.2.0"),
).Start(ctx, "span0")
got, err := endSpan(te, span)
if err != nil {
Expand All @@ -1308,8 +1309,9 @@ func TestWithInstrumentationVersion(t *testing.T) {
name: "span0",
spanKind: trace.SpanKindInternal,
instrumentationLibrary: instrumentation.Library{
Name: "WithInstrumentationVersion",
Version: "v0.1.0",
Name: "WithInstrumentationVersion",
Version: "v0.1.0",
SchemaURL: "https://opentelemetry.io/schemas/1.2.0",
},
}
if diff := cmpDiff(got, want); diff != "" {
Expand Down
14 changes: 14 additions & 0 deletions trace/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@ import (
// TracerConfig is a group of options for a Tracer.
type TracerConfig struct {
instrumentationVersion string
// Schema URL of the telemetry emitted by the Tracer.
schemaURL string
}

// InstrumentationVersion returns the version of the library providing instrumentation.
func (t *TracerConfig) InstrumentationVersion() string {
return t.instrumentationVersion
}

// SchemaURL returns the Schema URL of the telemetry emitted by the Tracer.
func (t *TracerConfig) SchemaURL() string {
return t.schemaURL
}

// NewTracerConfig applies all the options to a returned TracerConfig.
func NewTracerConfig(options ...TracerOption) *TracerConfig {
config := new(TracerConfig)
Expand Down Expand Up @@ -252,3 +259,10 @@ func WithInstrumentationVersion(version string) TracerOption {
cfg.instrumentationVersion = version
})
}

// WithSchemaURL sets the schema URL for the Tracer.
func WithSchemaURL(schemaURL string) TracerOption {
return tracerOptionFunc(func(cfg *TracerConfig) {
cfg.schemaURL = schemaURL
})
}
9 changes: 9 additions & 0 deletions trace/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func TestNewSpanConfig(t *testing.T) {
func TestTracerConfig(t *testing.T) {
v1 := "semver:0.0.1"
v2 := "semver:1.0.0"
schemaURL := "https://opentelemetry.io/schemas/1.2.0"
tests := []struct {
options []TracerOption
expected *TracerConfig
Expand Down Expand Up @@ -204,6 +205,14 @@ func TestTracerConfig(t *testing.T) {
instrumentationVersion: v2,
},
},
{
[]TracerOption{
WithSchemaURL(schemaURL),
},
&TracerConfig{
schemaURL: schemaURL,
},
},
}
for _, test := range tests {
config := NewTracerConfig(test.options...)
Expand Down

0 comments on commit faa7c69

Please sign in to comment.