Skip to main content

ledgence_worker_api/
invocation.rs

1use crate::CloudEvent;
2use serde::{Deserialize, Serialize};
3
4/// Correlation copied from the validated envelope, never from user-owned data.
5#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
6pub struct InvocationIdentity {
7    #[serde(default, skip_serializing_if = "Option::is_none")]
8    pub parent_workflow_id: Option<String>,
9    #[serde(default, skip_serializing_if = "Option::is_none")]
10    pub root_workflow_id: Option<String>,
11    pub source: String,
12    pub event_id: String,
13    pub tenant_id: String,
14    pub namespace: String,
15    pub run_id: String,
16    #[serde(default, skip_serializing_if = "Option::is_none")]
17    pub workflow_id: Option<String>,
18    #[serde(default, skip_serializing_if = "Option::is_none")]
19    pub activation_id: Option<String>,
20    pub task_id: String,
21    pub attempt_id: String,
22    pub attempt_no: u32,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub traceparent: Option<String>,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub tracestate: Option<String>,
27}
28
29impl From<&CloudEvent> for InvocationIdentity {
30    fn from(event: &CloudEvent) -> Self {
31        Self {
32            parent_workflow_id: event
33                .value()
34                .get("ldgparentworkflowid")
35                .and_then(serde_json::Value::as_str)
36                .map(str::to_owned),
37            root_workflow_id: event
38                .value()
39                .get("ldgrootworkflowid")
40                .and_then(serde_json::Value::as_str)
41                .map(str::to_owned),
42            source: event.string("source").to_owned(),
43            event_id: event.id().to_owned(),
44            tenant_id: event.tenant_id().to_owned(),
45            namespace: event.namespace().to_owned(),
46            run_id: event.string("ldgrunid").to_owned(),
47            workflow_id: event
48                .value()
49                .get("ldgworkflowid")
50                .and_then(serde_json::Value::as_str)
51                .map(str::to_owned),
52            activation_id: event
53                .value()
54                .get("ldgactivationid")
55                .and_then(serde_json::Value::as_str)
56                .map(str::to_owned),
57            task_id: event.task_id().to_owned(),
58            attempt_id: event.attempt_id().to_owned(),
59            attempt_no: event.value()["ldgattemptno"]
60                .as_u64()
61                .expect("validated attempt number") as u32,
62            traceparent: event.traceparent().map(str::to_owned),
63            tracestate: event
64                .value()
65                .get("tracestate")
66                .and_then(serde_json::Value::as_str)
67                .map(str::to_owned),
68        }
69    }
70}