tracing-stackdriver
tracing
is a scoped, structured logging and diagnostic system based on emitting Event
s in the context of potentially-nested Span
s across asynchronous await
points. These properties make tracing
ideal for use with Google Cloud Operations Suite structured logging (formerly Stackdriver).
This crate provides a Layer
for use with a tracing
Registry
that formats tracing
Spans and Events into properly-structured JSON for consumption by Google Operations Logging through the jsonPayload
field. This includes the following behaviors and enhancements:
rfc3339
-formatted timestamps for all Eventsseverity
(inLogSeverity
format) derived fromtracing
Level
target
derived from the Eventtarget
Metadata
- Span
name
and custom fields included under aspan
key - automatic nesting of
http_request.
-prefixed event fields - automatic nesting of
labels.
-prefixed event fields, re-written as a special field. - automatic re-writing of
insert_id
s as a special field. - automatic camelCase-ing of all field keys (e.g.
field_name
->fieldName
, orfield.name
->fieldName
) valuable
support, including anHttpRequest
helperstruct
- Cloud Trace support derived from OpenTelemetry Span and Trace IDs.
Examples
Basic setup:
use ;
Custom write location:
use ;
With httpRequest
fields:
See all available fields here.
// requires working global setup (see above examples)
use Request;
With labels
fields:
A key/value map of stringified labels mapped to the logging.googleapis.com/labels
special field. More information about labels
can be found here.
// requires working global setup (see above examples)
With insert_id
field:
A stringified insert_id
mapped to the logging.googleapis.com/insertId
special field. More information about insertId
can be found here. This is an optional field, as the Logging API assigns its own unique identifier to this field if insert_id
is omitted.
// requires working global setup (see above examples)
With more specific LogSeverity
levels:
Google supports a slightly different set of severity levels than tracing
. tracing
levels are automatically mapped to LogSeverity
levels, but you can customize the level beyond the intersection of tracing
levels and LogSeverity
levels by using the provided LogSeverity
level with a severity
key.
use LogSeverity;
With valuable
support:
tracing_stackdriver
supports deeply-nested structured logging through tracing
's unstable valuable
support. In addition, httpRequest
fields can be generated with the HttpRequest
helper struct exported from this library for better compile-time checking of fields.
To enable valuable
support, use the valuable
feature flag and compile your project with RUSTFLAGS="--cfg tracing_unstable"
.
// requires working global setup (see above examples)
use Request;
use HttpRequest;
use Valuable;
With Cloud Trace support:
tracing_stackdriver
supports integration with Cloud Trace and OpenTelemetry via tracing_opentelemetry and outputs special Cloud Trace LogEntry
fields for trace sampling and log correlation.
To enable Cloud Trace support, you need to enable the opentelemetry
feature flag and provide a CloudTraceConfiguration
to the with_cloud_trace
method of the layer.
use CloudTraceConfiguration;
With Source Locations:
By default, tracing_stackdriver
includes the source location of tracing
events in a special SourceLocation
composite field on the emitted LogEntry
. This behavior can be configured with the with_source_location
method of the layer.