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
16
17#include <array>
18#include <atomic>
19#include <chrono>
20#include <cstddef>
21#include <cstdint>
22#include <limits>
23#include <string>
24
29namespace StormByte {
34 namespace Database {
51
56 enum class BackendEvent : std::uint8_t {
57 Warning,
58 Busy,
60 Io,
63 Deadlock,
65 Other,
66 Count
67 };
68
74 std::uint64_t Attempts{};
75 std::uint64_t Successes{};
76 std::uint64_t Failures{};
77 std::uint64_t TotalNanoseconds{};
78 std::uint64_t MinimumNanoseconds{};
79 std::uint64_t MaximumNanoseconds{};
80
85 std::uint64_t MeanNanoseconds() const noexcept {
86 return Attempts == 0 ? 0 : TotalNanoseconds / Attempts;
87 }
88 };
89
99 public:
105 public:
112
117
122
128 void Complete(bool success, std::uint64_t rows_returned = 0) noexcept;
129
133 ~OperationScope() noexcept;
134
135 private:
136 StormByte::Safe::Shared<Telemetry> m_telemetry;
137 Operation m_operation;
138 std::chrono::microseconds m_started;
139 bool m_clock_started;
140 std::uint64_t m_rows_returned;
141 bool m_success;
142 bool m_completed;
143 };
144
148 virtual ~Telemetry() noexcept;
149
153 Telemetry() noexcept;
154
160 OperationMetrics Metrics(Operation operation) const noexcept;
161
166 std::uint64_t RowsReturned() const noexcept;
167
173 std::uint64_t Events(BackendEvent event) const noexcept;
174
179 virtual operator StormByte::Safe::String() const override;
180
185 STORMBYTE_FORCE_INLINE operator std::string() const {
186 return static_cast<std::string>(static_cast<StormByte::Safe::String>(*this));
187 }
188
189 protected:
190 Telemetry(const Telemetry&) = delete;
191 Telemetry(Telemetry&&) = delete;
192 Telemetry& operator=(const Telemetry&) = delete;
194
199 virtual void RecordEvent(BackendEvent event) noexcept;
200
206 void RecordEvents(BackendEvent event, std::uint64_t count) noexcept;
207
208 private:
209 friend class Database;
210 friend class PreparedSTMT;
211 friend class OperationScope;
212
213 mutable std::array<StormByte::ThreadLock, static_cast<std::size_t>(Operation::Count)> m_clock_locks;
214
219 struct Counter {
220 std::atomic<std::uint64_t> successes{0};
221 std::atomic<std::uint64_t> failures{0};
222 std::atomic<std::uint64_t> minimum_nanoseconds{std::numeric_limits<std::uint64_t>::max()};
223 std::atomic<std::uint64_t> maximum_nanoseconds{0};
224 };
225
233 void RecordOperation(Operation operation, bool success, std::chrono::nanoseconds elapsed, std::uint64_t rows_returned) noexcept;
234
235 std::array<Counter, static_cast<std::size_t>(Operation::Count)> m_operations;
236 std::array<std::atomic<std::uint64_t>, static_cast<std::size_t>(BackendEvent::Count)> m_events;
237 std::atomic<std::uint64_t> m_rows_returned;
238 };
239 }
240}
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:104
OperationScope(StormByte::Safe::Shared< Telemetry > telemetry, Operation operation) noexcept
OperationScope(OperationScope &&)=delete
Move constructor is deleted.
OperationScope(const OperationScope &)=delete
Copy constructor is deleted.
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:98
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:56
@ 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:39
@ 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:73
std::uint64_t Attempts
Number of recorded attempts.
Definition telemetry.hxx:74
std::uint64_t Failures
Number of failed attempts.
Definition telemetry.hxx:76
std::uint64_t MeanNanoseconds() const noexcept
Mean duration across attempts.
Definition telemetry.hxx:85
std::uint64_t MaximumNanoseconds
Maximum measured duration.
Definition telemetry.hxx:79
std::uint64_t Successes
Number of successful attempts.
Definition telemetry.hxx:75
std::uint64_t TotalNanoseconds
Sum of measured durations.
Definition telemetry.hxx:77
std::uint64_t MinimumNanoseconds
Minimum measured duration, or zero when empty.
Definition telemetry.hxx:78
#define STORMBYTE_DATABASE_PUBLIC
Definition visibility.h:54