use crate::{ExitCode, builtins::FatalError, dispatch::Resolution, limits::LimitExceeded};
pub(crate) const SCRIPT_SPAN: &str = "shell.script";
pub(crate) const MAX_TRACED_COMMANDS: u64 = 256;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum SpanLevel {
Info,
Debug,
}
#[derive(Debug, Default)]
pub(crate) struct ScriptCounters {
commands: u64,
traced: u64,
capability_commands: u64,
failed_commands: u64,
}
impl ScriptCounters {
pub(crate) fn charge(&mut self, kind: CommandKind) -> SpanLevel {
self.commands = self.commands.saturating_add(1);
if matches!(kind, CommandKind::Capability | CommandKind::ProviderCommand) {
self.capability_commands = self.capability_commands.saturating_add(1);
}
if self.traced >= MAX_TRACED_COMMANDS {
return SpanLevel::Debug;
}
self.traced = self.traced.saturating_add(1);
SpanLevel::Info
}
pub(crate) fn record_status(&mut self, status: ExitCode) {
if status != ExitCode::SUCCESS {
self.failed_commands = self.failed_commands.saturating_add(1);
}
}
pub(crate) fn record_on(&self, span: &tracing::Span) {
span.record("shell.script.commands", self.commands);
span.record("shell.script.commands_traced", self.traced);
span.record("shell.script.capability_commands", self.capability_commands);
span.record("shell.script.failed_commands", self.failed_commands);
}
}
pub(crate) fn command_span(
level: SpanLevel,
name: &str,
kind: CommandKind,
argument_count: usize,
) -> tracing::Span {
match level {
SpanLevel::Info => tracing::info_span!(
"shell.command",
shell.command.name = name,
shell.command.kind = kind.label(),
shell.command.argument_count = argument_count,
shell.command.exit_code = tracing::field::Empty,
capability.namespace = tracing::field::Empty,
outcome = tracing::field::Empty,
),
SpanLevel::Debug => tracing::debug_span!(
"shell.command",
shell.command.name = name,
shell.command.kind = kind.label(),
shell.command.argument_count = argument_count,
shell.command.exit_code = tracing::field::Empty,
capability.namespace = tracing::field::Empty,
outcome = tracing::field::Empty,
),
}
}
pub(crate) fn script_span() -> tracing::Span {
tracing::info_span!(
SCRIPT_SPAN,
shell.script.commands = tracing::field::Empty,
shell.script.commands_traced = tracing::field::Empty,
shell.script.capability_commands = tracing::field::Empty,
shell.script.failed_commands = tracing::field::Empty,
)
}
pub(crate) const WITHHELD: &str = "<withheld>";
pub(crate) const CONTROL_WORDS: &[&str] = &[
"break", "continue", "exit", "getopts", "local", "read", "return", "set", "shift", "unset", ":",
];
pub(crate) fn is_control_word(word: &str) -> bool {
CONTROL_WORDS.contains(&word)
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum CommandKind {
Control,
Function,
Builtin,
Capability,
ProviderCommand,
Rejected,
NotFound,
NotGranted,
}
impl CommandKind {
pub(crate) const fn of(resolution: &Resolution) -> Self {
match resolution {
Resolution::Function => Self::Function,
Resolution::Builtin(_) => Self::Builtin,
Resolution::Capability => Self::Capability,
Resolution::ProviderCommand => Self::ProviderCommand,
Resolution::NotGranted { .. } => Self::NotGranted,
Resolution::Rejected(_) => Self::Rejected,
Resolution::NotFound => Self::NotFound,
}
}
pub(crate) const fn label(self) -> &'static str {
match self {
Self::Control => "control",
Self::Function => "function",
Self::Builtin => "builtin",
Self::Capability => "capability",
Self::ProviderCommand => "provider-command",
Self::Rejected => "rejected",
Self::NotFound => "not-found",
Self::NotGranted => "not-granted",
}
}
pub(crate) const fn name_is_fixed_vocabulary(self) -> bool {
match self {
Self::Control
| Self::Builtin
| Self::Rejected
| Self::Capability
| Self::ProviderCommand => true,
Self::Function | Self::NotFound | Self::NotGranted => false,
}
}
}
pub(crate) fn traceable_name(kind: CommandKind, command: &str) -> &str {
if kind.name_is_fixed_vocabulary() || dekopon_core::telemetry_payloads() {
command
} else {
WITHHELD
}
}
pub(crate) fn outcome_label(status: ExitCode) -> &'static str {
match status {
ExitCode::SUCCESS => "succeeded",
ExitCode::SYNTAX => "usage-error",
ExitCode::TIMEOUT => "timed-out",
ExitCode::DENIED => "denied",
ExitCode::NOT_FOUND => "not-found",
_ => "failed",
}
}
pub(crate) fn fatal_exit_code(fatal: &FatalError) -> ExitCode {
match fatal {
FatalError::Limit(LimitExceeded::Deadline { .. }) => ExitCode::TIMEOUT,
FatalError::Limit(_) | FatalError::Unsupported(_) => ExitCode::SYNTAX,
FatalError::Assertion(_) => ExitCode::FAILURE,
}
}
pub(crate) fn fatal_outcome(fatal: &FatalError) -> &'static str {
match fatal {
FatalError::Limit(LimitExceeded::Deadline { .. }) => "timed-out",
FatalError::Limit(_) => "limit-exceeded",
FatalError::Unsupported(_) => "rejected",
FatalError::Assertion(_) => "assertion-failed",
}
}
#[cfg(test)]
mod tests;