Skip to content

Commit d31e20a

Browse files
Produced bytes enhanced metric (#587)
* add produced bytes metric * cargo fmt * fix test * fix test * merge imports
1 parent 6410c7f commit d31e20a

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

bottlecap/src/bin/bottlecap/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ async fn handle_event_bus_event(
535535
let mut p = invocation_processor.lock().await;
536536
p.on_platform_runtime_done(
537537
request_id,
538-
metrics.duration_ms,
538+
metrics,
539539
status,
540540
config.clone(),
541541
tags_provider.clone(),

bottlecap/src/lifecycle/invocation/processor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
CPUData, NetworkData,
2626
},
2727
tags::{lambda::tags::resolve_runtime_from_proc, provider},
28-
telemetry::events::{ReportMetrics, Status},
28+
telemetry::events::{ReportMetrics, RuntimeDoneMetrics, Status},
2929
traces::{
3030
context::SpanContext,
3131
propagation::{
@@ -261,7 +261,7 @@ impl Processor {
261261
pub async fn on_platform_runtime_done(
262262
&mut self,
263263
request_id: &String,
264-
duration_ms: f64,
264+
metrics: RuntimeDoneMetrics,
265265
status: Status,
266266
config: Arc<config::Config>,
267267
tags_provider: Arc<provider::Provider>,
@@ -270,11 +270,11 @@ impl Processor {
270270
timestamp: i64,
271271
) {
272272
self.context_buffer
273-
.add_runtime_duration(request_id, duration_ms);
273+
.add_runtime_duration(request_id, metrics.duration_ms);
274274

275275
// Set the runtime duration metric
276276
self.enhanced_metrics
277-
.set_runtime_duration_metric(duration_ms, timestamp);
277+
.set_runtime_done_metrics(&metrics, timestamp);
278278

279279
if status != Status::Success {
280280
// Increment the error metric

bottlecap/src/metrics/enhanced/lambda.rs

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::metrics::enhanced::{
33
statfs::statfs_info,
44
};
55
use crate::proc::{self, CPUData, NetworkData};
6-
use crate::telemetry::events::ReportMetrics;
6+
use crate::telemetry::events::{ReportMetrics, RuntimeDoneMetrics};
77
use dogstatsd::metric;
88
use dogstatsd::metric::{Metric, MetricValue};
99
use dogstatsd::{aggregator::Aggregator, metric::SortedTags};
@@ -127,13 +127,13 @@ impl Lambda {
127127
}
128128
}
129129

130-
pub fn set_runtime_duration_metric(&self, duration_ms: f64, timestamp: i64) {
130+
pub fn set_runtime_done_metrics(&self, metrics: &RuntimeDoneMetrics, timestamp: i64) {
131131
if !self.config.enhanced_metrics {
132132
return;
133133
}
134134
let metric = Metric::new(
135135
constants::RUNTIME_DURATION_METRIC.into(),
136-
MetricValue::distribution(duration_ms),
136+
MetricValue::distribution(metrics.duration_ms),
137137
// Datadog expects this value as milliseconds, not seconds
138138
self.get_dynamic_value_tags(),
139139
Some(timestamp),
@@ -146,6 +146,24 @@ impl Lambda {
146146
{
147147
error!("failed to insert runtime duration metric: {}", e);
148148
}
149+
150+
if let Some(produced_bytes) = metrics.produced_bytes {
151+
let metric = Metric::new(
152+
constants::PRODUCED_BYTES_METRIC.into(),
153+
MetricValue::distribution(produced_bytes as f64),
154+
// Datadog expects this value as milliseconds, not seconds
155+
self.get_dynamic_value_tags(),
156+
Some(timestamp),
157+
);
158+
if let Err(e) = self
159+
.aggregator
160+
.lock()
161+
.expect("lock poisoned")
162+
.insert(metric)
163+
{
164+
error!("failed to insert produced bytes metric: {}", e);
165+
}
166+
}
149167
}
150168

151169
pub fn set_post_runtime_duration_metric(&self, duration_ms: f64, timestamp: i64) {
@@ -842,7 +860,13 @@ mod tests {
842860
lambda.increment_errors_metric(now);
843861
lambda.increment_timeout_metric(now);
844862
lambda.set_init_duration_metric(100.0, now);
845-
lambda.set_runtime_duration_metric(100.0, now);
863+
lambda.set_runtime_done_metrics(
864+
&RuntimeDoneMetrics {
865+
duration_ms: 100.0,
866+
produced_bytes: Some(42 as u64),
867+
},
868+
now,
869+
);
846870
lambda.set_post_runtime_duration_metric(100.0, now);
847871
lambda.set_report_log_metrics(
848872
&ReportMetrics {
@@ -871,6 +895,9 @@ mod tests {
871895
assert!(aggr
872896
.get_entry_by_id(constants::RUNTIME_DURATION_METRIC.into(), &None, now)
873897
.is_none());
898+
assert!(aggr
899+
.get_entry_by_id(constants::PRODUCED_BYTES_METRIC.into(), &None, now)
900+
.is_none());
874901
assert!(aggr
875902
.get_entry_by_id(constants::POST_RUNTIME_DURATION_METRIC.into(), &None, now)
876903
.is_none());
@@ -949,6 +976,31 @@ mod tests {
949976
.is_none());
950977
}
951978

979+
#[test]
980+
fn test_set_runtime_done_metrics() {
981+
let (metrics_aggr, my_config) = setup();
982+
let lambda = Lambda::new(metrics_aggr.clone(), my_config);
983+
let runtime_done_metrics = RuntimeDoneMetrics {
984+
duration_ms: 100.0,
985+
produced_bytes: Some(42 as u64),
986+
};
987+
let now: i64 = std::time::UNIX_EPOCH
988+
.elapsed()
989+
.expect("unable to poll clock, unrecoverable")
990+
.as_secs()
991+
.try_into()
992+
.unwrap_or_default();
993+
lambda.set_runtime_done_metrics(&runtime_done_metrics, now);
994+
995+
assert_sketch(
996+
&metrics_aggr,
997+
constants::RUNTIME_DURATION_METRIC,
998+
100.0,
999+
now,
1000+
);
1001+
assert_sketch(&metrics_aggr, constants::PRODUCED_BYTES_METRIC, 42.0, now);
1002+
}
1003+
9521004
#[test]
9531005
fn test_set_report_log_metrics() {
9541006
let (metrics_aggr, my_config) = setup();

0 commit comments

Comments
 (0)