mirror of
https://github.com/astaxie/beego.git
synced 2025-06-12 21:20:40 +00:00
Support opentracing filter
This commit is contained in:
@ -15,24 +15,39 @@
|
||||
package opentracing
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
logKit "github.com/go-kit/kit/log"
|
||||
opentracingKit "github.com/go-kit/kit/tracing/opentracing"
|
||||
"github.com/opentracing/opentracing-go"
|
||||
|
||||
beego "github.com/astaxie/beego/pkg"
|
||||
"github.com/astaxie/beego/pkg/context"
|
||||
beegoCtx "github.com/astaxie/beego/pkg/context"
|
||||
)
|
||||
|
||||
// FilterChainBuilder provides an extension point that we can support more configurations if necessary
|
||||
type FilterChainBuilder struct {
|
||||
// CustomSpanFunc makes users to custom the span.
|
||||
CustomSpanFunc func(span opentracing.Span, ctx *context.Context)
|
||||
CustomSpanFunc func(span opentracing.Span, ctx *beegoCtx.Context)
|
||||
}
|
||||
|
||||
|
||||
func (builder *FilterChainBuilder) FilterChain(next beego.FilterFunc) beego.FilterFunc {
|
||||
return func(ctx *context.Context) {
|
||||
return func(ctx *beegoCtx.Context) {
|
||||
var (
|
||||
spanCtx context.Context
|
||||
span opentracing.Span
|
||||
)
|
||||
operationName := builder.operationName(ctx)
|
||||
|
||||
span, spanCtx := opentracing.StartSpanFromContext(ctx.Request.Context(), operationName)
|
||||
if preSpan := opentracing.SpanFromContext(ctx.Request.Context()); preSpan == nil {
|
||||
inject := opentracingKit.HTTPToContext(opentracing.GlobalTracer(), operationName, logKit.NewNopLogger())
|
||||
spanCtx = inject(ctx.Request.Context(), ctx.Request)
|
||||
span = opentracing.SpanFromContext(spanCtx)
|
||||
} else {
|
||||
span, spanCtx = opentracing.StartSpanFromContext(ctx.Request.Context(), operationName)
|
||||
}
|
||||
|
||||
defer span.Finish()
|
||||
|
||||
newReq := ctx.Request.Clone(spanCtx)
|
||||
@ -49,7 +64,7 @@ func (builder *FilterChainBuilder) FilterChain(next beego.FilterFunc) beego.Filt
|
||||
}
|
||||
}
|
||||
|
||||
func (builder *FilterChainBuilder) operationName(ctx *context.Context) string {
|
||||
func (builder *FilterChainBuilder) operationName(ctx *beegoCtx.Context) string {
|
||||
operationName := ctx.Input.URL()
|
||||
// it means that there is not any span, so we create a span as the root span.
|
||||
// TODO, if we support multiple servers, this need to be changed
|
||||
|
Reference in New Issue
Block a user