[英]can I trace sub called function with OpenCensus?
I want to trace the whole project with Opencensus and Jaeger.我想用 Opencensus 和 Jaeger 追踪整个项目。 I added HTTP trace in entry services and add stratspan
in middleware surrounded whol my services and this two-span called and show on Jaeger.我在入口服务中添加了 HTTP 跟踪,并在中间件中添加了stratspan
,包围了我的stratspan
服务,这两个跨度在 Jaeger 上调用并显示。 My problem is each service contain a lot of function and I want see a trace of all my functions but in this way not show overall service not shown each function.我的问题是每个服务都包含很多功能,我想查看我所有功能的踪迹,但以这种方式不显示整体服务,未显示每个功能。 I don't like add per function add one stratspan
.我不喜欢为每个函数添加一个stratspan
。 I use ctx context.Context
entry all my function but not different!我使用ctx context.Context
条目我所有的功能但没有什么不同!
There's really not many options for other than starting a span in each function you'd like to instrument:除了在您想要检测的每个函数中启动一个跨度之外,实际上没有太多选择:
func something(ctx context.Context) {
ctx, span := trace.StartSpan(ctx, "something")
defer span.End()
}
If your functions have a common call signature, or you can coalesce your function into a common call signature, you can write a wrapper.如果您的函数有一个公共调用签名,或者您可以将您的函数合并为一个公共调用签名,则您可以编写一个包装器。 Examples of this can be seen in http "middleware" .这方面的例子可以在 http "middleware" 中看到。
Consider the http.Handler, you could write a decorator for your functions that handles the span lifecycle:考虑 http.Handler,您可以为处理 span 生命周期的函数编写一个装饰器:
func WithTraced(handler http.Handler, opName string) http.Handler {
return func(w http.ResponseWriter, r *http.Request) {
ctx, span := trace.StartSpan(ctx, opName)
defer span.End()
handler.ServeHTTP(w, r.WithContext(ctx))
}
}
A similar pattern could be applied by embedding structs.通过嵌入结构可以应用类似的模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.