Skip to content

Commit

Permalink
chore(metrics): improve creation of Origin metadata structures (#18788)
Browse files Browse the repository at this point in the history
* chore(metrics): improve creation of Origin metadata structures

* spell checker

* feedback ds
  • Loading branch information
neuronull authored Oct 6, 2023
1 parent ec5238e commit f0adce7
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 103 deletions.
35 changes: 14 additions & 21 deletions lib/vector-core/src/event/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,38 +76,31 @@ pub struct DatadogMetricOriginMetadata {
}

impl DatadogMetricOriginMetadata {
/// Replaces the `OriginProduct`.
/// Creates a new `DatadogMetricOriginMetadata`.
/// When Vector sends out metrics containing the Origin metadata, it should do so with
/// all of the fields defined.
/// The edge case where the Origin metadata is created within a component and does not
/// initially contain all of the metadata fields, is in the `log_to_metric` transform.
#[must_use]
pub fn with_product(mut self, product: u32) -> Self {
self.product = Some(product);
self
}

/// Replaces the `OriginCategory`.
#[must_use]
pub fn with_category(mut self, category: u32) -> Self {
self.category = Some(category);
self
}

/// Replaces the `OriginService`.
#[must_use]
pub fn with_service(mut self, service: u32) -> Self {
self.service = Some(service);
self
pub fn new(product: Option<u32>, category: Option<u32>, service: Option<u32>) -> Self {
Self {
product,
category,
service,
}
}

/// Returns a reference to the `OriginProduct`
/// Returns a reference to the `OriginProduct`.
pub fn product(&self) -> Option<u32> {
self.product
}

/// Returns a reference to the `OriginCategory`
/// Returns a reference to the `OriginCategory`.
pub fn category(&self) -> Option<u32> {
self.category
}

/// Returns a reference to the `OriginService`
/// Returns a reference to the `OriginService`.
pub fn service(&self) -> Option<u32> {
self.service
}
Expand Down
27 changes: 7 additions & 20 deletions lib/vector-core/src/event/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,30 +564,17 @@ fn decode_metadata(
) {
let value = input.value.and_then(decode_value);

let datadog_origin_metadata = input
.datadog_origin_metadata
.as_ref()
.map(decode_origin_metadata);
let datadog_origin_metadata = input.datadog_origin_metadata.as_ref().map(|input| {
super::DatadogMetricOriginMetadata::new(
input.origin_product,
input.origin_category,
input.origin_service,
)
});

(value, datadog_origin_metadata)
}

fn decode_origin_metadata(input: &DatadogOriginMetadata) -> super::DatadogMetricOriginMetadata {
let mut origin = super::DatadogMetricOriginMetadata::default();

if let Some(product) = input.origin_product {
origin = origin.with_product(product);
}
if let Some(category) = input.origin_category {
origin = origin.with_category(category);
}
if let Some(service) = input.origin_service {
origin = origin.with_service(service);
}

origin
}

fn decode_value(input: Value) -> Option<event::Value> {
match input.kind {
Some(value::Kind::RawBytes(data)) => Some(event::Value::Bytes(data)),
Expand Down
50 changes: 30 additions & 20 deletions src/sinks/datadog/metrics/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,25 @@ fn generate_sketch_metadata(
origin_product_value: u32,
) -> Option<ddmetric_proto::Metadata> {
generate_origin_metadata(maybe_pass_through, maybe_source_type, origin_product_value).map(
|origin| ddmetric_proto::Metadata {
origin: Some(ddmetric_proto::Origin {
origin_product: origin.product().expect("OriginProduct should be set"),
origin_category: origin.category().expect("OriginCategory should be set"),
origin_service: origin.service().expect("OriginService should be set"),
}),
|origin| {
if origin.product().is_none()
|| origin.category().is_none()
|| origin.service().is_none()
{
warn!(
message = "Generated sketch origin metadata should have each field set.",
product = origin.product(),
category = origin.category(),
service = origin.service()
);
}
ddmetric_proto::Metadata {
origin: Some(ddmetric_proto::Origin {
origin_product: origin.product().unwrap_or_default(),
origin_category: origin.category().unwrap_or_default(),
origin_service: origin.service().unwrap_or_default(),
}),
}
},
)
}
Expand Down Expand Up @@ -626,12 +639,11 @@ fn generate_origin_metadata(
// - `log_to_metric` transform set the OriginService in the EventMetadata when it creates
// the new metric.
if let Some(pass_through) = maybe_pass_through {
Some(
DatadogMetricOriginMetadata::default()
.with_product(pass_through.product().unwrap_or(origin_product_value))
.with_category(pass_through.category().unwrap_or(ORIGIN_CATEGORY_VALUE))
.with_service(pass_through.service().unwrap_or(no_value)),
)
Some(DatadogMetricOriginMetadata::new(
pass_through.product().or(Some(origin_product_value)),
pass_through.category().or(Some(ORIGIN_CATEGORY_VALUE)),
pass_through.service().or(Some(no_value)),
))

// No metadata has been set upstream
} else {
Expand All @@ -640,10 +652,11 @@ fn generate_origin_metadata(
// In order to preserve consistent behavior, we intentionally don't set origin metadata
// for the case where the Datadog Agent did not set it.
source_type_to_service(source_type).map(|origin_service_value| {
DatadogMetricOriginMetadata::default()
.with_product(origin_product_value)
.with_category(ORIGIN_CATEGORY_VALUE)
.with_service(origin_service_value)
DatadogMetricOriginMetadata::new(
Some(origin_product_value),
Some(ORIGIN_CATEGORY_VALUE),
Some(origin_service_value),
)
})
})
}
Expand Down Expand Up @@ -1073,10 +1086,7 @@ mod tests {
let service = 9;

let event_metadata = EventMetadata::default().with_origin_metadata(
DatadogMetricOriginMetadata::default()
.with_product(product)
.with_category(category)
.with_service(service),
DatadogMetricOriginMetadata::new(Some(product), Some(category), Some(service)),
);
let counter = get_simple_counter_with_metadata(event_metadata);

Expand Down
11 changes: 5 additions & 6 deletions src/sources/datadog_agent/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,11 @@ fn get_event_metadata(metadata: Option<&Metadata>) -> EventMetadata {
origin.origin_category,
origin.origin_service,
);
EventMetadata::default().with_origin_metadata(
DatadogMetricOriginMetadata::default()
.with_product(origin.origin_product)
.with_category(origin.origin_category)
.with_service(origin.origin_service),
)
EventMetadata::default().with_origin_metadata(DatadogMetricOriginMetadata::new(
Some(origin.origin_product),
Some(origin.origin_category),
Some(origin.origin_service),
))
})
}

Expand Down
Loading

0 comments on commit f0adce7

Please sign in to comment.