官网链接: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#的更多相关文章

  1. Build Telemetry for Distributed Services之OpenTracing实践

    官网:https://opentracing.io/docs/best-practices/ Best Practices This page aims to illustrate common us ...

  2. Build Telemetry for Distributed Services之OpenTracing项目

    中文文档地址:https://wu-sheng.gitbooks.io/opentracing-io/content/pages/quick-start.html 中文github地址:https:/ ...

  3. Build Telemetry for Distributed Services之OpenTracing简介

    官网地址:https://opentracing.io/ What is Distributed Tracing? Who Uses Distributed Tracing? What is Open ...

  4. Build Telemetry for Distributed Services之Open Telemetry简介

    官网链接:https://opentelemetry.io/about/ OpenTelemetry is the next major version of the OpenTracing and  ...

  5. Build Telemetry for Distributed Services之Open Telemetry来历

    官网:https://opentelemetry.io/ github:https://github.com/open-telemetry/ Effective observability requi ...

  6. Build Telemetry for Distributed Services之Jaeger

    github链接:https://github.com/jaegertracing/jaeger 官网:https://www.jaegertracing.io/ Jaeger: open sourc ...

  7. Build Telemetry for Distributed Services之OpenCensus:C#

    OpenCensus Easily collect telemetry like metrics and distributed traces from your services OpenCensu ...

  8. 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 ...

  9. Build Telemetry for Distributed Services之OpenCensus:Tracing2(待续)

    part 1:Tracing1 Sampling Sampling Samplers Global sampler Per span sampler Rules References

随机推荐

  1. 配置由Spring处理json乱码

    <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> < ...

  2. 利用setuptools发布Python程序到PyPI,为Python添砖加瓦

    pip install的东西从哪里来的? 从PyPI (Python Package Index)来的,官网是:  https://pypi.python.org/pypi/执行pip install ...

  3. 由java派生出来的证书错误

    未安装请求对应接口证书时的异常:> javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: ...

  4. nuxt入门

    之前一直都是做vue-spa单页面,不利于SEO.而便于SEO的SSR(服务器端渲染)多页应用,可以使用nuxt.js这个框架来实现 (0)nuxt安装 npx create-nuxt-app < ...

  5. jpa 如果返回java对象,会自动更新对象值(坑!!)

    //上一段示例代码List<Member> memberList = memberDao.findByLoginNameIn(names);for (Member m : memberLi ...

  6. linux基础知识和常用命令

    1.修改主机名 切换到root,然后执行 vim /etc/sysconfig/network,进入其中,更改hostname即可.insert进入修改,esc+:,wq保存退出. 2.用户切换 普通 ...

  7. c语言1博客作业07

    一.本周作业头 这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/SE2019-3/homework/9929 我 ...

  8. BZOJ 5494: [2019省队联测]春节十二响 (左偏树 可并堆)

    题意 略 分析 稍微yy一下可以感觉就是一个不同子树合并堆,然后考场上写了一发左偏树,以为100分美滋滋.然而发现自己傻逼了,两个堆一一对应合并后剩下的一坨直接一次合并进去就行了.然鹅我这个sb把所有 ...

  9. 使用docker 起容器配置负载均衡(加权)

    首先要准备三个nginx的容器: 第二个容器: 第三个容器: 进入第一个容器(主容器)  要配置的容器(docker exec -it 容器id /bin/bash)  vi/etc/nginx/ng ...

  10. pyinstaller打包好的.exe程序在别的电脑上运行出错

    打开.exe提示: Failed to execute script... 查看命令行错误提示为: 总的来说呢,就是有的版本pyqt5库对系统变量的加载存在bug,具体原因只有官方才能解释了,咱也没法 ...