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
随机推荐
- FTP下载文件和操作系统的关系
标题不知道该怎么写了. 最近调试AGPS,嵌入式设备需要从FTP服务器上下载星历数据,星历数据是二进制数据.嵌入式设备下载完数据后和原始数据对比,发现数据量变大了(但是通过pc端的FTP软件下载下来的 ...
- PHP获取当前页面地址
#测试网址: http://localhost/blog/testurl.php?id=5 //获取域名或主机地址 echo $_SERVER['HTTP_HOST']."< ...
- SVN版本控制—branches、trunk、tag篇
新建资源仓库时,可选择默认创建三个文件夹.这三个文件夹分别是[trunk][branches][tags] [Trunk] 一般用于存放目前项目主线,也就是项目所有功能模块的集合体,一整个项目所有代码 ...
- Linux 01 LiunxvI命令大全
进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi +n filename :打开文件,并将光标置于第n行首 vi + filename :打开文件,并将光标置于最后 ...
- Derby 数据库基本操作 命令
0. 命令行 登录/退出 登录 java org.apache.derby.tools.ij ..\dirs>java org.apache.derby.tools.ij ij 版本 10.3 ...
- BCB6 使用TZCompressionStream压缩
最近由于项目需要涉及到解压第三方公司的数据,在此做一下记录环境部署和使用方法,免得以后忘记. 对方公司的数据是通过TCompressionStream 压缩之后,存到数据库中,采用的 ...
- Mysql批量更新的一个坑-&allowMultiQueries=true允许批量更新(转)
实际上,我们经常会遇到这样的需求,那就是利用Mybatis批量更新或者批量插入,但是,实际上即使Mybatis完美支持你的sql,你也得看看你说操作的数据库是否支持,而阿福,最近就遇到这样的一个坑. ...
- SIGAI机器学习第五集 贝叶斯分类器
讲授贝叶斯公式.朴素贝叶斯分类器.正态贝叶斯分类器的原理.实现以及实际应用 大纲: 贝叶斯公式(直接用贝叶斯公式完成分类,计算一个样本的特征向量X属于每个类c的概率,这个计算是通过贝叶斯公式来完成的. ...
- webpack+vue+Eslint+husky+lint-staged 统一项目编码规范
一. Eslint: 为什么我们要在项目中使用ESLint ESLint可以校验我们写的代码,给代码定义一个规范,项目里的代码必须按照这个规范写. 加入ESLint有非常多的好处,比如说可以帮助我们避 ...
- idea快捷方式1
Ctrl+Alt+O 优化导入的类和包 Alt+Insert 生成代码(如get,set方法,构造函数等) 或者右键(Generate) fori/sout/psvm + Tab Ctrl+Alt ...