Tracing standard¶
This page sets the rules for the spans ConnectSoft code creates with System.Diagnostics.ActivitySource. It is written for engineers who add or review tracing in services and libraries. How the OpenTelemetry tracing pipeline, exporters and automatic instrumentation are wired is explained in Distributed tracing; this page covers only what an activity source, a span and its tags must look like.
Rules¶
CS-TRC-001: Use one ActivitySource per library or service, named after its root namespace¶
Severity: major · Applies to: service, library, blazor, template · Enforced by: manual · Status: active
Why¶
An exporter receives the spans of a source only when the host subscribes to that source's name. A source created per class or per call, or named after something other than the root namespace, is easy to leave out of AddSource, and its spans then disappear without an error.
Do¶
- Declare one
static readonly ActivitySourceper library or service, in a<Name>Diagnosticsclass, as the library template does. - Keep the source name in a
public const stringequal to the root namespace, and use that constant wherever the name is needed.
Don't¶
- Create an
ActivitySourceper call, per instance or per class. - Name a source after a class or a feature instead of the root namespace.
Example¶
public static class OrdersDiagnostics
{
public const string ActivitySourceName = "Company.Orders";
public static readonly ActivitySource ActivitySource = new(ActivitySourceName);
}
How to check¶
- Every new
new ActivitySource(is astatic readonlyfield in the diagnostics class. - Its name is the
public const stringthat equals the root namespace.
CS-TRC-002: Register every activity source the host must export¶
Severity: major · Applies to: service, blazor, template · Enforced by: manual · Status: active
Why¶
ActivitySource.StartActivity returns null when nothing listens to the source. A host that does not call AddSource for a library's source therefore creates none of that library's spans, and the trace shows a gap where the library ran.
Do¶
- In the host's tracing setup, call
AddSourcewith the name constant of the host's own source and of every ConnectSoft library whose spans it should export. - When a wildcard is used, check that it matches each library's source name.
Don't¶
- Rely on a wildcard that does not match the library's source name.
- Copy a source name as a string literal when the library exposes it as a constant.
Example¶
tracingBuilder
.AddSource(OrdersDiagnostics.ActivitySourceName)
.AddSource(PaymentsClientDiagnostics.ActivitySourceName);
How to check¶
- Every
ActivitySourcename of the host and of each referenced ConnectSoft library that creates spans is covered by anAddSourcecall, by constant or by a matching wildcard.
CS-TRC-003: Mark a failed activity before the exception leaves it¶
Severity: major · Applies to: service, library, blazor, template · Enforced by: manual · Status: active
Why¶
An activity that ends without an error status shows the failed operation as successful in the trace view, while the log next to it shows the error. Whoever follows the trace stops at the wrong span.
Do¶
- In the
catchblock inside an activity's scope, callactivity?.SetStatus(ActivityStatusCode.Error, <description>)and record the exception withactivity?.AddException(exception)or OpenTelemetry'sRecordException, then rethrow. - Use a description that names the failure without data from the request, such as the exception type or a fixed text.
Don't¶
- Let an exception leave an activity's scope without setting the error status.
- Put a message that can carry personal data or secrets, such as a raw exception message from input validation or an HTTP response, in the status description.
Example¶
using var activity = OrdersDiagnostics.ActivitySource.StartActivity("ImportOrders");
try
{
await this.importer.ImportAsync(file, cancellationToken);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
activity?.SetStatus(ActivityStatusCode.Error, ex.GetType().Name);
activity?.AddException(ex);
throw;
}
How to check¶
- Every
catchblock inside an activity's scope sets the error status and records the exception before it rethrows. - No status description is built from request data or from a message that can contain it.
CS-TRC-004: Keep span names fixed and tags safe¶
Severity: major · Applies to: service, library, blazor, template · Enforced by: manual · Status: active
Why¶
A span name that contains an id or other input makes every span unique, so a trace backend cannot group or compare them. A tag that holds personal data or a secret is exported to every trace backend and kept as long as the traces are.
Do¶
- Name each span with a constant operation name, such as
ImportOrders. - Put identifiers in tags, and apply the same safety rules as for log properties (CS-LOG-005) and metric tags (CS-MET-005).
Don't¶
- Build a span name from input, such as an id, a path or a user name.
- Tag an activity with tokens, keys, passwords, e-mail addresses, personal names or request and response bodies.
Example¶
// Do
using var activity = OrdersDiagnostics.ActivitySource.StartActivity("ImportOrders");
activity?.SetTag("orders.batch_id", batchId);
// Don't
using var activity = OrdersDiagnostics.ActivitySource.StartActivity($"ImportOrders {batchId}");
activity?.SetTag("customer.email", customer.Email);
How to check¶
- Every
StartActivityname is a constant, not an interpolated or concatenated string. - No
SetTagorAddTagvalue comes from a secret, a personal field or a request or response body.
Related¶
- Distributed tracing explains the tracing pipeline.
- Logging standard
- Metrics standard
- Error handling standard
- Engineering standards