Skip to content

Commit

Permalink
Prioritized value of OTEL_SERVICE_NAME envvar over configuration if s…
Browse files Browse the repository at this point in the history
…et (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]>
  • Loading branch information
4 people authored Apr 19, 2024
1 parent 62f40df commit b3c6fc5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func (a *DaprRuntime) setupTracing(ctx context.Context, hostAddress string, tpSt
// Register a resource
r := resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(a.runtimeConfig.id),
semconv.ServiceNameKey.String(getOtelServiceName(a.runtimeConfig.id)),
)

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

func getOtelServiceName(fallback string) string {
if value := os.Getenv("OTEL_SERVICE_NAME"); value != "" {
return value
}
return fallback
}

func (a *DaprRuntime) initRuntime(ctx context.Context) error {
var err error
if a.hostAddress, err = utils.GetHostAddress(); err != nil {
Expand Down
26 changes: 26 additions & 0 deletions pkg/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2145,3 +2145,29 @@ func testSecurity(t *testing.T) security.Handler {

return sec
}

func TestGetOtelServiceName(t *testing.T) {
// Save the original value of the OTEL_SERVICE_NAME variable and restore at the end

tests := []struct {
env string // The value of the OTEL_SERVICE_NAME variable
fallback string // The fallback value
expected string // The expected value
}{
{"", "my-app", "my-app"}, // Case 1: No environment variable, use fallback
{"service-abc", "my-app", "service-abc"}, // Case 2: Environment variable set, use it
}

for _, tc := range tests {
t.Run(tc.env, func(t *testing.T) {
// Set the environment variable to the test case value
t.Setenv("OTEL_SERVICE_NAME", tc.env)
// Call the function and check the result
got := getOtelServiceName(tc.fallback)
if got != tc.expected {
// Report an error if the result doesn't match
t.Errorf("getOtelServiceName(%q) = %q; expected %q", tc.fallback, got, tc.expected)
}
})
}
}

0 comments on commit b3c6fc5

Please sign in to comment.