Skip to content

Commit

Permalink
[hotrod]: Upgrade redis service to native OTEL instrumentation
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?

Related to #3380 
Part of #3381 

- This adds `OTEL tracerProvider` and removes `OT tracer` for Redis
service hotROD app

Before:

![r2](https://github.com/jaegertracing/jaeger/assets/11625672/446d03b9-2da0-47b2-9312-b8bc86c7e78a)


After:

![r1](https://github.com/jaegertracing/jaeger/assets/11625672/ed3f2ca6-7a01-4ab2-a13c-56c1f3388fe6)

---------

Signed-off-by: afzal442 <[email protected]>
Co-authored-by: Afzal <[email protected]>
  • Loading branch information
afzal442 and sbin64 authored Jun 20, 2023
1 parent 050c632 commit 109b730
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
25 changes: 20 additions & 5 deletions examples/hotrod/pkg/tracing/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(
Expand All @@ -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
Expand Down
38 changes: 17 additions & 21 deletions examples/hotrod/services/driver/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

Expand All @@ -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,
Expand Down

0 comments on commit 109b730

Please sign in to comment.