Skip to content

Commit

Permalink
created package tracing/otlp
Browse files Browse the repository at this point in the history
Signed-off-by: Aditi Ahuja <ahuja.aditi@gmail.com>
  • Loading branch information
metonymic-smokey committed Jun 17, 2022
1 parent 7d3b4fe commit e9e42d2
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 38 deletions.
5 changes: 0 additions & 5 deletions cmd/thanos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -83,7 +82,6 @@ func main() {

var g run.Group
var tracer opentracing.Tracer
var otelTracer trace.Tracer

// Setup optional tracing.
{
Expand All @@ -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.
Expand Down
41 changes: 8 additions & 33 deletions pkg/tracing/client/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{}
Expand Down Expand Up @@ -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)
}
Expand Down
81 changes: 81 additions & 0 deletions pkg/tracing/otlp/otlp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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"
)

// todo - attempt to read from dummy file
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 == true {
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
}

0 comments on commit e9e42d2

Please sign in to comment.