From 96d2ccbc27da9c469f3513791ceca6e7674b921c Mon Sep 17 00:00:00 2001 From: Aditi Ahuja Date: Fri, 17 Jun 2022 21:09:55 +0530 Subject: [PATCH] created package tracing/otlp Signed-off-by: Aditi Ahuja --- cmd/thanos/main.go | 5 --- pkg/tracing/client/factory.go | 41 ++++-------------- pkg/tracing/otlp/otlp.go | 80 +++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 38 deletions(-) create mode 100644 pkg/tracing/otlp/otlp.go diff --git a/cmd/thanos/main.go b/cmd/thanos/main.go index 50fb4553412..b575c4f02c4 100644 --- a/cmd/thanos/main.go +++ b/cmd/thanos/main.go @@ -22,7 +22,6 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors" "github.com/prometheus/common/version" - "go.opentelemetry.io/otel/trace" "go.uber.org/automaxprocs/maxprocs" "gopkg.in/alecthomas/kingpin.v2" @@ -83,7 +82,6 @@ func main() { var g run.Group var tracer opentracing.Tracer - var otelTracer trace.Tracer // Setup optional tracing. { @@ -107,9 +105,6 @@ func main() { fmt.Fprintln(os.Stderr, errors.Wrapf(err, "tracing failed")) os.Exit(1) } - - otelTracer = client.NewOTELTracer(ctx, logger) - _ = otelTracer // just a dummy } // This is bad, but Prometheus does not support any other tracer injections than just global one. diff --git a/pkg/tracing/client/factory.go b/pkg/tracing/client/factory.go index 6c644b5dead..2ebe818c958 100644 --- a/pkg/tracing/client/factory.go +++ b/pkg/tracing/client/factory.go @@ -20,15 +20,7 @@ import ( "github.com/thanos-io/thanos/pkg/tracing/jaeger" "github.com/thanos-io/thanos/pkg/tracing/lightstep" "github.com/thanos-io/thanos/pkg/tracing/migration" - - "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/exporters/otlp/otlptrace" - "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" - "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/sdk/resource" - tracesdk "go.opentelemetry.io/otel/sdk/trace" - semconv "go.opentelemetry.io/otel/semconv/v1.4.0" - "go.opentelemetry.io/otel/trace" + "github.com/thanos-io/thanos/pkg/tracing/otlp" ) type TracingProvider string @@ -47,30 +39,6 @@ type TracingConfig struct { Config interface{} `yaml:"config"` } -// NewOTELTracer returns an OTLP exporter based tracer. -func NewOTELTracer(ctx context.Context, logger log.Logger) trace.Tracer { - client := otlptracehttp.NewClient() - exporter, err := otlptrace.New(ctx, client) - if err != nil { - level.Error(logger).Log("err with new client", err.Error()) - } - - tp := tracesdk.NewTracerProvider( - tracesdk.WithBatcher(exporter), - tracesdk.WithResource(resource.NewWithAttributes( - semconv.SchemaURL, - )), - ) - otel.SetTracerProvider(tp) - otel.SetTextMapPropagator(propagation.TraceContext{}) - - tracer := otel.GetTracerProvider().Tracer( - "thanos-tracer", - trace.WithSchemaURL(semconv.SchemaURL), - ) - return tracer -} - func NewTracer(ctx context.Context, logger log.Logger, metrics *prometheus.Registry, confContentYaml []byte) (opentracing.Tracer, io.Closer, error) { level.Info(logger).Log("msg", "loading tracing configuration") tracingConf := &TracingConfig{} @@ -107,6 +75,13 @@ func NewTracer(ctx context.Context, logger log.Logger, metrics *prometheus.Regis return elasticapm.NewTracer(config) case string(Lightstep): return lightstep.NewTracer(ctx, config) + case string(OpenTelemetryProtocol): + tracerProvider, err := otlp.NewTracerProvider(ctx, logger, config) + if err != nil { + return nil, nil, errors.Wrap(err, "new tracer provider err") + } + tracer, closerFunc := migration.Bridge(tracerProvider, logger) + return tracer, closerFunc, nil default: return nil, nil, errors.Errorf("tracing with type %s is not supported", tracingConf.Type) } diff --git a/pkg/tracing/otlp/otlp.go b/pkg/tracing/otlp/otlp.go new file mode 100644 index 00000000000..d040986ccc1 --- /dev/null +++ b/pkg/tracing/otlp/otlp.go @@ -0,0 +1,80 @@ +// Copyright (c) The Thanos Authors. +// Licensed under the Apache License 2.0. + +package otlp + +import ( + "context" + "time" + + "github.com/go-kit/log" + "github.com/go-kit/log/level" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/exporters/otlp/otlptrace" + "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" + "go.opentelemetry.io/otel/propagation" + "go.opentelemetry.io/otel/sdk/resource" + tracesdk "go.opentelemetry.io/otel/sdk/trace" + semconv "go.opentelemetry.io/otel/semconv/v1.4.0" + "gopkg.in/yaml.v2" +) + +type Config struct { + ReconnectionPeriod time.Duration `yaml:"reconnection_period"` + Compression string `yaml:"compression"` + Insecure bool `yaml:"insecure"` + Endpoint string `yaml:"endpoint"` + URLPath string `yaml:"url_path"` + Timeout time.Duration `yaml:"timeout"` +} + +// NewOTELTracer returns an OTLP exporter based tracer. +func NewTracerProvider(ctx context.Context, logger log.Logger, conf []byte) (*tracesdk.TracerProvider, error) { + config := Config{} + if err := yaml.Unmarshal(conf, &config); err != nil { + return nil, err + } + + var options []otlptracehttp.Option + if config.Endpoint != "" { + options = append(options, otlptracehttp.WithEndpoint(config.Endpoint)) + } + + if config.Insecure { + options = append(options, otlptracehttp.WithInsecure()) + } + + if config.URLPath != "" { + options = append(options, otlptracehttp.WithURLPath(config.URLPath)) + } + + if config.Compression != "" { + if config.Compression == "GzipCompression" { + // Todo: how to access otlpconfig.Compression here? + // Specifying 1 is just a workaround. + options = append(options, otlptracehttp.WithCompression(1)) + } + } + + if config.Timeout != 0 { + options = append(options, otlptracehttp.WithTimeout(config.Timeout)) + } + + client := otlptracehttp.NewClient(options...) + exporter, err := otlptrace.New(ctx, client) + if err != nil { + level.Error(logger).Log("err with new client", err.Error()) + return nil, err + } + + tp := tracesdk.NewTracerProvider( + tracesdk.WithBatcher(exporter), + tracesdk.WithResource(resource.NewWithAttributes( + semconv.SchemaURL, + )), + ) + otel.SetTracerProvider(tp) + otel.SetTextMapPropagator(propagation.TraceContext{}) + + return tp, nil +}