From 2af3e228e41d5c3000d7aecacc3a2a89e330a2ae Mon Sep 17 00:00:00 2001 From: Livio Amstutz Date: Thu, 28 Apr 2022 17:35:56 +0200 Subject: [PATCH] feat: set service name in tracing (#3533) --- .../telemetry/tracing/google/google_tracer.go | 11 ++++------- internal/telemetry/tracing/log/config.go | 8 +++----- internal/telemetry/tracing/otel/config.go | 10 ++++------ .../telemetry/tracing/otel/open_telemetry.go | 17 +++++++++++++++-- internal/telemetry/tracing/span.go | 3 ++- 5 files changed, 28 insertions(+), 21 deletions(-) diff --git a/internal/telemetry/tracing/google/google_tracer.go b/internal/telemetry/tracing/google/google_tracer.go index 5bf622b821..fde1525fa1 100644 --- a/internal/telemetry/tracing/google/google_tracer.go +++ b/internal/telemetry/tracing/google/google_tracer.go @@ -12,15 +12,13 @@ import ( ) type Config struct { - ProjectID string - MetricPrefix string - Fraction float64 + ProjectID string + Fraction float64 } func NewTracer(rawConfig map[string]interface{}) (err error) { c := new(Config) c.ProjectID, _ = rawConfig["projectid"].(string) - c.MetricPrefix, _ = rawConfig["metricprefix"].(string) fraction, ok := rawConfig["fraction"].(string) if ok { c.Fraction, err = strconv.ParseFloat(fraction, 32) @@ -43,7 +41,6 @@ func (c *Config) NewTracer() error { return err } - tracing.T = &Tracer{Tracer: *(otel.NewTracer(c.MetricPrefix, sampler, exporter))} - - return nil + tracing.T, err = otel.NewTracer(sampler, exporter) + return err } diff --git a/internal/telemetry/tracing/log/config.go b/internal/telemetry/tracing/log/config.go index 19399744a3..13b3b1e77f 100644 --- a/internal/telemetry/tracing/log/config.go +++ b/internal/telemetry/tracing/log/config.go @@ -12,13 +12,11 @@ import ( ) type Config struct { - Fraction float64 - MetricPrefix string + Fraction float64 } func NewTracer(rawConfig map[string]interface{}) (err error) { c := new(Config) - c.MetricPrefix, _ = rawConfig["metricprefix"].(string) fraction, ok := rawConfig["fraction"].(string) if ok { c.Fraction, err = strconv.ParseFloat(fraction, 32) @@ -41,6 +39,6 @@ func (c *Config) NewTracer() error { return err } - tracing.T = &Tracer{Tracer: *(otel.NewTracer(c.MetricPrefix, sampler, exporter))} - return nil + tracing.T, err = otel.NewTracer(sampler, exporter) + return err } diff --git a/internal/telemetry/tracing/otel/config.go b/internal/telemetry/tracing/otel/config.go index 4deb8d77e0..b03c8df020 100644 --- a/internal/telemetry/tracing/otel/config.go +++ b/internal/telemetry/tracing/otel/config.go @@ -12,15 +12,13 @@ import ( ) type Config struct { - Fraction float64 - MetricPrefix string - Endpoint string + Fraction float64 + Endpoint string } func NewTracerFromConfig(rawConfig map[string]interface{}) (err error) { c := new(Config) c.Endpoint, _ = rawConfig["endpoint"].(string) - c.MetricPrefix, _ = rawConfig["metricprefix"].(string) fraction, ok := rawConfig["fraction"].(string) if ok { c.Fraction, err = strconv.ParseFloat(fraction, 32) @@ -39,6 +37,6 @@ func (c *Config) NewTracer() error { return err } - tracing.T = NewTracer(c.MetricPrefix, sampler, exporter) - return nil + tracing.T, err = NewTracer(sampler, exporter) + return err } diff --git a/internal/telemetry/tracing/otel/open_telemetry.go b/internal/telemetry/tracing/otel/open_telemetry.go index 5ce60b4de2..a3de6fb414 100644 --- a/internal/telemetry/tracing/otel/open_telemetry.go +++ b/internal/telemetry/tracing/otel/open_telemetry.go @@ -6,7 +6,9 @@ import ( "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/propagation" + "go.opentelemetry.io/otel/sdk/resource" sdk_trace "go.opentelemetry.io/otel/sdk/trace" + semconv "go.opentelemetry.io/otel/semconv/v1.7.0" api_trace "go.opentelemetry.io/otel/trace" "github.com/zitadel/zitadel/internal/telemetry/tracing" @@ -17,19 +19,30 @@ type Tracer struct { sampler sdk_trace.Sampler } -func NewTracer(name string, sampler sdk_trace.Sampler, exporter sdk_trace.SpanExporter) *Tracer { +func NewTracer(sampler sdk_trace.Sampler, exporter sdk_trace.SpanExporter) (*Tracer, error) { + resource, err := resource.Merge( + resource.Default(), + resource.NewWithAttributes( + semconv.SchemaURL, + semconv.ServiceNameKey.String("ZITADEL"), + ), + ) + if err != nil { + return nil, err + } spanProcessor := sdk_trace.NewBatchSpanProcessor(exporter) tp := sdk_trace.NewTracerProvider( sdk_trace.WithSampler(sampler), sdk_trace.WithBatcher(exporter), sdk_trace.WithSpanProcessor(spanProcessor), + sdk_trace.WithResource(resource), ) otel.SetTracerProvider(tp) tc := propagation.TraceContext{} otel.SetTextMapPropagator(tc) - return &Tracer{Exporter: tp.Tracer(name), sampler: sampler} + return &Tracer{Exporter: tp.Tracer(""), sampler: sampler}, nil } func (t *Tracer) Sampler() sdk_trace.Sampler { diff --git a/internal/telemetry/tracing/span.go b/internal/telemetry/tracing/span.go index d49090f34b..9e6f3bc287 100644 --- a/internal/telemetry/tracing/span.go +++ b/internal/telemetry/tracing/span.go @@ -1,9 +1,10 @@ package tracing import ( - grpc_errs "github.com/zitadel/zitadel/internal/api/grpc/errors" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" + + grpc_errs "github.com/zitadel/zitadel/internal/api/grpc/errors" ) type Span struct {