官网链接: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();
}

最新文章

  1. sphinx 注意点
  2. linux修改系统编码
  3. Maven实战(四)生命周期
  4. Nginx+tomcat 做负载均衡
  5. 学习笔记_过滤器应用(粗粒度权限控制(拦截是否登录、拦截用户名admin权限))
  6. 如何把select出来的一列数据放在第一个单元格
  7. 找两个string[ ]里不同的元素
  8. ASP渲染下拉框使时间依次减少
  9. 如何利用git shell提交代码到github
  10. java导出excel 浏览器直接下载或者或以文件形式导出
  11. jquery向Django后台发送数组
  12. 【Linux】基于VMware搭建Linux系统
  13. C语言求矩阵的逆
  14. MySQL 字符集utf8和utf-8的关系
  15. python 全栈开发,Day97(Token 认证的来龙去脉,DRF认证,DRF权限,DRF节流)
  16. springmvc文件上传下载简单实现案例(ssm框架使用)
  17. 第16课 右值引用(3)_std::forward与完美转发
  18. windows系统tomcat日志输出至catalina.out配置说明
  19. ASP.NET MVC3 Model验证总结 @Html.ValidationSummary(true)
  20. pl/sql快速输入select等语句

热门文章

  1. win10关闭防火墙和其通知
  2. git 从存储库中删除敏感数据(删除文件历史)
  3. bash基础——终端
  4. Django drf:分页器详解
  5. mysql打开报错2013解决办法
  6. Java&Selenium控制滚动条方法封装
  7. React中setState的怪异行为 ——setState没有即时生效
  8. asp.net core 读取appsettings.json配置项
  9. Oracle 物理结构(一) 文件-Inventory
  10. wamp大文件上传