Skip to content

Commit

Permalink
adds futher changes
Browse files Browse the repository at this point in the history
  • Loading branch information
afzal442 committed Jun 17, 2023
1 parent 9282f60 commit c2ffb3d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 103 deletions.
69 changes: 1 addition & 68 deletions cmd/all-in-one/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,18 @@
package main

import (
"context"
"fmt"
"io"
"log"
"os"
"time"

"github.com/opentracing/opentracing-go"
"github.com/spf13/cobra"
"github.com/spf13/viper"
jaegerClientConfig "github.com/uber/jaeger-client-go/config"
jaegerClientZapLog "github.com/uber/jaeger-client-go/log/zap"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
otelBridge "go.opentelemetry.io/otel/bridge/opentracing"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
_ "go.uber.org/automaxprocs"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

agentApp "github.com/jaegertracing/jaeger/cmd/agent/app"
agentRep "github.com/jaegertracing/jaeger/cmd/agent/app/reporter"
Expand Down Expand Up @@ -285,28 +274,7 @@ func startQuery(
) *queryApp.Server {
spanReader = storageMetrics.NewReadMetricsDecorator(spanReader, baseFactory.Namespace(metrics.NSOptions{Name: "query"}))
qs := querysvc.NewQueryService(spanReader, depReader, *queryOpts)
// jtracer := jtracer.OT(opentracing.GlobalTracer())

ctx := context.Background()
exp, err := newExporter(ctx)
if err != nil {
log.Fatal(err)
}

tp := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exp),
sdktrace.WithResource(newResource()),
)

otelTracer := tp.Tracer("mytracer_name")
// Use the bridgeTracer as your OpenTracing tracer.
bridgeTracer, wrapperTracerProvider := otelBridge.NewTracerPair(otelTracer)
// Set the wrapperTracerProvider as the global OpenTelemetry
// TracerProvider so instrumentation will use it by default.
otel.SetTracerProvider(wrapperTracerProvider)

jtracer := jtracer.OT(bridgeTracer)

jtracer := jtracer.OT(opentracing.GlobalTracer())
server, err := queryApp.NewServer(svc.Logger, qs, metricsQueryService, qOpts, tm, jtracer)
if err != nil {
svc.Logger.Fatal("Could not start jaeger-query service", zap.Error(err))
Expand All @@ -322,41 +290,6 @@ func startQuery(
return server
}

// newExporter returns a console exporter.
func newExporter(ctx context.Context) (sdktrace.SpanExporter, error) {
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
conn, err := grpc.DialContext(ctx, "otel_collector:4317",
// Note the use of insecure transport here. TLS is recommended in production.
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
)
if err != nil {
return nil, fmt.Errorf("failed to create gRPC connection to collector: %w", err)
}

// Set up a trace exporter
traceExporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn))
if err != nil {
return nil, fmt.Errorf("failed to create trace exporter: %w", err)
}
return traceExporter, nil
}

// newResource returns a resource describing this application.
func newResource() *resource.Resource {
r, _ := resource.Merge(
resource.Default(),
resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceName("query"),
semconv.ServiceVersion("v0.1.0"),
attribute.String("environment", "demo"),
),
)
return r
}

func initTracer(svc *flags.Service) io.Closer {
logger := svc.Logger
traceCfg := &jaegerClientConfig.Configuration{
Expand Down
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.Init("frontend", otelExporter, metricsFactory, logger),
tracing.InitOP("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.Init("route", otelExporter, metricsFactory, logger),
tracing.InitOP("route", otelExporter, metricsFactory, logger),
logger,
)
return logError(zapLogger, server.Run())
Expand Down
45 changes: 15 additions & 30 deletions examples/hotrod/pkg/tracing/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,42 +44,28 @@ import (

var once sync.Once

// InitOTEL initializes OpenTelemetry SDK and uses OTel-OpenTracing Bridge
// to return an OpenTracing-compatible tracer.
// InitOTEL initializes OpenTelemetry SDK
// to return an Otel tracer.
func InitOTEL(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) trace.TracerProvider {
once.Do(func() {
otel.SetTextMapPropagator(
propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.Baggage{},
))
})
_, oteltp := InitBOTH(serviceName, exporterType, metricsFactory, logger)
otel.SetTracerProvider(oteltp)

exp, err := createOtelExporter(exporterType)
if err != nil {
logger.Bg().Fatal("cannot create exporter", zap.String("exporterType", exporterType), zap.Error(err))
}
logger.Bg().Debug("using " + exporterType + " trace exporter")
logger.Bg().Debug("Added OTEL tracer", zap.String("service-name", serviceName))
return oteltp
}

rpcmetricsObserver := rpcmetrics.NewObserver(metricsFactory, rpcmetrics.DefaultNameNormalizer)
// 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)

tp := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exp),
sdktrace.WithSpanProcessor(rpcmetricsObserver),
sdktrace.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(serviceName),
)),
)
otel.SetTracerProvider(tp)
// _, otelTracerProvider := otbridge.NewTracerPair(tp.Tracer(""))
logger.Bg().Debug("created OTEL->OT bridge", zap.String("service-name", serviceName))
return tp
return optracer
}

// Init initializes OpenTelemetry SDK and uses OTel-OpenTracing Bridge
// to return an OpenTracing-compatible tracer.
func Init(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) opentracing.Tracer {
// 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 @@ -105,8 +91,7 @@ func Init(serviceName string, exporterType string, metricsFactory metrics.Factor
)),
)
otTracer, _ := otbridge.NewTracerPair(tp.Tracer(""))
logger.Bg().Debug("created OTEL->OT bridge", zap.String("service-name", serviceName))
return otTracer
return otTracer, tp
}

// withSecure instructs the client to use HTTPS scheme, instead of hotrod's desired default HTTP
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.Init("customer", otelExporter, metricsFactory, logger),
tracer: tracing.InitOP("customer", otelExporter, metricsFactory, logger),
logger: logger,
database: newDatabase(
tracing.Init("mysql", otelExporter, metricsFactory, logger),
tracing.InitOP("mysql", otelExporter, metricsFactory, logger),
logger.With(zap.String("component", "mysql")),
),
}
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.Init("driver", otelExporter, metricsFactory, logger)
tracer := tracing.InitOP("driver", otelExporter, metricsFactory, logger)
server := grpc.NewServer(
grpc.UnaryInterceptor(otgrpc.OpenTracingServerInterceptor(tracer)),
grpc.StreamInterceptor(otgrpc.OpenTracingStreamServerInterceptor(tracer)),
Expand Down

0 comments on commit c2ffb3d

Please sign in to comment.