1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use metrics::{counter, gauge};
use vector_lib::internal_event::InternalEvent;
use vector_lib::internal_event::{error_stage, error_type, ComponentEventsDropped, UNINTENTIONAL};

use crate::transforms::lua::v2::BuildError;

#[derive(Debug)]
pub struct LuaGcTriggered {
    pub used_memory: usize,
}

impl InternalEvent for LuaGcTriggered {
    fn emit(self) {
        gauge!("lua_memory_used_bytes").set(self.used_memory as f64);
    }
}

#[derive(Debug)]
pub struct LuaScriptError {
    pub error: mlua::Error,
}

impl InternalEvent for LuaScriptError {
    fn emit(self) {
        error!(
            message = "Error in lua script.",
            error = ?self.error,
            error_code = mlua_error_code(&self.error),
            error_type = error_type::COMMAND_FAILED,
            stage = error_stage::PROCESSING,
            internal_log_rate_limit = true,
        );
        counter!(
            "component_errors_total",
            "error_code" => mlua_error_code(&self.error),
            "error_type" => error_type::SCRIPT_FAILED,
            "stage" => error_stage::PROCESSING,
        )
        .increment(1);
        emit!(ComponentEventsDropped::<UNINTENTIONAL> {
            count: 1,
            reason: "Error in lua script.",
        });
    }
}

#[derive(Debug)]
pub struct LuaBuildError {
    pub error: BuildError,
}

impl InternalEvent for LuaBuildError {
    fn emit(self) {
        let reason = "Error in building lua script.";
        error!(
            message = reason,
            error = ?self.error,
            error_type = error_type::SCRIPT_FAILED,
            error_code = lua_build_error_code(&self.error),
            stage = error_stage::PROCESSING,
            internal_log_rate_limit = true,
        );
        counter!(
            "component_errors_total",
            "error_code" => lua_build_error_code(&self.error),
            "error_type" => error_type::SCRIPT_FAILED,
            "stage" => error_stage:: PROCESSING,
        )
        .increment(1);

        emit!(ComponentEventsDropped::<UNINTENTIONAL> { count: 1, reason })
    }
}

fn mlua_error_code(err: &mlua::Error) -> &'static str {
    use mlua::Error::*;

    match err {
        SyntaxError { .. } => "syntax_error",
        RuntimeError(_) => "runtime_error",
        MemoryError(_) => "memory_error",
        SafetyError(_) => "memory_safety_error",
        MemoryControlNotAvailable => "memory_control_not_available",
        RecursiveMutCallback => "mutable_callback_called_recursively",
        CallbackDestructed => "callback_destructed",
        StackError => "out_of_stack",
        BindError => "too_many_arguments_to_function_bind",
        BadArgument { .. } => "bad_argument",
        ToLuaConversionError { .. } => "error_converting_value_to_lua",
        FromLuaConversionError { .. } => "error_converting_value_from_lua",
        CoroutineUnresumable => "coroutine_unresumable",
        UserDataTypeMismatch => "userdata_type_mismatch",
        UserDataDestructed => "userdata_destructed",
        UserDataBorrowError => "userdata_borrow_error",
        UserDataBorrowMutError => "userdata_already_borrowed",
        MetaMethodRestricted(_) => "restricted_metamethod",
        MetaMethodTypeError { .. } => "unsupported_metamethod_type",
        MismatchedRegistryKey => "mismatched_registry_key",
        CallbackError { .. } => "callback_error",
        PreviouslyResumedPanic => "previously_resumed_panic",
        ExternalError(_) => "external_error",
        WithContext { cause, .. } => mlua_error_code(cause),
        _ => "unknown",
    }
}

const fn lua_build_error_code(err: &BuildError) -> &'static str {
    use BuildError::*;

    match err {
        InvalidSearchDirs { .. } => "invalid_search_dir",
        InvalidSource { .. } => "invalid_source",
        InvalidHooksInit { .. } => "invalid_hook_init",
        InvalidHooksProcess { .. } => "invalid_hook_process",
        InvalidHooksShutdown { .. } => "invalid_hook_shutdown",
        InvalidTimerHandler { .. } => "invalid_timer_handler",
        RuntimeErrorHooksInit { .. } => "runtime_error_hook_init",
        RuntimeErrorHooksProcess { .. } => "runtime_error_hook_process",
        RuntimeErrorHooksShutdown { .. } => "runtime_error_hook_shutdown",
        RuntimeErrorTimerHandler { .. } => "runtime_error_timer_handler",
        RuntimeErrorGc { .. } => "runtime_error_gc",
    }
}