StormByte-Database 2.0.0
C++26 database module of the StormByte suite
 
Loading...
Searching...
No Matches
telemetry.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte-Database.
5 *
6 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
7 */
8
9#pragma once
10
14
15#include <array>
16#include <atomic>
17#include <chrono>
18#include <cstddef>
19#include <cstdint>
20#include <limits>
21#include <string>
22
27namespace StormByte {
32 namespace Database {
49
54 enum class BackendEvent : std::uint8_t {
55 Warning,
56 Busy,
58 Io,
61 Deadlock,
63 Other,
64 Count
65 };
66
72 std::uint64_t Attempts{};
73 std::uint64_t Successes{};
74 std::uint64_t Failures{};
75 std::uint64_t TotalNanoseconds{};
76 std::uint64_t MinimumNanoseconds{};
77 std::uint64_t MaximumNanoseconds{};
78
83 std::uint64_t MeanNanoseconds() const noexcept {
84 return Attempts == 0 ? 0 : TotalNanoseconds / Attempts;
85 }
86 };
87
97 public:
103 public:
110
115
120
126 void Complete(bool success, std::uint64_t rows_returned = 0) noexcept;
127
131 ~OperationScope() noexcept;
132
133 private:
134 StormByte::Shared<Telemetry> m_telemetry;
135 Operation m_operation;
136 std::chrono::steady_clock::time_point m_started;
137 std::uint64_t m_rows_returned;
138 bool m_success;
139 bool m_completed;
140 };
141
145 virtual ~Telemetry() noexcept;
146
150 Telemetry() noexcept;
151
157 OperationMetrics Metrics(Operation operation) const noexcept;
158
163 std::uint64_t RowsReturned() const noexcept;
164
170 std::uint64_t Events(BackendEvent event) const noexcept;
171
176 virtual operator StormByte::String::String() const;
177
182 STORMBYTE_FORCE_INLINE operator std::string() const {
183 return static_cast<std::string>(static_cast<StormByte::String::String>(*this));
184 }
185
186 protected:
187 Telemetry(const Telemetry&) = delete;
188 Telemetry(Telemetry&&) = delete;
189 Telemetry& operator=(const Telemetry&) = delete;
191
196 virtual void RecordEvent(BackendEvent event) noexcept;
197
203 void RecordEvents(BackendEvent event, std::uint64_t count) noexcept;
204
205 private:
206 friend class Database;
207 friend class PreparedSTMT;
208 friend class OperationScope;
209
214 struct Counter {
215 std::atomic<std::uint64_t> attempts{0};
216 std::atomic<std::uint64_t> successes{0};
217 std::atomic<std::uint64_t> failures{0};
218 std::atomic<std::uint64_t> total_nanoseconds{0};
219 std::atomic<std::uint64_t> minimum_nanoseconds{std::numeric_limits<std::uint64_t>::max()};
220 std::atomic<std::uint64_t> maximum_nanoseconds{0};
221 };
222
230 void RecordOperation(Operation operation, bool success, std::chrono::nanoseconds elapsed, std::uint64_t rows_returned) noexcept;
231
232 std::array<Counter, static_cast<std::size_t>(Operation::Count)> m_operations;
233 std::array<std::atomic<std::uint64_t>, static_cast<std::size_t>(BackendEvent::Count)> m_events;
234 std::atomic<std::uint64_t> m_rows_returned;
235 };
236 }
237}
Abstract backend.
Definition database.hxx:76
Abstract prepared statement.
Definition prepared_stmt.hxx:69
Records one operation and its elapsed time on destruction.
Definition telemetry.hxx:102
OperationScope(OperationScope &&)=delete
Move constructor is deleted.
OperationScope(const OperationScope &)=delete
Copy constructor is deleted.
OperationScope(StormByte::Shared< Telemetry > telemetry, Operation operation) noexcept
void Complete(bool success, std::uint64_t rows_returned=0) noexcept
Record the result and optional returned row count.
Thread-safe cumulative telemetry for one database connection.
Definition telemetry.hxx:96
virtual void RecordEvent(BackendEvent event) noexcept
Telemetry & operator=(const Telemetry &)=delete
Copy assignment is disabled.
Telemetry(Telemetry &&)=delete
Moving counters is disabled.
Telemetry(const Telemetry &)=delete
Copying counters is disabled.
Telemetry & operator=(Telemetry &&)=delete
Move assignment is disabled.
void RecordEvents(BackendEvent event, std::uint64_t count) noexcept
Add a known number of occurrences for one backend event.
BackendEvent
Shared error and warning categories for backend-specific telemetry.
Definition telemetry.hxx:54
@ Warning
Non-fatal warning reported by a backend.
@ Serialization
Transaction serialization conflict.
@ LockTimeout
Lock wait timed out.
@ Io
Storage or transport I/O failure.
@ Constraint
Constraint violation.
@ Other
Backend failure without a narrower category.
@ Deadlock
Deadlock victim or equivalent conflict.
@ Connection
Connection or protocol failure.
@ Busy
Busy, locked, or lock-timeout condition.
Operation
Database operation categories measured by telemetry.
Definition telemetry.hxx:37
@ RollbackTransaction
Explicit and automatic rollback attempts.
@ CommitTransaction
Transaction commit attempts.
@ Disconnect
Disconnect calls.
@ Connect
Connection attempts.
@ PreparedStatement
Prepared-statement execution attempts.
@ Query
Queries that return rows.
@ SilentQuery
Queries that return only success or failure.
@ BeginTransaction
Transaction begin attempts.
@ PrepareStatement
Prepared-statement registration attempts.
@ Count
Number of operation categories.
Root namespace of the StormByte C++ suite.
STL namespace.
Immutable view of cumulative metrics for one operation category.
Definition telemetry.hxx:71
std::uint64_t Attempts
Number of recorded attempts.
Definition telemetry.hxx:72
std::uint64_t Failures
Number of failed attempts.
Definition telemetry.hxx:74
std::uint64_t MeanNanoseconds() const noexcept
Mean duration across attempts.
Definition telemetry.hxx:83
std::uint64_t MaximumNanoseconds
Maximum measured duration.
Definition telemetry.hxx:77
std::uint64_t Successes
Number of successful attempts.
Definition telemetry.hxx:73
std::uint64_t TotalNanoseconds
Sum of measured durations.
Definition telemetry.hxx:75
std::uint64_t MinimumNanoseconds
Minimum measured duration, or zero when empty.
Definition telemetry.hxx:76
#define STORMBYTE_DATABASE_PUBLIC
Definition visibility.h:54