Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added OTEL TracerProvider instead of opentracing.Tracer for hotROD example app service (Redis) #4533

Merged
merged 3 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 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,28 @@ import (

var once sync.Once

// Init initializes OpenTelemetry SDK and uses OTel-OpenTracing Bridge
// InitOTEL initializes OpenTelemetry SDK
// to return an Otel tracer.
afzal442 marked this conversation as resolved.
Show resolved Hide resolved
func InitOTEL(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) trace.Tracer {
_, oteltp := initBOTH(serviceName, exporterType, metricsFactory, logger)
otel.SetTracerProvider(oteltp)
afzal442 marked this conversation as resolved.
Show resolved Hide resolved

logger.Bg().Debug("Added OTEL tracer", zap.String("service-name", serviceName))
afzal442 marked this conversation as resolved.
Show resolved Hide resolved
return oteltp.Tracer(serviceName)
}

// InitOP initializes OTel-OpenTracing Bridge
// to return an OpenTracing-compatible tracer.
func Init(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) opentracing.Tracer {
optracer, _ := initBOTH(serviceName, exporterType, metricsFactory, logger)
afzal442 marked this conversation as resolved.
Show resolved Hide resolved

logger.Bg().Debug("created OTEL->OT bridge", zap.String("service-name", serviceName))
afzal442 marked this conversation as resolved.
Show resolved Hide resolved
return optracer
}
afzal442 marked this conversation as resolved.
Show resolved Hide resolved

// Init initializes OpenTelemetry SDK and uses OTel-OpenTracing Bridge
// to return an OpenTracing-compatible tracer and Otel tracer.
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 +90,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))
afzal442 marked this conversation as resolved.
Show resolved Hide resolved
return otTracer, tp
}

// withSecure instructs the client to use HTTPS scheme, instead of hotrod's desired default HTTP
Expand Down
32 changes: 21 additions & 11 deletions examples/hotrod/services/driver/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"math/rand"
"sync"

"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"

"github.com/jaegertracing/jaeger/examples/hotrod/pkg/delay"
Expand All @@ -35,27 +35,32 @@ 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.Start(ctx, "FindDriverIDs", trace.WithSpanKind(trace.SpanKindClient))
afzal442 marked this conversation as resolved.
Show resolved Hide resolved
span.SetAttributes(attribute.Key("param.driver.location").String(location))

defer span.End()
afzal442 marked this conversation as resolved.
Show resolved Hide resolved

/* if span := opentracing.SpanFromContext(ctx); span != nil {
afzal442 marked this conversation as resolved.
Show resolved Hide resolved
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)
}
} */
// simulate RPC delay
delay.Sleep(config.RedisFindDelay, config.RedisFindDelayStdDev)

Expand All @@ -70,23 +75,28 @@ 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.Start(ctx, "GetDriver", trace.WithSpanKind(trace.SpanKindClient))
span.SetAttributes(attribute.Key("param.driverID").String(driverID))

defer span.End()
afzal442 marked this conversation as resolved.
Show resolved Hide resolved

/* 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)
}
} */
// 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)
afzal442 marked this conversation as resolved.
Show resolved Hide resolved
}
// trace.Span.RecordError(span, err)
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