Files
ResInsight/ApplicationLibCode/Application/RiaPreferencesOpenTelemetry.cpp
T
Magne Sjaastad 74324d14a4 Telemetry: Add event allowlist/denylist filtering
Extend opentelemetry_config.json with optional event_allowlist and
event_denylist arrays. Patterns support wildcard suffix (e.g. grpc.*).
Allowlist is checked first; denylist further restricts within the
allowed set. crash.* events always bypass all filters and queue limits.

Add RiaConnectorTools-Test.cpp covering all JSON value types including
the new array handling.
2026-02-23 12:29:41 +01:00

265 lines
10 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// 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.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiaPreferencesOpenTelemetry.h"
#include "RiaApplication.h"
#include "RiaLogging.h"
#include "RiaPreferences.h"
#include "RiaVersionInfo.h"
#include "cafPdmUiTextEditor.h"
namespace caf
{
template <>
void RiaPreferencesOpenTelemetry::LoggingStateType::setUp()
{
addItem( RiaPreferencesOpenTelemetry::LoggingState::DISABLED, "DISABLED", "Disabled" );
addItem( RiaPreferencesOpenTelemetry::LoggingState::DEFAULT, "DEFAULT", "Default" );
setDefault( RiaPreferencesOpenTelemetry::LoggingState::DEFAULT );
}
} // namespace caf
CAF_PDM_SOURCE_INIT( RiaPreferencesOpenTelemetry, "RiaPreferencesOpenTelemetry" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiaPreferencesOpenTelemetry::RiaPreferencesOpenTelemetry()
{
CAF_PDM_InitObject( "OpenTelemetry Configuration", "", "", "Configuration for OpenTelemetry crash reporting and telemetry" );
CAF_PDM_InitField( &m_configFile, "configFile", QString( "No config file detected" ), "Config File" );
CAF_PDM_InitField( &m_loggingState, "loggingState_v1", LoggingStateType( LoggingState::DEFAULT ), "Logging State" );
CAF_PDM_InitField( &m_connectionString, "connectionString", QString(), "Azure Connection String" );
m_connectionString.uiCapability()->setUiEditorTypeName( caf::PdmUiTextEditor::uiEditorTypeName() );
CAF_PDM_InitField( &m_batchTimeoutMs, "batchTimeoutMs", 5000, "Batch Timeout (ms)" );
CAF_PDM_InitField( &m_maxBatchSize, "maxBatchSize", 512, "Max Batch Size" );
CAF_PDM_InitField( &m_maxQueueSize, "maxQueueSize", 10000, "Max Queue Size" );
CAF_PDM_InitField( &m_memoryThresholdMb, "memoryThresholdMb", 50, "Memory Threshold (MB)" );
CAF_PDM_InitField( &m_samplingRate, "samplingRate", 1.0, "Sampling Rate" );
CAF_PDM_InitField( &m_connectionTimeoutMs, "connectionTimeoutMs", 10000, "Connection Timeout (ms)" );
CAF_PDM_InitField( &m_eventAllowlist, "eventAllowlist", QString(), "Event Allowlist" );
CAF_PDM_InitField( &m_eventDenylist, "eventDenylist", QString(), "Event Denylist" );
setFieldStates();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiaPreferencesOpenTelemetry* RiaPreferencesOpenTelemetry::current()
{
return RiaApplication::instance()->preferences()->openTelemetryPreferences();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaPreferencesOpenTelemetry::setData( const std::map<QString, QString>& keyValuePairs, const QString& configFile )
{
m_configFile = configFile;
for ( const auto& [key, value] : keyValuePairs )
{
if ( key == "connection_string" )
{
m_connectionString = value;
}
else if ( key == "batch_timeout_ms" )
{
m_batchTimeoutMs = value.toInt();
}
else if ( key == "max_batch_size" )
{
m_maxBatchSize = value.toInt();
}
else if ( key == "max_queue_size" )
{
m_maxQueueSize = value.toInt();
}
else if ( key == "memory_threshold_mb" )
{
m_memoryThresholdMb = value.toInt();
}
else if ( key == "sampling_rate" )
{
m_samplingRate = value.toDouble();
}
else if ( key == "connection_timeout_ms" )
{
m_connectionTimeoutMs = value.toInt();
}
else if ( key == "event_allowlist" )
{
m_eventAllowlist = value;
}
else if ( key == "event_denylist" )
{
m_eventDenylist = value;
}
else
{
RiaLogging::warning( QString( "Unknown OpenTelemetry config key: '%1'" ).arg( key ) );
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaPreferencesOpenTelemetry::setFieldStates()
{
std::vector<caf::PdmFieldHandle*> fields = this->fields();
for ( auto field : fields )
{
// Keep logging state editable
if ( field != &m_loggingState )
{
field->uiCapability()->setUiReadOnly( true );
field->xmlCapability()->disableIO();
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaPreferencesOpenTelemetry::defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering )
{
uiOrdering.add( &m_loggingState );
// Only show configuration fields if not disabled
if ( m_loggingState() != LoggingState::DISABLED )
{
uiOrdering.add( &m_configFile );
uiOrdering.add( &m_connectionString );
auto group = uiOrdering.addNewGroup( "Configuration" );
group->setCollapsedByDefault();
group->add( &m_batchTimeoutMs );
group->add( &m_maxBatchSize );
group->add( &m_maxQueueSize );
group->add( &m_memoryThresholdMb );
group->add( &m_samplingRate );
group->add( &m_connectionTimeoutMs );
group->add( &m_eventAllowlist );
group->add( &m_eventDenylist );
}
uiOrdering.skipRemainingFields();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaPreferencesOpenTelemetry::serviceName() const
{
return QStringLiteral( "ResInsight" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaPreferencesOpenTelemetry::serviceVersion() const
{
return QString( STRPRODUCTVER );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaPreferencesOpenTelemetry::connectionString() const
{
return m_connectionString;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiaPreferencesOpenTelemetry::batchTimeoutMs() const
{
return m_batchTimeoutMs;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiaPreferencesOpenTelemetry::maxBatchSize() const
{
return m_maxBatchSize;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiaPreferencesOpenTelemetry::maxQueueSize() const
{
return m_maxQueueSize;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiaPreferencesOpenTelemetry::memoryThresholdMb() const
{
return m_memoryThresholdMb;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RiaPreferencesOpenTelemetry::samplingRate() const
{
return m_samplingRate;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiaPreferencesOpenTelemetry::connectionTimeoutMs() const
{
return m_connectionTimeoutMs;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiaPreferencesOpenTelemetry::LoggingState RiaPreferencesOpenTelemetry::loggingState() const
{
return m_loggingState();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QStringList RiaPreferencesOpenTelemetry::eventAllowlist() const
{
return m_eventAllowlist().split( ',', Qt::SkipEmptyParts );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QStringList RiaPreferencesOpenTelemetry::eventDenylist() const
{
return m_eventDenylist().split( ',', Qt::SkipEmptyParts );
}