Skip to content

Commit

Permalink
Merge pull request #52 from CoderPoet/feat/spanname-formmater
Browse files Browse the repository at this point in the history
feat(tracing): support span name formatter
  • Loading branch information
GuangmingLuo authored May 16, 2024
2 parents 94b0649 + ceb595e commit c1931b4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
4 changes: 2 additions & 2 deletions tracing/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func ClientMiddleware(opts ...Option) client.Middleware {
// trace start
ctx, span := cfg.tracer.Start(
ctx,
clientSpanNaming(req),
cfg.clientSpanNameFormatter(req),
oteltrace.WithTimestamp(start),
oteltrace.WithSpanKind(oteltrace.SpanKindClient),
)
Expand Down Expand Up @@ -165,7 +165,7 @@ func ServerMiddleware(cfg *Config) app.HandlerFunc {
// set baggage
ctx = baggage.ContextWithBaggage(ctx, bags)

ctx, span := sTracer.Start(oteltrace.ContextWithRemoteSpanContext(ctx, spanCtx), serverSpanNaming(c), opts...)
ctx, span := sTracer.Start(oteltrace.ContextWithRemoteSpanContext(ctx, spanCtx), cfg.serverSpanNameFormatter(c), opts...)

// peer service attributes
span.SetAttributes(peerServiceAttributes...)
Expand Down
31 changes: 31 additions & 0 deletions tracing/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type Config struct {
clientHttpRouteFormatter func(req *protocol.Request) string
serverHttpRouteFormatter func(c *app.RequestContext) string

clientSpanNameFormatter func(req *protocol.Request) string
serverSpanNameFormatter func(c *app.RequestContext) string

tracerProvider trace.TracerProvider
meterProvider metric.MeterProvider
textMapPropagator propagation.TextMapPropagator
Expand Down Expand Up @@ -88,6 +91,9 @@ func defaultConfig() *Config {
clientHttpRouteFormatter: func(req *protocol.Request) string {
return string(req.Path())
},
clientSpanNameFormatter: func(req *protocol.Request) string {
return string(req.Method()) + " " + string(req.Path())
},
serverHttpRouteFormatter: func(c *app.RequestContext) string {
// FullPath returns a matched route full path. For not found routes
// returns an empty string.
Expand All @@ -98,6 +104,17 @@ func defaultConfig() *Config {
}
return route
},
serverSpanNameFormatter: func(c *app.RequestContext) string {
// Ref to https://github.com/open-telemetry/opentelemetry-specification/blob/ffddc289462dfe0c2041e3ca42a7b1df805706de/specification/trace/api.md#span
// FullPath returns a matched route full path. For not found routes
// returns an empty string.
route := c.FullPath()
// fall back to handler name
if route == "" {
route = string(c.Path())
}
return string(c.Method()) + " " + route
},
shouldIgnore: func(ctx context.Context, c *app.RequestContext) bool {
return false
},
Expand Down Expand Up @@ -139,6 +156,20 @@ func WithServerHttpRouteFormatter(serverHttpRouteFormatter func(c *app.RequestCo
})
}

// WithClientSpanNameFormatter configures clientSpanNameFormatter
func WithClientSpanNameFormatter(clientSpanNameFormatter func(req *protocol.Request) string) Option {
return option(func(cfg *Config) {
cfg.clientSpanNameFormatter = clientSpanNameFormatter
})
}

// WithServerSpanNameFormatter configures serverSpanNameFormatter
func WithServerSpanNameFormatter(serverSpanNameFormatter func(c *app.RequestContext) string) Option {
return option(func(cfg *Config) {
cfg.serverSpanNameFormatter = serverSpanNameFormatter
})
}

// WithShouldIgnore allows you to define the condition for enabling distributed tracing
func WithShouldIgnore(condition ConditionFunc) Option {
return option(func(cfg *Config) {
Expand Down
2 changes: 1 addition & 1 deletion tracing/tracer_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (s *serverTracer) Finish(ctx context.Context, c *app.RequestContext) {
// trace carrier from context
tc := internal.TraceCarrierFromContext(ctx)
if tc == nil {
hlog.Warnf("get tracer container failed")
hlog.Debugf("get tracer container failed")
return
}

Expand Down
16 changes: 0 additions & 16 deletions tracing/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,15 @@ import (
"fmt"
"time"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/tracer/stats"
"github.com/cloudwego/hertz/pkg/common/tracer/traceinfo"
"github.com/cloudwego/hertz/pkg/protocol"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
"go.opentelemetry.io/otel/trace"
)

// Ref to https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#name
// naming rule: $HandlerName:$FullPath
func serverSpanNaming(c *app.RequestContext) string {
handlerName := app.GetHandlerName(c.Handler())
if handlerName == "" {
handlerName = c.HandlerName()
}
return handlerName + ":" + c.FullPath()
}

func clientSpanNaming(req *protocol.Request) string {
return string(req.Path())
}

func handleErr(err error) {
if err != nil {
otel.Handle(err)
Expand Down

0 comments on commit c1931b4

Please sign in to comment.