Skip to content

Commit b3c6fc5

Browse files
WhitWaldoJoshVanLItalyPaleAleyaron2
authored
Prioritized value of OTEL_SERVICE_NAME envvar over configuration if set (dapr#7451)
* Prioritized value of OTEL_SERVICE_NAME envvar over configuration, if set, to resolve dapr#5942 for Aspire tracing support. Signed-off-by: Whit Waldo <[email protected]> * Added unit test for new function - passing Signed-off-by: Whit Waldo <[email protected]> * Prioritized value of OTEL_SERVICE_NAME envvar over configuration, if set, to resolve dapr#5942 for Aspire tracing support. Signed-off-by: Whit Waldo <[email protected]> * Added unit test for new function - passing Signed-off-by: Whit Waldo <[email protected]> * Update pkg/runtime/runtime_test.go Co-authored-by: Josh van Leeuwen <[email protected]> Signed-off-by: Whit Waldo <[email protected]> * Update pkg/runtime/runtime_test.go Co-authored-by: Josh van Leeuwen <[email protected]> Signed-off-by: Whit Waldo <[email protected]> * Update pkg/runtime/runtime_test.go Co-authored-by: Josh van Leeuwen <[email protected]> Signed-off-by: Whit Waldo <[email protected]> * Missing func keyword Co-authored-by: Alessandro (Ale) Segala <[email protected]> Signed-off-by: Whit Waldo <[email protected]> * Update pkg/runtime/runtime.go Co-authored-by: Alessandro (Ale) Segala <[email protected]> Signed-off-by: Whit Waldo <[email protected]> * Fixed linter errors Signed-off-by: Whit Waldo <[email protected]> --------- Signed-off-by: Whit Waldo <[email protected]> Co-authored-by: Josh van Leeuwen <[email protected]> Co-authored-by: Alessandro (Ale) Segala <[email protected]> Co-authored-by: Yaron Schneider <[email protected]>
1 parent 62f40df commit b3c6fc5

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

pkg/runtime/runtime.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ func (a *DaprRuntime) setupTracing(ctx context.Context, hostAddress string, tpSt
426426
// Register a resource
427427
r := resource.NewWithAttributes(
428428
semconv.SchemaURL,
429-
semconv.ServiceNameKey.String(a.runtimeConfig.id),
429+
semconv.ServiceNameKey.String(getOtelServiceName(a.runtimeConfig.id)),
430430
)
431431

432432
tpStore.RegisterResource(r)
@@ -441,6 +441,13 @@ func (a *DaprRuntime) setupTracing(ctx context.Context, hostAddress string, tpSt
441441
return nil
442442
}
443443

444+
func getOtelServiceName(fallback string) string {
445+
if value := os.Getenv("OTEL_SERVICE_NAME"); value != "" {
446+
return value
447+
}
448+
return fallback
449+
}
450+
444451
func (a *DaprRuntime) initRuntime(ctx context.Context) error {
445452
var err error
446453
if a.hostAddress, err = utils.GetHostAddress(); err != nil {

pkg/runtime/runtime_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,3 +2145,29 @@ func testSecurity(t *testing.T) security.Handler {
21452145

21462146
return sec
21472147
}
2148+
2149+
func TestGetOtelServiceName(t *testing.T) {
2150+
// Save the original value of the OTEL_SERVICE_NAME variable and restore at the end
2151+
2152+
tests := []struct {
2153+
env string // The value of the OTEL_SERVICE_NAME variable
2154+
fallback string // The fallback value
2155+
expected string // The expected value
2156+
}{
2157+
{"", "my-app", "my-app"}, // Case 1: No environment variable, use fallback
2158+
{"service-abc", "my-app", "service-abc"}, // Case 2: Environment variable set, use it
2159+
}
2160+
2161+
for _, tc := range tests {
2162+
t.Run(tc.env, func(t *testing.T) {
2163+
// Set the environment variable to the test case value
2164+
t.Setenv("OTEL_SERVICE_NAME", tc.env)
2165+
// Call the function and check the result
2166+
got := getOtelServiceName(tc.fallback)
2167+
if got != tc.expected {
2168+
// Report an error if the result doesn't match
2169+
t.Errorf("getOtelServiceName(%q) = %q; expected %q", tc.fallback, got, tc.expected)
2170+
}
2171+
})
2172+
}
2173+
}

0 commit comments

Comments
 (0)