Skip to content

Commit

Permalink
draft: added OTLP GRPC options
Browse files Browse the repository at this point in the history
Signed-off-by: Aditi Ahuja <[email protected]>
  • Loading branch information
metonymic-smokey committed Jun 22, 2022
1 parent 0f9e8b3 commit 35b1933
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
1 change: 0 additions & 1 deletion cmd/thanos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func main() {

var g run.Group
var tracer opentracing.Tracer

// Setup optional tracing.
{
var (
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ require github.com/stretchr/testify v1.7.1
require (
go.opentelemetry.io/otel/exporters/jaeger v1.7.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.3.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.3.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.3.0
)

Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1946,6 +1946,7 @@ go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.3.0 h1:R/OBkMoGgfy2fLh
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.3.0/go.mod h1:VpP4/RMn8bv8gNo9uK7/IMY4mtWLELsS+JIP0inH0h4=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.3.0 h1:giGm8w67Ja7amYNfYMdme7xSp2pIxThWopw8+QP51Yk=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.3.0/go.mod h1:hO1KLR7jcKaDDKDkvI9dP/FIhpmna5lkqPUQdEjFAM8=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.3.0 h1:VQbUHoJqytHHSJ1OZodPH9tvZZSVzUHjPHpkO85sT6k=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.3.0/go.mod h1:keUU7UfnwWTWpJ+FWnyqmogPa82nuU5VUANFq49hlMY=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.3.0 h1:Ydage/P0fRrSPpZeCVxzjqGcI6iVmG2xb43+IR8cjqM=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.3.0/go.mod h1:QNX1aly8ehqqX1LEa6YniTU7VY9I6R3X/oPxhGdTceE=
Expand Down
72 changes: 60 additions & 12 deletions pkg/tracing/otlp/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@ package otlp

import (
"context"
"errors"
"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/otlptracegrpc"
"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"
_ "google.golang.org/grpc/encoding/gzip"
"gopkg.in/yaml.v2"
)

type Config struct {
ClientType string `yaml:"client)type"`
ReconnectionPeriod time.Duration `yaml:"reconnection_period"`
Compression string `yaml:"compression"`
Insecure bool `yaml:"insecure"`
Expand All @@ -28,20 +31,40 @@ type Config struct {
Timeout time.Duration `yaml:"timeout"`
}

// add TLS config and HTTP headers

var (
TracingClientGRPC string = "grpc"
TracingClientHTTP string = "http"
)

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

options := traceOptions(config)
var exporter *otlptrace.Exporter
if config.ClientType == TracingClientHTTP {
var err error
options := traceHTTPOptions(config)

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
client := otlptracehttp.NewClient(options...)
exporter, err = otlptrace.New(ctx, client)
if err != nil {
return nil, err
}
} else if config.ClientType == TracingClientGRPC {
var err error
options := traceGRPCOptions(config)
client := otlptracegrpc.NewClient(options...)
exporter, err = otlptrace.New(ctx, client)
if err != nil {
return nil, err
}
} else {
return nil, errors.New("otlp: invalid client type. Only 'http' and 'grpc' are accepted. ")
}

tp := tracesdk.NewTracerProvider(
Expand All @@ -56,7 +79,34 @@ func NewTracerProvider(ctx context.Context, logger log.Logger, conf []byte) (*tr
return tp, nil
}

func traceOptions(config Config) []otlptracehttp.Option {
func traceGRPCOptions(config Config) []otlptracegrpc.Option {
var options []otlptracegrpc.Option
if config.Endpoint != "" {
options = append(options, otlptracegrpc.WithEndpoint(config.Endpoint))
}

if config.Insecure {
options = append(options, otlptracegrpc.WithInsecure())
}

if config.ReconnectionPeriod != 0 {
options = append(options, otlptracegrpc.WithReconnectionPeriod(config.ReconnectionPeriod))
}

if config.Timeout != 0 {
options = append(options, otlptracegrpc.WithTimeout(config.Timeout))
}

if config.Compression != "" {
if config.Compression == "gzip" {
options = append(options, otlptracegrpc.WithCompressor(config.Compression))
}
}

return options
}

func traceHTTPOptions(config Config) []otlptracehttp.Option {
var options []otlptracehttp.Option
if config.Endpoint != "" {
options = append(options, otlptracehttp.WithEndpoint(config.Endpoint))
Expand All @@ -71,10 +121,8 @@ func traceOptions(config Config) []otlptracehttp.Option {
}

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.Compression == "gzip" {
options = append(options, otlptracehttp.WithCompression(otlptracehttp.GzipCompression))
}
}

Expand Down

0 comments on commit 35b1933

Please sign in to comment.