Skip to content

Commit b9d6fac

Browse files
committed
Add an Environment Variable to use the Component Name as the Service Name for OTEL Tracing
1 parent f1b7a8c commit b9d6fac

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

pkg/http/tracing.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"log/slog"
77
"net/http"
88
"net/url"
9+
"os"
10+
"strconv"
911

1012
"github.com/go-chi/chi/v5"
1113
"github.com/go-chi/httplog/v2"
@@ -82,12 +84,23 @@ func (t *TraceInstaller) Install(r chi.Router, serviceName string, extraOpts ...
8284
return
8385
}
8486

87+
// For Distributed Deployments (Kubernetes), if the legacy env var is set and true, use the microservice name as the service name instead.
88+
// This is to help with Application Observability and Correlations between Metrics, Logs, Traces and Profiles.
89+
serviceNameAttrValue := "quickpizza"
90+
v, found := os.LookupEnv("QUICKPIZZA_OTEL_SERVICE_NAME_LEGACY")
91+
if found {
92+
b, _ := strconv.ParseBool(v)
93+
if b {
94+
serviceNameAttrValue = serviceName
95+
}
96+
}
97+
8598
// We discard the error here as it cannot possibly take place with the parameters we use.
8699
res, _ := resource.Merge(
87100
resource.Default(),
88101
resource.NewWithAttributes(
89102
semconv.SchemaURL,
90-
semconv.ServiceName("quickpizza"),
103+
semconv.ServiceName(serviceNameAttrValue),
91104
attribute.KeyValue{Key: "service.component", Value: attribute.StringValue(serviceName)},
92105
),
93106
)

0 commit comments

Comments
 (0)