mirror of
https://github.com/OPM/ResInsight.git
synced 2026-10-31 17:17:50 -05:00
Simplifies OpenTelemetry integration
Removes conditional compilation for OpenTelemetry, ensuring it is always included. Reinitializes OpenTelemetry based on user preferences, ensuring that changes to telemetry settings are applied immediately. Configures cloud services, including OpenTelemetry, after the logger is initialized, ensuring that telemetry data is properly captured and transmitted. Retrieves system username for telemetry tracking.
This commit is contained in:
1 parent
43755feb42
commit
7f0ef8dbc4
11 files changed
+151
-106
No files matched your search
@@ -25,6 +25,7 @@
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <QProcessEnvironment>
|
||||
#include <QString>
|
||||
#include <QSysInfo>
|
||||
#include <QTimer>
|
||||
@@ -64,6 +65,25 @@ RiaOpenTelemetryManager& RiaOpenTelemetryManager::instance()
|
||||
return instance;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// Get system username in a cross-platform way
|
||||
/// Returns username from environment variables (USERNAME on Windows, USER on Linux)
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiaOpenTelemetryManager::getSystemUsername()
|
||||
{
|
||||
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
||||
|
||||
// Try Windows environment variable first
|
||||
QString username = env.value( "USERNAME" );
|
||||
if ( !username.isEmpty() )
|
||||
{
|
||||
return username;
|
||||
}
|
||||
|
||||
// Try Linux/Unix environment variable. Can be empty on some systems
|
||||
return env.value( "USER" );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@@ -139,7 +159,7 @@ bool RiaOpenTelemetryManager::initialize()
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaOpenTelemetryManager::shutdown( std::chrono::seconds timeout )
|
||||
void RiaOpenTelemetryManager::shutdown()
|
||||
{
|
||||
if ( !m_initialized.load() )
|
||||
{
|
||||
@@ -165,12 +185,21 @@ void RiaOpenTelemetryManager::shutdown( std::chrono::seconds timeout )
|
||||
m_initialized = false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RiaOpenTelemetryManager::reinitialize()
|
||||
{
|
||||
shutdown();
|
||||
return initialize();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaOpenTelemetryManager::reportEventAsync( const std::string& eventName, const std::map<std::string, std::string>& attributes )
|
||||
{
|
||||
if ( !isEnabled() || isCircuitBreakerOpen() || !shouldSampleEvent() )
|
||||
if ( !isEnabled() || isCircuitBreakerOpen() )
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -229,11 +258,8 @@ void RiaOpenTelemetryManager::reportCrash( int signalCode, const std::stacktrace
|
||||
attributes["service.name"] = RiaPreferencesOpenTelemetry::current()->serviceName().toStdString();
|
||||
attributes["service.version"] = RiaPreferencesOpenTelemetry::current()->serviceVersion().toStdString();
|
||||
|
||||
// Report with high priority (bypass sampling)
|
||||
std::unique_lock<std::mutex> lock( m_queueMutex );
|
||||
m_eventQueue.emplace( "crash.signal_handler", attributes );
|
||||
m_healthMetrics.eventsQueued++;
|
||||
lock.unlock();
|
||||
reportEventAsync( "crash.signal_handler", attributes );
|
||||
flushPendingEvents();
|
||||
|
||||
RiaLogging::error( QString( "Crash reported to OpenTelemetry (signal: %1)" ).arg( signalCode ) );
|
||||
}
|
||||
@@ -266,7 +292,7 @@ void RiaOpenTelemetryManager::reportTestCrash( const std::stacktrace& trace )
|
||||
attributes["service.name"] = RiaPreferencesOpenTelemetry::current()->serviceName().toStdString();
|
||||
attributes["service.version"] = RiaPreferencesOpenTelemetry::current()->serviceVersion().toStdString();
|
||||
|
||||
reportEventAsync( "test.stack_trace", attributes );
|
||||
reportEventAsync( "crash.signal_handler", attributes );
|
||||
|
||||
RiaLogging::info( "Test stack trace reported to OpenTelemetry" );
|
||||
}
|
||||
@@ -296,6 +322,15 @@ void RiaOpenTelemetryManager::setErrorCallback( ErrorCallback callback )
|
||||
m_errorCallback = callback;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaOpenTelemetryManager::setUsername( const std::string& username )
|
||||
{
|
||||
std::lock_guard<std::mutex> lock( m_configMutex );
|
||||
m_username = username;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@@ -323,15 +358,6 @@ void RiaOpenTelemetryManager::setMemoryThreshold( size_t maxMemoryMB )
|
||||
m_memoryThresholdMB = maxMemoryMB;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaOpenTelemetryManager::setSamplingRate( double rate )
|
||||
{
|
||||
std::lock_guard<std::mutex> lock( m_configMutex );
|
||||
m_samplingRate = std::clamp( rate, 0.0, 1.0 );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@@ -551,19 +577,59 @@ void RiaOpenTelemetryManager::processEvent( const Event& event )
|
||||
properties["os.version"] = QSysInfo::productVersion();
|
||||
properties["os.name"] = QSysInfo::prettyProductName();
|
||||
|
||||
// Create Application Insights telemetry item
|
||||
// Add username if configured
|
||||
{
|
||||
std::lock_guard<std::mutex> lock( m_configMutex );
|
||||
if ( !m_username.empty() )
|
||||
{
|
||||
properties["user.name"] = QString::fromStdString( m_username );
|
||||
}
|
||||
}
|
||||
|
||||
// Determine if this is a crash event
|
||||
bool isCrashEvent = ( event.name == "crash.signal_handler" );
|
||||
|
||||
QMap<QString, QVariant> baseData;
|
||||
baseData["name"] = QString::fromStdString( event.name );
|
||||
baseData["properties"] = properties;
|
||||
|
||||
QMap<QString, QVariant> data;
|
||||
data["baseType"] = "EventData";
|
||||
data["baseData"] = baseData;
|
||||
|
||||
QMap<QString, QVariant> telemetryItem;
|
||||
|
||||
if ( isCrashEvent )
|
||||
{
|
||||
// Create ExceptionData for crash reports
|
||||
QMap<QString, QVariant> exception;
|
||||
exception["typeName"] = QString( "ResInsightCrash" );
|
||||
exception["message"] = QString( "Application crash (signal: %1)" ).arg( properties["crash.signal"].toString() );
|
||||
exception["hasFullStack"] = true;
|
||||
exception["stack"] = properties["crash.stack_trace"];
|
||||
|
||||
QList<QVariant> exceptions;
|
||||
exceptions.append( exception );
|
||||
|
||||
// Remove stack trace from properties as it's now in the exception
|
||||
properties.remove( "crash.stack_trace" );
|
||||
|
||||
baseData["exceptions"] = exceptions;
|
||||
baseData["properties"] = properties;
|
||||
|
||||
data["baseType"] = "ExceptionData";
|
||||
data["baseData"] = baseData;
|
||||
|
||||
telemetryItem["name"] = "Microsoft.ApplicationInsights.Exception";
|
||||
}
|
||||
else
|
||||
{
|
||||
// Create EventData for regular events
|
||||
baseData["name"] = QString::fromStdString( event.name );
|
||||
baseData["properties"] = properties;
|
||||
|
||||
data["baseType"] = "EventData";
|
||||
data["baseData"] = baseData;
|
||||
|
||||
telemetryItem["name"] = "Microsoft.ApplicationInsights.Event";
|
||||
}
|
||||
|
||||
telemetryItem["time"] = QString::fromStdString( timestamp );
|
||||
telemetryItem["iKey"] = connectionParams["InstrumentationKey"];
|
||||
telemetryItem["name"] = "Microsoft.ApplicationInsights.Event";
|
||||
telemetryItem["data"] = data;
|
||||
|
||||
// Convert to JSON string
|
||||
@@ -609,22 +675,6 @@ void RiaOpenTelemetryManager::processEvent( const Event& event )
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RiaOpenTelemetryManager::shouldSampleEvent() const
|
||||
{
|
||||
if ( m_samplingRate >= 1.0 )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
static thread_local std::mt19937 gen( std::random_device{}() );
|
||||
static thread_local std::uniform_real_distribution<double> dis( 0.0, 1.0 );
|
||||
|
||||
return dis( gen ) < m_samplingRate;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -80,9 +80,11 @@ public:
|
||||
};
|
||||
|
||||
static RiaOpenTelemetryManager& instance();
|
||||
static QString getSystemUsername();
|
||||
|
||||
bool initialize();
|
||||
void shutdown( std::chrono::seconds timeout = std::chrono::seconds( 30 ) );
|
||||
void shutdown();
|
||||
bool reinitialize();
|
||||
|
||||
// Event reporting
|
||||
void reportEventAsync( const std::string& eventName, const std::map<std::string, std::string>& attributes );
|
||||
@@ -93,12 +95,12 @@ public:
|
||||
bool isEnabled() const;
|
||||
bool isInitialized() const;
|
||||
void setErrorCallback( ErrorCallback callback );
|
||||
void setUsername( const std::string& username );
|
||||
|
||||
// Performance and memory management
|
||||
void setMaxQueueSize( size_t maxEvents );
|
||||
void enableBackpressure( bool enable );
|
||||
void setMemoryThreshold( size_t maxMemoryMB );
|
||||
void setSamplingRate( double rate );
|
||||
size_t getCurrentQueueSize() const;
|
||||
|
||||
// Health monitoring
|
||||
@@ -135,10 +137,8 @@ private:
|
||||
// Event processing
|
||||
void processEvents();
|
||||
void processEvent( const Event& event );
|
||||
bool shouldSampleEvent() const;
|
||||
void flushPendingEvents();
|
||||
void onProcessEventTimer();
|
||||
void onNetworkReplyFinished();
|
||||
|
||||
// Circuit breaker and resilience
|
||||
void handleError( TelemetryError error, const QString& context );
|
||||
@@ -167,10 +167,10 @@ private:
|
||||
QTimer* m_healthTimer{ nullptr };
|
||||
|
||||
// Configuration
|
||||
size_t m_maxQueueSize{ 10000 };
|
||||
bool m_backpressureEnabled{ true };
|
||||
size_t m_memoryThresholdMB{ 50 };
|
||||
double m_samplingRate{ 1.0 };
|
||||
size_t m_maxQueueSize{ 10000 };
|
||||
bool m_backpressureEnabled{ true };
|
||||
size_t m_memoryThresholdMB{ 50 };
|
||||
std::string m_username;
|
||||
|
||||
// Error handling
|
||||
ErrorCallback m_errorCallback;
|
||||
@@ -179,5 +179,5 @@ private:
|
||||
|
||||
// Health monitoring
|
||||
mutable HealthMetrics m_healthMetrics;
|
||||
bool m_healthMonitoringEnabled{ true };
|
||||
bool m_healthMonitoringEnabled{ false };
|
||||
};
|
||||
Reference in new issue
Block a user