Build Telemetry for Distributed Services之OpenTracing指导:C#
官网链接:https://opentracing.io/guides/
官方微博:https://medium.com/opentracing
Welcome to the OpenTracing Guides!
Guides are “how-to manuals” for using OpenTracing. Unlike the Overview, guides are language specific, describing common scenarios and use cases that you many encounter in that environment.
C#
Usage
This is intended to be a general overview of using OpenTracing in a C# application.
Initialization
Initialization is OpenTracing-implementation-specific. Generally speaking, the pattern is to initialize a ITracer
once for the entire process and to use that ITracer
for the remainder of the process lifetime. It is a best practice to set the GlobalTracer, even if also making use of cleaner, more modern dependency injection. (See the next section below for rationale)
Accessing the ITracer
Where possible, use some form of dependency injection (of which there are many) to access the ITracer
instance. For vanilla application code, this is often reasonable and cleaner for all of the usual DI reasons.
That said, instrumentation for packages that are themselves statically configured (e.g., ODBC drivers) may be unable to make use of said DI mechanisms for ITracer
access, and as such they should fall back on GlobalTracer. By and large, OpenTracing instrumentation should always allow the programmer to specify a ITracer
instance to use for instrumentation, though the GlobalTracer is a reasonable fallback or default value.
Scopes and within-process propagation
For any thread, at most one ISpan
may be “active”. Of course, there may be many other spans involved with the thread which are (a) started, (b) not finished, and yet © not “active”: perhaps they are waiting for I/O, blocked on a child span, or otherwise off of the critical path.
It’s inconvenient to pass an active ISpan
from function to function manually, so OpenTracing requires that every ITracer
contains a IScopeManager
that grants access to the active ISpan
through a IScope
. Any ISpan
may be transferred to another callback or thread, but not IScope
; more on this below.
Accessing the active Span through IScope
Access to the active span is straightforward:
OpenTracing.ITracer tracer = ...;
...
IScope scope = tracer.ScopeManager.Active;
if (scope != null) {
scope.Span.Log("...");
}
Starting a new Span
Starting a new Span
The common case starts a IScope
that’s automatically registered for intra-process propagation via IScopeManager
.
Note that StartActive(finishSpanOnDispose: true)
finishes the span on IScope.Dispose()
.
OpenTracing.ITracer tracer = ...;
...
using (IScope scope = tracer.BuildSpan("someWork").StartActive(finishSpanOnDispose: true))
{
try
{
// Do things.
}
catch (Exception ex)
{
Tags.Error.Set(scope.Span, true);
}
// No need to call scope.Span.Finish() as we've set finishSpanOnDispose:true in StartActive.
}
If there is a IScope
, it will act as the parent to any newly started ISpan
unless the programmer invokes IgnoreActiveSpan()
at BuildSpan()
time or specified parent context explicitly:
OpenTracing.ITracer tracer = ...;
...
IScope scope = tracer.BuildSpan("someWork").IgnoreActiveSpan().StartActive(finishSpanOnDispose: true);
Using scopes with async/await
OpenTracing contains an IScopeManager
implementation that uses AsyncLocal
to flow spans with the execution. It is therefore possible to use scopes and spans with async/await
:
OpenTracing.ITracer tracer = ...;
...
using (IScope parentScope = tracer.BuildSpan("Parent").StartActive(finishSpanOnDispose: true))
{
await SomeAsynchronousWork();
// It's still possible to access the current span
parentScope.Span.Log(...);
// The child scope will automatically use parentScope as its parent.
using (IScope childScope = tracer.BuildSpan("Child").StartActive(finishSpanOnDispose: true))
{
childScope.Span.Log(...);
await SomeMoreAsynchronousWork();
childScope.Span.Log(...);
}
}
public async Task SomeAsynchronousWork()
{
// use ITracer.ActiveSpan to access the current span - which will be "parentScope.Span".
tracer.ActiveSpan.Log(...);
await SomeExternalCall();
}
public async Task SomeMoreAsynchronousWork()
{
// The active span in this case will be "childScope.Span".
tracer.ActiveSpan.Log(...);
await SomeExternalCall();
}
最新文章
- sphinx 注意点
- linux修改系统编码
- Maven实战(四)生命周期
- Nginx+tomcat 做负载均衡
- 学习笔记_过滤器应用(粗粒度权限控制(拦截是否登录、拦截用户名admin权限))
- 如何把select出来的一列数据放在第一个单元格
- 找两个string[ ]里不同的元素
- ASP渲染下拉框使时间依次减少
- 如何利用git shell提交代码到github
- java导出excel 浏览器直接下载或者或以文件形式导出
- jquery向Django后台发送数组
- 【Linux】基于VMware搭建Linux系统
- C语言求矩阵的逆
- MySQL 字符集utf8和utf-8的关系
- python 全栈开发,Day97(Token 认证的来龙去脉,DRF认证,DRF权限,DRF节流)
- springmvc文件上传下载简单实现案例(ssm框架使用)
- 第16课 右值引用(3)_std::forward与完美转发
- windows系统tomcat日志输出至catalina.out配置说明
- ASP.NET MVC3 Model验证总结 @Html.ValidationSummary(true)
- pl/sql快速输入select等语句