Skip to content

Commit

Permalink
Warn on missing backend only.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Aug 25, 2024
1 parent d6c3501 commit 9c3f3da
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/metrics/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
# Released under the MIT License.
# Copyright, 2021-2023, by Samuel Williams.

require 'console/event/failure'

module Metrics
# Require a specific trace backend.
def self.require_backend(env = ENV)
if path = env['METRICS_BACKEND']
require(path)
if backend = env['METRICS_BACKEND']
begin
require(backend)
rescue LoadError => error
::Console::Event::Failure.for(error).emit(self, "Unable to load metrics backend!", backend: backend, severity: :warn)
end

Metrics.extend(Backend::Interface)
end
Expand Down
23 changes: 23 additions & 0 deletions test/metrics/backend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2022-2023, by Samuel Williams.

require "metrics/backend"
require "json"

require "sus/fixtures/console"

describe Metrics do
with ".require_backend" do
include_context Sus::Fixtures::Console::CapturedLogger

it "logs a warning if backend cannot be loaded" do
subject.require_backend({"METRICS_BACKEND" => "metrics/backend/missing"})

expect_console.to have_logged(
severity: be == :warn,
)
end
end
end

0 comments on commit 9c3f3da

Please sign in to comment.