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(tracing): support span name formatter #52

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading