mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-01-03 22:52:30 +02:00
Add schema URL support to Tracer (#1889)
This adds support for schema URL to the Tracer according to the specification: https://github.com/open-telemetry/opentelemetry-specification/pull/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.
This commit is contained in:
parent
c1f460e097
commit
bd935866f4
@ -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.
|
- 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)
|
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 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)
|
- An example of using OpenTelemetry Go as a trace context forwarder. (#1912)
|
||||||
- `ParseTraceState` is added to the `go.opentelemetry.io/otel/trace` package.
|
- `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)
|
It can be used to decode a `TraceState` from a `tracestate` header string value. (#1937)
|
||||||
|
@ -61,6 +61,8 @@ func Spans(sdl []tracesdk.ReadOnlySpan) []*tracepb.ResourceSpans {
|
|||||||
InstrumentationLibrary: instrumentationLibrary(sd.InstrumentationLibrary()),
|
InstrumentationLibrary: instrumentationLibrary(sd.InstrumentationLibrary()),
|
||||||
Spans: []*tracepb.Span{},
|
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))
|
ils.Spans = append(ils.Spans, span(sd))
|
||||||
ilsm[iKey] = ils
|
ilsm[iKey] = ils
|
||||||
|
@ -263,8 +263,9 @@ func TestSpanData(t *testing.T) {
|
|||||||
DroppedLinks: 3,
|
DroppedLinks: 3,
|
||||||
Resource: resource.NewWithAttributes(attribute.String("rk1", "rv1"), attribute.Int64("rk2", 5)),
|
Resource: resource.NewWithAttributes(attribute.String("rk1", "rv1"), attribute.Int64("rk2", 5)),
|
||||||
InstrumentationLibrary: instrumentation.Library{
|
InstrumentationLibrary: instrumentation.Library{
|
||||||
Name: "go.opentelemetry.io/test/otel",
|
Name: "go.opentelemetry.io/test/otel",
|
||||||
Version: "v0.0.1",
|
Version: "v0.0.1",
|
||||||
|
SchemaURL: "https://opentelemetry.io/schemas/1.2.0",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -295,6 +296,7 @@ func TestSpanData(t *testing.T) {
|
|||||||
assert.Equal(t, got[0].GetResource(), Resource(spanData.Resource))
|
assert.Equal(t, got[0].GetResource(), Resource(spanData.Resource))
|
||||||
ilSpans := got[0].GetInstrumentationLibrarySpans()
|
ilSpans := got[0].GetInstrumentationLibrarySpans()
|
||||||
require.Len(t, ilSpans, 1)
|
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))
|
assert.Equal(t, ilSpans[0].GetInstrumentationLibrary(), instrumentationLibrary(spanData.InstrumentationLibrary))
|
||||||
require.Len(t, ilSpans[0].Spans, 1)
|
require.Len(t, ilSpans[0].Spans, 1)
|
||||||
actualSpan := ilSpans[0].Spans[0]
|
actualSpan := ilSpans[0].Spans[0]
|
||||||
|
@ -35,6 +35,7 @@ var (
|
|||||||
tracer = otel.GetTracerProvider().Tracer(
|
tracer = otel.GetTracerProvider().Tracer(
|
||||||
instrumentationName,
|
instrumentationName,
|
||||||
trace.WithInstrumentationVersion(instrumentationVersion),
|
trace.WithInstrumentationVersion(instrumentationVersion),
|
||||||
|
trace.WithSchemaURL("https://opentelemetry.io/schemas/1.2.0"),
|
||||||
)
|
)
|
||||||
|
|
||||||
meter = global.GetMeterProvider().Meter(
|
meter = global.GetMeterProvider().Meter(
|
||||||
|
@ -170,7 +170,8 @@ func TestExporter_ExportSpan(t *testing.T) {
|
|||||||
],
|
],
|
||||||
"InstrumentationLibrary": {
|
"InstrumentationLibrary": {
|
||||||
"Name": "",
|
"Name": "",
|
||||||
"Version": ""
|
"Version": "",
|
||||||
|
"SchemaURL": ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
@ -32,4 +32,6 @@ type Library struct {
|
|||||||
Name string
|
Name string
|
||||||
// Version is the version of the instrumentation library.
|
// Version is the version of the instrumentation library.
|
||||||
Version string
|
Version string
|
||||||
|
// SchemaURL of the telemetry emitted by the library.
|
||||||
|
SchemaURL string
|
||||||
}
|
}
|
||||||
|
@ -115,8 +115,9 @@ func (p *TracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.T
|
|||||||
name = defaultTracerName
|
name = defaultTracerName
|
||||||
}
|
}
|
||||||
il := instrumentation.Library{
|
il := instrumentation.Library{
|
||||||
Name: name,
|
Name: name,
|
||||||
Version: c.InstrumentationVersion(),
|
Version: c.InstrumentationVersion(),
|
||||||
|
SchemaURL: c.SchemaURL(),
|
||||||
}
|
}
|
||||||
t, ok := p.namedTracer[il]
|
t, ok := p.namedTracer[il]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -20,6 +20,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"go.opentelemetry.io/otel/trace"
|
||||||
)
|
)
|
||||||
|
|
||||||
type basicSpanProcesor struct {
|
type basicSpanProcesor struct {
|
||||||
@ -82,3 +84,13 @@ func TestFailedProcessorShutdownInUnregister(t *testing.T) {
|
|||||||
err := stp.Shutdown(context.Background())
|
err := stp.Shutdown(context.Background())
|
||||||
assert.NoError(t, err)
|
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)
|
||||||
|
}
|
||||||
|
@ -1284,7 +1284,7 @@ func TestWithResource(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithInstrumentationVersion(t *testing.T) {
|
func TestWithInstrumentationVersionAndSchema(t *testing.T) {
|
||||||
te := NewTestExporter()
|
te := NewTestExporter()
|
||||||
tp := NewTracerProvider(WithSyncer(te), WithResource(resource.Empty()))
|
tp := NewTracerProvider(WithSyncer(te), WithResource(resource.Empty()))
|
||||||
|
|
||||||
@ -1293,6 +1293,7 @@ func TestWithInstrumentationVersion(t *testing.T) {
|
|||||||
_, span := tp.Tracer(
|
_, span := tp.Tracer(
|
||||||
"WithInstrumentationVersion",
|
"WithInstrumentationVersion",
|
||||||
trace.WithInstrumentationVersion("v0.1.0"),
|
trace.WithInstrumentationVersion("v0.1.0"),
|
||||||
|
trace.WithSchemaURL("https://opentelemetry.io/schemas/1.2.0"),
|
||||||
).Start(ctx, "span0")
|
).Start(ctx, "span0")
|
||||||
got, err := endSpan(te, span)
|
got, err := endSpan(te, span)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1308,8 +1309,9 @@ func TestWithInstrumentationVersion(t *testing.T) {
|
|||||||
name: "span0",
|
name: "span0",
|
||||||
spanKind: trace.SpanKindInternal,
|
spanKind: trace.SpanKindInternal,
|
||||||
instrumentationLibrary: instrumentation.Library{
|
instrumentationLibrary: instrumentation.Library{
|
||||||
Name: "WithInstrumentationVersion",
|
Name: "WithInstrumentationVersion",
|
||||||
Version: "v0.1.0",
|
Version: "v0.1.0",
|
||||||
|
SchemaURL: "https://opentelemetry.io/schemas/1.2.0",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if diff := cmpDiff(got, want); diff != "" {
|
if diff := cmpDiff(got, want); diff != "" {
|
||||||
|
@ -23,6 +23,8 @@ import (
|
|||||||
// TracerConfig is a group of options for a Tracer.
|
// TracerConfig is a group of options for a Tracer.
|
||||||
type TracerConfig struct {
|
type TracerConfig struct {
|
||||||
instrumentationVersion string
|
instrumentationVersion string
|
||||||
|
// Schema URL of the telemetry emitted by the Tracer.
|
||||||
|
schemaURL string
|
||||||
}
|
}
|
||||||
|
|
||||||
// InstrumentationVersion returns the version of the library providing instrumentation.
|
// InstrumentationVersion returns the version of the library providing instrumentation.
|
||||||
@ -30,6 +32,11 @@ func (t *TracerConfig) InstrumentationVersion() string {
|
|||||||
return t.instrumentationVersion
|
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.
|
// NewTracerConfig applies all the options to a returned TracerConfig.
|
||||||
func NewTracerConfig(options ...TracerOption) *TracerConfig {
|
func NewTracerConfig(options ...TracerOption) *TracerConfig {
|
||||||
config := new(TracerConfig)
|
config := new(TracerConfig)
|
||||||
@ -252,3 +259,10 @@ func WithInstrumentationVersion(version string) TracerOption {
|
|||||||
cfg.instrumentationVersion = version
|
cfg.instrumentationVersion = version
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithSchemaURL sets the schema URL for the Tracer.
|
||||||
|
func WithSchemaURL(schemaURL string) TracerOption {
|
||||||
|
return tracerOptionFunc(func(cfg *TracerConfig) {
|
||||||
|
cfg.schemaURL = schemaURL
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -177,6 +177,7 @@ func TestNewSpanConfig(t *testing.T) {
|
|||||||
func TestTracerConfig(t *testing.T) {
|
func TestTracerConfig(t *testing.T) {
|
||||||
v1 := "semver:0.0.1"
|
v1 := "semver:0.0.1"
|
||||||
v2 := "semver:1.0.0"
|
v2 := "semver:1.0.0"
|
||||||
|
schemaURL := "https://opentelemetry.io/schemas/1.2.0"
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
options []TracerOption
|
options []TracerOption
|
||||||
expected *TracerConfig
|
expected *TracerConfig
|
||||||
@ -204,6 +205,14 @@ func TestTracerConfig(t *testing.T) {
|
|||||||
instrumentationVersion: v2,
|
instrumentationVersion: v2,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
[]TracerOption{
|
||||||
|
WithSchemaURL(schemaURL),
|
||||||
|
},
|
||||||
|
&TracerConfig{
|
||||||
|
schemaURL: schemaURL,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
config := NewTracerConfig(test.options...)
|
config := NewTracerConfig(test.options...)
|
||||||
|
Loading…
Reference in New Issue
Block a user