Skip to content

Commit

Permalink
Add Metrics::Config which includes prepare hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Nov 5, 2024
1 parent 7c34b8d commit 6104c79
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 25 deletions.
12 changes: 12 additions & 0 deletions bake.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2020-2024, by Samuel Williams.

# Update the project documentation with the new version number.
#
# @parameter version [String] The new version number.
def after_gem_release_version_increment(version)
context["releases:update"].call(version)
context["utopia:project:readme:update"].call
end
4 changes: 4 additions & 0 deletions lib/metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
require_relative "metrics/version"
require_relative "metrics/provider"
require_relative "metrics/tags"

# @namespace
module Metrics
end
22 changes: 3 additions & 19 deletions lib/metrics/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,11 @@
# Released under the MIT License.
# Copyright, 2021-2024, by Samuel Williams.

require "console/event/failure"
require_relative 'config'

module Metrics
module Backend
# Require a specific trace backend.
def self.require_backend(env = ENV)
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)

return false
end

Metrics.extend(Backend::Interface)

return true
end
end
end

Config::DEFAULT.require_backend
end

Metrics::Backend.require_backend
1 change: 0 additions & 1 deletion lib/metrics/backend/capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Released under the MIT License.
# Copyright, 2023-2024, by Samuel Williams.

require "console"
require_relative "../metric"

module Metrics
Expand Down
3 changes: 2 additions & 1 deletion lib/metrics/backend/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
# Released under the MIT License.
# Copyright, 2021-2024, by Samuel Williams.

require "console"
require_relative "../metric"

require "console"

module Metrics
module Backend
module Console
Expand Down
1 change: 0 additions & 1 deletion lib/metrics/backend/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Released under the MIT License.
# Copyright, 2021-2024, by Samuel Williams.

require "console"
require_relative "../metric"

module Metrics
Expand Down
56 changes: 56 additions & 0 deletions lib/metrics/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

module Metrics
# Represents a configuration for the metrics library.
class Config
DEFAULT_PATH = ENV.fetch("METRICS_CONFIG_DEFAULT_PATH", "config/metrics.rb")

# Load the configuration from the given path.
# @parameter path [String] The path to the configuration file.
# @returns [Config] The loaded configuration.
def self.load(path)
config = self.new

if File.exist?(path)
config.instance_eval(File.read(path), path)
end

return config
end

# Load the default configuration.
# @returns [Config] The default configuration.
def self.default
@default ||= self.load(DEFAULT_PATH)
end

# Prepare the backend, e.g. by loading additional libraries or instrumentation.
def prepare
end

# Require a specific metrics backend implementation.
def require_backend(env = ENV)
if backend = env['METRICS_BACKEND']
begin
if require(backend)
Metrics.extend(Backend::Interface)

self.prepare

return true
end
rescue LoadError => error
warn "Unable to load metrics backend: #{backend.inspect}!"
end
end

return false
end

# Load the default configuration.
DEFAULT = self.default
end
end
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ Please see the [project documentation](https://socketry.github.io/metrics/) for

- [Testing](https://socketry.github.io/metrics/guides/testing/index) - This guide explains how to write assertions in your test suite to validate `metrics` are being emitted correctly.

## Releases

Please see the [project releases](https://socketry.github.io/metrics/releases/index) for all releases.

### Unreleased

- [Introduce `Metrics::Config` to Expose `prepare` Hook](https://socketry.github.io/metrics/releases/index#introduce-metrics::config-to-expose-prepare-hook)

## Contributing

We welcome contributions to this project.
Expand Down
18 changes: 18 additions & 0 deletions releases.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Releases

## Unreleased

### Introduce `Metrics::Config` to Expose `prepare` Hook

The `metrics` gem uses aspect-oriented programming to wrap existing methods to emit metrics. However, while there are some reasonable defaults for emitting metrics, it can be useful to customize the behavior and level of detail. To that end, the `metrics` gem now optionally loads a `config/metrics.rb` which includes a `prepare` hook that can be used to load additional providers.

``` ruby
# config/metrics.rb

def prepare
require 'metrics/provider/async'
require 'metrics/provider/async/http'
end
```

The `prepare` method is called immediately after the metrics backend is loaded. You can require any provider you want in this file, or even add your own custom providers.
8 changes: 5 additions & 3 deletions test/metrics/backend.rb → test/metrics/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require "metrics/backend"
require "metrics/config"
require "json"

require "sus/fixtures/console"

describe Metrics::Backend do
describe Metrics::Config do
let(:config) {subject.default}

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

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

expect_console.to have_logged(
Expand Down

0 comments on commit 6104c79

Please sign in to comment.