Skip to content

Commit 1a87923

Browse files
committed
allow metadata enrichment for metrics and traces
1 parent 7fb22d0 commit 1a87923

1 file changed

Lines changed: 75 additions & 13 deletions

File tree

src/sources/http_server.rs

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl SimpleHttpConfig {
220220
)
221221
.with_standard_vector_source_metadata();
222222

223-
// for metadata that is added to the events dynamically from config options
223+
// For metadata that is added to the events dynamically from config options.
224224
if log_namespace == LogNamespace::Legacy {
225225
schema_definition = schema_definition.unknown_fields(Kind::bytes());
226226
}
@@ -529,23 +529,33 @@ impl HttpSource for SimpleHttpSource {
529529
/// Both namespaces use insert-if-empty semantics so auth enrichment never
530530
/// overwrites built-in source metadata (`path`, `host`, `headers`, …) that
531531
/// `enrich_events` already populated.
532-
/// Legacy namespace: inserted into the event body.
533-
/// Vector namespace: inserted into event metadata under `http_server.<field>`.
532+
/// Vector namespace: inserted into event metadata under `http_server.<field>` for
533+
/// all event types (Log, Metric, Trace).
534+
/// Legacy namespace: inserted into the Log event body only (Metric/Trace are skipped).
534535
fn inject_auth_enrichment(&self, events: &mut [Event], enrichment: ObjectMap) {
535536
for event in events.iter_mut() {
536-
if let Event::Log(log) = event {
537-
for (key, value) in &enrichment {
538-
match self.log_namespace {
539-
LogNamespace::Vector => {
540-
log.try_insert(
541-
(
542-
PathPrefix::Metadata,
543-
path!(SimpleHttpConfig::NAME).concat(path!(key.as_str())),
544-
),
537+
match self.log_namespace {
538+
LogNamespace::Vector => {
539+
// metadata_mut() dispatches to Log, Metric, and Trace so every
540+
// decoded event type receives the auth enrichment fields.
541+
let meta = event.metadata_mut().value_mut();
542+
for (key, value) in &enrichment {
543+
let key_str = key.as_str();
544+
if meta
545+
.get(path!(SimpleHttpConfig::NAME).concat(path!(key_str)))
546+
.is_none()
547+
{
548+
meta.insert(
549+
path!(SimpleHttpConfig::NAME).concat(path!(key_str)),
545550
value.clone(),
546551
);
547552
}
548-
LogNamespace::Legacy => {
553+
}
554+
}
555+
LogNamespace::Legacy => {
556+
// Legacy enrichment targets the event body; only Log events have one.
557+
if let Event::Log(log) = event {
558+
for (key, value) in &enrichment {
549559
log.try_insert((PathPrefix::Event, path!(key.as_str())), value.clone());
550560
}
551561
}
@@ -1824,6 +1834,58 @@ mod tests {
18241834
);
18251835
}
18261836

1837+
#[test]
1838+
fn inject_auth_enrichment_applies_to_non_log_events_in_vector_namespace() {
1839+
use crate::{codecs::DecodingConfig, sources::util::HttpSource as _};
1840+
use vector_lib::{
1841+
codecs::BytesDeserializerConfig,
1842+
event::{Metric, MetricKind, MetricValue},
1843+
};
1844+
use vrl::value::KeyString;
1845+
1846+
let decoder = DecodingConfig::new(
1847+
BytesDecoderConfig::new().into(),
1848+
BytesDeserializerConfig::new().into(),
1849+
LogNamespace::Vector,
1850+
)
1851+
.build()
1852+
.unwrap()
1853+
.with_log_namespace(LogNamespace::Vector);
1854+
1855+
let source = super::SimpleHttpSource {
1856+
headers: vec![],
1857+
query_parameters: vec![],
1858+
path_key: OptionalValuePath::none(),
1859+
host_key: OptionalValuePath::none(),
1860+
decoder,
1861+
log_namespace: LogNamespace::Vector,
1862+
};
1863+
1864+
let metric = Metric::new(
1865+
"requests",
1866+
MetricKind::Incremental,
1867+
MetricValue::Counter { value: 1.0 },
1868+
);
1869+
let mut events = vec![Event::Metric(metric)];
1870+
1871+
let mut enrichment = ObjectMap::new();
1872+
enrichment.insert(KeyString::from("tenant_id"), Value::from("t-456"));
1873+
1874+
source.inject_auth_enrichment(&mut events, enrichment);
1875+
1876+
let Event::Metric(metric) = &events[0] else {
1877+
panic!("expected metric event");
1878+
};
1879+
assert_eq!(
1880+
metric
1881+
.metadata()
1882+
.value()
1883+
.get(path!(SimpleHttpConfig::NAME).concat(path!("tenant_id")),),
1884+
Some(&Value::from("t-456")),
1885+
"auth enrichment must be written to non-log event metadata"
1886+
);
1887+
}
1888+
18271889
impl ValidatableComponent for SimpleHttpConfig {
18281890
fn validation_configuration() -> ValidationConfiguration {
18291891
let config = Self {

0 commit comments

Comments
 (0)