vector/internal_events/
lua.rs

1use metrics::{counter, gauge};
2use vector_lib::internal_event::{
3    ComponentEventsDropped, InternalEvent, UNINTENTIONAL, error_stage, error_type,
4};
5
6use crate::transforms::lua::v2::BuildError;
7
8#[derive(Debug)]
9pub struct LuaGcTriggered {
10    pub used_memory: usize,
11}
12
13impl InternalEvent for LuaGcTriggered {
14    fn emit(self) {
15        gauge!("lua_memory_used_bytes").set(self.used_memory as f64);
16    }
17}
18
19#[derive(Debug)]
20pub struct LuaScriptError {
21    pub error: mlua::Error,
22}
23
24impl InternalEvent for LuaScriptError {
25    fn emit(self) {
26        error!(
27            message = "Error in lua script.",
28            error = ?self.error,
29            error_code = mlua_error_code(&self.error),
30            error_type = error_type::COMMAND_FAILED,
31            stage = error_stage::PROCESSING,
32            internal_log_rate_limit = true,
33        );
34        counter!(
35            "component_errors_total",
36            "error_code" => mlua_error_code(&self.error),
37            "error_type" => error_type::SCRIPT_FAILED,
38            "stage" => error_stage::PROCESSING,
39        )
40        .increment(1);
41        emit!(ComponentEventsDropped::<UNINTENTIONAL> {
42            count: 1,
43            reason: "Error in lua script.",
44        });
45    }
46}
47
48#[derive(Debug)]
49pub struct LuaBuildError {
50    pub error: BuildError,
51}
52
53impl InternalEvent for LuaBuildError {
54    fn emit(self) {
55        let reason = "Error in building lua script.";
56        error!(
57            message = reason,
58            error = ?self.error,
59            error_type = error_type::SCRIPT_FAILED,
60            error_code = lua_build_error_code(&self.error),
61            stage = error_stage::PROCESSING,
62            internal_log_rate_limit = true,
63        );
64        counter!(
65            "component_errors_total",
66            "error_code" => lua_build_error_code(&self.error),
67            "error_type" => error_type::SCRIPT_FAILED,
68            "stage" => error_stage:: PROCESSING,
69        )
70        .increment(1);
71
72        emit!(ComponentEventsDropped::<UNINTENTIONAL> { count: 1, reason })
73    }
74}
75
76fn mlua_error_code(err: &mlua::Error) -> &'static str {
77    use mlua::Error::*;
78
79    match err {
80        SyntaxError { .. } => "syntax_error",
81        RuntimeError(_) => "runtime_error",
82        MemoryError(_) => "memory_error",
83        SafetyError(_) => "memory_safety_error",
84        MemoryControlNotAvailable => "memory_control_not_available",
85        RecursiveMutCallback => "mutable_callback_called_recursively",
86        CallbackDestructed => "callback_destructed",
87        StackError => "out_of_stack",
88        BindError => "too_many_arguments_to_function_bind",
89        BadArgument { .. } => "bad_argument",
90        ToLuaConversionError { .. } => "error_converting_value_to_lua",
91        FromLuaConversionError { .. } => "error_converting_value_from_lua",
92        CoroutineUnresumable => "coroutine_unresumable",
93        UserDataTypeMismatch => "userdata_type_mismatch",
94        UserDataDestructed => "userdata_destructed",
95        UserDataBorrowError => "userdata_borrow_error",
96        UserDataBorrowMutError => "userdata_already_borrowed",
97        MetaMethodRestricted(_) => "restricted_metamethod",
98        MetaMethodTypeError { .. } => "unsupported_metamethod_type",
99        MismatchedRegistryKey => "mismatched_registry_key",
100        CallbackError { .. } => "callback_error",
101        PreviouslyResumedPanic => "previously_resumed_panic",
102        ExternalError(_) => "external_error",
103        WithContext { cause, .. } => mlua_error_code(cause),
104        _ => "unknown",
105    }
106}
107
108const fn lua_build_error_code(err: &BuildError) -> &'static str {
109    use BuildError::*;
110
111    match err {
112        InvalidSearchDirs { .. } => "invalid_search_dir",
113        InvalidSource { .. } => "invalid_source",
114        InvalidHooksInit { .. } => "invalid_hook_init",
115        InvalidHooksProcess { .. } => "invalid_hook_process",
116        InvalidHooksShutdown { .. } => "invalid_hook_shutdown",
117        InvalidTimerHandler { .. } => "invalid_timer_handler",
118        RuntimeErrorHooksInit { .. } => "runtime_error_hook_init",
119        RuntimeErrorHooksProcess { .. } => "runtime_error_hook_process",
120        RuntimeErrorHooksShutdown { .. } => "runtime_error_hook_shutdown",
121        RuntimeErrorTimerHandler { .. } => "runtime_error_timer_handler",
122        RuntimeErrorGc { .. } => "runtime_error_gc",
123    }
124}