Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(metrics): improve creation of Origin metadata structures #18788

Merged
merged 3 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
}

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 Orign metadata is created within a component and does not
Fixed Show fixed Hide fixed
/// 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
48 changes: 28 additions & 20 deletions src/sinks/datadog/metrics/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,23 @@ 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()
neuronull marked this conversation as resolved.
Show resolved Hide resolved
{
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().expect("OriginProduct should be set"),
origin_category: origin.category().expect("OriginCategory should be set"),
origin_service: origin.service().expect("OriginService should be set"),
}),
neuronull marked this conversation as resolved.
Show resolved Hide resolved
}
},
)
}
Expand Down Expand Up @@ -626,12 +637,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 +650,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 +1084,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
Loading