ContextResolver

Struct ContextResolver 

Source
pub struct ContextResolver { /* private fields */ }
Expand description

A centralized store for resolved contexts.

Contexts are the combination of a name and a set of tags. They are used to identify a specific metric series. As contexts are constructed entirely of strings, they are expensive to construct in a way that allows sending between tasks, as this usually requires allocations. Even further, the same context may be “hot”, used frequently by the applications/services sending us metrics.

In order to optimize this, the context resolver is responsible for both interning the strings involved where possible, as well as keeping a map of contexts that can be referred to with a cheap handle. We can cheaply search for an existing context without needing to allocate an entirely new one, and get a clone of the handle to use going forward.

§Design

ContextResolver specifically manages interning and mapping of contexts. It can be cheaply cloned itself.

In order to resolve a context, resolve must be called which requires taking a lock to check for an existing context. A read/write lock is used in order to prioritize lookups over inserts, as lookups are expected to be more common given how often a given context is used and resolved.

Once a context is resolved, a cheap handle – Context – is returned. This handle, like ContextResolver, can be cheaply cloned. It points directly to the underlying context data (name and tags) and provides access to these components.

Implementations§

Source§

impl ContextResolver

Source

pub fn resolve<N, I, T>( &mut self, name: N, tags: I, maybe_origin: Option<RawOrigin<'_>>, ) -> Option<Context>
where N: AsRef<str> + CheapMetaString, I: IntoIterator<Item = T> + Clone, T: AsRef<str> + CheapMetaString,

Resolves the given context.

If the context has not yet been resolved, the name and tags are interned and a new context is created and stored. Otherwise, the existing context is returned. If an origin tags resolver is configured, and origin info is available, any enriched tags will be added to the context.

None may be returned if the interner is full and outside allocations are disallowed. See allow_heap_allocations for more information.

Source

pub fn resolve_with_origin_tags<N, I, T>( &mut self, name: N, tags: I, origin_tags: SharedTagSet, ) -> Option<Context>
where N: AsRef<str> + CheapMetaString, I: IntoIterator<Item = T> + Clone, T: AsRef<str> + CheapMetaString,

Resolves the given context using the provided origin tags.

If the context has not yet been resolved, the name and tags are interned and a new context is created and stored. Otherwise, the existing context is returned. The provided origin tags are used to enrich the context.

None may be returned if the interner is full and outside allocations are disallowed. See allow_heap_allocations for more information.

§Origin tags resolver mismatch

When passing in origin tags, they will be inherently tied to a specific OriginTagsResolver, which may differ from the configured origin tags resolver in this context resolver. This means that the context that is generated and cached may not be reused in the future if an attempt is made to resolve it using the raw origin information instead.

This method is intended primarily to allow for resolving contexts in a consistent way while reusing the origin tags from another context, such as when remapping the name and/or instrumented tags of a given metric, while maintaining its origin association.

Trait Implementations§

Source§

impl Clone for ContextResolver

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T> Track for T

§

fn track_allocations(self, token: AllocationGroupToken) -> Tracked<Self>

Instruments this type by attaching the given allocation group token, returning a Tracked wrapper. Read more
§

fn in_current_allocation_group(self) -> Tracked<Self>

Instruments this type by attaching the current allocation group, returning a Tracked wrapper. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more