Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::diagnostics::job_execution_event Struct Reference

Event data for job execution tracing. More...

#include <execution_event.h>

Collaboration diagram for kcenon::thread::diagnostics::job_execution_event:
Collaboration graph

Public Member Functions

auto format_timestamp () const -> std::string
 Formats the event timestamp as ISO 8601 string.
 
auto wait_time_ms () const -> double
 Converts wait_time to milliseconds.
 
auto execution_time_ms () const -> double
 Converts execution_time to milliseconds.
 
auto is_terminal () const -> bool
 Checks if this is a terminal event (job finished).
 
auto is_error () const -> bool
 Checks if this event indicates an error.
 
auto to_json () const -> std::string
 Converts the event to a JSON string.
 
auto to_string () const -> std::string
 Converts the event to a human-readable string.
 

Public Attributes

std::uint64_t event_id {0}
 Unique identifier for this event.
 
std::uint64_t job_id {0}
 ID of the job this event relates to.
 
std::string job_name
 Human-readable name of the job.
 
event_type type {event_type::enqueued}
 Type of event that occurred.
 
std::chrono::steady_clock::time_point timestamp
 Time when the event occurred.
 
std::chrono::system_clock::time_point system_timestamp
 System time when the event occurred.
 
std::thread::id thread_id
 ID of the thread that processed this event.
 
std::size_t worker_id {0}
 Worker ID that processed this job.
 
std::chrono::nanoseconds wait_time {0}
 Time spent waiting in queue before dequeue.
 
std::chrono::nanoseconds execution_time {0}
 Time spent executing the job.
 
std::optional< int > error_code
 Error code if the job failed.
 
std::optional< std::string > error_message
 Error message if the job failed.
 

Detailed Description

Event data for job execution tracing.

Contains detailed information about a job execution event, suitable for logging, tracing, and monitoring purposes.

Event Flow

retriedstarted → ...
@ dequeued
Job was taken from queue by a worker.
@ failed
Job failed with an error.
@ retried
Job is being retried after failure.
@ completed
Job completed successfully.
@ enqueued
Job was added to the queue.

Usage Example

pool->diagnostics().add_event_listener(
std::make_shared<MyEventLogger>()
);
// In MyEventLogger::on_event
void on_event(const job_execution_event& event) override {
LOG_INFO("{} [job:{}] {}",
event.job_name,
}
auto event_type_to_string(event_type type) -> std::string
Converts event_type to human-readable string.
Event data for job execution tracing.
std::string job_name
Human-readable name of the job.
event_type type
Type of event that occurred.
auto format_timestamp() const -> std::string
Formats the event timestamp as ISO 8601 string.

Definition at line 97 of file execution_event.h.

Member Function Documentation

◆ execution_time_ms()

auto kcenon::thread::diagnostics::job_execution_event::execution_time_ms ( ) const -> double
inlinenodiscard

Converts execution_time to milliseconds.

Returns
Execution time in milliseconds.

Definition at line 212 of file execution_event.h.

213 {
214 return std::chrono::duration<double, std::milli>(execution_time).count();
215 }
std::chrono::nanoseconds execution_time
Time spent executing the job.

References execution_time.

Referenced by to_json(), and to_string().

Here is the caller graph for this function:

◆ format_timestamp()

auto kcenon::thread::diagnostics::job_execution_event::format_timestamp ( ) const -> std::string
inlinenodiscard

Formats the event timestamp as ISO 8601 string.

Returns
Formatted timestamp string.

Definition at line 185 of file execution_event.h.

186 {
187 auto time_t = std::chrono::system_clock::to_time_t(system_timestamp);
188 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
189 system_timestamp.time_since_epoch()) % 1000;
190
191 char buffer[32];
192 std::strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S",
193 std::gmtime(&time_t));
194
195 return std::string(buffer) + "." +
196 std::to_string(ms.count()) + "Z";
197 }
std::chrono::system_clock::time_point system_timestamp
System time when the event occurred.

References system_timestamp.

Referenced by to_json(), and to_string().

Here is the caller graph for this function:

◆ is_error()

auto kcenon::thread::diagnostics::job_execution_event::is_error ( ) const -> bool
inlinenodiscard

Checks if this event indicates an error.

Returns
true if failed or cancelled.

Definition at line 232 of file execution_event.h.

233 {
234 return type == event_type::failed ||
236 }

References kcenon::thread::diagnostics::cancelled, kcenon::thread::diagnostics::failed, and type.

◆ is_terminal()

auto kcenon::thread::diagnostics::job_execution_event::is_terminal ( ) const -> bool
inlinenodiscard

Checks if this is a terminal event (job finished).

Returns
true if completed, failed, or cancelled.

Definition at line 221 of file execution_event.h.

222 {
223 return type == event_type::completed ||
226 }

References kcenon::thread::diagnostics::cancelled, kcenon::thread::diagnostics::completed, kcenon::thread::diagnostics::failed, and type.

◆ to_json()

auto kcenon::thread::diagnostics::job_execution_event::to_json ( ) const -> std::string
inlinenodiscard

Converts the event to a JSON string.

Returns
JSON representation of the event.

Output format:

{
"event_id": 123,
"job_id": 456,
"job_name": "ProcessOrder",
"type": "completed",
"timestamp": "2025-01-08T10:30:00.123Z",
"thread_id": "12345",
"worker_id": 0,
"wait_time_ms": 1.5,
"execution_time_ms": 10.2,
"error_code": null,
"error_message": null
}

Definition at line 259 of file execution_event.h.

260 {
261 std::ostringstream oss;
262 oss << "{\n";
263 oss << " \"event_id\": " << event_id << ",\n";
264 oss << " \"job_id\": " << job_id << ",\n";
265 oss << " \"job_name\": \"" << job_name << "\",\n";
266 oss << " \"type\": \"" << event_type_to_string(type) << "\",\n";
267 oss << " \"timestamp\": \"" << format_timestamp() << "\",\n";
268
269 // Format thread_id as string
270 std::ostringstream tid_oss;
271 tid_oss << thread_id;
272 oss << " \"thread_id\": \"" << tid_oss.str() << "\",\n";
273
274 oss << " \"worker_id\": " << worker_id << ",\n";
275 oss << std::fixed << std::setprecision(3);
276 oss << " \"wait_time_ms\": " << wait_time_ms() << ",\n";
277 oss << " \"execution_time_ms\": " << execution_time_ms();
278
279 if (error_code.has_value())
280 {
281 oss << ",\n \"error_code\": " << error_code.value();
282 }
283 else
284 {
285 oss << ",\n \"error_code\": null";
286 }
287
288 if (error_message.has_value())
289 {
290 oss << ",\n \"error_message\": \"" << error_message.value() << "\"";
291 }
292 else
293 {
294 oss << ",\n \"error_message\": null";
295 }
296
297 oss << "\n}";
298 return oss.str();
299 }
std::optional< std::string > error_message
Error message if the job failed.
std::uint64_t event_id
Unique identifier for this event.
std::uint64_t job_id
ID of the job this event relates to.
auto wait_time_ms() const -> double
Converts wait_time to milliseconds.
std::optional< int > error_code
Error code if the job failed.
auto execution_time_ms() const -> double
Converts execution_time to milliseconds.
std::size_t worker_id
Worker ID that processed this job.
std::thread::id thread_id
ID of the thread that processed this event.

References error_message, event_id, kcenon::thread::diagnostics::event_type_to_string(), execution_time_ms(), format_timestamp(), job_name, thread_id, type, wait_time_ms(), and worker_id.

Here is the call graph for this function:

◆ to_string()

auto kcenon::thread::diagnostics::job_execution_event::to_string ( ) const -> std::string
inlinenodiscard

Converts the event to a human-readable string.

Returns
String representation of the event.

Output format:

[2025-01-08T10:30:00.123Z] Event#123 job:ProcessOrder#456 type:completed
worker:0 thread:12345 wait:1.500ms exec:10.200ms
Represents a unit of work (task) to be executed, typically by a job queue.
Definition job.h:136

Definition at line 311 of file execution_event.h.

312 {
313 std::ostringstream oss;
314
315 // First line: timestamp, event info, job info, type
316 oss << "[" << format_timestamp() << "] ";
317 oss << "Event#" << event_id << " ";
318 oss << "job:" << job_name << "#" << job_id << " ";
319 oss << "type:" << event_type_to_string(type) << "\n";
320
321 // Second line: worker, thread, timing
322 oss << " worker:" << worker_id << " ";
323 oss << "thread:" << thread_id << " ";
324 oss << std::fixed << std::setprecision(3);
325 oss << "wait:" << wait_time_ms() << "ms ";
326 oss << "exec:" << execution_time_ms() << "ms";
327
328 // Error info if present
329 if (error_code.has_value() || error_message.has_value())
330 {
331 oss << "\n error:";
332 if (error_code.has_value())
333 {
334 oss << " code=" << error_code.value();
335 }
336 if (error_message.has_value())
337 {
338 oss << " msg=\"" << error_message.value() << "\"";
339 }
340 }
341
342 return oss.str();
343 }

References error_message, event_id, kcenon::thread::diagnostics::event_type_to_string(), execution_time_ms(), format_timestamp(), job_name, thread_id, type, wait_time_ms(), and worker_id.

Here is the call graph for this function:

◆ wait_time_ms()

auto kcenon::thread::diagnostics::job_execution_event::wait_time_ms ( ) const -> double
inlinenodiscard

Converts wait_time to milliseconds.

Returns
Wait time in milliseconds.

Definition at line 203 of file execution_event.h.

204 {
205 return std::chrono::duration<double, std::milli>(wait_time).count();
206 }
std::chrono::nanoseconds wait_time
Time spent waiting in queue before dequeue.

References wait_time.

Referenced by to_json(), and to_string().

Here is the caller graph for this function:

Member Data Documentation

◆ error_code

std::optional<int> kcenon::thread::diagnostics::job_execution_event::error_code

Error code if the job failed.

Definition at line 170 of file execution_event.h.

Referenced by kcenon::thread::thread_worker::do_work().

◆ error_message

std::optional<std::string> kcenon::thread::diagnostics::job_execution_event::error_message

Error message if the job failed.

Definition at line 175 of file execution_event.h.

Referenced by kcenon::thread::thread_worker::do_work(), to_json(), and to_string().

◆ event_id

std::uint64_t kcenon::thread::diagnostics::job_execution_event::event_id {0}

Unique identifier for this event.

Monotonically increasing within the thread pool lifetime.

Definition at line 104 of file execution_event.h.

104{0};

Referenced by to_json(), and to_string().

◆ execution_time

std::chrono::nanoseconds kcenon::thread::diagnostics::job_execution_event::execution_time {0}

Time spent executing the job.

Only valid for completed, failed, cancelled events.

Definition at line 161 of file execution_event.h.

161{0};

Referenced by kcenon::thread::thread_worker::do_work(), and execution_time_ms().

◆ job_id

std::uint64_t kcenon::thread::diagnostics::job_execution_event::job_id {0}

ID of the job this event relates to.

Definition at line 109 of file execution_event.h.

109{0};

Referenced by kcenon::thread::thread_worker::do_work().

◆ job_name

std::string kcenon::thread::diagnostics::job_execution_event::job_name

Human-readable name of the job.

Definition at line 114 of file execution_event.h.

Referenced by kcenon::thread::thread_worker::do_work(), to_json(), and to_string().

◆ system_timestamp

std::chrono::system_clock::time_point kcenon::thread::diagnostics::job_execution_event::system_timestamp

System time when the event occurred.

Used for logging and correlation with external systems.

Definition at line 131 of file execution_event.h.

Referenced by kcenon::thread::thread_worker::do_work(), and format_timestamp().

◆ thread_id

std::thread::id kcenon::thread::diagnostics::job_execution_event::thread_id

ID of the thread that processed this event.

May be empty for enqueued events.

Definition at line 138 of file execution_event.h.

Referenced by kcenon::thread::thread_worker::do_work(), to_json(), and to_string().

◆ timestamp

std::chrono::steady_clock::time_point kcenon::thread::diagnostics::job_execution_event::timestamp

Time when the event occurred.

Definition at line 124 of file execution_event.h.

Referenced by kcenon::thread::thread_worker::do_work().

◆ type

event_type kcenon::thread::diagnostics::job_execution_event::type {event_type::enqueued}

Type of event that occurred.

Definition at line 119 of file execution_event.h.

Referenced by kcenon::thread::thread_worker::do_work(), is_error(), is_terminal(), to_json(), and to_string().

◆ wait_time

std::chrono::nanoseconds kcenon::thread::diagnostics::job_execution_event::wait_time {0}

Time spent waiting in queue before dequeue.

Only valid for dequeued, started events and later.

Definition at line 154 of file execution_event.h.

154{0};

Referenced by kcenon::thread::thread_worker::do_work(), and wait_time_ms().

◆ worker_id

std::size_t kcenon::thread::diagnostics::job_execution_event::worker_id {0}

Worker ID that processed this job.

Definition at line 143 of file execution_event.h.

143{0};

Referenced by kcenon::thread::thread_worker::do_work(), to_json(), and to_string().


The documentation for this struct was generated from the following file: