Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not raise when configuration is missing #14

Merged
merged 1 commit into from
Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ log_level: 1
Example of environment variables:

```shell
# required
# required (if missing adapter is no-op)
YABEDA_DATADOG_API_KEY=<your Datadog API key>
YABEDA_DATADOG_APP_KEY=<your Datadog App key>

Expand Down
58 changes: 31 additions & 27 deletions lib/yabeda/datadog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,40 @@ module Datadog
SECOND = 1
COLLECT_INTERVAL = 60 * SECOND

# Gem configuration object
def self.config
@config ||= Config.new
end

# Check the gem configuration has valid state
def self.ensure_configured
raise ApiKeyError unless config.api_key
raise AppKeyError unless config.app_key
end
class << self
# Gem configuration object
def config
@config ||= Config.new
end

# Prepare the adapter to work
def self.start
ensure_configured
# Check the gem configuration has valid state
def ensure_configured
raise ApiKeyError unless config.api_key
raise AppKeyError unless config.app_key
end

worker = Yabeda::Datadog::Worker.start(config)
adapter = Yabeda::Datadog::Adapter.new(worker: worker)
Yabeda.register_adapter(:datadog, adapter)
adapter
end
# Prepare the adapter to work
def start
ensure_configured
worker = Yabeda::Datadog::Worker.start(config)
adapter = Yabeda::Datadog::Adapter.new(worker: worker)
Yabeda.register_adapter(:datadog, adapter)
adapter
rescue ConfigError => e
Logging.instance.warn e.message
nil
end

# Start collection metrics from Yabeda collectors
def self.start_exporter(collect_interval: COLLECT_INTERVAL)
Thread.new do
Logging.instance.debug("initilize collectors harvest")
loop do
Logging.instance.debug("start collectors harvest")
Yabeda.collectors.each(&:call)
Logging.instance.debug("end collectors harvest")
sleep(collect_interval)
# Start collection metrics from Yabeda collectors
def start_exporter(collect_interval: COLLECT_INTERVAL)
Thread.new do
Logging.instance.debug("initilize collectors harvest")
loop do
Logging.instance.debug("start collectors harvest")
Yabeda.collectors.each(&:call)
Logging.instance.debug("end collectors harvest")
sleep(collect_interval)
end
end
end
end
Expand Down
9 changes: 5 additions & 4 deletions lib/yabeda/datadog/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

module Yabeda
module Datadog
class ConfigError < StandardError; end
# = This error raised when no Datadog API key provided
class ApiKeyError < StandardError
def initialize(msg = "Datadog API key doesn't set")
class ApiKeyError < ConfigError
def initialize(msg = "Datadog API key is missing")
super
end
end

# = This error raised when no Datadog application key provided
class AppKeyError < StandardError
def initialize(msg = "Datadog application key doesn't set")
class AppKeyError < ConfigError
def initialize(msg = "Datadog application key is missing")
super
end
end
Expand Down
18 changes: 12 additions & 6 deletions spec/yabeda/datadog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@
it "requires API key" do
described_class.config.api_key = nil
described_class.config.app_key = "qwe"
expect do
described_class.start
end.to raise_error(Yabeda::Datadog::ApiKeyError)

expect(Yabeda::Datadog::Logging.instance).to receive(:warn).with(/api key is missing/i)

described_class.start

expect(Yabeda.adapters[:datadog]).to be_nil
end

it "requires App key" do
described_class.config.api_key = "qwe"
described_class.config.app_key = nil
expect do
described_class.start
end.to raise_error(Yabeda::Datadog::AppKeyError)

expect(Yabeda::Datadog::Logging.instance).to receive(:warn).with(/application key is missing/i)

described_class.start

expect(Yabeda.adapters[:datadog]).to be_nil
end
end

Expand Down