From 568e7556dd15fbe84dd1f4340fed162620ca9c0e Mon Sep 17 00:00:00 2001 From: Tigran Najaryan <4194920+tigrannajaryan@users.noreply.github.com> Date: Thu, 16 Sep 2021 12:33:39 -0400 Subject: [PATCH] Set Schema URL when exporting traces to OTLP (#2242) We previously were recording the Schema URL but were not setting the recorded value when exporting. This change now uses the recorded value when exporting to OTLP. --- CHANGELOG.md | 4 ++++ .../otlptrace/internal/tracetransform/span.go | 2 ++ .../internal/tracetransform/span_test.go | 20 +++++++++++++------ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3491aeaa426..bd906ea7ca5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +### Added + +- OTLP trace exporter now sets the SchemaURL field in the exported telemetry if the Tracer has WithSchemaURL option. (#2242) + ### Changed - NoopMeterProvider is now private and NewNoopMeterProvider must be used to obtain a noopMeterProvider. (#2237) diff --git a/exporters/otlp/otlptrace/internal/tracetransform/span.go b/exporters/otlp/otlptrace/internal/tracetransform/span.go index d1d027e52e1..adbba7755f6 100644 --- a/exporters/otlp/otlptrace/internal/tracetransform/span.go +++ b/exporters/otlp/otlptrace/internal/tracetransform/span.go @@ -60,6 +60,7 @@ func Spans(sdl []tracesdk.ReadOnlySpan) []*tracepb.ResourceSpans { ils = &tracepb.InstrumentationLibrarySpans{ InstrumentationLibrary: InstrumentationLibrary(sd.InstrumentationLibrary()), Spans: []*tracepb.Span{}, + SchemaUrl: sd.InstrumentationLibrary().SchemaURL, } } ils.Spans = append(ils.Spans, span(sd)) @@ -72,6 +73,7 @@ func Spans(sdl []tracesdk.ReadOnlySpan) []*tracepb.ResourceSpans { rs = &tracepb.ResourceSpans{ Resource: Resource(sd.Resource()), InstrumentationLibrarySpans: []*tracepb.InstrumentationLibrarySpans{ils}, + SchemaUrl: sd.Resource().SchemaURL(), } rsm[rKey] = rs continue diff --git a/exporters/otlp/otlptrace/internal/tracetransform/span_test.go b/exporters/otlp/otlptrace/internal/tracetransform/span_test.go index e9392c42b38..481604db140 100644 --- a/exporters/otlp/otlptrace/internal/tracetransform/span_test.go +++ b/exporters/otlp/otlptrace/internal/tracetransform/span_test.go @@ -25,14 +25,14 @@ import ( "google.golang.org/protobuf/proto" "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/trace" - tracepb "go.opentelemetry.io/proto/otlp/trace/v1" - "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/sdk/instrumentation" "go.opentelemetry.io/otel/sdk/resource" tracesdk "go.opentelemetry.io/otel/sdk/trace" "go.opentelemetry.io/otel/sdk/trace/tracetest" + semconv "go.opentelemetry.io/otel/semconv/v1.4.0" + "go.opentelemetry.io/otel/trace" + tracepb "go.opentelemetry.io/proto/otlp/trace/v1" ) func TestSpanKind(t *testing.T) { @@ -266,10 +266,16 @@ func TestSpanData(t *testing.T) { DroppedAttributes: 1, DroppedEvents: 2, DroppedLinks: 3, - Resource: resource.NewSchemaless(attribute.String("rk1", "rv1"), attribute.Int64("rk2", 5), attribute.StringSlice("rk3", []string{"sv1", "sv2"})), + Resource: resource.NewWithAttributes( + "http://example.com/custom-resource-schema", + attribute.String("rk1", "rv1"), + attribute.Int64("rk2", 5), + attribute.StringSlice("rk3", []string{"sv1", "sv2"}), + ), InstrumentationLibrary: instrumentation.Library{ - Name: "go.opentelemetry.io/test/otel", - Version: "v0.0.1", + Name: "go.opentelemetry.io/test/otel", + Version: "v0.0.1", + SchemaURL: semconv.SchemaURL, }, } @@ -298,8 +304,10 @@ func TestSpanData(t *testing.T) { require.Len(t, got, 1) assert.Equal(t, got[0].GetResource(), Resource(spanData.Resource)) + assert.Equal(t, got[0].SchemaUrl, spanData.Resource.SchemaURL()) ilSpans := got[0].GetInstrumentationLibrarySpans() require.Len(t, ilSpans, 1) + assert.Equal(t, ilSpans[0].SchemaUrl, spanData.InstrumentationLibrary.SchemaURL) assert.Equal(t, ilSpans[0].GetInstrumentationLibrary(), InstrumentationLibrary(spanData.InstrumentationLibrary)) require.Len(t, ilSpans[0].Spans, 1) actualSpan := ilSpans[0].Spans[0]