Skip to content
Snippets Groups Projects
Commit 2152f73e authored by ale's avatar ale
Browse files

Drop the global Tracer instance

Use the preferred otel.GetTracerProvider() idiom and create dedicated
Tracer instances as needed, which seems to be best practice.
parent e0ceb958
Branches
No related tags found
No related merge requests found
...@@ -7,10 +7,10 @@ import ( ...@@ -7,10 +7,10 @@ import (
"net/url" "net/url"
"time" "time"
"git.autistici.org/ai3/go-common/tracing"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"github.com/go-ldap/ldap/v3" "github.com/go-ldap/ldap/v3"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
...@@ -158,14 +158,11 @@ func NewConnectionPool(uri, bindDN, bindPw string, cacheSize int) (*ConnectionPo ...@@ -158,14 +158,11 @@ func NewConnectionPool(uri, bindDN, bindPw string, cacheSize int) (*ConnectionPo
func (p *ConnectionPool) doRequest(ctx context.Context, name string, attrs []attribute.KeyValue, fn func(*ldap.Conn) error) error { func (p *ConnectionPool) doRequest(ctx context.Context, name string, attrs []attribute.KeyValue, fn func(*ldap.Conn) error) error {
// Tracing: initialize a new client span. // Tracing: initialize a new client span.
var span trace.Span ctx, span := otel.GetTracerProvider().Tracer("ldap").Start(ctx, name, trace.WithSpanKind(trace.SpanKindClient))
if tracing.Enabled { defer span.End()
ctx, span = tracing.Tracer.Start(ctx, name, trace.WithSpanKind(trace.SpanKindClient))
defer span.End()
if len(attrs) > 0 { if len(attrs) > 0 {
span.SetAttributes(attrs...) span.SetAttributes(attrs...)
}
} }
rerr := backoff.Retry(func() error { rerr := backoff.Retry(func() error {
...@@ -269,10 +266,6 @@ func isProtocolError(err error) bool { ...@@ -269,10 +266,6 @@ func isProtocolError(err error) bool {
} }
func setSpanStatus(span trace.Span, err error) { func setSpanStatus(span trace.Span, err error) {
if span == nil {
return
}
switch err { switch err {
case nil: case nil:
span.SetStatus(codes.Ok, "OK") span.SetStatus(codes.Ok, "OK")
......
...@@ -19,16 +19,12 @@ import ( ...@@ -19,16 +19,12 @@ import (
"go.opentelemetry.io/otel/sdk/resource" "go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace" "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0" semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
apitrace "go.opentelemetry.io/otel/trace"
) )
var ( var (
// Enabled reports whether tracing is globally enabled or not. // Enabled reports whether tracing is globally enabled or not.
Enabled bool Enabled bool
// Global Tracer instance.
Tracer apitrace.Tracer
initOnce sync.Once initOnce sync.Once
) )
...@@ -116,6 +112,9 @@ func initTracing(serviceName string) { ...@@ -116,6 +112,9 @@ func initTracing(serviceName string) {
return return
} }
// The sampling policy only applies to incoming requests for
// which tracing is not already enabled: in this case, we
// always pass-through.
var sampler trace.Sampler var sampler trace.Sampler
switch config.Sample { switch config.Sample {
case "", "always": case "", "always":
...@@ -132,13 +131,12 @@ func initTracing(serviceName string) { ...@@ -132,13 +131,12 @@ func initTracing(serviceName string) {
} }
tp := trace.NewTracerProvider( tp := trace.NewTracerProvider(
trace.WithSampler(sampler), trace.WithSampler(trace.ParentBased(sampler)),
trace.WithBatcher(ze), trace.WithBatcher(ze),
trace.WithResource(defaultResource(serviceName)), trace.WithResource(defaultResource(serviceName)),
) )
otel.SetTracerProvider(tp) otel.SetTracerProvider(tp)
Tracer = tp.Tracer(serviceName)
otel.SetTextMapPropagator( otel.SetTextMapPropagator(
propagation.NewCompositeTextMapPropagator( propagation.NewCompositeTextMapPropagator(
...@@ -163,7 +161,7 @@ func Init() { ...@@ -163,7 +161,7 @@ func Init() {
// Must call Init() first. // Must call Init() first.
func WrapTransport(t http.RoundTripper) http.RoundTripper { func WrapTransport(t http.RoundTripper) http.RoundTripper {
if Enabled { if Enabled {
t = othttp.NewTransport(t, othttp.WithPublicEndpoint()) t = othttp.NewTransport(t)
} }
return t return t
} }
...@@ -178,7 +176,10 @@ func WrapHandler(h http.Handler, endpointAddr string) http.Handler { ...@@ -178,7 +176,10 @@ func WrapHandler(h http.Handler, endpointAddr string) http.Handler {
} }
// Format span names with the request URL path. // Format span names with the request URL path.
return othttp.NewHandler(h, serviceName, othttp.WithSpanNameFormatter(func(op string, r *http.Request) string { return othttp.NewHandler(
return r.URL.Path h, serviceName,
})) othttp.WithSpanNameFormatter(func(op string, r *http.Request) string {
return r.URL.Path
}),
)
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment