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:

  1. OpenTracing.ITracer tracer = ...;
  2. ...
  3. IScope scope = tracer.ScopeManager.Active;
  4. if (scope != null) {
  5. scope.Span.Log("...");
  6. }

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().

  1. OpenTracing.ITracer tracer = ...;
  2. ...
  3. using (IScope scope = tracer.BuildSpan("someWork").StartActive(finishSpanOnDispose: true))
  4. {
  5. try
  6. {
  7. // Do things.
  8. }
  9. catch (Exception ex)
  10. {
  11. Tags.Error.Set(scope.Span, true);
  12. }
  13. // No need to call scope.Span.Finish() as we've set finishSpanOnDispose:true in StartActive.
  14. }

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:

  1. OpenTracing.ITracer tracer = ...;
  2. ...
  3. 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:

  1. OpenTracing.ITracer tracer = ...;
  2. ...
  3. using (IScope parentScope = tracer.BuildSpan("Parent").StartActive(finishSpanOnDispose: true))
  4. {
  5. await SomeAsynchronousWork();
  6. // It's still possible to access the current span
  7. parentScope.Span.Log(...);
  8. // The child scope will automatically use parentScope as its parent.
  9. using (IScope childScope = tracer.BuildSpan("Child").StartActive(finishSpanOnDispose: true))
  10. {
  11. childScope.Span.Log(...);
  12. await SomeMoreAsynchronousWork();
  13. childScope.Span.Log(...);
  14. }
  15. }
  16. public async Task SomeAsynchronousWork()
  17. {
  18. // use ITracer.ActiveSpan to access the current span - which will be "parentScope.Span".
  19. tracer.ActiveSpan.Log(...);
  20. await SomeExternalCall();
  21. }
  22. public async Task SomeMoreAsynchronousWork()
  23. {
  24. // The active span in this case will be "childScope.Span".
  25. tracer.ActiveSpan.Log(...);
  26. await SomeExternalCall();
  27. }