Skip to content

Commit

Permalink
Prefer Metrics.metric rather than modify the current class. (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored Jun 19, 2023
1 parent 536f22d commit aa83725
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions guides/getting-started/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ end

# If metrics are disabled, this is a no-op.
Metrics::Provider(MyClass) do
CALL_COUNT = metric('call_count', :counter, description: 'Number of times invoked.')
CALL_COUNT = Metrics.metric('call_count', :counter, description: 'Number of times invoked.')

def my_method
CALL_COUNT.emit(1)
Expand Down Expand Up @@ -67,7 +67,7 @@ class MyClass
end

Metrics::Provider(MyClass.singleton_class) do
CALL_COUNT = metric('call_count', :counter, description: 'Number of times invoked.')
CALL_COUNT = Metrics.metric('call_count', :counter, description: 'Number of times invoked.')

def my_method
CALL_COUNT.emit(1)
Expand Down
2 changes: 2 additions & 0 deletions lib/metrics/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module Metrics
def self.require_backend(env = ENV)
if path = env['METRICS_BACKEND']
require(path)

Metrics.extend(Backend::Interface)
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion lib/metrics/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ def metrics_provider
end
end

module Deprecated
def metric(...)
warn "Metrics::Provider#metric is deprecated. Please use Metrics.metric instead."
Metrics.metric(...)
end
end

private_constant :Singleton

# Bail out if there is no backend configured.
Expand All @@ -30,7 +37,7 @@ def self.Provider(klass, &block)
klass.extend(Singleton)

provider = klass.metrics_provider
provider.extend(Backend::Interface)
provider.extend(Deprecated)

klass.prepend(provider)

Expand Down
2 changes: 1 addition & 1 deletion test/metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def my_method(argument)
end

Metrics::Provider(MyClass) do
MYCLASS_CALL_COUNT = metric('my_class.call', :counter, description: 'Call counter.')
MYCLASS_CALL_COUNT = Metrics.metric('my_class.call', :counter, description: 'Call counter.')

def my_method(argument)
MYCLASS_CALL_COUNT.emit(1, tags: ["foo", "bar"])
Expand Down
2 changes: 1 addition & 1 deletion test/metrics/backend/.capture/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def call
end

Metrics::Provider(App) do
MY_METRIC = metric(:my_metric, :gauge, description: "My metric", unit: "seconds")
MY_METRIC = Metrics.metric(:my_metric, :gauge, description: "My metric", unit: "seconds")

def call
MY_METRIC.emit(1, tags: ['environment:test'])
Expand Down

0 comments on commit aa83725

Please sign in to comment.