Skip to content

Commit

Permalink
upgrade init otel
Browse files Browse the repository at this point in the history
Signed-off-by: afzal442 <afzal442@gmail.com>
  • Loading branch information
afzal442 committed Jun 19, 2023
1 parent 917be4d commit 3ec70cb
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 26 deletions.
2 changes: 1 addition & 1 deletion examples/hotrod/cmd/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var frontendCmd = &cobra.Command{
logger := log.NewFactory(zapLogger)
server := frontend.NewServer(
options,
tracing.InitOP("frontend", otelExporter, metricsFactory, logger),
tracing.Init("frontend", otelExporter, metricsFactory, logger),
logger,
)
return logError(zapLogger, server.Run())
Expand Down
2 changes: 1 addition & 1 deletion examples/hotrod/cmd/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var routeCmd = &cobra.Command{
logger := log.NewFactory(zapLogger)
server := route.NewServer(
net.JoinHostPort("0.0.0.0", strconv.Itoa(routePort)),
tracing.InitOP("route", otelExporter, metricsFactory, logger),
tracing.Init("route", otelExporter, metricsFactory, logger),
logger,
)
return logError(zapLogger, server.Run())
Expand Down
14 changes: 7 additions & 7 deletions examples/hotrod/pkg/tracing/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,26 @@ var once sync.Once

// InitOTEL initializes OpenTelemetry SDK
// to return an Otel tracer.
func InitOTEL(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) trace.TracerProvider {
_, oteltp := InitBOTH(serviceName, exporterType, metricsFactory, logger)
func InitOTEL(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) trace.Tracer {
_, oteltp := initBOTH(serviceName, exporterType, metricsFactory, logger)
otel.SetTracerProvider(oteltp)

logger.Bg().Debug("Added OTEL tracer", zap.String("service-name", serviceName))
return oteltp
return oteltp.Tracer(serviceName)
}

// InitOP initializes OTel-OpenTracing Bridge
// to return an OpenTracing-compatible tracer.
func InitOP(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) opentracing.Tracer {
optracer, _ := InitBOTH(serviceName, exporterType, metricsFactory, logger)
func Init(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) opentracing.Tracer {
optracer, _ := initBOTH(serviceName, exporterType, metricsFactory, logger)

logger.Bg().Debug("created OTEL->OT bridge", zap.String("service-name", serviceName))
return optracer
}

// 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) {
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 @@ -90,7 +90,7 @@ func InitBOTH(serviceName string, exporterType string, metricsFactory metrics.Fa
semconv.ServiceNameKey.String(serviceName),
)),
)
otTracer, _ := otbridge.NewTracerPair(tp.Tracer(""))
otTracer, _ := otbridge.NewTracerPair(tp.Tracer(serviceName))
return otTracer, tp
}

Expand Down
4 changes: 2 additions & 2 deletions examples/hotrod/services/customer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ type Server struct {
func NewServer(hostPort string, otelExporter string, metricsFactory metrics.Factory, logger log.Factory) *Server {
return &Server{
hostPort: hostPort,
tracer: tracing.InitOP("customer", otelExporter, metricsFactory, logger),
tracer: tracing.Init("customer", otelExporter, metricsFactory, logger),
logger: logger,
database: newDatabase(
tracing.InitOP("mysql", otelExporter, metricsFactory, logger),
tracing.Init("mysql", otelExporter, metricsFactory, logger),
logger.With(zap.String("component", "mysql")),
),
}
Expand Down
24 changes: 10 additions & 14 deletions examples/hotrod/services/driver/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,25 @@ import (

// Redis is a simulator of remote Redis cache
type Redis struct {
tracer trace.TracerProvider // 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.InitOTEL("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 {
tracer := r.tracer.Tracer("")
if _, span := tracer.Start(ctx, "FindDriverIDs", trace.WithSpanKind(trace.SpanKindClient) /*...set child of option*/); span != nil {
span.SetAttributes(attribute.Key("param.location").String(location))
_, span := r.tracer.Start(ctx, "FindDriverIDs", trace.WithSpanKind(trace.SpanKindClient))
span.SetAttributes(attribute.Key("param.driver.location").String(location))

defer span.End()

defer span.End()
ctx = trace.ContextWithSpan(ctx, span)
}
/* if span := opentracing.SpanFromContext(ctx); span != nil {
span := r.tracer.StartSpan("FindDriverIDs", opentracing.ChildOf(span.Context()))
span.SetTag("param.location", location)
Expand All @@ -77,13 +75,11 @@ 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) {
tracer := r.tracer.Tracer("")
if _, span := tracer.Start(ctx, "GetDriver", trace.WithSpanKind(trace.SpanKindClient) /*...set child of option*/); span != nil {
span.SetAttributes(attribute.Key("param.driverID").String(driverID))
_, span := r.tracer.Start(ctx, "GetDriver", trace.WithSpanKind(trace.SpanKindClient))
span.SetAttributes(attribute.Key("param.driverID").String(driverID))

defer span.End()

defer span.End()
ctx = trace.ContextWithSpan(ctx, span)
}
/* if span := opentracing.SpanFromContext(ctx); span != nil {
span := r.tracer.StartSpan("GetDriver", opentracing.ChildOf(span.Context()))
span.SetTag("param.driverID", driverID)
Expand Down
2 changes: 1 addition & 1 deletion examples/hotrod/services/driver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var _ DriverServiceServer = (*Server)(nil)

// NewServer creates a new driver.Server
func NewServer(hostPort string, otelExporter string, metricsFactory metrics.Factory, logger log.Factory) *Server {
tracer := tracing.InitOP("driver", otelExporter, metricsFactory, logger)
tracer := tracing.Init("driver", otelExporter, metricsFactory, logger)
server := grpc.NewServer(
grpc.UnaryInterceptor(otgrpc.OpenTracingServerInterceptor(tracer)),
grpc.StreamInterceptor(otgrpc.OpenTracingStreamServerInterceptor(tracer)),
Expand Down

0 comments on commit 3ec70cb

Please sign in to comment.