vector/internal_events/
lua.rs

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