Skip to content

Commit 1f7f264

Browse files
authored
Change distributed context to propagation (open-telemetry#61)
This patch moves the B3 and trace context propagators into the api/trace module and renames the distributed context module to `propagation`. It also incorporates the doc changes from open-telemetry/opentelemetry-specification#416
1 parent faf2802 commit 1f7f264

10 files changed

Lines changed: 109 additions & 47 deletions

File tree

src/api/distributed_context/mod.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/api/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
//! which implements the delivery of the telemetry. The application must also configure exporters
1515
//! so that the SDK knows where and how to deliver the telemetry.
1616
pub mod core;
17-
pub mod distributed_context;
1817
pub mod metrics;
18+
pub mod propagation;
1919
pub mod trace;
2020

2121
pub use self::core::{Key, KeyValue, Unit, Value};
22-
pub use distributed_context::b3_propagator::B3Propagator;
2322
pub use metrics::{
2423
counter::{Counter, CounterHandle},
2524
gauge::{Gauge, GaugeHandle},
@@ -28,15 +27,17 @@ pub use metrics::{
2827
value::MeasurementValue,
2928
Instrument, InstrumentHandle, LabelSet, Measurement, Meter, MetricOptions,
3029
};
30+
pub use propagation::{binary_propagator::BinaryFormat, text_propagator::HttpTextFormat, Carrier};
3131
pub use trace::{
32+
b3_propagator::B3Propagator,
3233
event::Event,
3334
link::Link,
3435
noop::{NoopProvider, NoopSpan, NoopTracer},
35-
propagator::{BinaryFormat, Carrier, HttpTextFormat},
3636
provider::Provider,
3737
sampler::{Sampler, SamplingDecision, SamplingResult},
3838
span::{Span, SpanKind, SpanStatus},
3939
span_context::{SpanContext, TRACE_FLAGS_UNUSED, TRACE_FLAG_SAMPLED},
4040
span_processor::SpanProcessor,
41+
trace_context_propagator::TraceContextPropagator,
4142
tracer::{Tracer, TracerGenerics},
4243
};

src/api/distributed_context/binary_propagator.rs renamed to src/api/propagation/binary_propagator.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
use crate::api;
99
use std::convert::TryInto;
1010

11+
/// Used to serialize and deserialize `SpanContext`s to and from a binary
12+
/// representation.
13+
pub trait BinaryFormat {
14+
/// Serializes span context into a byte array and returns the array.
15+
fn to_bytes(&self, context: &api::SpanContext) -> [u8; 29];
16+
17+
/// Deserializes a span context from a byte array.
18+
fn from_bytes(&self, bytes: Vec<u8>) -> api::SpanContext;
19+
}
20+
1121
/// Extracts and injects `SpanContext`s from byte arrays.
1222
#[derive(Debug, Default)]
1323
pub struct BinaryPropagator {}
@@ -19,7 +29,7 @@ impl BinaryPropagator {
1929
}
2030
}
2131

22-
impl api::BinaryFormat for BinaryPropagator {
32+
impl BinaryFormat for BinaryPropagator {
2333
/// Serializes span context into a byte array and returns the array.
2434
fn to_bytes(&self, context: &api::SpanContext) -> [u8; 29] {
2535
let mut res = [0u8; 29];
Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@
109109
//! - the key of the field.
110110
//! - the value of the field.
111111
//!
112+
//! The implementation SHOULD preserve casing (e.g. it should not transform
113+
//! `Content-Type` to `content-type`) if the used protocol is case insensitive,
114+
//! otherwise it MUST preserve casing.
115+
//!
112116
//! ### Extract
113117
//!
114118
//! Extracts the value from upstream. For example, as http headers.
@@ -138,42 +142,27 @@
138142
//!
139143
//! ##### Get
140144
//!
141-
//! Returns the first value of the given propagation key or returns `None`
142-
//! if the key doesn't exist.
145+
//! The Get function MUST return the first value of the given propagation
146+
//! key or return `None` if the key doesn't exist.
143147
//!
144148
//! Required arguments:
145149
//!
146-
//! - the carrier of propagation fields, such as an http request.
150+
//! - the carrier of propagation fields, such as an HTTP request.
147151
//! - the key of the field.
148152
//!
149-
//! Returns the first value of the given propagation key or returns `None`
150-
//! if the key doesn't exist.
153+
//! The `get` function is responsible for handling case sensitivity. If
154+
//! the getter is intended to work with an HTTP request object, the getter
155+
//! MUST be case insensitive. To improve compatibility with other text-based
156+
//! protocols, text format implementations MUST ensure to always use the
157+
//! canonical casing for their attributes. NOTE: Canonical casing for HTTP
158+
//! headers is usually title case (e.g. `Content-Type` instead of `content-type`).
159+
//!
151160
use crate::api;
152161
use std::collections::HashMap;
153162

154-
/// Used to serialize and deserialize `SpanContext`s to and from a binary
155-
/// representation.
156-
pub trait BinaryFormat {
157-
/// Serializes span context into a byte array and returns the array.
158-
fn to_bytes(&self, context: &api::SpanContext) -> [u8; 29];
159-
160-
/// Deserializes a span context from a byte array.
161-
fn from_bytes(&self, bytes: Vec<u8>) -> api::SpanContext;
162-
}
163-
164-
///is used to inject and extract a value as text into carriers that travel
165-
/// in-band across process boundaries.
166-
pub trait HttpTextFormat {
167-
/// Properly encodes the values of the `SpanContext` and injects them
168-
/// into the `Carrier`.
169-
fn inject(&self, context: api::SpanContext, carrier: &mut dyn Carrier);
170-
171-
/// Retrieves encoded `SpanContext`s using the `Carrier`. It decodes
172-
/// the `SpanContext` and returns it. If no `SpanContext` was retrieved
173-
/// OR if the retrieved SpanContext is invalid then an empty `SpanContext`
174-
/// is returned.
175-
fn extract(&self, carrier: &dyn Carrier) -> api::SpanContext;
176-
}
163+
pub mod binary_propagator;
164+
pub mod noop;
165+
pub mod text_propagator;
177166

178167
/// Carriers provide an interface for adding and removing fields from an
179168
/// underlying struct like `HashMap`.

src/api/propagation/noop.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//! # No-op OpenTelemetry Propagation Implementation
2+
//!
3+
//! This implementation is useful for testing purposes as it is intended
4+
//! to have minimal resource utilization and runtime impact.
5+
use crate::api;
6+
7+
/// A no-op instance of a `HttpTextFormat`.
8+
#[derive(Debug)]
9+
pub struct NoopTextFormat {}
10+
11+
impl api::HttpTextFormat for NoopTextFormat {
12+
/// Ignores calls to `inject`
13+
fn inject(&self, _context: api::SpanContext, _carrier: &mut dyn api::Carrier) {
14+
// Ignored
15+
}
16+
17+
/// Always returns invalid span contexts
18+
fn extract(&self, _carrier: &dyn api::Carrier) -> api::SpanContext {
19+
api::SpanContext::new(0, 0, 0, false)
20+
}
21+
}
22+
23+
/// A no-op instance of `BinaryFormat`
24+
#[derive(Debug)]
25+
pub struct NoopBinaryFormat {}
26+
27+
impl api::BinaryFormat for NoopBinaryFormat {
28+
fn to_bytes(&self, _context: &api::SpanContext) -> [u8; 29] {
29+
[0; 29]
30+
}
31+
32+
/// Always returns invalid span contexts
33+
fn from_bytes(&self, _bytes: Vec<u8>) -> api::SpanContext {
34+
api::SpanContext::new(0, 0, 0, false)
35+
}
36+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//! # Text Propagator
2+
//!
3+
//! `HttpTextFormat` is a formatter to serialize and deserialize a
4+
//! value into a text format.
5+
use crate::api;
6+
7+
///is used to inject and extract a value as text into carriers that travel
8+
/// in-band across process boundaries.
9+
pub trait HttpTextFormat {
10+
/// Properly encodes the values of the `SpanContext` and injects them
11+
/// into the `Carrier`.
12+
fn inject(&self, context: api::SpanContext, carrier: &mut dyn api::Carrier);
13+
14+
/// Retrieves encoded `SpanContext`s using the `Carrier`. It decodes
15+
/// the `SpanContext` and returns it. If no `SpanContext` was retrieved
16+
/// OR if the retrieved SpanContext is invalid then an empty `SpanContext`
17+
/// is returned.
18+
fn extract(&self, carrier: &dyn api::Carrier) -> api::SpanContext;
19+
}
File renamed without changes.

src/api/trace/mod.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
//!
3535
//! `Span`s encapsulate:
3636
//!
37-
//! - The operation name
37+
//! - The span name
3838
//! - An immutable `SpanContext` that uniquely identifies the `Span`
3939
//! - A parent span in the form of a `SpanContext`, or None
4040
//! - A start timestamp
@@ -44,6 +44,24 @@
4444
//! - A list of timestamped `Event`s
4545
//! - A `Status`.
4646
//!
47+
//! The _span name_ is a human-readable string which concisely identifies the work
48+
//! represented by the `Span`, for example, an RPC method name, a function name,
49+
//! or the name of a subtask or stage within a larger computation. The span name
50+
//! should be the most general string that identifies a (statistically) interesting
51+
//! _class of Spans_, rather than individual Span instances. That is, "get_user" is
52+
//! a reasonable name, while "get_user/314159", where "314159" is a user ID, is not
53+
//! a good name due to its high cardinality.
54+
//!
55+
//! For example, here are potential span names for an endpoint that gets a
56+
//! hypothetical account information:
57+
//!
58+
//! | Span Name | Guidance |
59+
//! | ------------------------- | ------------ |
60+
//! | `get` | Too general |
61+
//! | `get_account/42` | Too specific |
62+
//! | `get_account` | Good, and account_id=42 would make a nice Span attribute |
63+
//! | `get_account/{accountId}` | Also good (using the "HTTP route") |
64+
//!
4765
//! The `Span`'s start and end timestamps reflect the elapsed real time of the
4866
//! operation. A `Span`'s start time SHOULD be set to the current time on span
4967
//! creation. After the `Span` is created, it SHOULD be possible to
@@ -91,14 +109,15 @@
91109
//! Please review the W3C specification for details on the [Tracestate
92110
//! field](https://www.w3.org/TR/trace-context/#tracestate-field).
93111
//!
112+
pub mod b3_propagator;
94113
pub mod event;
95114
pub mod futures;
96115
pub mod link;
97116
pub mod noop;
98-
pub mod propagator;
99117
pub mod provider;
100118
pub mod sampler;
101119
pub mod span;
102120
pub mod span_context;
103121
pub mod span_processor;
122+
pub mod trace_context_propagator;
104123
pub mod tracer;
File renamed without changes.

src/exporter/trace/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub struct SpanData {
6868
pub parent_span_id: u64,
6969
/// Span kind
7070
pub span_kind: api::SpanKind,
71-
/// Span operation name
71+
/// Span name
7272
pub name: String,
7373
/// Span start time
7474
pub start_time: SystemTime,

0 commit comments

Comments
 (0)