diff --git a/go.mod b/go.mod index a74bf70..69ed367 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,8 @@ require ( github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible github.com/sirupsen/logrus v1.9.0 go.opentelemetry.io/otel v1.11.2 + go.opentelemetry.io/otel/exporters/jaeger v1.11.2 + go.opentelemetry.io/otel/exporters/zipkin v1.11.2 go.opentelemetry.io/otel/sdk v1.11.2 gopkg.in/yaml.v3 v3.0.1 ) @@ -18,7 +20,8 @@ require ( github.com/go-logr/stdr v1.2.2 // indirect github.com/jonboulle/clockwork v0.3.0 // indirect github.com/lestrrat-go/strftime v1.0.6 // indirect + github.com/openzipkin/zipkin-go v0.4.1 // indirect github.com/pkg/errors v0.9.1 // indirect go.opentelemetry.io/otel/trace v1.11.2 // indirect - golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect + golang.org/x/sys v0.0.0-20221010170243-090e33056c14 // indirect ) diff --git a/go.sum b/go.sum index efa4431..019753b 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,8 @@ github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible h1:Y6sqxHMyB1D2YSzWkL github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA= github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205AhTIGQQ= github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw= +github.com/openzipkin/zipkin-go v0.4.1 h1:kNd/ST2yLLWhaWrkgchya40TJabe8Hioj9udfPcEO5A= +github.com/openzipkin/zipkin-go v0.4.1/go.mod h1:qY0VqDSN1pOBN94dBc6w2GJlWLiovAyg7Qt6/I9HecM= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -34,12 +36,17 @@ github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0 github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= go.opentelemetry.io/otel v1.11.2 h1:YBZcQlsVekzFsFbjygXMOXSs6pialIZxcjfO/mBDmR0= go.opentelemetry.io/otel v1.11.2/go.mod h1:7p4EUV+AqgdlNV9gL97IgUZiVR3yrFXYo53f9BM3tRI= +go.opentelemetry.io/otel/exporters/jaeger v1.11.2 h1:ES8/j2+aB+3/BUw51ioxa50V9btN1eew/2J7N7n1tsE= +go.opentelemetry.io/otel/exporters/jaeger v1.11.2/go.mod h1:nwcF/DK4Hk0auZ/a5vw20uMsaJSXbzeeimhN5f9d0Lc= +go.opentelemetry.io/otel/exporters/zipkin v1.11.2 h1:wGdWn04d1sEnxfO4TUF/UcQfEIu80IvqUXU1lENKyFg= +go.opentelemetry.io/otel/exporters/zipkin v1.11.2/go.mod h1:I60/FdYilVKkuDOzenyp8LqJLryRC/Mr918G5hchvkM= go.opentelemetry.io/otel/sdk v1.11.2 h1:GF4JoaEx7iihdMFu30sOyRx52HDHOkl9xQ8SMqNXUiU= go.opentelemetry.io/otel/sdk v1.11.2/go.mod h1:wZ1WxImwpq+lVRo4vsmSOxdd+xwoUJ6rqyLc3SyX9aU= go.opentelemetry.io/otel/trace v1.11.2 h1:Xf7hWSF2Glv0DE3MH7fBHvtpSBsjcBUe5MYAmZM/+y0= @@ -47,8 +54,8 @@ go.opentelemetry.io/otel/trace v1.11.2/go.mod h1:4N+yC7QEz7TTsG9BSRLNAa63eg5E06O golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc= -golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20221010170243-090e33056c14 h1:k5II8e6QD8mITdi+okbbmR/cIyEbeXLBhy5Ha4nevyc= +golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/tracing/exporter/jaeger/exporter.go b/tracing/exporter/jaeger/exporter.go new file mode 100644 index 0000000..aed6ad9 --- /dev/null +++ b/tracing/exporter/jaeger/exporter.go @@ -0,0 +1,23 @@ +package jaeger + +import ( + "github.com/juju/errors" + "go.opentelemetry.io/otel/exporters/jaeger" + + sdktrace "go.opentelemetry.io/otel/sdk/trace" +) + +// NewExporter will create a Jaeger exporter for tracing. +func NewExporter(c *ConfigStruct) (sdktrace.SpanExporter, error) { + + err := c.IsValid() + if err != nil { + return nil, errors.Trace(err) + } + + return jaeger.New( + jaeger.WithCollectorEndpoint( + jaeger.WithEndpoint(c.URL), + ), + ) +} diff --git a/tracing/exporter/zipkin/exporter.go b/tracing/exporter/zipkin/exporter.go new file mode 100644 index 0000000..792d105 --- /dev/null +++ b/tracing/exporter/zipkin/exporter.go @@ -0,0 +1,21 @@ +package zipkin + +import ( + "github.com/juju/errors" + "go.opentelemetry.io/otel/exporters/zipkin" + + sdktrace "go.opentelemetry.io/otel/sdk/trace" +) + +// NewExporter will create a Zipkin exporter for tracing. +func NewExporter(c *ConfigStruct) (sdktrace.SpanExporter, error) { + + err := c.IsValid() + if err != nil { + return nil, errors.Trace(err) + } + + return zipkin.New( + c.URL, + ) +} diff --git a/tracing/tracing.go b/tracing/tracing.go index b125642..71b4c49 100644 --- a/tracing/tracing.go +++ b/tracing/tracing.go @@ -1,8 +1,11 @@ package tracing import ( + "git.dev.m-and-m.ovh/mderasse/gocommon/tracing/exporter/jaeger" + "git.dev.m-and-m.ovh/mderasse/gocommon/tracing/exporter/zipkin" "github.com/juju/errors" + "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/sdk/resource" sdktrace "go.opentelemetry.io/otel/sdk/trace" @@ -60,22 +63,70 @@ func InitFromCustomConfig(c *ConfigStruct) (*sdktrace.TracerProvider, error) { return nil, errors.Trace(err) } + // if disabled, never sample + if !c.Enabled { + return sdktrace.NewTracerProvider( + sdktrace.WithSampler(sdktrace.NeverSample()), + ), nil + } + + attributes := []attribute.KeyValue{ + semconv.ServiceNameKey.String(c.ServiceName), + } + + if c.ServiceVersion != "" { + attributes = append( + attributes, + semconv.ServiceVersionKey.String(c.ServiceVersion), + ) + } + + if c.ServiceInstanceID != "" { + attributes = append( + attributes, + semconv.ServiceInstanceIDKey.String(c.ServiceInstanceID), + ) + } + + for k, v := range c.Attributes { + attributes = append( + attributes, + attribute.String(k, v), + ) + } + // Ensure default SDK resources and the required service name are set. r, err := resource.Merge( resource.Default(), resource.NewWithAttributes( semconv.SchemaURL, - semconv.ServiceNameKey.String("myService"), - semconv.ServiceVersionKey.String("1.0.0"), - semconv.ServiceInstanceIDKey.String("abcdef12345"), + attributes..., ), ) - if err != nil { - panic(err) + return nil, errors.Trace(err) + } + + // Get the exporter + var exporter sdktrace.SpanExporter + + switch c.Exporter { + case ExporterName_JAEGER: + exporter, err = jaeger.NewExporter(c.JaegerConfig) + if err != nil { + return nil, errors.Trace(err) + } + case ExporterName_ZIPKIN: + exporter, err = zipkin.NewExporter(c.ZipkinConfig) + if err != nil { + return nil, errors.Trace(err) + } + default: + return nil, errors.BadRequestf("Exporter is not handled.") } return sdktrace.NewTracerProvider( sdktrace.WithResource(r), + sdktrace.WithBatcher(exporter), ), nil }