Skip to content

Commit

Permalink
Update documentation and add example of class method metrics.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Mar 21, 2023
1 parent d8cdfeb commit 536f22d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
33 changes: 28 additions & 5 deletions guides/getting-started/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ There are two main aspects to integrating within this gem.

Adding metrics to libraries requires the use of {ruby Metrics::Provider}:

~~~ ruby
### Push Metrics

~~~ ruby
require 'metrics'

Expand All @@ -42,10 +39,10 @@ end

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

def my_method
metric_adjust('called', 1)
CALL_COUNT.emit(1)

super
end
Expand All @@ -56,6 +53,32 @@ MyClass.new.my_method

This code by itself will not create any metrics. In order to execute it and output metrics, you must set up a backend to consume them.

#### Class Methods

You can also expose metrics for class methods:

~~~ ruby
require 'metrics'

class MyClass
def self.my_method
puts "Hello World"
end
end

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

def my_method
CALL_COUNT.emit(1)

super
end
end

MyClass.my_method
~~~

### Consuming Metrics

Consuming metrics means proving a backend implementation which can record those metrics to some log or service. There are several options, but two backends are included by default:
Expand Down
2 changes: 0 additions & 2 deletions test/metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ def my_method(argument)
it "can invoke metric wrapper" do
instance = MyClass.new

# expect(instance).to receive(:metric_adjust).and_call_original

instance.my_method(10)
end
end

0 comments on commit 536f22d

Please sign in to comment.