Expand description
Common telemetry tools.
We focus on supporting the following Rust APIs:
tracingfor tracing, with support for fowarding fromlog.metricsfor monitoring.
We specifically try to integrate with OpenTelemetry and to support standard
"traceparent" and "tracestate" headers.
§Usage
For a simple async CLI tool, you could use this library like this:
use anyhow::Result;
use opinionated_telemetry::{
run_with_telemetry, set_parent_span_from_env, AppType,
};
use tracing::instrument;
#[tokio::main]
async fn main() -> Result<()> {
run_with_telemetry(
AppType::Cli,
env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"),
main_helper(),
).await
}
// Note that `instrument` will only work correctly on functions called from
// inside `run_with_telemetry`.
#[instrument(
name = "my-app",
fields(version = env!("CARGO_PKG_VERSION"))
)]
async fn main_helper() -> Result<()> {
// Use TRACEPARENT and TRACESTATE from the environment to link into any
// existing trace. Or start a new trace if none are present.
set_parent_span_from_env();
Ok(())
}For more complex applications, you can use TelemetryConfig. For
synchronous applications, see [run_with_telemetry_sync] and
[TelemetryConfig::install_sync], which are available if the sync feature
is enabled.
§Features
sync: Enable synchronous telemetry support. Use this for otherwise synchronous applications. This is not enabled by default.
§Environment Variables
The following variables can be used to configure telemetry:
RUST_LOGcan be used to control our logging levels in the normal fashion.OPINIONATED_TELEMETRY_TRACERcan be set tocloud_traceordebugto enable OpenTelelmetry tracing. If not set, we will log to stderr usingtracing, honoring the filter specified byRUST_LOG.OPINIONATED_TELEMETRY_METRICScan be set toprometheusto enable Prometheus metrics, ordebugto log metrics. Otherwise metrics will not be reported.OPINIONATED_TELEMETRY_PROMETHEUS_LISTEN_ADDRdefaults to `“0.0.0.0:9090”.OPINIONATED_TELEMETRY_PROMETHEUS_PUSHGATEWAY_URLmust be specified for CLI tools using Prometheus. We strongly recommend usingprom-aggregation-gatewayinstead of Prometheus’s defaultpushgateway.OTEL_SERVICE_NAMEandOTEL_SERVICE_VERSIONcan be used to identify your service. If not set, we will use theservice_nameandservice_versionparameters toTelemetryConfig,run_with_telemetryor [run_with_telemetry_sync]. OtherOTEL_variables supported by theopentelemetrycrate may also be respected.
For CLI tools, these variables will normally be set by the calling app:
TRACEPARENTandTRACESTATEcan be passed to CLI tools to link them into an existing trace. These follow the conventions of the W3C Trace Context.
§Metric naming conventions
For best results across different metrics reporting systems, we recommend following the Prometheus metric naming conventions.
Example metric names:
myapp_requests_total: Counter with no units.myapp_processed_bytes_total: Counter with units.myapp_memory_usage_bytes: Gauge with units.
§Label naming
Labels should be “low-arity”. Specifically, that means that labels should have only a small number of possible values, because each possible label value will require most backends to store a new time series.
Re-exports§
Structs§
- Telemetry
Config - Interface used to configure and install telemetry.
- Telemetry
Handle - A handle that can be used to shut down telemetry and flush any remaining
data. If you do not call
TelemetryHandle::flush_and_shutdown, telemetry data may be lost.
Enums§
- AppType
- What type of application should we configure telemetry for? This will affect
how various kinds of telemetry are configured. For example,
AppType::Serverwould create a Prometheus scape endpoint, butAppType::Cliwould push metrics to a Prometheus push gateway once on shutdown. - Error
- A monitoring-related error.
Traits§
- SetParent
From Extractor - Trait that allows adding an external
opentelemetry::Contextto an existingtracing::Span.
Functions§
- current_
span_ as_ env - Export the current
tracing::Spanin a format suitable for passing totokio::process::Command::envs. - current_
span_ as_ headers - Export the current
tracing::Spanas HTTP-style headers stored in aHashMap. - inject_
current_ span_ into - Export the current
tracing::SpanusingInjector. - run_
with_ telemetry - Start all telemetry subsystems, run the given future, and then stop all telemetry subsystems.
- set_
parent_ span_ from - Set the parent of the current span using the given extractor. If no trace span can be found using the extractor, start a new trace instead
- set_
parent_ span_ from_ env - Set the parent of the current span using the environment. This will use
the
TRACEPARENTandTRACESTATEif present.
Type Aliases§
- Result
- The result type of this library.