fix: Make service name configurable for Metrics and Tracing (#9563)

# Which Problems Are Solved

The service name is hardcoded in the metrics code. Making the service
name to be configurable helps when running multiple instances of
Zitadel.

The defaults remain unchanged, the service name will be defaulted to
ZITADEL.

# How the Problems Are Solved

Add a config option to override the name in defaults.yaml and pass it
down to the corresponding metrics or tracing module (google or otel)

# Additional Changes
NA

# Additional Context
NA

(cherry picked from commit dc64e35128)
This commit is contained in:
Harsha Reddy
2025-03-20 15:05:54 +05:30
committed by Livio Spring
parent 94680b22c7
commit ee9d55cf95
7 changed files with 22 additions and 13 deletions

View File

@@ -14,6 +14,7 @@ Tracing:
# for type 'otel' is used for standard [open telemetry](https://opentelemetry.io)
# Fraction: 1.0
# Endpoint: 'otel.collector.endpoint'
# ServiceName: 'ZITADEL' # Name of the service in traces
#
# type 'log' or '' disables tracing
#
@@ -24,6 +25,8 @@ Tracing:
Fraction: 1.0 # ZITADEL_TRACING_FRACTION
# The endpoint of the otel collector endpoint
Endpoint: "" #ZITADEL_TRACING_ENDPOINT
# The name of the service in traces
ServiceName: "ZITADEL" #ZITADEL_TRACING_SERVICENAME
# Profiler enables capturing profiling data (CPU, Memory, ...) for performance analysis
Profiler:

View File

@@ -28,7 +28,7 @@ type Metrics struct {
}
func NewMetrics(meterName string) (metrics.Metrics, error) {
resource, err := otel_resource.ResourceWithService()
resource, err := otel_resource.ResourceWithService("ZITADEL")
if err != nil {
return nil, err
}

View File

@@ -8,9 +8,9 @@ import (
"github.com/zitadel/zitadel/cmd/build"
)
func ResourceWithService() (*resource.Resource, error) {
func ResourceWithService(serviceName string) (*resource.Resource, error) {
attributes := []attribute.KeyValue{
semconv.ServiceNameKey.String("ZITADEL"),
semconv.ServiceNameKey.String(serviceName),
}
if build.Version() != "" {
attributes = append(attributes, semconv.ServiceVersionKey.String(build.Version()))

View File

@@ -9,13 +9,15 @@ import (
)
type Config struct {
ProjectID string
Fraction float64
ProjectID string
Fraction float64
ServiceName string
}
func NewTracer(rawConfig map[string]interface{}) (err error) {
c := new(Config)
c.ProjectID, _ = rawConfig["projectid"].(string)
c.ServiceName, _ = rawConfig["servicename"].(string)
c.Fraction, err = otel.FractionFromConfig(rawConfig["fraction"])
if err != nil {
return err
@@ -34,6 +36,6 @@ func (c *Config) NewTracer() error {
return err
}
tracing.T, err = otel.NewTracer(sampler, exporter)
tracing.T, err = otel.NewTracer(sampler, exporter, c.ServiceName)
return err
}

View File

@@ -9,12 +9,14 @@ import (
)
type Config struct {
Fraction float64
Fraction float64
ServiceName string
}
func NewTracer(rawConfig map[string]interface{}) (err error) {
c := new(Config)
c.Fraction, err = otel.FractionFromConfig(rawConfig["fraction"])
c.ServiceName, _ = rawConfig["servicename"].(string)
if err != nil {
return err
}
@@ -32,6 +34,6 @@ func (c *Config) NewTracer() error {
return err
}
tracing.T, err = otel.NewTracer(sampler, exporter)
tracing.T, err = otel.NewTracer(sampler, exporter, c.ServiceName)
return err
}

View File

@@ -13,13 +13,15 @@ import (
)
type Config struct {
Fraction float64
Endpoint string
Fraction float64
Endpoint string
ServiceName string
}
func NewTracerFromConfig(rawConfig map[string]interface{}) (err error) {
c := new(Config)
c.Endpoint, _ = rawConfig["endpoint"].(string)
c.ServiceName, _ = rawConfig["servicename"].(string)
c.Fraction, err = FractionFromConfig(rawConfig["fraction"])
if err != nil {
return err
@@ -54,7 +56,7 @@ func (c *Config) NewTracer() error {
return err
}
tracing.T, err = NewTracer(sampler, exporter)
tracing.T, err = NewTracer(sampler, exporter, c.ServiceName)
return err
}

View File

@@ -18,8 +18,8 @@ type Tracer struct {
sampler sdk_trace.Sampler
}
func NewTracer(sampler sdk_trace.Sampler, exporter sdk_trace.SpanExporter) (*Tracer, error) {
resource, err := otel_resource.ResourceWithService()
func NewTracer(sampler sdk_trace.Sampler, exporter sdk_trace.SpanExporter, serviceName string) (*Tracer, error) {
resource, err := otel_resource.ResourceWithService(serviceName)
if err != nil {
return nil, err
}