[][src]Module opentelemetry::api::correlation

OpenTelemetry Correlation Context API

A Correlation Context is used to annotate telemetry, adding context and information to metrics, traces, and logs. It is an abstract data type represented by a set of name/value pairs describing user-defined properties. Each name in a CorrelationContext is associated with exactly one value. CorrelationContexts are serialized according to the editor's draft of the W3C Correlation Context specification.

Examples

use opentelemetry::api::{
    CorrelationContextExt, CorrelationContextPropagator, HttpTextFormat, Key
};
use std::collections::HashMap;

// Example correlation value passed in externally via http headers
let mut headers = HashMap::new();
headers.insert("Correlation-Context", "user_id=1".to_string());

let propagator = CorrelationContextPropagator::new();
// can extract from any type that impls `Carrier`, usually an HTTP header map
let cx = propagator.extract(&headers);

// Iterate over extracted name / value pairs
for (name, value) in cx.correlation_context() {
  // ...
}

// Add new correlations
let cx_with_additions = cx.with_correlations(vec![Key::new("server_id").u64(42)]);

// Inject correlations into http request
propagator.inject_context(&cx_with_additions, &mut headers);

let header_value = headers.get("Correlation-Context").expect("header is injected");
assert!(header_value.contains("user_id=1"), "still contains previous name / value");
assert!(header_value.contains("server_id=42"), "contains new name / value pair");

Structs

CorrelationContext

A set of name/value pairs describing user-defined properties across systems.

CorrelationContextPropagator

Propagates name/value pairs in W3C Correlation Context format.

Iter

An iterator over the entries of a CorrelationContext.

Traits

CorrelationContextExt

Methods for soring and retrieving correlation data in a context.