metrics 0.14.0 → 0.14.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d09fc9e1904cb72737025a01e68a6ad963fb05bfb6dc02dce39987a6b7a4877b
4
- data.tar.gz: aff1567553de8db5cce01a4c0d51dc2dbf5dadc28fa34c66df7c1d390d539a93
3
+ metadata.gz: '08437830e8e7643c85176e29fbb2f3c6290efff4537b37e529b76c6d8d602bce'
4
+ data.tar.gz: 1037cc6943319a6a64d5f5ed35224efeda1fd51616bc4df3fb5f718f095e0f4e
5
5
  SHA512:
6
- metadata.gz: b937797ed321ef56736eff66f632a721c29fdceec34c11d863799f24abcf237c2282f49294b3b7bec95a9538350d7ef6f591eb8b87e19bc5eefd10e000a24e10
7
- data.tar.gz: 94bff04a35790e6e6f077745a47702c80803a6e01bf6e4a8d3d4d284d14b23051ee3836b714e3d8774c94f653dbe4f725b208199c9a8f577d0732c6e6cb527e3
6
+ metadata.gz: 74597bc51d38149c561b6447811abdd53bbde539ee1011fcd8faff35aa758dfdbc30525696c5916b00cfd54ec9e0ae9c0058711753e2a9def2ed49a58674cd00
7
+ data.tar.gz: 596b37c40c112acae9c232894cc155ca421a499e0f5665bfb5281ff88382bfe5e267a52f795f68a54b20882e9c7d7d8763dcc2872c3ae90339279e5d80df589e
checksums.yaml.gz.sig CHANGED
Binary file
@@ -0,0 +1,57 @@
1
+ # Capture
2
+
3
+ This guide explains how to use `metrics` for exporting metric definitions from your application.
4
+
5
+ ## With Provider Metrics
6
+
7
+ If your application defines one or more metrics, you can export them using the `bake metrics:document` command. This command will generate a list of metrics which you can export.
8
+
9
+ ```bash
10
+ $ cd test/metrics/backend/.capture/
11
+ $ bake metrics:capture environment metrics:capture:list output --format json
12
+ [
13
+ {
14
+ "name": "my_metric",
15
+ "type": "gauge",
16
+ "description": "My metric",
17
+ "unit": "seconds",
18
+ "values": [
19
+
20
+ ],
21
+ "tags": [
22
+
23
+ ],
24
+ "sample_rates": [
25
+
26
+ ]
27
+ }
28
+ ]
29
+ ```
30
+
31
+ ## With Test Suite
32
+
33
+ If your application has a test suite which emits metrics, you can capture those as samples for the purpose of your documentation. This includes fields like tags.
34
+
35
+ ```bash
36
+ $ cd test/metrics/backend/.capture/
37
+ $ bake metrics:capture run metrics:capture:list output --format json
38
+ [
39
+ {
40
+ "name": "my_metric",
41
+ "type": "gauge",
42
+ "description": "My metric",
43
+ "unit": "seconds",
44
+ "values": [
45
+ 1
46
+ ],
47
+ "tags": [
48
+ "environment:test"
49
+ ],
50
+ "sample_rates": [
51
+ 1.0
52
+ ]
53
+ }
54
+ ]
55
+ ```
56
+
57
+ This uses a custom task called `run` in the above example, but you should probably consider using `bake test` which runs your test suite.
@@ -0,0 +1,93 @@
1
+ # Getting Started
2
+
3
+ This guide explains how to use `metrics` for capturing run-time metrics.
4
+
5
+ ## Installation
6
+
7
+ Add the gem to your project:
8
+
9
+ ~~~ bash
10
+ $ bundle add metrics
11
+ ~~~
12
+
13
+ ## Core Concepts
14
+
15
+ `metrics` has several core concepts:
16
+
17
+ - A {ruby Metrics::Provider} which implements custom logic for extracting metrics from existing code.
18
+ - A {ruby Metrics::Backend} which connects metrics to a specific backend system for processing.
19
+
20
+ ## Usage
21
+
22
+ There are two main aspects to integrating within this gem.
23
+
24
+ 1. Libraries and applications must expose metrics.
25
+ 2. Those metrics must be consumed or emitted somewhere.
26
+
27
+ ### Exposing Metrics
28
+
29
+ Adding metrics to libraries requires the use of {ruby Metrics::Provider}:
30
+
31
+ ~~~ ruby
32
+ require 'metrics'
33
+
34
+ class MyClass
35
+ def my_method
36
+ puts "Hello World"
37
+ end
38
+ end
39
+
40
+ # If metrics are disabled, this is a no-op.
41
+ Metrics::Provider(MyClass) do
42
+ CALL_COUNT = Metrics.metric('call_count', :counter, description: 'Number of times invoked.')
43
+
44
+ def my_method
45
+ CALL_COUNT.emit(1)
46
+
47
+ super
48
+ end
49
+ end
50
+
51
+ MyClass.new.my_method
52
+ ~~~
53
+
54
+ 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.
55
+
56
+ #### Class Methods
57
+
58
+ You can also expose metrics for class methods:
59
+
60
+ ~~~ ruby
61
+ require 'metrics'
62
+
63
+ class MyClass
64
+ def self.my_method
65
+ puts "Hello World"
66
+ end
67
+ end
68
+
69
+ Metrics::Provider(MyClass.singleton_class) do
70
+ CALL_COUNT = Metrics.metric('call_count', :counter, description: 'Number of times invoked.')
71
+
72
+ def my_method
73
+ CALL_COUNT.emit(1)
74
+
75
+ super
76
+ end
77
+ end
78
+
79
+ MyClass.my_method
80
+ ~~~
81
+
82
+ ### Consuming Metrics
83
+
84
+ 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:
85
+
86
+ - `metrics/backend/test` does not emit any metrics, but validates the usage of the metric interface.
87
+ - `metrics/backend/console` emits metrics using the [`console`](https://github.com/socketry/console) gem.
88
+
89
+ In order to use a specific backend, set the `METRICS_BACKEND` environment variable, e.g.
90
+
91
+ ~~~ shell
92
+ $ METRICS_BACKEND=metrics/backend/console ./my_script.rb
93
+ ~~~
@@ -0,0 +1,19 @@
1
+ # Automatically generated context index for Utopia::Project guides.
2
+ # Do not edit then files in this directory directly, instead edit the guides and then run `bake utopia:project:agent:context:update`.
3
+ ---
4
+ description: Application metrics and instrumentation.
5
+ metadata:
6
+ documentation_uri: https://socketry.github.io/metrics/
7
+ source_code_uri: https://github.com/socketry/metrics.git
8
+ files:
9
+ - path: getting-started.md
10
+ title: Getting Started
11
+ description: This guide explains how to use `metrics` for capturing run-time metrics.
12
+ - path: capture.md
13
+ title: Capture
14
+ description: This guide explains how to use `metrics` for exporting metric definitions
15
+ from your application.
16
+ - path: testing.md
17
+ title: Testing
18
+ description: This guide explains how to write assertions in your test suite to validate
19
+ `metrics` are being emitted correctly.
@@ -0,0 +1,43 @@
1
+ # Testing
2
+
3
+ This guide explains how to write assertions in your test suite to validate `metrics` are being emitted correctly.
4
+
5
+ ## Application Code
6
+
7
+ In your application code, you should emit metrics, e.g.
8
+
9
+ ```ruby
10
+ require 'metrics/provider'
11
+
12
+ class MyApplication
13
+ def work
14
+ # ...
15
+ end
16
+
17
+ Metrics::Provider(self) do
18
+ WORK_METRIC = Metrics.metric('my_application.work.count', :counter, description: 'Work counter')
19
+
20
+ def work
21
+ WORK_METRIC.emit(1)
22
+ super
23
+ end
24
+ end
25
+ end
26
+ ```
27
+
28
+ ## Test Code
29
+
30
+ In your test code, you should assert that the metrics are being emitted correctly, e.g.
31
+
32
+ ```ruby
33
+ ENV['METRICS_BACKEND'] ||= 'metrics/backend/test'
34
+
35
+ require_relative 'app'
36
+
37
+ describe MyApplication do
38
+ it 'should emit metrics' do
39
+ expect(MyApplication::WORK_METRIC).to receive(:emit).with(1)
40
+ MyApplication.new.work
41
+ end
42
+ end
43
+ ```
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Released under the MIT License.
4
- # Copyright, 2021-2024, by Samuel Williams.
4
+ # Copyright, 2021-2025, by Samuel Williams.
5
5
 
6
6
  require_relative "backend"
7
7
 
@@ -4,5 +4,5 @@
4
4
  # Copyright, 2021-2025, by Samuel Williams.
5
5
 
6
6
  module Metrics
7
- VERSION = "0.14.0"
7
+ VERSION = "0.14.1"
8
8
  end
data/lib/metrics.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Released under the MIT License.
4
- # Copyright, 2021-2024, by Samuel Williams.
4
+ # Copyright, 2021-2025, by Samuel Williams.
5
5
 
6
6
  require_relative "metrics/version"
7
7
  require_relative "metrics/provider"
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.0
4
+ version: 0.14.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -44,6 +44,10 @@ extra_rdoc_files: []
44
44
  files:
45
45
  - bake/metrics/capture.rb
46
46
  - bake/metrics/provider.rb
47
+ - context/capture.md
48
+ - context/getting-started.md
49
+ - context/index.yaml
50
+ - context/testing.md
47
51
  - lib/metrics.rb
48
52
  - lib/metrics/backend.rb
49
53
  - lib/metrics/backend/capture.rb
metadata.gz.sig CHANGED
Binary file