From 49df7e4402ea3783f94427b7564de7c403883c7e Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Fri, 5 Jul 2024 13:54:40 +0200 Subject: [PATCH] add settings for telemetry metrics --- lib/datadog/core/configuration/settings.rb | 23 +++++++ lib/datadog/core/telemetry/ext.rb | 2 + sig/datadog/core/telemetry/ext.rbs | 2 + .../core/configuration/settings_spec.rb | 62 +++++++++++++++++++ 4 files changed, 89 insertions(+) diff --git a/lib/datadog/core/configuration/settings.rb b/lib/datadog/core/configuration/settings.rb index 71e43d89f8d..a241b73cc7f 100644 --- a/lib/datadog/core/configuration/settings.rb +++ b/lib/datadog/core/configuration/settings.rb @@ -663,6 +663,16 @@ def initialize(*_) o.type :bool end + # Enable metrics collection for telemetry. Metrics collection only works when telemetry is enabled and + # metrics are enabled. + # @default `DD_TELEMETRY_METRICS_ENABLED` environment variable, otherwise `true`. + # @return [Boolean] + option :metrics_enabled do |o| + o.type :bool + o.env Core::Telemetry::Ext::ENV_METRICS_ENABLED + o.default true + end + # The interval in seconds when telemetry must be sent. # # This method is used internally, for testing purposes only. @@ -676,6 +686,19 @@ def initialize(*_) o.default 60.0 end + # The interval in seconds when telemetry metrics are aggregated. + # Should be a denominator of `heartbeat_interval_seconds`. + # + # This method is used internally, for testing purposes only. + # @default `DD_TELEMETRY_METRICS_AGGREGATION_INTERVAL` environment variable, otherwise `10`. + # @return [Float] + # @!visibility private + option :metrics_aggregation_interval_seconds do |o| + o.type :float + o.env Core::Telemetry::Ext::ENV_METRICS_AGGREGATION_INTERVAL + o.default 10.0 + end + # The install id of the application. # # This method is used internally, by library injection. diff --git a/lib/datadog/core/telemetry/ext.rb b/lib/datadog/core/telemetry/ext.rb index a70b39a28de..2c2e15c0781 100644 --- a/lib/datadog/core/telemetry/ext.rb +++ b/lib/datadog/core/telemetry/ext.rb @@ -5,7 +5,9 @@ module Core module Telemetry module Ext ENV_ENABLED = 'DD_INSTRUMENTATION_TELEMETRY_ENABLED' + ENV_METRICS_ENABLED = 'DD_TELEMETRY_METRICS_ENABLED' ENV_HEARTBEAT_INTERVAL = 'DD_TELEMETRY_HEARTBEAT_INTERVAL' + ENV_METRICS_AGGREGATION_INTERVAL = 'DD_TELEMETRY_METRICS_AGGREGATION_INTERVAL' ENV_DEPENDENCY_COLLECTION = 'DD_TELEMETRY_DEPENDENCY_COLLECTION_ENABLED' ENV_INSTALL_ID = 'DD_INSTRUMENTATION_INSTALL_ID' ENV_INSTALL_TYPE = 'DD_INSTRUMENTATION_INSTALL_TYPE' diff --git a/sig/datadog/core/telemetry/ext.rbs b/sig/datadog/core/telemetry/ext.rbs index f1b3326530b..96ad27c06d9 100644 --- a/sig/datadog/core/telemetry/ext.rbs +++ b/sig/datadog/core/telemetry/ext.rbs @@ -4,7 +4,9 @@ module Datadog module Ext ENV_DEPENDENCY_COLLECTION: ::String ENV_ENABLED: ::String + ENV_METRICS_ENABLED: ::String ENV_HEARTBEAT_INTERVAL: ::String + ENV_METRICS_AGGREGATION_INTERVAL: ::String ENV_INSTALL_ID: ::String ENV_INSTALL_TIME: ::String ENV_INSTALL_TYPE: ::String diff --git a/spec/datadog/core/configuration/settings_spec.rb b/spec/datadog/core/configuration/settings_spec.rb index 33fe290f315..79077bb87c9 100644 --- a/spec/datadog/core/configuration/settings_spec.rb +++ b/spec/datadog/core/configuration/settings_spec.rb @@ -1476,6 +1476,39 @@ end end + describe '#metrics_enabled' do + subject(:metrics_enabled) { settings.telemetry.metrics_enabled } + let(:env_var_name) { 'DD_TELEMETRY_METRICS_ENABLED' } + + context 'when DD_TELEMETRY_METRICS_ENABLED' do + context 'is not defined' do + let(:env_var_value) { nil } + + it { is_expected.to be true } + end + + [true, false].each do |value| + context "is defined as #{value}" do + let(:env_var_value) { value.to_s } + + it { is_expected.to be value } + end + end + end + end + + describe '#metrics_enabled=' do + let(:env_var_name) { 'DD_TELEMETRY_METRICS_ENABLED' } + let(:env_var_value) { 'true' } + + it 'updates the #metrics_enabled setting' do + expect { settings.telemetry.metrics_enabled = false } + .to change { settings.telemetry.metrics_enabled } + .from(true) + .to(false) + end + end + describe '#heartbeat_interval' do subject(:heartbeat_interval_seconds) { settings.telemetry.heartbeat_interval_seconds } let(:env_var_name) { 'DD_TELEMETRY_HEARTBEAT_INTERVAL' } @@ -1505,6 +1538,35 @@ end end + describe '#metrics_aggregation_interval_seconds' do + subject(:metrics_aggregation_interval_seconds) { settings.telemetry.metrics_aggregation_interval_seconds } + let(:env_var_name) { 'DD_TELEMETRY_METRICS_AGGREGATION_INTERVAL' } + + context 'when DD_TELEMETRY_METRICS_AGGREGATION_INTERVAL' do + context 'is not defined' do + let(:env_var_value) { nil } + + it { is_expected.to eq 10.0 } + end + + context 'is defined' do + let(:env_var_value) { '1.1' } + + it { is_expected.to eq 1.1 } + end + end + end + + describe '#metrics_aggregation_interval_seconds=' do + let(:env_var_name) { 'DD_TELEMETRY_METRICS_AGGREGATION_INTERVAL' } + let(:env_var_value) { '1.1' } + + it 'updates the #metrics_aggregation_interval_seconds setting' do + expect { settings.telemetry.metrics_aggregation_interval_seconds = 2.2 } + .to change { settings.telemetry.metrics_aggregation_interval_seconds }.from(1.1).to(2.2) + end + end + describe '#install_id' do subject(:install_id) { settings.telemetry.install_id } let(:env_var_name) { 'DD_INSTRUMENTATION_INSTALL_ID' }