Skip to content

Commit 917a3b8

Browse files
authored
fix: add otel_attributes to event metadata (#602)
1 parent 73628eb commit 917a3b8

File tree

3 files changed

+40
-32
lines changed

3 files changed

+40
-32
lines changed

crates/base/src/runtime/mod.rs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ use deno_cache::SqliteBackedCache;
5555
use deno_core::error::AnyError;
5656
use deno_core::error::JsError;
5757
use deno_core::serde_json;
58-
use deno_core::serde_json::Value;
5958
use deno_core::url::Url;
6059
use deno_core::v8;
6160
use deno_core::v8::GCCallbackFlags;
@@ -80,7 +79,6 @@ use deno_facade::EmitterFactory;
8079
use deno_facade::EszipPayloadKind;
8180
use deno_facade::Metadata;
8281
use either::Either;
83-
use ext_event_worker::events::EventMetadata;
8482
use ext_event_worker::events::WorkerEventWithMetadata;
8583
use ext_runtime::cert::ValueRootCertStoreProvider;
8684
use ext_runtime::external_memory::CustomAllocator;
@@ -462,6 +460,7 @@ where
462460
pub(crate) async fn new(mut worker: Worker) -> Result<Self, Error> {
463461
let init_opts = worker.init_opts.take();
464462
let flags = worker.flags.clone();
463+
let event_metadata = worker.event_metadata.clone();
465464

466465
debug_assert!(init_opts.is_some(), "init_opts must not be None");
467466

@@ -1111,7 +1110,6 @@ where
11111110
s3_fs,
11121111
beforeunload_cpu_threshold,
11131112
beforeunload_mem_threshold,
1114-
context,
11151113
..
11161114
} = match bootstrap_ret {
11171115
Ok(Ok(v)) => v,
@@ -1123,7 +1121,7 @@ where
11231121
}
11241122
};
11251123

1126-
let context = context.unwrap_or_default();
1124+
let otel_attributes = event_metadata.otel_attributes.clone();
11271125
let span = Span::current();
11281126
let post_task_ret = unsafe {
11291127
spawn_blocking_non_send(|| {
@@ -1151,46 +1149,30 @@ where
11511149
op_state.put::<HashMap<usize, CancellationToken>>(HashMap::new());
11521150
}
11531151

1154-
let mut otel_attributes = HashMap::new();
1155-
1156-
otel_attributes.insert(
1157-
"edge_runtime.worker.kind".into(),
1158-
conf.to_worker_kind().to_string().into(),
1159-
);
1160-
11611152
if conf.is_user_worker() {
11621153
let conf = conf.as_user_worker().unwrap();
11631154
let key = conf.key.map_or("".to_string(), |k| k.to_string());
11641155

11651156
// set execution id for user workers
11661157
env_vars.insert("SB_EXECUTION_ID".to_string(), key.clone());
11671158

1168-
if let Some(Value::Object(attributes)) = context.get("otel") {
1169-
for (k, v) in attributes {
1170-
otel_attributes.insert(
1171-
k.to_string().into(),
1172-
match v {
1173-
Value::String(str) => str.to_string().into(),
1174-
others => others.to_string().into(),
1175-
},
1176-
);
1177-
}
1178-
}
1179-
11801159
if let Some(events_msg_tx) = conf.events_msg_tx.clone() {
11811160
op_state.put::<mpsc::UnboundedSender<WorkerEventWithMetadata>>(
11821161
events_msg_tx,
11831162
);
1184-
op_state.put::<EventMetadata>(EventMetadata {
1185-
service_path: conf.service_path.clone(),
1186-
execution_id: conf.key,
1187-
});
1163+
op_state.put(event_metadata);
11881164
}
11891165
}
11901166

11911167
op_state.put(ext_env::EnvVars(env_vars));
11921168
op_state.put(DenoRuntimeDropToken(DropToken(drop_token.clone())));
1193-
op_state.put(RuntimeOtelExtraAttributes(otel_attributes));
1169+
op_state.put(RuntimeOtelExtraAttributes(
1170+
otel_attributes
1171+
.unwrap_or_default()
1172+
.into_iter()
1173+
.map(|(k, v)| (k.into(), v.into()))
1174+
.collect(),
1175+
));
11941176
}
11951177

11961178
if is_user_worker {

crates/base/src/worker/utils.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
use std::collections::HashMap;
2+
13
use anyhow::anyhow;
24
use anyhow::bail;
5+
use deno_core::serde_json::Value;
36
use ext_event_worker::events::EventMetadata;
47
use ext_event_worker::events::WorkerEventWithMetadata;
58
use ext_event_worker::events::WorkerEvents;
@@ -15,18 +18,39 @@ use tokio::sync::oneshot;
1518
use tokio_util::sync::CancellationToken;
1619

1720
pub fn get_event_metadata(conf: &WorkerRuntimeOpts) -> EventMetadata {
21+
let mut otel_attributes = HashMap::new();
1822
let mut event_metadata = EventMetadata {
1923
service_path: None,
2024
execution_id: None,
25+
otel_attributes: None,
2126
};
27+
28+
otel_attributes.insert(
29+
"edge_runtime.worker.kind".to_string(),
30+
conf.to_worker_kind().to_string(),
31+
);
32+
2233
if conf.is_user_worker() {
2334
let conf = conf.as_user_worker().unwrap();
24-
event_metadata = EventMetadata {
25-
service_path: conf.service_path.clone(),
26-
execution_id: conf.key,
27-
};
35+
let context = conf.context.clone().unwrap_or_default();
36+
37+
event_metadata.service_path = conf.service_path.clone();
38+
event_metadata.execution_id = conf.key;
39+
40+
if let Some(Value::Object(attributes)) = context.get("otel") {
41+
for (k, v) in attributes {
42+
otel_attributes.insert(
43+
k.to_string(),
44+
match v {
45+
Value::String(str) => str.to_string(),
46+
others => others.to_string(),
47+
},
48+
);
49+
}
50+
}
2851
}
2952

53+
event_metadata.otel_attributes = Some(otel_attributes);
3054
event_metadata
3155
}
3256

ext/event_worker/events.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::HashMap;
12
use std::convert::Infallible;
23

34
use base_mem_check::MemCheckState;
@@ -106,6 +107,7 @@ impl WorkerEvents {
106107
pub struct EventMetadata {
107108
pub service_path: Option<String>,
108109
pub execution_id: Option<Uuid>,
110+
pub otel_attributes: Option<HashMap<String, String>>,
109111
}
110112

111113
#[derive(Serialize, Deserialize, Debug)]

0 commit comments

Comments
 (0)