Skip to content

Commit f70b426

Browse files
committed
Add guards in component for Ruby and libdatadog
1 parent 0081bff commit f70b426

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

.cursor/rules/architecture.mdc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ alwaysApply: false
55
---
66
# Architecture and Design
77

8-
This library provides tools to get visibility into the performance and security of Ruby applications. This includes distributed tracing, profiling, app & api protection, dynamic instrumentation, metrics emission, etc.
8+
This library provides tools to get visibility into the performance and security of Ruby applications.
9+
This includes distributed tracing, profiling, app & api protection, dynamic instrumentation, metrics emission, open feature, etc.
910

1011
## Project Structure
1112

1213
- @lib/datadog/appsec - app & api protection implementation (formely known as appsec)
1314
- @lib/datadog/appsec/contrib - app & api protection integrations with third-party libraries
1415
- @lib/datadog/core - glue and shared code
1516
- @lib/datadog/di - dynamic instrumentation
17+
- @lib/datadog/open_feature - an implementation of OpenFeature Provider https://openfeature.dev/docs/reference/sdks/server/ruby
1618
- @lib/datadog/profiling - profiling
1719
- @lib/datadog/tracing - distributed tracing
1820
- @lib/datadog/tracing/contrib - distributed tracing integrations with third-party libraries

lib/datadog/open_feature/component.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,26 @@ def self.build(settings, agent_settings, logger:, telemetry:)
1818

1919
unless settings.respond_to?(:remote) && settings.remote.enabled
2020
message = 'OpenFeature could not be enabled as Remote Configuration is currently disabled. ' \
21-
'To enable Remote Configuration, see https://docs.datadoghq.com/agent/remote_config'
21+
'To enable Remote Configuration, see https://docs.datadoghq.com/remote_configuration/.'
22+
23+
logger.warn(message)
24+
return
25+
end
26+
27+
if RUBY_ENGINE != 'ruby'
28+
message = 'OpenFeature could not be enabled as MRI is required, ' \
29+
"but running on #{RUBY_ENGINE.inspect}"
30+
2231
logger.warn(message)
32+
return
33+
end
2334

35+
if (libdatadog_api_failure = Core::LIBDATADOG_API_FAILURE)
36+
message = 'OpenFeature could not be enabled as `libdatadog` is not loaded: ' \
37+
"#{libdatadog_api_failure.inspect}. For help solving this issue, " \
38+
'please contact Datadog support at https://docs.datadoghq.com/help/.'
39+
40+
logger.warn(message)
2441
return
2542
end
2643

spec/datadog/open_feature/component_spec.rb

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,44 @@
2727
before { settings.open_feature.enabled = true }
2828

2929
context 'when remote configuration is enabled' do
30-
before { settings.remote.enabled = true }
30+
before do
31+
stub_const('Datadog::Core::LIBDATADOG_API_FAILURE', nil)
32+
settings.remote.enabled = true
33+
end
3134

3235
it 'returns configured component instance' do
3336
expect(component).to be_a(described_class)
3437
expect(component.engine).to be_a(Datadog::OpenFeature::EvaluationEngine)
38+
3539
expect(Datadog::OpenFeature::Exposures::Reporter).to have_received(:new)
3640
end
41+
42+
context 'when libdatadog is unavailable' do
43+
before { stub_const('Datadog::Core::LIBDATADOG_API_FAILURE', 'Failed to load') }
44+
45+
it 'logs warning and returns nil' do
46+
expect(logger).to receive(:warn).with(/`libdatadog` is not loaded: "Failed to load"/)
47+
48+
expect(component).to be_nil
49+
end
50+
end
51+
52+
context 'when not running on MRI' do
53+
before { stub_const('RUBY_ENGINE', 'jruby') }
54+
55+
it 'logs warning and returns nil' do
56+
expect(logger).to receive(:warn).with(/MRI is required, but running on "jruby"/)
57+
58+
expect(component).to be_nil
59+
end
60+
end
3761
end
3862

3963
context 'when remote configuration is disabled' do
4064
before { settings.remote.enabled = false }
4165

4266
it 'logs warning and returns nil' do
43-
expect(logger).to receive(:warn)
44-
.with(/could not be enabled as Remote Configuration is currently disabled/)
67+
expect(logger).to receive(:warn).with(/Remote Configuration is currently disabled/)
4568

4669
expect(component).to be_nil
4770
end

0 commit comments

Comments
 (0)