Skip to main content

saluki_components/sources/otlp/
resolver.rs

1use std::{num::NonZeroUsize, sync::Arc, time::Duration};
2
3use saluki_core::{
4    components::ComponentContext,
5    data_model::event::metric::context::{ContextResolver, ContextResolverBuilder, TagsResolverBuilder},
6};
7use saluki_error::{generic_error, GenericError};
8use stringtheory::interning::GenericMapInterner;
9
10use crate::sources::otlp::OtlpOriginTagResolver;
11
12const RESOLVER_CACHE_EXPIRATION: Duration = Duration::from_secs(30);
13
14/// Settings of the context resolver for OTLP metrics and logs.
15#[derive(Clone, Debug, PartialEq)]
16pub struct OtlpContextSettings {
17    /// Whether contexts may allocate on the heap when the string interner is full.
18    pub allow_context_heap_allocs: bool,
19
20    /// Maximum number of cached contexts.
21    pub cached_contexts_limit: usize,
22
23    /// Maximum number of cached tag sets.
24    pub cached_tagsets_limit: usize,
25
26    /// Size of the context string interner, in bytes. Must be greater than `0`.
27    pub string_interner_size: u64,
28}
29
30/// Creates a new `ContextResolver` instance from the given configuration.
31///
32/// # Errors
33///
34/// If the context resolver string interner size is invalid, or there is an error creating either of the context
35/// resolvers, an error is returned.
36pub fn build_context_resolver(
37    contexts: &OtlpContextSettings, context: &ComponentContext,
38    maybe_origin_tags_resolver: Option<OtlpOriginTagResolver>,
39) -> Result<ContextResolver, GenericError> {
40    let context_string_interner_size = NonZeroUsize::new(contexts.string_interner_size as usize)
41        .ok_or_else(|| generic_error!("context_string_interner_size must be greater than 0"))?;
42
43    let cached_contexts_limit = contexts.cached_contexts_limit;
44    let cached_tagsets_limit = contexts.cached_tagsets_limit;
45
46    let interner = GenericMapInterner::new(context_string_interner_size);
47
48    let tags_resolver = TagsResolverBuilder::new(format!("{}/otlp/tags", context.component_id()), interner.clone())?
49        .with_cached_tagsets_limit(cached_tagsets_limit)
50        .with_idle_tagsets_expiration(RESOLVER_CACHE_EXPIRATION)
51        .with_heap_allocations(contexts.allow_context_heap_allocs)
52        .with_origin_tags_resolver(
53            maybe_origin_tags_resolver
54                .map(|resolver| -> Arc<dyn saluki_core::data_model::origin::OriginTagsResolver> { Arc::new(resolver) }),
55        )
56        .build();
57
58    let resolver = ContextResolverBuilder::from_name(format!("{}/otlp/primary", context.component_id()))?
59        .with_interner_capacity_bytes(context_string_interner_size)
60        .with_cached_contexts_limit(cached_contexts_limit)
61        .with_idle_context_expiration(RESOLVER_CACHE_EXPIRATION)
62        .with_heap_allocations(contexts.allow_context_heap_allocs)
63        .with_tags_resolver(Some(tags_resolver.clone()))
64        .with_interner(interner.clone())
65        .build();
66
67    Ok(resolver)
68}