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
impl ContextResolver
Sourcepub fn resolve<N, I, T>(
&mut self,
name: N,
tags: I,
maybe_origin: Option<RawOrigin<'_>>,
) -> Option<Context>
pub fn resolve<N, I, T>( &mut self, name: N, tags: I, maybe_origin: Option<RawOrigin<'_>>, ) -> Option<Context>
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.
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§
Auto Trait Implementations§
impl Freeze for ContextResolver
impl !RefUnwindSafe for ContextResolver
impl Send for ContextResolver
impl Sync for ContextResolver
impl Unpin for ContextResolver
impl !UnwindSafe for ContextResolver
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Track for T
impl<T> Track for T
§fn track_allocations(self, token: AllocationGroupToken) -> Tracked<Self>
fn track_allocations(self, token: AllocationGroupToken) -> Tracked<Self>
Tracked
wrapper. Read more§fn in_current_allocation_group(self) -> Tracked<Self>
fn in_current_allocation_group(self) -> Tracked<Self>
Tracked
wrapper. Read more