feat: set service name in tracing (#3533)

This commit is contained in:
Livio Amstutz 2022-04-28 17:35:56 +02:00 committed by GitHub
parent 44a2b81bef
commit 2af3e228e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 21 deletions

View File

@ -12,15 +12,13 @@ import (
) )
type Config struct { type Config struct {
ProjectID string ProjectID string
MetricPrefix string Fraction float64
Fraction float64
} }
func NewTracer(rawConfig map[string]interface{}) (err error) { func NewTracer(rawConfig map[string]interface{}) (err error) {
c := new(Config) c := new(Config)
c.ProjectID, _ = rawConfig["projectid"].(string) c.ProjectID, _ = rawConfig["projectid"].(string)
c.MetricPrefix, _ = rawConfig["metricprefix"].(string)
fraction, ok := rawConfig["fraction"].(string) fraction, ok := rawConfig["fraction"].(string)
if ok { if ok {
c.Fraction, err = strconv.ParseFloat(fraction, 32) c.Fraction, err = strconv.ParseFloat(fraction, 32)
@ -43,7 +41,6 @@ func (c *Config) NewTracer() error {
return err return err
} }
tracing.T = &Tracer{Tracer: *(otel.NewTracer(c.MetricPrefix, sampler, exporter))} tracing.T, err = otel.NewTracer(sampler, exporter)
return err
return nil
} }

View File

@ -12,13 +12,11 @@ import (
) )
type Config struct { type Config struct {
Fraction float64 Fraction float64
MetricPrefix string
} }
func NewTracer(rawConfig map[string]interface{}) (err error) { func NewTracer(rawConfig map[string]interface{}) (err error) {
c := new(Config) c := new(Config)
c.MetricPrefix, _ = rawConfig["metricprefix"].(string)
fraction, ok := rawConfig["fraction"].(string) fraction, ok := rawConfig["fraction"].(string)
if ok { if ok {
c.Fraction, err = strconv.ParseFloat(fraction, 32) c.Fraction, err = strconv.ParseFloat(fraction, 32)
@ -41,6 +39,6 @@ func (c *Config) NewTracer() error {
return err return err
} }
tracing.T = &Tracer{Tracer: *(otel.NewTracer(c.MetricPrefix, sampler, exporter))} tracing.T, err = otel.NewTracer(sampler, exporter)
return nil return err
} }

View File

@ -12,15 +12,13 @@ import (
) )
type Config struct { type Config struct {
Fraction float64 Fraction float64
MetricPrefix string Endpoint string
Endpoint string
} }
func NewTracerFromConfig(rawConfig map[string]interface{}) (err error) { func NewTracerFromConfig(rawConfig map[string]interface{}) (err error) {
c := new(Config) c := new(Config)
c.Endpoint, _ = rawConfig["endpoint"].(string) c.Endpoint, _ = rawConfig["endpoint"].(string)
c.MetricPrefix, _ = rawConfig["metricprefix"].(string)
fraction, ok := rawConfig["fraction"].(string) fraction, ok := rawConfig["fraction"].(string)
if ok { if ok {
c.Fraction, err = strconv.ParseFloat(fraction, 32) c.Fraction, err = strconv.ParseFloat(fraction, 32)
@ -39,6 +37,6 @@ func (c *Config) NewTracer() error {
return err return err
} }
tracing.T = NewTracer(c.MetricPrefix, sampler, exporter) tracing.T, err = NewTracer(sampler, exporter)
return nil return err
} }

View File

@ -6,7 +6,9 @@ import (
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdk_trace "go.opentelemetry.io/otel/sdk/trace" sdk_trace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
api_trace "go.opentelemetry.io/otel/trace" api_trace "go.opentelemetry.io/otel/trace"
"github.com/zitadel/zitadel/internal/telemetry/tracing" "github.com/zitadel/zitadel/internal/telemetry/tracing"
@ -17,19 +19,30 @@ type Tracer struct {
sampler sdk_trace.Sampler 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) spanProcessor := sdk_trace.NewBatchSpanProcessor(exporter)
tp := sdk_trace.NewTracerProvider( tp := sdk_trace.NewTracerProvider(
sdk_trace.WithSampler(sampler), sdk_trace.WithSampler(sampler),
sdk_trace.WithBatcher(exporter), sdk_trace.WithBatcher(exporter),
sdk_trace.WithSpanProcessor(spanProcessor), sdk_trace.WithSpanProcessor(spanProcessor),
sdk_trace.WithResource(resource),
) )
otel.SetTracerProvider(tp) otel.SetTracerProvider(tp)
tc := propagation.TraceContext{} tc := propagation.TraceContext{}
otel.SetTextMapPropagator(tc) 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 { func (t *Tracer) Sampler() sdk_trace.Sampler {

View File

@ -1,9 +1,10 @@
package tracing package tracing
import ( import (
grpc_errs "github.com/zitadel/zitadel/internal/api/grpc/errors"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
grpc_errs "github.com/zitadel/zitadel/internal/api/grpc/errors"
) )
type Span struct { type Span struct {