2025-12-17 10:47:36 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// Copyright (C) 2025- Equinor ASA
|
|
|
|
|
//
|
|
|
|
|
// ResInsight is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
//
|
|
|
|
|
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
|
|
|
|
// for more details.
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <queue>
|
|
|
|
|
#include <string>
|
2026-08-21 14:37:58 +02:00
|
|
|
#include <vector>
|
2026-06-01 12:38:55 +02:00
|
|
|
#include <version>
|
|
|
|
|
|
2026-09-10 07:29:31 -04:00
|
|
|
// std::stacktrace is C++23 and not usable on all toolchains. The build system
|
|
|
|
|
// probes it by linking and may force it off via RIA_HAS_STD_STACKTRACE=0, see
|
|
|
|
|
// cmake/StdStacktrace.cmake and #14045.
|
|
|
|
|
#ifndef RIA_HAS_STD_STACKTRACE
|
2026-06-01 12:38:55 +02:00
|
|
|
#if defined( __cpp_lib_stacktrace ) && __cpp_lib_stacktrace >= 202011L
|
|
|
|
|
#define RIA_HAS_STD_STACKTRACE 1
|
|
|
|
|
#else
|
|
|
|
|
#define RIA_HAS_STD_STACKTRACE 0
|
|
|
|
|
#endif
|
2026-09-10 07:29:31 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if RIA_HAS_STD_STACKTRACE
|
|
|
|
|
#include <stacktrace>
|
|
|
|
|
#endif
|
2025-12-17 10:47:36 +01:00
|
|
|
|
|
|
|
|
class QString;
|
|
|
|
|
class QNetworkAccessManager;
|
|
|
|
|
class QTimer;
|
|
|
|
|
|
|
|
|
|
//==================================================================================================
|
|
|
|
|
//
|
|
|
|
|
// OpenTelemetry Manager for ResInsight
|
|
|
|
|
// Handles async telemetry reporting with privacy filtering and resilience
|
|
|
|
|
//
|
|
|
|
|
//==================================================================================================
|
|
|
|
|
class RiaOpenTelemetryManager : public QObject
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
// Error handling
|
|
|
|
|
enum class TelemetryError
|
|
|
|
|
{
|
|
|
|
|
ConfigurationError,
|
|
|
|
|
NetworkError,
|
|
|
|
|
AuthenticationError,
|
|
|
|
|
QuotaExceeded,
|
|
|
|
|
PrivacyViolation,
|
|
|
|
|
InternalError
|
|
|
|
|
};
|
|
|
|
|
using ErrorCallback = std::function<void( TelemetryError, const QString& details )>;
|
|
|
|
|
|
|
|
|
|
// Health monitoring
|
|
|
|
|
struct HealthSnapshot
|
|
|
|
|
{
|
|
|
|
|
uint64_t eventsQueued{ 0 };
|
|
|
|
|
uint64_t eventsSent{ 0 };
|
|
|
|
|
uint64_t eventsDropped{ 0 };
|
|
|
|
|
uint64_t networkFailures{ 0 };
|
|
|
|
|
std::chrono::steady_clock::time_point lastSuccessfulSend;
|
|
|
|
|
std::chrono::steady_clock::time_point systemStartTime;
|
|
|
|
|
|
|
|
|
|
double getSuccessRate() const;
|
|
|
|
|
bool isHealthy() const;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct HealthMetrics
|
|
|
|
|
{
|
|
|
|
|
std::atomic<uint64_t> eventsQueued{ 0 };
|
|
|
|
|
std::atomic<uint64_t> eventsSent{ 0 };
|
|
|
|
|
std::atomic<uint64_t> eventsDropped{ 0 };
|
|
|
|
|
std::atomic<uint64_t> networkFailures{ 0 };
|
|
|
|
|
std::chrono::steady_clock::time_point lastSuccessfulSend;
|
|
|
|
|
std::chrono::steady_clock::time_point systemStartTime;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static RiaOpenTelemetryManager& instance();
|
2025-12-22 08:29:18 +01:00
|
|
|
static QString getSystemUsername();
|
2025-12-17 10:47:36 +01:00
|
|
|
|
|
|
|
|
bool initialize();
|
2025-12-22 08:29:18 +01:00
|
|
|
void shutdown();
|
|
|
|
|
bool reinitialize();
|
2025-12-17 10:47:36 +01:00
|
|
|
|
|
|
|
|
// Event reporting
|
|
|
|
|
void reportEventAsync( const std::string& eventName, const std::map<std::string, std::string>& attributes );
|
2026-06-01 12:38:55 +02:00
|
|
|
#if RIA_HAS_STD_STACKTRACE
|
2026-03-23 08:48:26 +01:00
|
|
|
void reportCrash( int signalCode, const std::stacktrace& trace, const std::map<std::string, std::string>& extraAttributes = {} );
|
2026-06-01 12:38:55 +02:00
|
|
|
#endif
|
2025-12-17 10:47:36 +01:00
|
|
|
|
|
|
|
|
// Configuration
|
2026-10-19 10:05:55 +00:00
|
|
|
bool isEnabled() const;
|
2026-10-19 11:34:45 +02:00
|
|
|
bool isInitialized() const;
|
|
|
|
|
static bool isReportingCrash();
|
2026-10-19 10:05:55 +00:00
|
|
|
void setErrorCallback( ErrorCallback callback );
|
|
|
|
|
void setUsername( const std::string& username );
|
2026-10-25 09:24:22 +01:00
|
|
|
void setGraphicsInfo( const std::map<std::string, std::string>& graphicsInfo );
|
2025-12-17 10:47:36 +01:00
|
|
|
|
|
|
|
|
// Performance and memory management
|
|
|
|
|
void setMaxQueueSize( size_t maxEvents );
|
|
|
|
|
void enableBackpressure( bool enable );
|
|
|
|
|
void setMemoryThreshold( size_t maxMemoryMB );
|
|
|
|
|
size_t getCurrentQueueSize() const;
|
|
|
|
|
|
|
|
|
|
// Health monitoring
|
|
|
|
|
HealthSnapshot getHealthMetrics() const;
|
|
|
|
|
bool isHealthy() const;
|
|
|
|
|
void enableHealthMonitoring( bool enable );
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
RiaOpenTelemetryManager();
|
2025-12-23 02:35:24 +00:00
|
|
|
~RiaOpenTelemetryManager() override;
|
2025-12-17 10:47:36 +01:00
|
|
|
|
|
|
|
|
RiaOpenTelemetryManager( const RiaOpenTelemetryManager& ) = delete;
|
|
|
|
|
RiaOpenTelemetryManager& operator=( const RiaOpenTelemetryManager& ) = delete;
|
|
|
|
|
|
|
|
|
|
struct Event
|
|
|
|
|
{
|
|
|
|
|
std::string name;
|
|
|
|
|
std::map<std::string, std::string> attributes;
|
|
|
|
|
std::chrono::system_clock::time_point timestamp;
|
|
|
|
|
|
|
|
|
|
Event( const std::string& eventName, const std::map<std::string, std::string>& attrs )
|
|
|
|
|
: name( eventName )
|
|
|
|
|
, attributes( attrs )
|
|
|
|
|
, timestamp( std::chrono::system_clock::now() )
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Initialization
|
|
|
|
|
bool initializeProvider();
|
|
|
|
|
bool createExporter();
|
|
|
|
|
void setupResourceAttributes();
|
|
|
|
|
|
|
|
|
|
// Event processing
|
2026-08-21 14:37:58 +02:00
|
|
|
void processEvents( bool ignoreCircuitBreaker = false );
|
|
|
|
|
void sendBatch( const std::vector<Event>& events );
|
2025-12-17 10:47:36 +01:00
|
|
|
void flushPendingEvents();
|
|
|
|
|
void onProcessEventTimer();
|
|
|
|
|
|
|
|
|
|
// Circuit breaker and resilience
|
|
|
|
|
void handleError( TelemetryError error, const QString& context );
|
|
|
|
|
void attemptReconnection();
|
|
|
|
|
bool isCircuitBreakerOpen() const;
|
|
|
|
|
void resetCircuitBreaker();
|
|
|
|
|
|
2026-02-20 14:46:42 +01:00
|
|
|
// Event filtering
|
|
|
|
|
bool isEventAllowed( const std::string& eventName ) const;
|
|
|
|
|
|
2025-12-17 10:47:36 +01:00
|
|
|
// Health monitoring
|
|
|
|
|
void updateHealthMetrics( bool success );
|
|
|
|
|
void sendHealthSpan();
|
|
|
|
|
|
|
|
|
|
// Thread safety
|
|
|
|
|
mutable std::mutex m_configMutex;
|
|
|
|
|
mutable std::mutex m_queueMutex;
|
|
|
|
|
std::queue<Event> m_eventQueue;
|
|
|
|
|
|
|
|
|
|
// State
|
2026-10-19 10:05:55 +00:00
|
|
|
std::atomic<bool> m_initialized{ false };
|
|
|
|
|
std::atomic<bool> m_enabled{ false };
|
|
|
|
|
std::atomic<bool> m_isShuttingDown{ false };
|
|
|
|
|
std::atomic<bool> m_circuitBreakerOpen{ false };
|
2026-10-19 11:34:45 +02:00
|
|
|
static inline std::atomic<bool> m_isReportingCrash{ false };
|
2025-12-17 10:47:36 +01:00
|
|
|
|
|
|
|
|
// Qt networking and timer
|
|
|
|
|
QNetworkAccessManager* m_networkAccessManager{ nullptr };
|
|
|
|
|
QTimer* m_processTimer{ nullptr };
|
|
|
|
|
QTimer* m_healthTimer{ nullptr };
|
|
|
|
|
|
|
|
|
|
// Configuration
|
2025-12-22 08:29:18 +01:00
|
|
|
size_t m_maxQueueSize{ 10000 };
|
|
|
|
|
bool m_backpressureEnabled{ true };
|
|
|
|
|
size_t m_memoryThresholdMB{ 50 };
|
2026-05-26 15:07:37 +02:00
|
|
|
std::string m_userHash;
|
2025-12-17 10:47:36 +01:00
|
|
|
|
2026-10-25 09:24:22 +01:00
|
|
|
// Graphics environment attached to all events, see RiaGraphicsEnvironmentTools
|
|
|
|
|
std::map<std::string, std::string> m_graphicsInfo;
|
|
|
|
|
|
2025-12-17 10:47:36 +01:00
|
|
|
// Error handling
|
|
|
|
|
ErrorCallback m_errorCallback;
|
|
|
|
|
std::atomic<int> m_consecutiveFailures{ 0 };
|
|
|
|
|
std::chrono::steady_clock::time_point m_lastReconnectAttempt;
|
|
|
|
|
|
2026-03-09 15:07:39 +01:00
|
|
|
// Pending network reply tracking (for synchronous crash flush)
|
|
|
|
|
std::atomic<int> m_pendingReplies{ 0 };
|
|
|
|
|
|
2025-12-17 10:47:36 +01:00
|
|
|
// Health monitoring
|
|
|
|
|
mutable HealthMetrics m_healthMetrics;
|
2025-12-22 08:29:18 +01:00
|
|
|
bool m_healthMonitoringEnabled{ false };
|
2025-12-17 10:47:36 +01:00
|
|
|
};
|