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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use std::time::Duration;

use metrics::{counter, histogram};
use tokio::time::error::Elapsed;
use vector_lib::internal_event::InternalEvent;
use vector_lib::{
    internal_event::{error_stage, error_type, ComponentEventsDropped, UNINTENTIONAL},
    json_size::JsonSize,
};

use super::prelude::io_error_code;

#[derive(Debug)]
pub struct ExecEventsReceived<'a> {
    pub count: usize,
    pub command: &'a str,
    pub byte_size: JsonSize,
}

impl InternalEvent for ExecEventsReceived<'_> {
    fn emit(self) {
        trace!(
            message = "Events received.",
            count = self.count,
            byte_size = self.byte_size.get(),
            command = %self.command,
        );
        counter!(
            "component_received_events_total",
            "command" => self.command.to_owned(),
        )
        .increment(self.count as u64);
        counter!(
            "component_received_event_bytes_total",
            "command" => self.command.to_owned(),
        )
        .increment(self.byte_size.get() as u64);
    }
}

#[derive(Debug)]
pub struct ExecFailedError<'a> {
    pub command: &'a str,
    pub error: std::io::Error,
}

impl InternalEvent for ExecFailedError<'_> {
    fn emit(self) {
        error!(
            message = "Unable to exec.",
            command = %self.command,
            error = ?self.error,
            error_type = error_type::COMMAND_FAILED,
            error_code = %io_error_code(&self.error),
            stage = error_stage::RECEIVING,
            internal_log_rate_limit = true,
        );
        counter!(
            "component_errors_total",
            "command" => self.command.to_owned(),
            "error_type" => error_type::COMMAND_FAILED,
            "error_code" => io_error_code(&self.error),
            "stage" => error_stage::RECEIVING,
        )
        .increment(1);
    }
}

#[derive(Debug)]
pub struct ExecTimeoutError<'a> {
    pub command: &'a str,
    pub elapsed_seconds: u64,
    pub error: Elapsed,
}

impl InternalEvent for ExecTimeoutError<'_> {
    fn emit(self) {
        error!(
            message = "Timeout during exec.",
            command = %self.command,
            elapsed_seconds = %self.elapsed_seconds,
            error = %self.error,
            error_type = error_type::TIMED_OUT,
            stage = error_stage::RECEIVING,
            internal_log_rate_limit = true,
        );
        counter!(
            "component_errors_total",
            "command" => self.command.to_owned(),
            "error_type" => error_type::TIMED_OUT,
            "stage" => error_stage::RECEIVING,
        )
        .increment(1);
    }
}

#[derive(Debug)]
pub struct ExecCommandExecuted<'a> {
    pub command: &'a str,
    pub exit_status: Option<i32>,
    pub exec_duration: Duration,
}

impl ExecCommandExecuted<'_> {
    fn exit_status_string(&self) -> String {
        match self.exit_status {
            Some(exit_status) => exit_status.to_string(),
            None => "unknown".to_string(),
        }
    }
}

impl InternalEvent for ExecCommandExecuted<'_> {
    fn emit(self) {
        let exit_status = self.exit_status_string();
        trace!(
            message = "Executed command.",
            command = %self.command,
            exit_status = %exit_status,
            elapsed_millis = %self.exec_duration.as_millis(),
            internal_log_rate_limit = true,
        );
        counter!(
            "command_executed_total",
            "command" => self.command.to_owned(),
            "exit_status" => exit_status.clone(),
        )
        .increment(1);

        histogram!(
            "command_execution_duration_seconds",
            "exit_status" => exit_status,
            "command" => self.command.to_owned(),
        )
        .record(self.exec_duration);
    }
}

pub enum ExecFailedToSignalChild {
    #[cfg(unix)]
    SignalError(nix::errno::Errno),
    #[cfg(unix)]
    FailedToMarshalPid(std::num::TryFromIntError),
    #[cfg(unix)]
    NoPid,
    #[cfg(windows)]
    IoError(std::io::Error),
}

impl ExecFailedToSignalChild {
    fn to_error_code(&self) -> String {
        use ExecFailedToSignalChild::*;

        match self {
            #[cfg(unix)]
            SignalError(err) => format!("errno_{}", err),
            #[cfg(unix)]
            FailedToMarshalPid(_) => String::from("failed_to_marshal_pid"),
            #[cfg(unix)]
            NoPid => String::from("no_pid"),
            #[cfg(windows)]
            IoError(err) => err.to_string(),
        }
    }
}

impl std::fmt::Display for ExecFailedToSignalChild {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        use ExecFailedToSignalChild::*;

        match self {
            #[cfg(unix)]
            SignalError(err) => write!(f, "errno: {}", err),
            #[cfg(unix)]
            FailedToMarshalPid(err) => write!(f, "failed to marshal pid to i32: {}", err),
            #[cfg(unix)]
            NoPid => write!(f, "child had no pid"),
            #[cfg(windows)]
            IoError(err) => write!(f, "io error: {}", err),
        }
    }
}

pub struct ExecFailedToSignalChildError<'a> {
    pub command: &'a tokio::process::Command,
    pub error: ExecFailedToSignalChild,
}

impl InternalEvent for ExecFailedToSignalChildError<'_> {
    fn emit(self) {
        error!(
            message = %format!("Failed to send SIGTERM to child, aborting early: {}", self.error),
            command = ?self.command.as_std(),
            error_code = %self.error.to_error_code(),
            error_type = error_type::COMMAND_FAILED,
            stage = error_stage::RECEIVING,
            internal_log_rate_limit = true,
        );
        counter!(
            "component_errors_total",
            "command" => format!("{:?}", self.command.as_std()),
            "error_code" => self.error.to_error_code(),
            "error_type" => error_type::COMMAND_FAILED,
            "stage" => error_stage::RECEIVING,
        )
        .increment(1);
    }
}

pub struct ExecChannelClosedError;

impl InternalEvent for ExecChannelClosedError {
    fn emit(self) {
        let exec_reason = "Receive channel closed, unable to send.";
        error!(
            message = exec_reason,
            error_type = error_type::COMMAND_FAILED,
            stage = error_stage::RECEIVING,
            internal_log_rate_limit = true,
        );
        counter!(
            "component_errors_total",
            "error_type" => error_type::COMMAND_FAILED,
            "stage" => error_stage::RECEIVING,
        )
        .increment(1);
        emit!(ComponentEventsDropped::<UNINTENTIONAL> {
            count: 1,
            reason: exec_reason
        });
    }
}