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 {
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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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 {

View File

@ -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 {