Skip to content

Commit 5eb5a88

Browse files
authored
aj/add hello route (#311)
* feat: Add hello route * feat: call it * feat: fmt * fix: clippy * fix: 8124 not 8126 for hello * move telemetry listener temporarily * feat: Fix an issue where dogstatsd port is wrong. Drop enhanced invocation metric from clients because we always set it * fix: fmt
1 parent 1e59f15 commit 5eb5a88

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed

bottlecap/src/bin/bottlecap/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use bottlecap::{
3737
listener::TelemetryListener,
3838
},
3939
traces::{
40+
hello_agent,
4041
stats_flusher::{self, StatsFlusher},
4142
stats_processor, trace_agent,
4243
trace_flusher::{self, TraceFlusher},
@@ -313,6 +314,14 @@ async fn extension_loop_active(
313314
error!("Error starting trace agent: {e:?}");
314315
}
315316
});
317+
// TODO(astuyve): deprioritize this task after the first request
318+
tokio::spawn(async move {
319+
let res = hello_agent::start_handler().await;
320+
if let Err(e) = res {
321+
error!("Error starting hello agent: {e:?}");
322+
}
323+
});
324+
316325
let lambda_enhanced_metrics =
317326
enhanced_metrics::new(Arc::clone(&metrics_aggr), Arc::clone(config));
318327
let dogstatsd_cancel_token = start_dogstatsd(event_bus.get_sender_copy(), &metrics_aggr).await;

bottlecap/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ pub const EXTENSION_ROUTE: &str = "2020-01-01/extension";
4141
pub const LAMBDA_RUNTIME_SLUG: &str = "lambda";
4242

4343
// todo: make sure we can override those with environment variables
44-
pub const DOGSTATSD_PORT: u16 = 8185;
44+
pub const DOGSTATSD_PORT: u16 = 8125;
4545

4646
pub const TELEMETRY_SUBSCRIPTION_ROUTE: &str = "2022-07-01/telemetry";
47-
pub const TELEMETRY_PORT: u16 = 8124;
47+
// todo(astuyve) should be 8124 on /lambda/logs but
48+
// telemetry is implemented on a raw socket now and
49+
// does not multiplex routes on the same port.
50+
pub const TELEMETRY_PORT: u16 = 8999;
4851

4952
/// Return the base URL for the lambda runtime API
5053
///

bottlecap/src/metrics/dogstatsd.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ impl DogStatsD {
6565
continue;
6666
}
6767
};
68+
if parsed_metric.name == "aws.lambda.enhanced.invocations" {
69+
debug!("dropping invocation metric from layer, as it's set by agent");
70+
continue;
71+
}
6872
let first_value = match parsed_metric.first_value() {
6973
Ok(val) => val,
7074
Err(e) => {
@@ -85,6 +89,7 @@ impl DogStatsD {
8589
// Don't publish until after validation and adding metric_event to buff
8690
let _ = self.event_bus.send(Event::Metric(metric_event)).await; // todo check the result
8791
if self.cancel_token.is_cancelled() {
92+
debug!("closing dogstatsd listener");
8893
break;
8994
}
9095
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// TODO(Astuyve): Deprecate.
5+
// older clients require the 127.0.0.1:8126/lambda/hello route
6+
// to identify the presence of the extension.
7+
8+
use hyper::service::{make_service_fn, service_fn};
9+
use hyper::{http, Body, Method, Request, Response, Server, StatusCode};
10+
use serde_json::json;
11+
use std::convert::Infallible;
12+
use std::net::SocketAddr;
13+
use tracing::error;
14+
15+
const HELLO_PATH: &str = "/lambda/hello";
16+
const AGENT_PORT: usize = 8124;
17+
18+
pub async fn start_handler() -> Result<(), Box<dyn std::error::Error>> {
19+
let make_svc = make_service_fn(move |_| {
20+
let service = service_fn(hello_handler);
21+
22+
async move { Ok::<_, Infallible>(service) }
23+
});
24+
25+
let port = u16::try_from(AGENT_PORT).expect("AGENT_PORT is too large");
26+
let addr = SocketAddr::from(([127, 0, 0, 1], port));
27+
let server_builder = Server::try_bind(&addr)?;
28+
29+
let server = server_builder.serve(make_svc);
30+
31+
// start hyper http server
32+
if let Err(e) = server.await {
33+
error!("Server error: {e}");
34+
return Err(e.into());
35+
}
36+
37+
Ok(())
38+
}
39+
40+
async fn hello_handler(req: Request<Body>) -> http::Result<Response<Body>> {
41+
if let (&Method::GET, HELLO_PATH) = (req.method(), req.uri().path()) {
42+
Response::builder()
43+
.status(200)
44+
.body(Body::from(json!({}).to_string()))
45+
} else {
46+
let mut not_found = Response::default();
47+
*not_found.status_mut() = StatusCode::NOT_FOUND;
48+
Ok(not_found)
49+
}
50+
}

bottlecap/src/traces/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
22
// SPDX-License-Identifier: Apache-2.0
33

4+
pub mod hello_agent;
45
pub mod stats_flusher;
56
pub mod stats_processor;
67
pub mod trace_agent;

0 commit comments

Comments
 (0)