greentic_runner_host/
telemetry.rs1use greentic_types::telemetry::set_current_tenant_ctx;
2use greentic_types::{EnvId, TenantCtx, TenantId};
3use rand::{Rng, rng};
4use std::str::FromStr;
5use tracing::Span;
6
7pub const PROVIDER_ID: &str = "greentic-runner";
8
9#[derive(Debug, Clone)]
10pub struct FlowSpanAttributes<'a> {
11 pub tenant: &'a str,
12 pub flow_id: &'a str,
13 pub node_id: Option<&'a str>,
14 pub tool: Option<&'a str>,
15 pub action: Option<&'a str>,
16}
17
18pub fn annotate_span(span: &Span, attrs: &FlowSpanAttributes<'_>) {
19 span.record("tenant", attrs.tenant);
20 span.record("flow_id", attrs.flow_id);
21 if let Some(node) = attrs.node_id {
22 span.record("node_id", node);
23 }
24 if let Some(tool) = attrs.tool {
25 span.record("tool", tool);
26 }
27 if let Some(action) = attrs.action {
28 span.record("action", action);
29 }
30}
31
32pub fn tenant_context(
33 env: &str,
34 tenant: &str,
35 flow_id: Option<&str>,
36 node_id: Option<&str>,
37 provider_id: Option<&str>,
38 session_id: Option<&str>,
39) -> TenantCtx {
40 let env_id = EnvId::from_str(env).expect("invalid env id");
41 let tenant_id = TenantId::from_str(tenant).expect("invalid tenant id");
42 let mut ctx = TenantCtx::new(env_id, tenant_id);
43 let provider = provider_id.unwrap_or(PROVIDER_ID);
44 ctx = ctx.with_provider(provider.to_string());
45 if let Some(flow) = flow_id {
46 ctx = ctx.with_flow(flow.to_string());
47 }
48 if let Some(node) = node_id {
49 ctx = ctx.with_node(node.to_string());
50 }
51 if let Some(session) = session_id {
52 ctx = ctx.with_session(session.to_string());
53 }
54 ctx
55}
56
57pub fn set_flow_context(
58 env: &str,
59 tenant: &str,
60 flow_id: &str,
61 node_id: Option<&str>,
62 provider_id: Option<&str>,
63 session_id: Option<&str>,
64) {
65 let ctx = tenant_context(env, tenant, Some(flow_id), node_id, provider_id, session_id);
66 set_current_tenant_ctx(&ctx);
67}
68
69pub fn backoff_delay_ms(base: u64, attempt: u32) -> u64 {
70 let multiplier = 1_u64 << attempt.min(10);
71 let exp = base.saturating_mul(multiplier);
72 let mut rng = rng();
73 let jitter = rng.random_range(0..=exp.min(1000));
74 exp + jitter
75}