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();
}
Build Telemetry for Distributed Services之OpenTracing指导:C#的更多相关文章
- Build Telemetry for Distributed Services之OpenTracing实践
官网:https://opentracing.io/docs/best-practices/ Best Practices This page aims to illustrate common us ...
- Build Telemetry for Distributed Services之OpenTracing项目
中文文档地址:https://wu-sheng.gitbooks.io/opentracing-io/content/pages/quick-start.html 中文github地址:https:/ ...
- Build Telemetry for Distributed Services之OpenTracing简介
官网地址:https://opentracing.io/ What is Distributed Tracing? Who Uses Distributed Tracing? What is Open ...
- Build Telemetry for Distributed Services之Open Telemetry简介
官网链接:https://opentelemetry.io/about/ OpenTelemetry is the next major version of the OpenTracing and ...
- Build Telemetry for Distributed Services之Open Telemetry来历
官网:https://opentelemetry.io/ github:https://github.com/open-telemetry/ Effective observability requi ...
- Build Telemetry for Distributed Services之Jaeger
github链接:https://github.com/jaegertracing/jaeger 官网:https://www.jaegertracing.io/ Jaeger: open sourc ...
- Build Telemetry for Distributed Services之OpenCensus:C#
OpenCensus Easily collect telemetry like metrics and distributed traces from your services OpenCensu ...
- Build Telemetry for Distributed Services之Elastic APM
官网地址:https://www.elastic.co/guide/en/apm/get-started/current/index.html Overview Elastic APM is an a ...
- Build Telemetry for Distributed Services之OpenCensus:Tracing2(待续)
part 1:Tracing1 Sampling Sampling Samplers Global sampler Per span sampler Rules References
随机推荐
- DNS zone file
zone file格式最初由Berkeley Internet Name Domain(BIND)软件包使用,但已被其他DNS server software广泛采用.它们中的一些(例如NSD,Pow ...
- C# 直播录制视频
//项目引用 ffmpeg.exe 下载地址http://ffmpeg.org/ var time = DateTime.Now; ; //录制分钟 var fileName = Guid.NewGu ...
- CentOS 8 (1905)系统安装
本章内容: CentOS 8 的安装(CentOS-8-1905) 一.安装光盘,选择Install CentOS Linux 8.0.1905 二.选择系统语言,我这里选的是英文,也可以选择中文,往 ...
- python之列表、元组
Day 2-Morning 创建列表 创建列表和创建普通变量一样,用中括号括起一堆数据即可(这里的数据可以是整型.字符串.浮点型...甚至可以包含另一个列表),数据间用逗号隔开. eg:number= ...
- layer弹出框中icon数字参数说明
前言icon参数为0,如下代码: layer.msg(}); 运行结果如图: icon参数为1,如下图 icon参数为2,如下图: icon参数为3,如下图: icon参数为4,如下图: icon参数 ...
- FormCollection使用
FormCollection用来在controller中获取页面表单元素的数据.它是表单元素的集合,包括<input type="submit" />元素. 用法举例: ...
- 0009SpringBoot静态资源访问路径
访问静态资源: 静态资源需要放在哪些路径下才能被访问呢: 通过WebMvcAutoConfiguration.java中 addResourceHandlers()方法查找绑定的路径,一个是通过web ...
- uhd镜像重新安装
USRP LW-X300连接网口,设置IP地址:192.168.10.1,子网验码:255.255.255.0 连接后uhd_usrp_probe提示更新镜像. 终端输入: uhd_image_loa ...
- HTML5游戏2D开发引擎
1.PixiJS(基于webGl和canvas) 官网:http://www.pixijs.com/ github(star:20672):https://github.com/pixijs/pixi ...
- Dapper+Mysql 使用LIKE模糊查询写法踩坑
LIKE '%@Title%' 会解析成'%'@Title'%' 这里用拼接也是不行的'%'+@Title+'%' 只能用MySQL函数方法拼接 public dynamic GetListByFil ...