2020-11-20 07:57:39 +01:00
|
|
|
package otel
|
|
|
|
|
|
|
|
import (
|
2021-08-10 07:27:27 +02:00
|
|
|
"context"
|
2022-04-28 14:44:13 +02:00
|
|
|
"strconv"
|
2021-08-10 07:27:27 +02:00
|
|
|
|
|
|
|
otlpgrpc "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
|
2020-11-20 07:57:39 +01:00
|
|
|
sdk_trace "go.opentelemetry.io/otel/sdk/trace"
|
2025-02-04 09:55:26 +01:00
|
|
|
api_trace "go.opentelemetry.io/otel/trace"
|
2022-04-28 14:44:13 +02:00
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-11-20 07:57:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2022-04-28 17:35:56 +02:00
|
|
|
Fraction float64
|
|
|
|
Endpoint string
|
2020-11-20 07:57:39 +01:00
|
|
|
}
|
|
|
|
|
2022-04-28 14:44:13 +02:00
|
|
|
func NewTracerFromConfig(rawConfig map[string]interface{}) (err error) {
|
|
|
|
c := new(Config)
|
|
|
|
c.Endpoint, _ = rawConfig["endpoint"].(string)
|
2022-05-24 11:18:25 +02:00
|
|
|
c.Fraction, err = FractionFromConfig(rawConfig["fraction"])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.NewTracer()
|
|
|
|
}
|
|
|
|
|
|
|
|
func FractionFromConfig(i interface{}) (float64, error) {
|
|
|
|
if i == nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
switch fraction := i.(type) {
|
|
|
|
case float64:
|
|
|
|
return fraction, nil
|
|
|
|
case int:
|
|
|
|
return float64(fraction), nil
|
|
|
|
case string:
|
|
|
|
f, err := strconv.ParseFloat(fraction, 64)
|
2022-04-28 14:44:13 +02:00
|
|
|
if err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return 0, zerrors.ThrowInternal(err, "OTEL-SAfe1", "could not map fraction")
|
2022-04-28 14:44:13 +02:00
|
|
|
}
|
2022-05-24 11:18:25 +02:00
|
|
|
return f, nil
|
|
|
|
default:
|
2023-12-08 16:30:55 +02:00
|
|
|
return 0, zerrors.ThrowInternal(nil, "OTEL-Dd2s", "could not map fraction, unknown type")
|
2022-04-28 14:44:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 07:57:39 +01:00
|
|
|
func (c *Config) NewTracer() error {
|
2025-02-04 09:55:26 +01:00
|
|
|
sampler := NewSampler(sdk_trace.TraceIDRatioBased(c.Fraction))
|
2021-08-10 07:27:27 +02:00
|
|
|
exporter, err := otlpgrpc.New(context.Background(), otlpgrpc.WithEndpoint(c.Endpoint), otlpgrpc.WithInsecure())
|
2020-11-20 07:57:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-28 17:35:56 +02:00
|
|
|
tracing.T, err = NewTracer(sampler, exporter)
|
|
|
|
return err
|
2020-11-20 07:57:39 +01:00
|
|
|
}
|
2025-02-04 09:55:26 +01:00
|
|
|
|
|
|
|
// NewSampler returns a sampler decorator which behaves differently,
|
|
|
|
// based on the parent of the span. If the span has no parent and is of kind server,
|
|
|
|
// the decorated sampler is used to make sampling decision.
|
|
|
|
// If the span has a parent, depending on whether the parent is remote and whether it
|
|
|
|
// is sampled, one of the following samplers will apply:
|
|
|
|
// - remote parent sampled -> always sample
|
|
|
|
// - remote parent not sampled -> sample based on the decorated sampler (fraction based)
|
|
|
|
// - local parent sampled -> always sample
|
|
|
|
// - local parent not sampled -> never sample
|
|
|
|
func NewSampler(sampler sdk_trace.Sampler) sdk_trace.Sampler {
|
|
|
|
return sdk_trace.ParentBased(
|
|
|
|
tracing.SpanKindBased(sampler, api_trace.SpanKindServer),
|
|
|
|
sdk_trace.WithRemoteParentNotSampled(sampler),
|
|
|
|
)
|
|
|
|
}
|