-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add telemetry client implementation to data pipeline. #802
Open
hoolioh
wants to merge
2
commits into
main
Choose a base branch
from
julio/APMSP-1449-add-telemetry-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+819
−22
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! Provides an abstraction layer to hold metrics that comes from 'SendDataResult'. | ||
use ddcommon::tag; | ||
use ddtelemetry::data::metrics::{MetricNamespace, MetricType}; | ||
use ddtelemetry::metrics::ContextKey; | ||
use ddtelemetry::worker::TelemetryWorkerHandle; | ||
use std::collections::HashMap; | ||
|
||
/// trace_api.requests metric | ||
pub const API_REQUEST_STR: &str = "trace_api.requests"; | ||
/// trace_api.errors metric | ||
pub const API_ERRORS_STR: &str = "trace_api.errors"; | ||
/// trace_api.bytes metric | ||
pub const API_BYTES_STR: &str = "trace_api.bytes"; | ||
/// trace_api.responses metric | ||
pub const API_RESPONSES_STR: &str = "trace_api.responses"; | ||
/// trace_chunk_sent metric | ||
pub const CHUNKS_SENT_STR: &str = "trace_chunk_sent"; | ||
/// trace_chunk_dropped metric | ||
pub const CHUNKS_DROPPED_STR: &str = "trace_chunk_dropped"; | ||
|
||
struct Metric { | ||
name: &'static str, | ||
metric_type: MetricType, | ||
namespace: MetricNamespace, | ||
} | ||
|
||
const METRICS: &[Metric] = &[ | ||
Metric { | ||
name: API_REQUEST_STR, | ||
metric_type: MetricType::Count, | ||
namespace: MetricNamespace::Tracers, | ||
}, | ||
Metric { | ||
name: API_ERRORS_STR, | ||
metric_type: MetricType::Count, | ||
namespace: MetricNamespace::Tracers, | ||
}, | ||
Metric { | ||
name: API_BYTES_STR, | ||
metric_type: MetricType::Distribution, | ||
namespace: MetricNamespace::Tracers, | ||
}, | ||
Metric { | ||
name: API_RESPONSES_STR, | ||
metric_type: MetricType::Count, | ||
namespace: MetricNamespace::Tracers, | ||
}, | ||
Metric { | ||
name: CHUNKS_SENT_STR, | ||
metric_type: MetricType::Count, | ||
namespace: MetricNamespace::Tracers, | ||
}, | ||
Metric { | ||
name: CHUNKS_DROPPED_STR, | ||
metric_type: MetricType::Count, | ||
namespace: MetricNamespace::Tracers, | ||
}, | ||
]; | ||
|
||
/// Structure to accumulate partial results coming from sending traces to the agent. | ||
#[derive(Debug)] | ||
pub struct Metrics(HashMap<String, ContextKey>); | ||
|
||
impl Metrics { | ||
/// Creates a new Metrics instance | ||
pub fn new(worker: &TelemetryWorkerHandle) -> Self { | ||
let mut map = HashMap::new(); | ||
for metric in METRICS { | ||
let key = worker.register_metric_context( | ||
metric.name.to_string(), | ||
vec![tag!("src_library", "libdatadog")], | ||
metric.metric_type, | ||
true, | ||
metric.namespace, | ||
); | ||
map.insert(metric.name.to_string(), key); | ||
} | ||
|
||
Self(map) | ||
} | ||
|
||
/// Gets the context key associated with the metric. | ||
pub fn get(&self, metric_name: &str) -> Option<&ContextKey> { | ||
self.0.get(metric_name) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use ddtelemetry::config::Config; | ||
use ddtelemetry::worker::TelemetryWorkerBuilder; | ||
|
||
#[cfg_attr(miri, ignore)] | ||
#[tokio::test] | ||
async fn new_test() { | ||
let (worker, _) = TelemetryWorkerBuilder::new_fetch_host( | ||
"service".to_string(), | ||
"language".to_string(), | ||
"0.1".to_string(), | ||
"1.0".to_string(), | ||
) | ||
.spawn_with_config(Config::default()) | ||
.await | ||
.unwrap(); | ||
|
||
let metrics = Metrics::new(&worker); | ||
|
||
assert!(!metrics.0.is_empty()); | ||
|
||
assert!(metrics.get(API_REQUEST_STR).is_some()); | ||
assert!(metrics.get(API_RESPONSES_STR).is_some()); | ||
assert!(metrics.get(API_BYTES_STR).is_some()); | ||
assert!(metrics.get(API_ERRORS_STR).is_some()); | ||
assert!(metrics.get(CHUNKS_SENT_STR).is_some()); | ||
assert!(metrics.get(CHUNKS_DROPPED_STR).is_some()); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of using hashmaps to lookup harcoded keys, for performance and correctness, but I don't really have a good counter proposition that'd be shorter to write...
Maybe associate an index to each metric name and hold Contexts in a Vec/array?