Skip to content

Commit

Permalink
reformats HTTPClient
Browse files Browse the repository at this point in the history
Signed-off-by: Afzal Ansari <[email protected]>

reformats init otel func

Signed-off-by: Afzal Ansari <[email protected]>
  • Loading branch information
afzal442 committed Jul 2, 2023
1 parent c3849c9 commit 44657ec
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 126 deletions.
7 changes: 7 additions & 0 deletions examples/hotrod/pkg/tracing/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ type HTTPClient struct {
Client *http.Client
}

func NewHTTPClient(tp trace.TracerProvider) HTTPClient {
return HTTPClient{
TracerProvider: tp,
Client: &http.Client{Transport: &otelhttp.Transport{}},
}
}

// GetJSON executes HTTP GET against specified url and tried to parse
// the response into out object.
func (c *HTTPClient) GetJSON(ctx context.Context, url string, out interface{}) error {
Expand Down
4 changes: 2 additions & 2 deletions examples/hotrod/pkg/tracing/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ import (
var once sync.Once

// InitOTEL initializes OpenTelemetry SDK.
func InitOTEL(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) trace.Tracer {
func InitOTEL(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) trace.TracerProvider {
_, oteltp := initBOTH(serviceName, exporterType, metricsFactory, logger)

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

// Init returns OTel-OpenTracing Bridge.
Expand Down
4 changes: 2 additions & 2 deletions examples/hotrod/pkg/tracing/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type TracedServeMux struct {
func (tm *TracedServeMux) Handle(pattern string, handler http.Handler) {
tm.logger.Bg().Debug("registering traced handler", zap.String("endpoint", pattern))

middleware := otelhttp.NewHandler(tm.mux, pattern,
middleware := otelhttp.NewHandler(handler, pattern,
otelhttp.WithTracerProvider(tm.tracer))
tm.mux.Handle(pattern, otelBaggageExtractor(middleware))
}
Expand All @@ -58,7 +58,7 @@ func (tm *TracedServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
tm.mux.ServeHTTP(w, r)
}

// Used with nethttp.MWSpanObserver above.
// Used with otelhttp.NewHandler above.
func otelBaggageExtractor(next http.Handler) http.Handler {
propagator := propagation.Baggage{}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
11 changes: 3 additions & 8 deletions examples/hotrod/services/customer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ package customer
import (
"context"
"fmt"
"net/http"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"

Expand All @@ -31,18 +29,15 @@ import (
// Client is a remote client that implements customer.Interface
type Client struct {
logger log.Factory
client *tracing.HTTPClient
client tracing.HTTPClient
hostPort string
}

// NewClient creates a new customer.Client
func NewClient(tracer trace.TracerProvider, logger log.Factory, hostPort string) *Client {
return &Client{
logger: logger,
client: &tracing.HTTPClient{
Client: &http.Client{Transport: &otelhttp.Transport{}},
TracerProvider: tracer,
},
logger: logger,
client: tracing.NewHTTPClient(tracer),
hostPort: hostPort,
}
}
Expand Down
21 changes: 13 additions & 8 deletions examples/hotrod/services/customer/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"context"
"errors"

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

"github.com/jaegertracing/jaeger/examples/hotrod/pkg/delay"
Expand All @@ -31,13 +31,13 @@ import (

// database simulates Customer repository implemented on top of an SQL database
type database struct {
tracer trace.Tracer
tracer opentracing.Tracer
logger log.Factory
customers map[string]*Customer
lock *tracing.Mutex
}

func newDatabase(tracer trace.Tracer, logger log.Factory) *database {
func newDatabase(tracer opentracing.Tracer, logger log.Factory) *database {
return &database{
tracer: tracer,
logger: logger,
Expand Down Expand Up @@ -73,10 +73,15 @@ func (d *database) Get(ctx context.Context, customerID string) (*Customer, error
d.logger.For(ctx).Info("Loading customer", zap.String("customer_id", customerID))

// simulate opentracing instrumentation of an SQL query
ctx, span := d.tracer.Start(ctx, "SQL SELECT", trace.WithSpanKind(trace.SpanKindClient))
// #nosec
span.SetAttributes(attribute.Key("sql.query").String("SELECT * FROM customer WHERE customer_id=" + customerID))
defer span.End()
if span := opentracing.SpanFromContext(ctx); span != nil {
span := d.tracer.StartSpan("SQL SELECT", opentracing.ChildOf(span.Context()))
tags.SpanKindRPCClient.Set(span)
tags.PeerService.Set(span, "mysql")
// #nosec
span.SetTag("sql.query", "SELECT * FROM customer WHERE customer_id="+customerID)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
}

if !config.MySQLMutexDisabled {
// simulate misconfigured connection pool that only gives one connection at a time
Expand Down
5 changes: 2 additions & 3 deletions examples/hotrod/services/customer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"encoding/json"
"net/http"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"

Expand All @@ -44,7 +43,7 @@ func NewServer(hostPort string, otelExporter string, metricsFactory metrics.Fact
tracer: tracing.InitOTEL("customer", otelExporter, metricsFactory, logger),
logger: logger,
database: newDatabase(
tracing.InitOTEL("mysql", otelExporter, metricsFactory, logger).Tracer("mysql"),
tracing.Init("mysql", otelExporter, metricsFactory, logger),
logger.With(zap.String("component", "mysql")),
),
}
Expand All @@ -59,7 +58,7 @@ func (s *Server) Run() error {

func (s *Server) createServeMux() http.Handler {
mux := tracing.NewServeMux(false, s.tracer, s.logger)
mux.Handle("/customer", otelhttp.WithRouteTag("/customer", http.HandlerFunc(s.customer)))
mux.Handle("/customer", http.HandlerFunc(s.customer))
return mux
}

Expand Down
3 changes: 2 additions & 1 deletion examples/hotrod/services/driver/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ type Redis struct {
}

func newRedis(otelExporter string, metricsFactory metrics.Factory, logger log.Factory) *Redis {
tp := tracing.InitOTEL("redis-manual", otelExporter, metricsFactory, logger)
return &Redis{
tracer: tracing.InitOTEL("redis-manual", otelExporter, metricsFactory, logger),
tracer: tp.Tracer("redis-manual"),
logger: logger,
}
}
Expand Down
90 changes: 0 additions & 90 deletions examples/hotrod/services/driver/server_test.go

This file was deleted.

7 changes: 3 additions & 4 deletions examples/hotrod/services/frontend/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"net/http"
"path"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"

Expand Down Expand Up @@ -79,9 +78,9 @@ func (s *Server) Run() error {
func (s *Server) createServeMux() http.Handler {
mux := tracing.NewServeMux(true, s.tracer, s.logger)
p := path.Join("/", s.basepath)
mux.Handle(p, otelhttp.WithRouteTag(p, http.StripPrefix(p, http.FileServer(s.assetFS))))
mux.Handle(path.Join(p, "/dispatch"), otelhttp.WithRouteTag(path.Join(p, "/dispatch"), http.HandlerFunc(s.dispatch)))
mux.Handle(path.Join(p, "/config"), otelhttp.WithRouteTag(path.Join(p, "/config"), http.HandlerFunc(s.config)))
mux.Handle(p, http.StripPrefix(p, http.FileServer(s.assetFS)))
mux.Handle(path.Join(p, "/dispatch"), http.HandlerFunc(s.dispatch))
mux.Handle(path.Join(p, "/config"), http.HandlerFunc(s.config))
return mux
}

Expand Down
11 changes: 3 additions & 8 deletions examples/hotrod/services/route/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ package route

import (
"context"
"net/http"
"net/url"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"

Expand All @@ -31,18 +29,15 @@ import (
// Client is a remote client that implements route.Interface
type Client struct {
logger log.Factory
client *tracing.HTTPClient
client tracing.HTTPClient
hostPort string
}

// NewClient creates a new route.Client
func NewClient(tracer trace.TracerProvider, logger log.Factory, hostPort string) *Client {
return &Client{
logger: logger,
client: &tracing.HTTPClient{
Client: &http.Client{Transport: &otelhttp.Transport{}},
TracerProvider: tracer,
},
logger: logger,
client: tracing.NewHTTPClient(tracer),
hostPort: hostPort,
}
}
Expand Down

0 comments on commit 44657ec

Please sign in to comment.