PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
monitoring_adapter.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
18#pragma once
19
20#include <chrono>
21#include <cstdint>
22#include <functional>
23#include <map>
24#include <memory>
25#include <optional>
26#include <string>
27#include <string_view>
28
30
31// ─────────────────────────────────────────────────────
32// Forward Declarations
33// ─────────────────────────────────────────────────────
34
35// Note: query_level is defined in logger_adapter.hpp
36// If logger_adapter.hpp is not included, we define it here
37#ifndef PACS_INTEGRATION_QUERY_LEVEL_DEFINED
38#define PACS_INTEGRATION_QUERY_LEVEL_DEFINED
44#endif
45
46// ─────────────────────────────────────────────────────
47// Configuration
48// ─────────────────────────────────────────────────────
49
56 bool enable_metrics{true};
57
59 bool enable_tracing{true};
60
62 std::chrono::seconds export_interval{30};
63
65 std::optional<std::uint16_t> metrics_port;
66
68 std::optional<std::string> tracing_endpoint;
69
71 std::string service_name{"pacs_server"};
72
74 std::size_t max_samples_per_operation{10000};
75};
76
77// ─────────────────────────────────────────────────────
78// Monitoring Adapter Class
79// ─────────────────────────────────────────────────────
80
129public:
130 // ─────────────────────────────────────────────────────
131 // Initialization
132 // ─────────────────────────────────────────────────────
133
142 static void initialize(const monitoring_config& config);
143
150 static void shutdown();
151
156 [[nodiscard]] static auto is_initialized() noexcept -> bool;
157
158 // ─────────────────────────────────────────────────────
159 // Metrics
160 // ─────────────────────────────────────────────────────
161
167 static void increment_counter(std::string_view name, std::int64_t value = 1);
168
174 static void set_gauge(std::string_view name, double value);
175
181 static void record_histogram(std::string_view name, double value);
182
188 static void record_timing(std::string_view name,
189 std::chrono::nanoseconds duration);
190
191 // ─────────────────────────────────────────────────────
192 // DICOM-Specific Metrics
193 // ─────────────────────────────────────────────────────
194
210 static void record_c_store(std::chrono::nanoseconds duration,
211 std::size_t bytes,
212 bool success);
213
228 static void record_c_find(std::chrono::nanoseconds duration,
229 std::size_t matches,
230 query_level level);
231
244 static void record_association(const std::string& calling_ae,
245 bool established);
246
259 static void update_storage_stats(std::size_t total_instances,
260 std::size_t total_bytes);
261
262 // ─────────────────────────────────────────────────────
263 // Distributed Tracing
264 // ─────────────────────────────────────────────────────
265
274 class span {
275 public:
280 explicit span(std::string_view operation_name);
281
286
287 // Disable copy
288 span(const span&) = delete;
289 span& operator=(const span&) = delete;
290
291 // Enable move
292 span(span&& other) noexcept;
293 span& operator=(span&& other) noexcept;
294
300 void set_tag(std::string_view key, std::string_view value);
301
306 void add_event(std::string_view name);
307
312 void set_error(const std::exception& e);
313
318 [[nodiscard]] auto trace_id() const -> std::string;
319
324 [[nodiscard]] auto span_id() const -> std::string;
325
330 [[nodiscard]] auto is_valid() const noexcept -> bool;
331
332 private:
333 class impl;
334 std::unique_ptr<impl> impl_;
335 };
336
342 [[nodiscard]] static auto start_span(std::string_view operation) -> span;
343
344 // ─────────────────────────────────────────────────────
345 // Health Check
346 // ─────────────────────────────────────────────────────
347
354 bool healthy{true};
355
357 std::string status{"healthy"};
358
360 std::map<std::string, std::string> components;
361 };
362
367 [[nodiscard]] static auto get_health() -> health_status;
368
378 static void register_health_check(std::string_view component,
379 std::function<bool()> check);
380
385 static void unregister_health_check(std::string_view component);
386
387 // ─────────────────────────────────────────────────────
388 // Configuration
389 // ─────────────────────────────────────────────────────
390
395 [[nodiscard]] static auto get_config() -> const monitoring_config&;
396
397private:
398 // Private implementation helpers
399 [[nodiscard]] static auto query_level_to_string(query_level level)
400 -> std::string;
401
402 // Internal state (managed through pimpl or static members)
403 class impl;
404 static std::unique_ptr<impl> pimpl_;
405};
406
407} // namespace kcenon::pacs::integration
Represents a unit of work in distributed tracing.
~span()
Destructor - automatically finishes the span.
void set_error(const std::exception &e)
Mark the span as an error.
void set_tag(std::string_view key, std::string_view value)
Set a tag on the span.
auto span_id() const -> std::string
Get the span ID.
void add_event(std::string_view name)
Add an event to the span.
auto is_valid() const noexcept -> bool
Check if span is valid (properly initialized)
auto trace_id() const -> std::string
Get the trace ID.
span(std::string_view operation_name)
Construct a new span.
Adapter for PACS performance metrics and distributed tracing.
static void update_storage_stats(std::size_t total_instances, std::size_t total_bytes)
Update storage statistics.
static void record_c_find(std::chrono::nanoseconds duration, std::size_t matches, query_level level)
Record C-FIND operation metrics.
static auto get_health() -> health_status
Get current health status.
static auto get_config() -> const monitoring_config &
Get the current configuration.
static void unregister_health_check(std::string_view component)
Unregister a health check.
static void record_timing(std::string_view name, std::chrono::nanoseconds duration)
Record a timing measurement.
static void record_c_store(std::chrono::nanoseconds duration, std::size_t bytes, bool success)
Record C-STORE operation metrics.
static void record_association(const std::string &calling_ae, bool established)
Record DICOM association metrics.
static auto start_span(std::string_view operation) -> span
Start a new trace span.
static void record_histogram(std::string_view name, double value)
Record a histogram sample.
static void register_health_check(std::string_view component, std::function< bool()> check)
Register a health check for a component.
static void initialize(const monitoring_config &config)
Initialize the monitoring adapter with configuration.
static void increment_counter(std::string_view name, std::int64_t value=1)
Increment a counter metric.
static void set_gauge(std::string_view name, double value)
Set a gauge metric value.
static auto query_level_to_string(query_level level) -> std::string
static void shutdown()
Shutdown the monitoring adapter.
static auto is_initialized() noexcept -> bool
Check if the monitoring adapter is initialized.
query_level
DICOM query retrieve level.
Health check result containing component status.
std::map< std::string, std::string > components
Per-component health status.
Configuration options for the monitoring adapter.
bool enable_tracing
Enable distributed tracing.
std::optional< std::uint16_t > metrics_port
Prometheus-style metrics endpoint port (nullopt = disabled)
std::string service_name
Service name for tracing.
std::chrono::seconds export_interval
Interval for exporting metrics.
bool enable_metrics
Enable metrics collection.
std::optional< std::string > tracing_endpoint
Jaeger/Zipkin-style tracing endpoint (nullopt = disabled)
std::size_t max_samples_per_operation
Maximum samples to keep per operation.
std::string_view name