diff --git a/examples/hotrod/pkg/tracing/init.go b/examples/hotrod/pkg/tracing/init.go index e6dc142bdb6..367d328b30d 100644 --- a/examples/hotrod/pkg/tracing/init.go +++ b/examples/hotrod/pkg/tracing/init.go @@ -34,6 +34,7 @@ import ( "go.opentelemetry.io/otel/sdk/resource" sdktrace "go.opentelemetry.io/otel/sdk/trace" semconv "go.opentelemetry.io/otel/semconv/v1.7.0" + "go.opentelemetry.io/otel/trace" "go.uber.org/zap" "github.com/jaegertracing/jaeger/examples/hotrod/pkg/log" @@ -43,9 +44,24 @@ import ( var once sync.Once -// Init initializes OpenTelemetry SDK and uses OTel-OpenTracing Bridge -// to return an OpenTracing-compatible tracer. +// InitOTEL initializes OpenTelemetry SDK. +func InitOTEL(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) trace.Tracer { + _, oteltp := initBOTH(serviceName, exporterType, metricsFactory, logger) + + logger.Bg().Debug("Created OTEL tracer", zap.String("service-name", serviceName)) + return oteltp.Tracer(serviceName) +} + +// Init returns OTel-OpenTracing Bridge. func Init(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) opentracing.Tracer { + otTracer, _ := initBOTH(serviceName, exporterType, metricsFactory, logger) + + logger.Bg().Debug("Created OTEL->OT bridge", zap.String("service-name", serviceName)) + return otTracer +} + +// initBOTH initializes OpenTelemetry SDK and uses OTel-OpenTracing Bridge +func initBOTH(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) (opentracing.Tracer, trace.TracerProvider) { once.Do(func() { otel.SetTextMapPropagator( propagation.NewCompositeTextMapPropagator( @@ -70,9 +86,8 @@ func Init(serviceName string, exporterType string, metricsFactory metrics.Factor semconv.ServiceNameKey.String(serviceName), )), ) - otTracer, _ := otbridge.NewTracerPair(tp.Tracer("")) - logger.Bg().Debug("created OTEL->OT bridge", zap.String("service-name", serviceName)) - return otTracer + otTracer, _ := otbridge.NewTracerPair(tp.Tracer(serviceName)) + return otTracer, tp } // withSecure instructs the client to use HTTPS scheme, instead of hotrod's desired default HTTP diff --git a/examples/hotrod/services/driver/redis.go b/examples/hotrod/services/driver/redis.go index ae29785182b..1199017dfb0 100644 --- a/examples/hotrod/services/driver/redis.go +++ b/examples/hotrod/services/driver/redis.go @@ -22,8 +22,9 @@ import ( "math/rand" "sync" - "github.com/opentracing/opentracing-go" - "github.com/opentracing/opentracing-go/ext" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/codes" + "go.opentelemetry.io/otel/trace" "go.uber.org/zap" "github.com/jaegertracing/jaeger/examples/hotrod/pkg/delay" @@ -35,27 +36,24 @@ import ( // Redis is a simulator of remote Redis cache type Redis struct { - tracer opentracing.Tracer // simulate redis as a separate process + tracer trace.Tracer // simulate redis as a separate process logger log.Factory errorSimulator } func newRedis(otelExporter string, metricsFactory metrics.Factory, logger log.Factory) *Redis { return &Redis{ - tracer: tracing.Init("redis", otelExporter, metricsFactory, logger), + tracer: tracing.InitOTEL("redis-manual", otelExporter, metricsFactory, logger), logger: logger, } } // FindDriverIDs finds IDs of drivers who are near the location. func (r *Redis) FindDriverIDs(ctx context.Context, location string) []string { - if span := opentracing.SpanFromContext(ctx); span != nil { - span := r.tracer.StartSpan("FindDriverIDs", opentracing.ChildOf(span.Context())) - span.SetTag("param.location", location) - ext.SpanKindRPCClient.Set(span) - defer span.Finish() - ctx = opentracing.ContextWithSpan(ctx, span) - } + ctx, span := r.tracer.Start(ctx, "FindDriverIDs", trace.WithSpanKind(trace.SpanKindClient)) + span.SetAttributes(attribute.Key("param.driver.location").String(location)) + defer span.End() + // simulate RPC delay delay.Sleep(config.RedisFindDelay, config.RedisFindDelayStdDev) @@ -70,23 +68,21 @@ func (r *Redis) FindDriverIDs(ctx context.Context, location string) []string { // GetDriver returns driver and the current car location func (r *Redis) GetDriver(ctx context.Context, driverID string) (Driver, error) { - if span := opentracing.SpanFromContext(ctx); span != nil { - span := r.tracer.StartSpan("GetDriver", opentracing.ChildOf(span.Context())) - span.SetTag("param.driverID", driverID) - ext.SpanKindRPCClient.Set(span) - defer span.Finish() - ctx = opentracing.ContextWithSpan(ctx, span) - } + ctx, span := r.tracer.Start(ctx, "GetDriver", trace.WithSpanKind(trace.SpanKindClient)) + span.SetAttributes(attribute.Key("param.driverID").String(driverID)) + defer span.End() + // simulate RPC delay delay.Sleep(config.RedisGetDelay, config.RedisGetDelayStdDev) if err := r.checkError(); err != nil { - if span := opentracing.SpanFromContext(ctx); span != nil { - ext.Error.Set(span, true) - } + span.RecordError(err) + span.SetStatus(codes.Error, "An error occurred") r.logger.For(ctx).Error("redis timeout", zap.String("driver_id", driverID), zap.Error(err)) return Driver{}, err } + r.logger.For(ctx).Info("Got driver's ID", zap.String("driverID", driverID)) + // #nosec return Driver{ DriverID: driverID,