Skip to content

Commit

Permalink
Suppress startup logs in test and development environments
Browse files Browse the repository at this point in the history
  • Loading branch information
marcotc committed Aug 8, 2024
1 parent acd9271 commit 53a3900
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 53 deletions.
15 changes: 4 additions & 11 deletions lib/datadog/core/diagnostics/environment_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module Datadog
module Core
module Diagnostics
# Base class for EnvironmentLoggers - should allow for easy reporting by users to Datadog support.
#
# The EnvironmentLogger should not pollute the logs in a development environment.
module EnvironmentLogging
def log_configuration!(prefix, data)
logger.info("DATADOG CONFIGURATION - #{prefix} - #{data}")
Expand All @@ -31,21 +33,12 @@ def logger
def log?
startup_logs_enabled = Datadog.configuration.diagnostics.startup_logs.enabled
if startup_logs_enabled.nil?
!repl? && !rspec? # Suppress logs if we are running in a REPL or rspec
# Do not pollute the logs in a development environment.
!Datadog::Core::Environment::Execution.development?
else
startup_logs_enabled
end
end

REPL_PROGRAM_NAMES = %w[irb pry].freeze

def repl?
REPL_PROGRAM_NAMES.include?($PROGRAM_NAME)
end

def rspec?
$PROGRAM_NAME.end_with?('rspec')
end
end

# Collects and logs Core diagnostic information
Expand Down
17 changes: 7 additions & 10 deletions sig/datadog/core/diagnostics/environment_logger.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@ module Datadog
module Core
module Diagnostics
module EnvironmentLogging
def log_configuration!: (untyped prefix, untyped data) -> untyped
def log_configuration!: (string prefix, string data) -> void

def log_error!: (untyped prefix, untyped type, untyped error) -> untyped
def log_debug!: (string prefix, string data) -> void

def logger: () -> untyped
def log?: () -> untyped
def log_error!: (string prefix, string type, string error) -> void

REPL_PROGRAM_NAMES: ::Array[::String]

def repl?: () -> untyped

def rspec?: () -> untyped
def logger: () -> ::Logger
def log?: () -> bool
end
module EnvironmentLogger
extend EnvironmentLogging

def self.collect_and_log!: (untyped extra_fields) -> untyped
def self.collect_and_log!: (Hash[Symbol, untyped] extra_fields) -> void
end
module EnvironmentCollector
def self.collect_config!: () -> { date: untyped, os_name: untyped, version: untyped, lang: untyped, lang_version: untyped, env: untyped, service: untyped, dd_version: untyped, debug: untyped, tags: untyped, runtime_metrics_enabled: untyped, vm: untyped, health_metrics_enabled: untyped }

def self.date: () -> untyped
def self.os_name: () -> untyped
def self.version: () -> untyped
Expand Down
4 changes: 2 additions & 2 deletions sig/datadog/core/environment/execution.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ module Datadog
def self.development?: () -> bool
def self.webmock_enabled?: () -> bool

def self.cucumber?: -> bool

private
def self.test?: () -> bool
def self.repl?: () -> bool
Expand All @@ -17,6 +15,8 @@ module Datadog
RSPEC_PROGRAM_NAME: ::String
def self.minitest?: () -> bool

def self.cucumber?: -> bool

def self.rails_development?: -> bool

RAILS_ENV_DEVELOPMENT: Set[String]
Expand Down
19 changes: 4 additions & 15 deletions spec/datadog/core/diagnostics/environment_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
end

describe '#collect_and_log!' do
include_context 'non-development execution environment'

subject(:collect_and_log!) { env_logger.collect_and_log! }

let(:logger) { instance_double(Datadog::Core::Logger) }
Expand All @@ -40,7 +42,6 @@
end

before do
allow(env_logger).to receive(:rspec?).and_return(false) # Allow rspec to log for testing purposes
allow(Datadog).to receive(:logger).and_return(logger)
allow(logger).to receive(:debug?).and_return(true)
allow(logger).to receive(:debug)
Expand All @@ -56,22 +57,10 @@
end
end

context 'under a REPL' do
around do |example|
begin
original = $PROGRAM_NAME
$0 = 'irb'
example.run
ensure
$0 = original
end
end
context 'in a development execution environment' do
before { allow(Datadog::Core::Environment::Execution).to receive(:development?).and_return(true) }

context 'with default settings' do
before do
allow(env_logger).to receive(:rspec?).and_return(true) # Prevent rspec from logging
end

it { expect(logger).to_not have_received(:info) }
end

Expand Down
19 changes: 4 additions & 15 deletions spec/datadog/tracing/diagnostics/environment_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
end

describe '#collect_and_log!' do
include_context 'non-development execution environment'

subject(:collect_and_log!) { env_logger.collect_and_log! }

let(:logger) { instance_double(Datadog::Core::Logger) }

before do
allow(env_logger).to receive(:rspec?).and_return(false) # Allow rspec to log for testing purposes
allow(Datadog).to receive(:logger).and_return(logger)
allow(logger).to receive(:debug?).and_return true
allow(logger).to receive(:debug)
Expand Down Expand Up @@ -91,22 +92,10 @@
end
end

context 'under a REPL' do
around do |example|
begin
original = $PROGRAM_NAME
$0 = 'irb'
example.run
ensure
$0 = original
end
end
context 'in a development execution environment' do
before { allow(Datadog::Core::Environment::Execution).to receive(:development?).and_return(true) }

context 'with default settings' do
before do
allow(env_logger).to receive(:rspec?).and_return(true) # Prevent rspec from logging
end

it do
collect_and_log!
expect(logger).to_not have_received(:info)
Expand Down

0 comments on commit 53a3900

Please sign in to comment.