use hatchet_proto::root::AssignedAction;
use tracing::Span;
use tracing::field::Empty;
#[cfg(feature = "otel")]
mod traceparent;
#[cfg(feature = "otel")]
pub(crate) use traceparent::inject;
#[cfg(not(feature = "otel"))]
pub(crate) fn inject(_span: &Span, _metadata: &mut Option<String>) {}
const INSTRUMENTOR: &str = "hatchet";
fn count(n: usize) -> i64 {
i64::try_from(n).unwrap_or(i64::MAX)
}
pub(crate) fn run_workflow(name: &str) -> Span {
tracing::info_span!(
"hatchet.run_workflow",
otel.kind = "producer",
otel.status_code = Empty,
otel.status_description = Empty,
instrumentor = INSTRUMENTOR,
hatchet.workflow_name = name,
hatchet.child_workflow_run_id = Empty,
)
}
pub(crate) fn run_workflows(name: &str, runs: usize) -> Span {
tracing::info_span!(
"hatchet.run_workflows",
otel.kind = "producer",
otel.status_code = Empty,
otel.status_description = Empty,
instrumentor = INSTRUMENTOR,
hatchet.workflow_name = name,
hatchet.num_workflows = count(runs),
)
}
pub(crate) fn start_step_run(action: &AssignedAction, worker_id: &str) -> Span {
let span = tracing::info_span!(
"hatchet.start_step_run",
otel.kind = "consumer",
otel.status_code = Empty,
otel.status_description = Empty,
instrumentor = INSTRUMENTOR,
hatchet.tenant_id = action.tenant_id.as_str(),
hatchet.worker_id = worker_id,
hatchet.workflow_run_id = action.workflow_run_id.as_str(),
hatchet.step_run_id = action.task_run_external_id.as_str(),
hatchet.step_id = action.task_id.as_str(),
hatchet.action_name = action.action_id.as_str(),
hatchet.step_name = action.task_name.as_str(),
hatchet.retry_count = i64::from(action.retry_count),
hatchet.workflow_id = Empty,
hatchet.workflow_version_id = Empty,
hatchet.parent_workflow_run_id = Empty,
hatchet.child_workflow_index = Empty,
hatchet.child_workflow_key = Empty,
);
#[cfg(feature = "otel")]
if !span.is_disabled() {
traceparent::continue_from(&span, action.additional_metadata.as_deref());
}
if let Some(id) = &action.workflow_id {
span.record("hatchet.workflow_id", id.as_str());
}
if let Some(id) = &action.workflow_version_id {
span.record("hatchet.workflow_version_id", id.as_str());
}
if let Some(id) = &action.parent_workflow_run_id {
span.record("hatchet.parent_workflow_run_id", id.as_str());
}
if let Some(index) = action.child_workflow_index {
span.record("hatchet.child_workflow_index", i64::from(index));
}
if let Some(key) = &action.child_workflow_key {
span.record("hatchet.child_workflow_key", key.as_str());
}
span
}
pub(crate) fn start_batch_run(action: &AssignedAction, worker_id: &str, members: usize) -> Span {
let span = tracing::info_span!(
"hatchet.start_step_run",
otel.kind = "consumer",
otel.status_code = Empty,
otel.status_description = Empty,
instrumentor = INSTRUMENTOR,
hatchet.tenant_id = action.tenant_id.as_str(),
hatchet.worker_id = worker_id,
hatchet.action_name = action.action_id.as_str(),
hatchet.batch_id = action.batch_id.as_deref().unwrap_or_default(),
hatchet.batch_size = count(members),
);
#[cfg(feature = "otel")]
if !span.is_disabled() {
traceparent::continue_from(&span, action.additional_metadata.as_deref());
}
span
}
pub(crate) fn cancel_step_run(task_run_external_id: &str) -> Span {
tracing::info_span!(
"hatchet.cancel_step_run",
otel.kind = "consumer",
instrumentor = INSTRUMENTOR,
hatchet.step_run_id = task_run_external_id,
)
}
pub(crate) fn push_event(key: &str) -> Span {
tracing::info_span!(
"hatchet.push_event",
otel.kind = "producer",
otel.status_code = Empty,
otel.status_description = Empty,
instrumentor = INSTRUMENTOR,
hatchet.event_key = key,
)
}
pub(crate) fn bulk_push_event(events: usize) -> Span {
tracing::info_span!(
"hatchet.bulk_push_event",
otel.kind = "producer",
otel.status_code = Empty,
otel.status_description = Empty,
instrumentor = INSTRUMENTOR,
hatchet.num_events = count(events),
)
}
pub(crate) fn record_ok(span: &Span) {
span.record("otel.status_code", "ok");
}
pub(crate) fn record_error(span: &Span, message: &str) {
span.record("otel.status_description", message);
}
pub(crate) fn record_result<T, E: std::fmt::Display>(span: &Span, result: &Result<T, E>) {
match result {
Ok(_) => record_ok(span),
Err(error) => record_error(span, &error.to_string()),
}
}