Skip to content

Commit

Permalink
Rename Correlation Context into Baggage (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyCpp authored Sep 3, 2020
1 parent 0e37632 commit dc6fd88
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 111 deletions.
6 changes: 3 additions & 3 deletions examples/basic-otlp/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use futures::stream::{Stream, StreamExt};
use opentelemetry::api::metrics::{self, MetricsError, ObserverResult};
use opentelemetry::api::{Context, CorrelationContextExt, Key, KeyValue, TraceContextExt, Tracer};
use opentelemetry::api::{Context, BaggageExt, Key, KeyValue, TraceContextExt, Tracer};
use opentelemetry::exporter;
use opentelemetry::sdk::metrics::PushController;
use opentelemetry::{global, sdk};
Expand Down Expand Up @@ -68,7 +68,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let value_recorder_two = meter.f64_value_recorder("ex.com.two").init();

let _correlations =
Context::current_with_correlations(vec![FOO_KEY.string("foo1"), BAR_KEY.string("bar1")])
Context::current_with_baggage(vec![FOO_KEY.string("foo1"), BAR_KEY.string("bar1")])
.attach();

let value_recorder = value_recorder_two.bind(COMMON_LABELS.as_ref());
Expand All @@ -83,7 +83,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

meter.record_batch_with_context(
// Note: call-site variables added as context Entries:
&Context::current_with_correlations(vec![ANOTHER_KEY.string("xyz")]),
&Context::current_with_baggage(vec![ANOTHER_KEY.string("xyz")]),
COMMON_LABELS.as_ref(),
vec![value_recorder_two.measurement(2.0)],
);
Expand Down
6 changes: 3 additions & 3 deletions examples/basic/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use futures::stream::{Stream, StreamExt};
use opentelemetry::api::metrics::{self, MetricsError, ObserverResult};
use opentelemetry::api::{Context, CorrelationContextExt, Key, KeyValue, TraceContextExt, Tracer};
use opentelemetry::api::{Context, BaggageExt, Key, KeyValue, TraceContextExt, Tracer};
use opentelemetry::exporter;
use opentelemetry::sdk::metrics::PushController;
use opentelemetry::{global, sdk};
Expand Down Expand Up @@ -64,7 +64,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let value_recorder_two = meter.f64_value_recorder("ex.com.two").init();

let _correlations =
Context::current_with_correlations(vec![FOO_KEY.string("foo1"), BAR_KEY.string("bar1")])
Context::current_with_baggage(vec![FOO_KEY.string("foo1"), BAR_KEY.string("bar1")])
.attach();

let value_recorder = value_recorder_two.bind(COMMON_LABELS.as_ref());
Expand All @@ -79,7 +79,7 @@ async fn main() -> Result<(), Box<dyn Error>> {

meter.record_batch_with_context(
// Note: call-site variables added as context Entries:
&Context::current_with_correlations(vec![ANOTHER_KEY.string("xyz")]),
&Context::current_with_baggage(vec![ANOTHER_KEY.string("xyz")]),
COMMON_LABELS.as_ref(),
vec![value_recorder_two.measurement(2.0)],
);
Expand Down
68 changes: 34 additions & 34 deletions src/api/correlation/mod.rs → src/api/baggage/mod.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
//! # OpenTelemetry Correlation Context API
//! # OpenTelemetry Baggage API
//!
//! A Correlation Context is used to annotate telemetry, adding context and
//! Baggage 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.
//! `CorrelationContext`s are serialized according to the editor's draft of
//! the [W3C Correlation Context] specification.
//! Each name in a [`Baggage`] is associated with exactly one value.
//! `Baggage`s are serialized according to the editor's draft of
//! the [W3C Baggage] specification.
//!
//! [`CorrelationContext`]: struct.CorrelationContext.html
//! [W3C Correlation Context]: https://w3c.github.io/correlation-context/
//! [`CorrelationContext`]: struct.Baggage.html
//! [W3C Baggage]: https://w3c.github.io/baggage/
//!
//! # Examples
//!
//! ```
//! use opentelemetry::api::{
//! CorrelationContextExt, CorrelationContextPropagator, TextMapFormat, Key
//! BaggageExt, BaggagePropagator, TextMapFormat, Key
//! };
//! use std::collections::HashMap;
//!
//! // Example correlation value passed in externally via http headers
//! // Example baggage value passed in externally via http headers
//! let mut headers = HashMap::new();
//! headers.insert("otcorrelations".to_string(), "user_id=1".to_string());
//!
//! let propagator = CorrelationContextPropagator::new();
//! let propagator = BaggagePropagator::new();
//! // can extract from any type that impls `Extractor`, usually an HTTP header map
//! let cx = propagator.extract(&headers);
//!
//! // Iterate over extracted name / value pairs
//! for (name, value) in cx.correlation_context() {
//! for (name, value) in cx.baggage() {
//! // ...
//! }
//!
//! // Add new correlations
//! let cx_with_additions = cx.with_correlations(vec![Key::new("server_id").u64(42)]);
//! // Add new baggage
//! let cx_with_additions = cx.with_baggage(vec![Key::new("server_id").u64(42)]);
//!
//! // Inject correlations into http request
//! // Inject aggage into http request
//! propagator.inject_context(&cx_with_additions, &mut headers);
//!
//! let header_value = headers.get("otcorrelations").expect("header is injected");
Expand All @@ -48,18 +48,18 @@ use std::iter::FromIterator;
mod propagation;

#[cfg(feature = "trace")]
pub use propagation::{CorrelationContextExt, CorrelationContextPropagator};
pub use propagation::{BaggageExt, BaggagePropagator};

/// A set of name/value pairs describing user-defined properties across systems.
#[derive(Debug, Default)]
pub struct CorrelationContext {
pub struct Baggage {
inner: HashMap<api::Key, api::Value>,
}

impl CorrelationContext {
/// Creates an empty `CorrelationContext`.
impl Baggage {
/// Creates an empty `Baggage`.
pub fn new() -> Self {
CorrelationContext {
Baggage {
inner: HashMap::default(),
}
}
Expand All @@ -69,9 +69,9 @@ impl CorrelationContext {
/// # Examples
///
/// ```
/// use opentelemetry::api::{CorrelationContext, Value};
/// use opentelemetry::api::{Baggage, Value};
///
/// let mut cc = CorrelationContext::new();
/// let mut cc = Baggage::new();
/// let _ = cc.insert("my-name", "my-value");
///
/// assert_eq!(cc.get("my-name"), Some(&Value::String("my-value".to_string())))
Expand All @@ -80,17 +80,17 @@ impl CorrelationContext {
self.inner.get(&key.into())
}

/// Inserts a name-value pair into the correlation context.
/// Inserts a name-value pair into the baggage.
///
/// If the name was not present, [`None`] is returned. If the name was present,
/// the value is updated, and the old value is returned.
///
/// # Examples
///
/// ```
/// use opentelemetry::api::{CorrelationContext, Value};
/// use opentelemetry::api::{Baggage, Value};
///
/// let mut cc = CorrelationContext::new();
/// let mut cc = Baggage::new();
/// let _ = cc.insert("my-name", "my-value");
///
/// assert_eq!(cc.get("my-name"), Some(&Value::String("my-value".to_string())))
Expand All @@ -103,29 +103,29 @@ impl CorrelationContext {
self.inner.insert(key.into(), value.into())
}

/// Removes a name from the correlation context, returning the value
/// Removes a name from the baggage, returning the value
/// corresponding to the name if the pair was previously in the map.
pub fn remove<K: Into<api::Key>>(&mut self, key: K) -> Option<api::Value> {
self.inner.remove(&key.into())
}

/// Returns the number of attributes for this correlation context
/// Returns the number of attributes for this baggage
pub fn len(&self) -> usize {
self.inner.len()
}

/// Returns `true` if the correlation context contains no items.
/// Returns `true` if the baggage contains no items.
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}

/// Gets an iterator over the correlation context items, sorted by name.
/// Gets an iterator over the baggage items, sorted by name.
pub fn iter(&self) -> Iter {
self.into_iter()
}
}

/// An iterator over the entries of a `CorrelationContext`.
/// An iterator over the entries of a `Baggage`.
#[derive(Debug)]
pub struct Iter<'a>(hash_map::Iter<'a, api::Key, api::Value>);
impl<'a> Iterator for Iter<'a> {
Expand All @@ -136,7 +136,7 @@ impl<'a> Iterator for Iter<'a> {
}
}

impl<'a> IntoIterator for &'a CorrelationContext {
impl<'a> IntoIterator for &'a Baggage {
type Item = (&'a api::Key, &'a api::Value);
type IntoIter = Iter<'a>;

Expand All @@ -145,17 +145,17 @@ impl<'a> IntoIterator for &'a CorrelationContext {
}
}

impl FromIterator<(api::Key, api::Value)> for CorrelationContext {
impl FromIterator<(api::Key, api::Value)> for Baggage {
fn from_iter<I: IntoIterator<Item = (api::Key, api::Value)>>(iter: I) -> Self {
CorrelationContext {
Baggage {
inner: iter.into_iter().collect(),
}
}
}

impl FromIterator<api::KeyValue> for CorrelationContext {
impl FromIterator<api::KeyValue> for Baggage {
fn from_iter<I: IntoIterator<Item = api::KeyValue>>(iter: I) -> Self {
CorrelationContext {
Baggage {
inner: iter.into_iter().map(|kv| (kv.key, kv.value)).collect(),
}
}
Expand Down
Loading

0 comments on commit dc6fd88

Please sign in to comment.