Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: make TraceStateError priviate #755

Merged
merged 1 commit into from Mar 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion opentelemetry-api/src/trace/mod.rs
Expand Up @@ -145,7 +145,7 @@ mod tracer_provider;
pub use self::{
context::{get_active_span, mark_span_as_active, FutureExt, SpanRef, TraceContextExt},
span::{Span, SpanKind, StatusCode},
span_context::{SpanContext, SpanId, TraceFlags, TraceId, TraceState, TraceStateError},
span_context::{SpanContext, SpanId, TraceFlags, TraceId, TraceState},
tracer::{SamplingDecision, SamplingResult, SpanBuilder, Tracer},
tracer_provider::TracerProvider,
};
Expand Down
51 changes: 32 additions & 19 deletions opentelemetry-api/src/trace/span_context.rs
@@ -1,3 +1,4 @@
use crate::trace::{TraceError, TraceResult};
use std::collections::VecDeque;
use std::fmt;
use std::hash::Hash;
Expand Down Expand Up @@ -263,15 +264,15 @@ impl TraceState {
/// # Examples
///
/// ```
/// use opentelemetry_api::trace::{TraceState, TraceStateError};
/// use opentelemetry_api::trace::TraceState;
///
/// let kvs = vec![("foo", "bar"), ("apple", "banana")];
/// let trace_state: Result<TraceState, TraceStateError> = TraceState::from_key_value(kvs);
/// let trace_state = TraceState::from_key_value(kvs);
///
/// assert!(trace_state.is_ok());
/// assert_eq!(trace_state.unwrap().header(), String::from("foo=bar,apple=banana"))
/// ```
pub fn from_key_value<T, K, V>(trace_state: T) -> Result<Self, TraceStateError>
pub fn from_key_value<T, K, V>(trace_state: T) -> TraceResult<Self>
where
T: IntoIterator<Item = (K, V)>,
K: ToString,
Expand All @@ -282,10 +283,10 @@ impl TraceState {
.map(|(key, value)| {
let (key, value) = (key.to_string(), value.to_string());
if !TraceState::valid_key(key.as_str()) {
return Err(TraceStateError::InvalidKey(key));
return Err(TraceStateError::Key(key));
}
if !TraceState::valid_value(value.as_str()) {
return Err(TraceStateError::InvalidValue(value));
return Err(TraceStateError::Value(value));
}

Ok((key, value))
Expand Down Expand Up @@ -318,17 +319,17 @@ impl TraceState {
/// updated key/value is returned.
///
/// [W3 Spec]: https://www.w3.org/TR/trace-context/#mutating-the-tracestate-field
pub fn insert<K, V>(&self, key: K, value: V) -> Result<TraceState, TraceStateError>
pub fn insert<K, V>(&self, key: K, value: V) -> TraceResult<TraceState>
where
K: Into<String>,
V: Into<String>,
{
let (key, value) = (key.into(), value.into());
if !TraceState::valid_key(key.as_str()) {
return Err(TraceStateError::InvalidKey(key));
return Err(TraceStateError::Key(key).into());
}
if !TraceState::valid_value(value.as_str()) {
return Err(TraceStateError::InvalidValue(value));
return Err(TraceStateError::Value(value).into());
}

let mut trace_state = self.delete_from_deque(key.clone());
Expand All @@ -346,10 +347,10 @@ impl TraceState {
/// If the key is not in `TraceState`. The original `TraceState` will be cloned and returned.
///
/// [W3 Spec]: https://www.w3.org/TR/trace-context/#mutating-the-tracestate-field
pub fn delete<K: Into<String>>(&self, key: K) -> Result<TraceState, TraceStateError> {
pub fn delete<K: Into<String>>(&self, key: K) -> TraceResult<TraceState> {
let key = key.into();
if !TraceState::valid_key(key.as_str()) {
return Err(TraceStateError::InvalidKey(key));
return Err(TraceStateError::Key(key).into());
}

Ok(self.delete_from_deque(key))
Expand Down Expand Up @@ -387,15 +388,15 @@ impl TraceState {
}

impl FromStr for TraceState {
type Err = TraceStateError;
type Err = TraceError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let list_members: Vec<&str> = s.split_terminator(',').collect();
let mut key_value_pairs: Vec<(String, String)> = Vec::with_capacity(list_members.len());

for list_member in list_members {
match list_member.find('=') {
None => return Err(TraceStateError::InvalidList(list_member.to_string())),
None => return Err(TraceStateError::List(list_member.to_string()).into()),
Some(separator_index) => {
let (key, value) = list_member.split_at(separator_index);
key_value_pairs
Expand All @@ -411,18 +412,30 @@ impl FromStr for TraceState {
/// Error returned by `TraceState` operations.
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum TraceStateError {
/// The key is invalid. See <https://www.w3.org/TR/trace-context/#key> for requirement for keys.
enum TraceStateError {
/// The key is invalid.
///
/// See <https://www.w3.org/TR/trace-context/#key> for requirement for keys.
#[error("{0} is not a valid key in TraceState, see https://www.w3.org/TR/trace-context/#key for more details")]
InvalidKey(String),
Key(String),

/// The value is invalid. See <https://www.w3.org/TR/trace-context/#value> for requirement for values.
/// The value is invalid.
///
/// See <https://www.w3.org/TR/trace-context/#value> for requirement for values.
#[error("{0} is not a valid value in TraceState, see https://www.w3.org/TR/trace-context/#value for more details")]
InvalidValue(String),
Value(String),

/// The value is invalid. See <https://www.w3.org/TR/trace-context/#list> for requirement for list members.
/// The list is invalid.
///
/// See <https://www.w3.org/TR/trace-context/#list> for requirement for list members.
#[error("{0} is not a valid list member in TraceState, see https://www.w3.org/TR/trace-context/#list for more details")]
InvalidList(String),
List(String),
}

impl From<TraceStateError> for TraceError {
fn from(err: TraceStateError) -> Self {
TraceError::Other(Box::new(err))
}
}

/// Immutable portion of a [`Span`] which can be serialized and propagated.
Expand Down