Skip to content

Commit

Permalink
fix: OTEL schema incompatibility after upgrade
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jamestelfer committed Oct 5, 2024
1 parent 6bbb904 commit 1014aea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
20 changes: 12 additions & 8 deletions internal/observe/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions internal/observe/telemetry_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 1014aea

Please sign in to comment.