Skip to content

Commit 374e22a

Browse files
p-datadogp
andauthored
Continue injecting logger (#4441)
Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 4fbd11b commit 374e22a

File tree

22 files changed

+81
-73
lines changed

22 files changed

+81
-73
lines changed

lib/datadog/core/configuration/components.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ class Components
2626
class << self
2727
include Datadog::Tracing::Component
2828

29-
def build_health_metrics(settings)
29+
def build_health_metrics(settings, logger)
3030
settings = settings.health_metrics
3131
options = { enabled: settings.enabled }
3232
options[:statsd] = settings.statsd unless settings.statsd.nil?
3333

34-
Core::Diagnostics::Health::Metrics.new(**options)
34+
Core::Diagnostics::Health::Metrics.new(logger: logger, **options)
3535
end
3636

3737
def build_logger(settings)
@@ -41,19 +41,20 @@ def build_logger(settings)
4141
logger
4242
end
4343

44-
def build_runtime_metrics(settings)
44+
def build_runtime_metrics(settings, logger)
4545
options = { enabled: settings.runtime_metrics.enabled }
4646
options[:statsd] = settings.runtime_metrics.statsd unless settings.runtime_metrics.statsd.nil?
4747
options[:services] = [settings.service] unless settings.service.nil?
4848

49-
Core::Runtime::Metrics.new(**options)
49+
Core::Runtime::Metrics.new(logger: logger, **options)
5050
end
5151

52-
def build_runtime_metrics_worker(settings)
52+
def build_runtime_metrics_worker(settings, logger)
5353
# NOTE: Should we just ignore building the worker if its not enabled?
5454
options = settings.runtime_metrics.opts.merge(
5555
enabled: settings.runtime_metrics.enabled,
56-
metrics: build_runtime_metrics(settings)
56+
metrics: build_runtime_metrics(settings, logger),
57+
logger: logger,
5758
)
5859

5960
Core::Workers::RuntimeMetrics.new(options)
@@ -116,8 +117,8 @@ def initialize(settings)
116117
)
117118
@environment_logger_extra.merge!(profiler_logger_extra) if profiler_logger_extra
118119

119-
@runtime_metrics = self.class.build_runtime_metrics_worker(settings)
120-
@health_metrics = self.class.build_health_metrics(settings)
120+
@runtime_metrics = self.class.build_runtime_metrics_worker(settings, @logger)
121+
@health_metrics = self.class.build_health_metrics(settings, @logger)
121122
@appsec = Datadog::AppSec::Component.build_appsec_component(settings, telemetry: telemetry)
122123
@dynamic_instrumentation = Datadog::DI::Component.build(settings, agent_settings, @logger, telemetry: telemetry)
123124
@environment_logger_extra[:dynamic_instrumentation_enabled] = !!@dynamic_instrumentation

lib/datadog/core/metrics/client.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ class Client
2121
extend Options
2222
extend Helpers
2323

24-
attr_reader :statsd
24+
attr_reader :statsd, :logger
2525

26-
def initialize(statsd: nil, enabled: true, **_)
26+
def initialize(logger:, statsd: nil, enabled: true, **_)
27+
@logger = logger
2728
@statsd =
2829
if supported?
2930
statsd || default_statsd_client
@@ -98,7 +99,7 @@ def count(stat, value = nil, options = nil, &block)
9899

99100
statsd.count(stat, value, metric_options(options))
100101
rescue StandardError => e
101-
Datadog.logger.error(
102+
logger.error(
102103
"Failed to send count stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
103104
)
104105
Telemetry::Logger.report(e, description: 'Failed to send count stat')
@@ -112,7 +113,7 @@ def distribution(stat, value = nil, options = nil, &block)
112113

113114
statsd.distribution(stat, value, metric_options(options))
114115
rescue StandardError => e
115-
Datadog.logger.error(
116+
logger.error(
116117
"Failed to send distribution stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
117118
)
118119
Telemetry::Logger.report(e, description: 'Failed to send distribution stat')
@@ -125,7 +126,7 @@ def increment(stat, options = nil)
125126

126127
statsd.increment(stat, metric_options(options))
127128
rescue StandardError => e
128-
Datadog.logger.error(
129+
logger.error(
129130
"Failed to send increment stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
130131
)
131132
Telemetry::Logger.report(e, description: 'Failed to send increment stat')
@@ -139,7 +140,7 @@ def gauge(stat, value = nil, options = nil, &block)
139140

140141
statsd.gauge(stat, value, metric_options(options))
141142
rescue StandardError => e
142-
Datadog.logger.error(
143+
logger.error(
143144
"Failed to send gauge stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
144145
)
145146
Telemetry::Logger.report(e, description: 'Failed to send gauge stat')
@@ -159,7 +160,7 @@ def time(stat, options = nil)
159160
end
160161
rescue StandardError => e
161162
# TODO: Likely to be redundant, since `distribution` handles its own errors.
162-
Datadog.logger.error(
163+
logger.error(
163164
"Failed to send time stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
164165
)
165166
Telemetry::Logger.report(e, description: 'Failed to send time stat')
@@ -194,7 +195,7 @@ def dogstatsd_version
194195

195196
def ignored_statsd_warning
196197
IGNORED_STATSD_ONLY_ONCE.run do
197-
Datadog.logger.warn(
198+
logger.warn(
198199
'Ignoring user-supplied statsd instance as currently-installed version of dogstastd-ruby is incompatible. ' \
199200
"To fix this, ensure that you have `gem 'dogstatsd-ruby', '~> 5.3'` on your Gemfile or gems.rb file."
200201
)

lib/datadog/core/remote/client.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ class Client
1313
class TransportError < StandardError; end
1414
class SyncError < StandardError; end
1515

16-
attr_reader :transport, :repository, :id, :dispatcher
16+
attr_reader :transport, :repository, :id, :dispatcher, :logger
1717

18-
def initialize(transport, capabilities, repository: Configuration::Repository.new)
18+
def initialize(transport, capabilities, logger:, repository: Configuration::Repository.new)
1919
@transport = transport
20+
@logger = logger
2021

2122
@repository = repository
2223
@id = SecureRandom.uuid
@@ -40,7 +41,7 @@ def sync
4041
def process_response(response)
4142
# when response is completely empty, do nothing as in: leave as is
4243
if response.empty?
43-
Datadog.logger.debug { 'remote: empty response => NOOP' }
44+
logger.debug { 'remote: empty response => NOOP' }
4445

4546
return
4647
end
@@ -112,7 +113,7 @@ def apply_config(paths, targets, contents)
112113
end
113114

114115
if changes.empty?
115-
Datadog.logger.debug { 'remote: no changes' }
116+
logger.debug { 'remote: no changes' }
116117
else
117118
dispatcher.dispatch(changes, repository)
118119
end

lib/datadog/core/remote/component.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def initialize(settings, capabilities, agent_settings, logger:)
2626

2727
@barrier = Barrier.new(settings.remote.boot_timeout_seconds)
2828

29-
@client = Client.new(transport_v7, capabilities)
29+
@client = Client.new(transport_v7, capabilities, logger: logger)
3030
@healthy = false
3131
logger.debug { "new remote configuration client: #{@client.id}" }
3232

@@ -58,7 +58,7 @@ def initialize(settings, capabilities, agent_settings, logger:)
5858
end
5959

6060
# client state is unknown, state might be corrupted
61-
@client = Client.new(transport_v7, capabilities)
61+
@client = Client.new(transport_v7, capabilities, logger: logger)
6262
@healthy = false
6363
logger.debug { "new remote configuration client: #{@client.id}" }
6464

lib/datadog/core/workers/runtime_metrics.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class RuntimeMetrics < Worker
2121
:metrics
2222

2323
def initialize(options = {})
24-
@metrics = options.fetch(:metrics) { Core::Runtime::Metrics.new }
24+
@metrics = options.fetch(:metrics) { Core::Runtime::Metrics.new(logger: options[:logger]) }
2525

2626
# Workers::Async::Thread settings
2727
self.fork_policy = options.fetch(:fork_policy, Workers::Async::Thread::FORK_POLICY_STOP)

sig/datadog/core/configuration/components.rbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ module Datadog
66

77
def self.build_logger: (untyped settings) -> untyped
88

9-
def self.build_runtime_metrics: (untyped settings) -> untyped
9+
def self.build_runtime_metrics: (untyped settings, Core::Logger logger) -> untyped
1010

11-
def self.build_runtime_metrics_worker: (untyped settings) -> untyped
11+
def self.build_runtime_metrics_worker: (untyped settings, Core::Logger logger) -> untyped
1212

1313
def self.build_tracer: (untyped settings, untyped agent_settings) -> untyped
1414

sig/datadog/core/metrics/client.rbs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ module Datadog
77
extend Options
88

99
extend Helpers
10+
11+
@logger: Core::Logger
1012

1113
attr_reader statsd: untyped
14+
attr_reader logger: Core::Logger
1215

13-
def initialize: (?statsd: untyped?, ?enabled: bool, **untyped _) -> void
16+
def initialize: (?statsd: untyped?, ?enabled: bool, logger: Core::Logger, **untyped _) -> void
1417

1518
def supported?: () -> untyped
1619

sig/datadog/core/remote/client.rbs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ module Datadog
1717
attr_reader dispatcher: Remote::Dispatcher
1818

1919
attr_reader capabilities: Capabilities
20+
21+
attr_reader logger: Core::Logger
2022

21-
def initialize: (Datadog::Core::Remote::Transport::Config::Transport transport, Capabilities capabilities, ?repository: Configuration::Repository) -> void
23+
def initialize: (Datadog::Core::Remote::Transport::Config::Transport transport, Capabilities capabilities, ?repository: Configuration::Repository, logger: Core::Logger) -> void
2224

2325
def sync: () -> void
2426

@@ -28,6 +30,7 @@ module Datadog
2830
@native_platform: ::String
2931
@ruby_engine_version: ::String
3032
@tracer_version: ::String
33+
@logger: Core::Logger
3134

3235
def payload: () -> ::Hash[Symbol, untyped]
3336
def service_name: () -> ::String

sig/datadog/core/remote/component.rbs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ module Datadog
77
attr_reader client: Datadog::Core::Remote::Client
88
attr_reader worker: Datadog::Core::Remote::Worker
99
attr_reader healthy: bool
10-
attr_reader logger: ::Logger
10+
attr_reader logger: Core::Logger
1111

1212
@barrier: Barrier
13-
@logger: ::Logger
13+
@logger: Core::Logger
1414

15-
def initialize: (untyped settings, Datadog::Core::Remote::Client::Capabilities capabilities, Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings agent_settings, logger: ::Logger) -> void
15+
def initialize: (untyped settings, Datadog::Core::Remote::Client::Capabilities capabilities, Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings agent_settings, logger: Core::Logger) -> void
1616

1717
def start: -> void
1818

@@ -27,7 +27,7 @@ module Datadog
2727
def self.build: (
2828
untyped settings,
2929
Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings agent_settings,
30-
logger: ::Logger,
30+
logger: Core::Logger,
3131
telemetry: Datadog::Core::Telemetry::Component
3232
) -> Datadog::Core::Remote::Component?
3333

spec/datadog/core/configuration/components_spec.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@
8282
).and_return([profiler, environment_logger_extra])
8383

8484
expect(described_class).to receive(:build_runtime_metrics_worker)
85-
.with(settings)
85+
.with(settings, logger)
8686
.and_return(runtime_metrics)
8787

8888
expect(described_class).to receive(:build_health_metrics)
89-
.with(settings)
89+
.with(settings, logger)
9090
.and_return(health_metrics)
9191
end
9292

@@ -155,7 +155,7 @@
155155
end
156156

157157
describe '::build_health_metrics' do
158-
subject(:build_health_metrics) { described_class.build_health_metrics(settings) }
158+
subject(:build_health_metrics) { described_class.build_health_metrics(settings, logger) }
159159

160160
context 'given settings' do
161161
shared_examples_for 'new health metrics' do
@@ -165,7 +165,7 @@
165165

166166
before do
167167
expect(Datadog::Core::Diagnostics::Health::Metrics).to receive(:new)
168-
.with(default_options.merge(options))
168+
.with(default_options.merge(options).merge(logger: logger))
169169
.and_return(health_metrics)
170170
end
171171

@@ -386,7 +386,7 @@
386386
end
387387

388388
describe '::build_runtime_metrics' do
389-
subject(:build_runtime_metrics) { described_class.build_runtime_metrics(settings) }
389+
subject(:build_runtime_metrics) { described_class.build_runtime_metrics(settings, logger) }
390390

391391
context 'given settings' do
392392
shared_examples_for 'new runtime metrics' do
@@ -396,7 +396,7 @@
396396

397397
before do
398398
expect(Datadog::Core::Runtime::Metrics).to receive(:new)
399-
.with(default_options.merge(options))
399+
.with(default_options.merge(options).merge(logger: logger))
400400
.and_return(runtime_metrics)
401401
end
402402

@@ -452,7 +452,7 @@
452452
end
453453

454454
describe '::build_runtime_metrics_worker' do
455-
subject(:build_runtime_metrics_worker) { described_class.build_runtime_metrics_worker(settings) }
455+
subject(:build_runtime_metrics_worker) { described_class.build_runtime_metrics_worker(settings, logger) }
456456

457457
context 'given settings' do
458458
shared_examples_for 'new runtime metrics worker' do
@@ -468,11 +468,11 @@
468468

469469
before do
470470
allow(described_class).to receive(:build_runtime_metrics)
471-
.with(settings)
471+
.with(settings, logger)
472472
.and_return(runtime_metrics)
473473

474474
expect(Datadog::Core::Workers::RuntimeMetrics).to receive(:new)
475-
.with(default_options.merge(options))
475+
.with(default_options.merge(options).merge(logger: logger))
476476
.and_return(runtime_metrics_worker)
477477
end
478478

0 commit comments

Comments
 (0)