Skip to content

Commit 132d828

Browse files
authored
RUST-1703 Add docker and kubernetes metrics (#940)
1 parent b15c504 commit 132d828

File tree

1 file changed

+62
-61
lines changed

1 file changed

+62
-61
lines changed

src/cmap/establish/handshake.rs

Lines changed: 62 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct ClientMetadata {
3333
driver: DriverMetadata,
3434
os: OsMetadata,
3535
platform: String,
36-
env: Option<FaasEnvironment>,
36+
env: Option<RuntimeEnvironment>,
3737
}
3838

3939
#[derive(Clone, Debug)]
@@ -55,17 +55,18 @@ struct OsMetadata {
5555
version: Option<String>,
5656
}
5757

58-
#[derive(Clone, Debug)]
59-
struct FaasEnvironment {
60-
name: FaasEnvironmentName,
58+
#[derive(Clone, Debug, PartialEq)]
59+
struct RuntimeEnvironment {
60+
name: Option<FaasEnvironmentName>,
6161
runtime: Option<String>,
6262
timeout_sec: Option<i32>,
6363
memory_mb: Option<i32>,
6464
region: Option<String>,
6565
url: Option<String>,
66+
container: Option<Document>,
6667
}
6768

68-
#[derive(Copy, Clone, Debug)]
69+
#[derive(Copy, Clone, Debug, PartialEq)]
6970
enum FaasEnvironmentName {
7071
AwsLambda,
7172
AzureFunc,
@@ -120,19 +121,21 @@ impl From<OsMetadata> for Bson {
120121
}
121122
}
122123

123-
impl From<FaasEnvironment> for Bson {
124-
fn from(env: FaasEnvironment) -> Self {
125-
let FaasEnvironment {
124+
impl From<RuntimeEnvironment> for Bson {
125+
fn from(env: RuntimeEnvironment) -> Self {
126+
let RuntimeEnvironment {
126127
name,
127128
runtime,
128129
timeout_sec,
129130
memory_mb,
130131
region,
131132
url,
133+
container,
132134
} = env;
133-
let mut out = doc! {
134-
"name": name.name(),
135-
};
135+
let mut out = doc! {};
136+
if let Some(name) = name {
137+
out.insert("name", name.name());
138+
}
136139
if let Some(rt) = runtime {
137140
out.insert("runtime", rt);
138141
}
@@ -148,70 +151,68 @@ impl From<FaasEnvironment> for Bson {
148151
if let Some(u) = url {
149152
out.insert("url", u);
150153
}
154+
if let Some(c) = container {
155+
out.insert("container", c);
156+
}
151157
Bson::Document(out)
152158
}
153159
}
154160

155-
impl FaasEnvironment {
156-
const UNSET: Self = FaasEnvironment {
157-
name: FaasEnvironmentName::AwsLambda,
161+
impl RuntimeEnvironment {
162+
const UNSET: Self = RuntimeEnvironment {
163+
name: None,
158164
runtime: None,
159165
timeout_sec: None,
160166
memory_mb: None,
161167
region: None,
162168
url: None,
169+
container: None,
163170
};
164171

165172
fn new() -> Option<Self> {
166-
let name = FaasEnvironmentName::new()?;
167-
Some(match name {
168-
FaasEnvironmentName::AwsLambda => {
169-
let runtime = env::var("AWS_EXECUTION_ENV").ok();
170-
let region = env::var("AWS_REGION").ok();
171-
let memory_mb = env::var("AWS_LAMBDA_FUNCTION_MEMORY_SIZE")
172-
.ok()
173-
.and_then(|s| s.parse().ok());
174-
Self {
175-
name,
176-
runtime,
177-
region,
178-
memory_mb,
179-
..Self::UNSET
173+
let mut out = Self::UNSET;
174+
if let Some(name) = FaasEnvironmentName::new() {
175+
out.name = Some(name);
176+
match name {
177+
FaasEnvironmentName::AwsLambda => {
178+
out.runtime = env::var("AWS_EXECUTION_ENV").ok();
179+
out.region = env::var("AWS_REGION").ok();
180+
out.memory_mb = env::var("AWS_LAMBDA_FUNCTION_MEMORY_SIZE")
181+
.ok()
182+
.and_then(|s| s.parse().ok());
180183
}
181-
}
182-
FaasEnvironmentName::AzureFunc => {
183-
let runtime = env::var("FUNCTIONS_WORKER_RUNTIME").ok();
184-
Self {
185-
name,
186-
runtime,
187-
..Self::UNSET
184+
FaasEnvironmentName::AzureFunc => {
185+
out.runtime = env::var("FUNCTIONS_WORKER_RUNTIME").ok();
188186
}
189-
}
190-
FaasEnvironmentName::GcpFunc => {
191-
let memory_mb = env::var("FUNCTION_MEMORY_MB")
192-
.ok()
193-
.and_then(|s| s.parse().ok());
194-
let timeout_sec = env::var("FUNCTION_TIMEOUT_SEC")
195-
.ok()
196-
.and_then(|s| s.parse().ok());
197-
let region = env::var("FUNCTION_REGION").ok();
198-
Self {
199-
name,
200-
memory_mb,
201-
timeout_sec,
202-
region,
203-
..Self::UNSET
187+
FaasEnvironmentName::GcpFunc => {
188+
out.memory_mb = env::var("FUNCTION_MEMORY_MB")
189+
.ok()
190+
.and_then(|s| s.parse().ok());
191+
out.timeout_sec = env::var("FUNCTION_TIMEOUT_SEC")
192+
.ok()
193+
.and_then(|s| s.parse().ok());
194+
out.region = env::var("FUNCTION_REGION").ok();
204195
}
205-
}
206-
FaasEnvironmentName::Vercel => {
207-
let region = env::var("VERCEL_REGION").ok();
208-
Self {
209-
name,
210-
region,
211-
..Self::UNSET
196+
FaasEnvironmentName::Vercel => {
197+
out.region = env::var("VERCEL_REGION").ok();
212198
}
213199
}
214-
})
200+
}
201+
let mut container = doc! {};
202+
if std::path::Path::new("/.dockerenv").exists() {
203+
container.insert("runtime", "docker");
204+
}
205+
if var_set("KUBERNETES_SERVICE_HOST") {
206+
container.insert("orchestrator", "kubernetes");
207+
}
208+
if !container.is_empty() {
209+
out.container = Some(container);
210+
}
211+
if out == Self::UNSET {
212+
None
213+
} else {
214+
Some(out)
215+
}
215216
}
216217
}
217218

@@ -288,9 +289,9 @@ const METADATA_TRUNCATIONS: &[Truncation] = &[
288289
// clear `env.*` except `name`
289290
|metadata| {
290291
if let Some(env) = &mut metadata.env {
291-
*env = FaasEnvironment {
292+
*env = RuntimeEnvironment {
292293
name: env.name,
293-
..FaasEnvironment::UNSET
294+
..RuntimeEnvironment::UNSET
294295
}
295296
}
296297
},
@@ -364,7 +365,7 @@ impl Handshaker {
364365
}
365366
}
366367

367-
metadata.env = FaasEnvironment::new();
368+
metadata.env = RuntimeEnvironment::new();
368369

369370
if options.load_balanced {
370371
command.body.insert("loadBalanced", true);

0 commit comments

Comments
 (0)