聊聊dubbo-go的tracingFilter

本文主要研究一下dubbo-go的tracingFilter

tracingFilter

dubbo-go-v1.4.2/filter/filter_impl/tracing_filter.go

const (
    tracingFilterName = "tracing"
)

// this should be executed before users set their own Tracer
func init() {
    extension.SetFilter(tracingFilterName, newTracingFilter)
    opentracing.SetGlobalTracer(opentracing.NoopTracer{})
}

var (
    errorKey   = "ErrorMsg"
    successKey = "Success"
)

// if you wish to using opentracing, please add the this filter into your filter attribute in your configure file.
// notice that this could be used in both client-side and server-side.
type tracingFilter struct {
}
  • tracingFilter的init方法设置了newTracingFilter

newTracingFilter

dubbo-go-v1.4.2/filter/filter_impl/tracing_filter.go

func newTracingFilter() filter.Filter {
    if tracingFilterInstance == nil {
        tracingFilterInstance = &tracingFilter{}
    }
    return tracingFilterInstance
}
  • newTracingFilter方法实例化tracingFilter

Invoke

dubbo-go-v1.4.2/filter/filter_impl/tracing_filter.go

func (tf *tracingFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
    var (
        spanCtx context.Context
        span    opentracing.Span
    )
    operationName := invoker.GetUrl().ServiceKey() + "#" + invocation.MethodName()

    wiredCtx := ctx.Value(constant.TRACING_REMOTE_SPAN_CTX)
    preSpan := opentracing.SpanFromContext(ctx)

    if preSpan != nil {
        // it means that someone already create a span to trace, so we use the span to be the parent span
        span = opentracing.StartSpan(operationName, opentracing.ChildOf(preSpan.Context()))
        spanCtx = opentracing.ContextWithSpan(ctx, span)

    } else if wiredCtx != nil {

        // it means that there has a remote span, usually from client side. so we use this as the parent
        span = opentracing.StartSpan(operationName, opentracing.ChildOf(wiredCtx.(opentracing.SpanContext)))
        spanCtx = opentracing.ContextWithSpan(ctx, span)
    } else {
        // it means that there is not any span, so we create a span as the root span.
        span, spanCtx = opentracing.StartSpanFromContext(ctx, operationName)
    }

    defer func() {
        span.Finish()
    }()

    result := invoker.Invoke(spanCtx, invocation)
    span.SetTag(successKey, result.Error() != nil)
    if result.Error() != nil {
        span.LogFields(log.String(errorKey, result.Error().Error()))
    }
    return result
}
  • Invoke方法首先构建operationName,然后使用opentracing.StartSpan开启span,之后defer执行span.Finish(),然后执行invoker.Invoke(spanCtx, invocation),最后设置span.SetTag及span.LogFields

OnResponse

dubbo-go-v1.4.2/filter/filter_impl/tracing_filter.go

func (tf *tracingFilter) OnResponse(ctx context.Context, result protocol.Result,
    invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
    return result
}
  • OnResponse方法直接返回result

小结

tracingFilter的Invoke方法首先构建operationName,然后使用opentracing.StartSpan开启span,之后defer执行span.Finish(),然后执行invoker.Invoke(spanCtx, invocation),最后设置span.SetTag及span.LogFields

doc

  • tracing_filter


很遗憾的说,推酷将在这个月底关闭。人生海海,几度秋凉,感谢那些有你的时光。

原文 

https://segmentfault.com/a/1190000023339756

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » 聊聊dubbo-go的tracingFilter

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址