-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathhttp_client.rs
More file actions
208 lines (188 loc) · 6.7 KB
/
http_client.rs
File metadata and controls
208 lines (188 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2021-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0
use http_body_util::BodyExt;
use libdd_common::{http_common, HttpRequestBuilder};
use std::{
fs::OpenOptions,
future::Future,
io::Write,
pin::Pin,
sync::{Arc, Mutex},
};
use crate::config::Config;
use tracing::{debug, error};
pub mod header {
#![allow(clippy::declare_interior_mutable_const)]
use http::header::HeaderName;
pub const REQUEST_TYPE: HeaderName = HeaderName::from_static("dd-telemetry-request-type");
pub const API_VERSION: HeaderName = HeaderName::from_static("dd-telemetry-api-version");
pub const LIBRARY_LANGUAGE: HeaderName = HeaderName::from_static("dd-client-library-language");
pub const LIBRARY_VERSION: HeaderName = HeaderName::from_static("dd-client-library-version");
pub const DEBUG_ENABLED: HeaderName = HeaderName::from_static("dd-telemetry-debug-enabled");
pub const DD_SESSION_ID: HeaderName = HeaderName::from_static("dd-session-id");
pub const DD_ROOT_SESSION_ID: HeaderName = HeaderName::from_static("dd-root-session-id");
pub const DD_PARENT_SESSION_ID: HeaderName = HeaderName::from_static("dd-parent-session-id");
}
pub(crate) fn add_instrumentation_session_headers(
mut builder: HttpRequestBuilder,
session_id: Option<&str>,
root_session_id: Option<&str>,
parent_session_id: Option<&str>,
) -> HttpRequestBuilder {
let Some(s) = session_id.filter(|id| !id.is_empty()) else {
return builder;
};
builder = builder.header(header::DD_SESSION_ID, s);
if let Some(r) = root_session_id
.filter(|r| !r.is_empty())
.filter(|r| *r != s)
{
builder = builder.header(header::DD_ROOT_SESSION_ID, r);
}
if let Some(p) = parent_session_id
.filter(|p| !p.is_empty())
.filter(|p| *p != s)
{
builder = builder.header(header::DD_PARENT_SESSION_ID, p);
}
builder
}
pub type ResponseFuture =
Pin<Box<dyn Future<Output = Result<http_common::HttpResponse, http_common::Error>> + Send>>;
pub trait HttpClient {
fn request(&self, req: http_common::HttpRequest) -> ResponseFuture;
}
pub fn request_builder(c: &Config) -> anyhow::Result<HttpRequestBuilder> {
match &c.endpoint {
Some(e) => {
debug!(
endpoint.url = %e.url,
endpoint.timeout_ms = e.timeout_ms,
telemetry.version = env!("CARGO_PKG_VERSION"),
"Building telemetry request"
);
let mut builder =
e.to_request_builder(concat!("telemetry/", env!("CARGO_PKG_VERSION")));
if c.debug_enabled {
debug!(
telemetry.debug_enabled = true,
"Telemetry debug mode enabled"
);
builder = Ok(builder?.header(header::DEBUG_ENABLED, "true"))
}
builder
}
None => {
error!("No valid telemetry endpoint found, cannot build request");
Err(anyhow::Error::msg(
"no valid endpoint found, can't build the request".to_string(),
))
}
}
}
pub fn from_config(c: &Config) -> Box<dyn HttpClient + Sync + Send> {
match &c.endpoint {
Some(e) if e.url.scheme_str() == Some("file") => {
#[allow(clippy::expect_used)]
let file_path = libdd_common::decode_uri_path_in_authority(&e.url)
.expect("file urls should always have been encoded in authority");
debug!(
file.path = ?file_path,
"Using file-based mock telemetry client"
);
return Box::new(MockClient {
#[allow(clippy::expect_used)]
file: Arc::new(Mutex::new(Box::new(
OpenOptions::new()
.create(true)
.append(true)
.open(file_path.as_path())
.expect("Couldn't open mock client file"),
))),
});
}
Some(e) => {
debug!(
endpoint.url = %e.url,
endpoint.timeout_ms = e.timeout_ms,
"Using HTTP telemetry client"
);
}
None => {
debug!(
endpoint = "default",
"No telemetry endpoint configured, using default HTTP client"
);
}
};
Box::new(HyperClient {
inner: http_common::new_client_periodic(),
})
}
pub struct HyperClient {
inner: libdd_common::HttpClient,
}
impl HttpClient for HyperClient {
fn request(&self, req: http_common::HttpRequest) -> ResponseFuture {
let resp = self.inner.request(req);
Box::pin(async move {
match resp.await {
Ok(response) => Ok(http_common::into_response(response)),
Err(e) => Err(http_common::Error::Client(http_common::into_error(e))),
}
})
}
}
#[derive(Clone)]
pub struct MockClient {
file: Arc<Mutex<Box<dyn Write + Sync + Send>>>,
}
impl HttpClient for MockClient {
fn request(&self, req: http_common::HttpRequest) -> ResponseFuture {
let s = self.clone();
Box::pin(async move {
debug!("MockClient writing request to file");
let mut body = req.collect().await?.to_bytes().to_vec();
body.push(b'\n');
{
#[allow(clippy::expect_used)]
let mut writer = s.file.lock().expect("mutex poisoned");
match writer.write_all(body.as_ref()) {
Ok(()) => debug!(
file.bytes_written = body.len(),
"Successfully wrote payload to mock file"
),
Err(e) => {
error!(
error = %e,
"Failed to write to mock file"
);
return Err(http_common::Error::from(e));
}
}
}
debug!(http.status = 202, "MockClient returning success response");
http_common::empty_response(http::Response::builder().status(202))
})
}
}
#[cfg(test)]
mod tests {
use libdd_common::HttpRequestBuilder;
use super::*;
#[tokio::test]
#[cfg_attr(miri, ignore)]
async fn test_mock_client() {
let output: Vec<u8> = Vec::new();
let c = MockClient {
file: Arc::new(Mutex::new(Box::new(output))),
};
c.request(
HttpRequestBuilder::new()
.body(http_common::Body::from("hello world\n"))
.unwrap(),
)
.await
.unwrap();
}
}