Skip to content

Commit ec4ff12

Browse files
fix: override otel service name and version (#1956)
Setting `service.name` or `service.version` via OTEL_RESOURCE_ATTRIBUTES (or OTEL_SERVICE_NAME) now correctly overrides the values flagd sets programmatically Fixes #1938 Signed-off-by: Lea Konvalinka <lea.konvalinka@dynatrace.com>
1 parent 8aadbd9 commit ec4ff12

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

core/pkg/telemetry/builder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ func buildResourceFor(ctx context.Context, serviceName string, serviceVersion st
161161
resource.WithAttributes(
162162
semconv.ServiceNameKey.String(serviceName),
163163
semconv.ServiceVersionKey.String(serviceVersion)),
164+
resource.WithFromEnv(),
164165
)
165166
if err != nil {
166167
return nil, fmt.Errorf("unable to create resource identifier: %w", err)

core/pkg/telemetry/builder_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,23 @@ func TestBuildResourceFor(t *testing.T) {
155155
}, "expected resource to contain service version")
156156
}
157157

158+
func TestBuildResourceForEnvOverride(t *testing.T) {
159+
t.Setenv("OTEL_RESOURCE_ATTRIBUTES", "service.version=9.9.9,service.name=overridden-svc")
160+
161+
res, err := buildResourceFor(context.Background(), "defaultSvc", "0.0.1")
162+
require.Nil(t, err, "expected no error, but got: %v", err)
163+
164+
attributes := res.Attributes()
165+
require.Containsf(t, attributes, attribute.KeyValue{
166+
Key: semconv.ServiceNameKey,
167+
Value: attribute.StringValue("overridden-svc"),
168+
}, "expected OTEL_RESOURCE_ATTRIBUTES to override the programmatic service name")
169+
require.Containsf(t, attributes, attribute.KeyValue{
170+
Key: semconv.ServiceVersionKey,
171+
Value: attribute.StringValue("9.9.9"),
172+
}, "expected OTEL_RESOURCE_ATTRIBUTES to override the programmatic service version")
173+
}
174+
158175
func TestErrorIntercepted(t *testing.T) {
159176
// register the OTel error handling
160177
observedZapCore, observedLogs := observer.New(zap.DebugLevel)

0 commit comments

Comments
 (0)