From 1014aea73b75e64f2ce5c5c3a89a2a37cbb6e6aa Mon Sep 17 00:00:00 2001 From: James Telfer <792299+jamestelfer@users.noreply.github.com> Date: Sat, 5 Oct 2024 14:52:03 +1000 Subject: [PATCH] fix: OTEL schema incompatibility after upgrade After the recent upgrade, the server failed to start because the OTel schema version had changed. This is undesirable behaviour: added a unit test to catch this easily before commit. --- internal/observe/telemetry.go | 20 ++++++++++++-------- internal/observe/telemetry_test.go | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 internal/observe/telemetry_test.go diff --git a/internal/observe/telemetry.go b/internal/observe/telemetry.go index f49adf2..9f2e082 100644 --- a/internal/observe/telemetry.go +++ b/internal/observe/telemetry.go @@ -23,7 +23,7 @@ import ( "go.opentelemetry.io/otel/sdk/metric" "go.opentelemetry.io/otel/sdk/resource" "go.opentelemetry.io/otel/sdk/trace" - semconv "go.opentelemetry.io/otel/semconv/v1.24.0" + semconv "go.opentelemetry.io/otel/semconv/v1.26.0" ) // Configure sets up OpenTelemetry according to the configuration. If it does @@ -134,13 +134,7 @@ func newTraceProvider(ctx context.Context, cfg config.ObserveConfig, e exporters return nil, err } - r, err := resource.Merge( - resource.Default(), - resource.NewWithAttributes( - semconv.SchemaURL, - semconv.ServiceName(cfg.ServiceName), - ), - ) + r, err := resourceWithServiceName(resource.Default(), cfg.ServiceName) if err != nil { return nil, err } @@ -154,6 +148,16 @@ func newTraceProvider(ctx context.Context, cfg config.ObserveConfig, e exporters return traceProvider, nil } +func resourceWithServiceName(base *resource.Resource, serviceName string) (*resource.Resource, error) { + return resource.Merge( + base, + resource.NewWithAttributes( + semconv.SchemaURL, + semconv.ServiceName(serviceName), + ), + ) +} + func newMeterProvider(ctx context.Context, cfg config.ObserveConfig, e exporters) (*metric.MeterProvider, error) { metricExporter, err := e.Metric(ctx) if err != nil { diff --git a/internal/observe/telemetry_test.go b/internal/observe/telemetry_test.go new file mode 100644 index 0000000..f16512f --- /dev/null +++ b/internal/observe/telemetry_test.go @@ -0,0 +1,18 @@ +package observe + +import ( + "testing" + + "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/sdk/resource" +) + +func Test_ResourceMerge(t *testing.T) { + // Ensure that schema incompatibility on OTEL upgrades is detected before + // merge + _, err := resourceWithServiceName( + resource.Default(), + "serviceName") + + require.NoError(t, err) +}