apia-yabeda 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +18 -0
- data/LICENSE.txt +21 -0
- data/README.md +49 -0
- data/Rakefile +10 -0
- data/apia-yabeda.gemspec +32 -0
- data/lib/apia/yabeda/prometheus_collector.rb +71 -0
- data/lib/apia/yabeda/version.rb +9 -0
- data/lib/apia/yabeda.rb +12 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 617d2053859bdaf2e40d00f3f057c1e1c978f5e47542703f5fcbb83a15d293c5
|
4
|
+
data.tar.gz: eebd3879a098c2fe79498f2f053566fd76665585f929a5fbe8dd627fc585c808
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 406fea75bac88d35c34f58a01799eefe5b33e249e5ea1996e3599d0065f6a206232b41b2004aeca0b657fa4c55c15611cf901ee2df3a86e55d4f77422d20fd3f
|
7
|
+
data.tar.gz: 86dcb9d2c1bd68ec80d8fc75c3c03691104e3491e54e1964574baac4191c372a403540531daf10b7d8d9982bcb32db6339a9ed50254ab56ef4cbca75bd4a3565
|
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
6
|
+
|
7
|
+
# Specify your gem's dependencies in apia-yabeda.gemspec
|
8
|
+
gemspec
|
9
|
+
|
10
|
+
gem "apia"
|
11
|
+
gem "rake", "~> 13.1"
|
12
|
+
gem "rspec"
|
13
|
+
gem "rubocop"
|
14
|
+
gem "rubocop-rspec"
|
15
|
+
|
16
|
+
group :test do
|
17
|
+
gem "pry"
|
18
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Krystal Hosting Ltd
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Apia::Yabeda
|
2
|
+
|
3
|
+
This gem collects [Prometheus](https://prometheus.io/) metrics for applications that use [Apia](https://github.com/krystal/apia) for their API and [Yabeda](https://github.com/yabeda-rb/yabeda) for their metrics.
|
4
|
+
|
5
|
+
It records:
|
6
|
+
|
7
|
+
- The total number of requests handled by Apia (as a counter)
|
8
|
+
- Time spent in Apia requests in seconds (as a histogram)
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'apia-yabeda'
|
16
|
+
```
|
17
|
+
|
18
|
+
## Usage in Ruby on Rails
|
19
|
+
|
20
|
+
In an initializer, Hook into Apia's notifications by adding a handler that instruments the event with [ActiveSupport::Notifications](https://api.rubyonrails.org/classes/ActiveSupport/Notifications.html).
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
Apia::Notifications.add_handler do |event, args|
|
24
|
+
ActiveSupport::Notifications.instrument("#{event}.apia", args)
|
25
|
+
end
|
26
|
+
```
|
27
|
+
|
28
|
+
Rails automatically requires `apia/yabeda` which triggers the Yabeda configuration. `Apia::Yabeda` will then listen for these events and record metrics for them.
|
29
|
+
|
30
|
+
## Tests and Linting
|
31
|
+
|
32
|
+
- `bin/rspec`
|
33
|
+
- `bin/rubocop`
|
34
|
+
|
35
|
+
## Development
|
36
|
+
|
37
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
38
|
+
|
39
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
40
|
+
|
41
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/krystal/apia-yabeda.
|
46
|
+
|
47
|
+
## License
|
48
|
+
|
49
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/apia-yabeda.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "apia/yabeda/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "apia-yabeda"
|
9
|
+
spec.version = Apia::Yabeda::VERSION
|
10
|
+
spec.authors = ["Paul Sturgess"]
|
11
|
+
|
12
|
+
spec.summary = "Apia Yabeda integration"
|
13
|
+
spec.homepage = "https://github.com/krystal/apia-yabeda"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.7.0"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/krystal/apia-yabeda"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/krystal/apia-yabeda/changelog.md"
|
20
|
+
|
21
|
+
spec.metadata["rubygems_mfa_required"] = "false" # rubocop:disable Gemspec/RequireMFA (enabling MFA means we cannot auto publish via the CI)
|
22
|
+
|
23
|
+
spec.files = Dir[File.join("lib", "**", "*.rb")] +
|
24
|
+
Dir["{*.gemspec,Gemfile,Rakefile,README.*,LICENSE*}"]
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(/^exe\//) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_dependency "activesupport", ">= 6"
|
30
|
+
spec.add_dependency "yabeda", ">= 0.12"
|
31
|
+
spec.add_dependency "yabeda-rails", ">= 0.9.0"
|
32
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support"
|
4
|
+
require "yabeda"
|
5
|
+
require "yabeda/rails"
|
6
|
+
|
7
|
+
module Apia
|
8
|
+
module Yabeda
|
9
|
+
class PrometheusCollector
|
10
|
+
|
11
|
+
def start
|
12
|
+
register_metrics
|
13
|
+
ActiveSupport::Notifications.subscribe(/(request(_error)?)\.apia/) do |*args|
|
14
|
+
event = ActiveSupport::Notifications::Event.new(*args)
|
15
|
+
|
16
|
+
increment_request_total_metric(event)
|
17
|
+
observe_endpoint_duration_metric(event)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def register_metrics
|
22
|
+
::Yabeda.configure do
|
23
|
+
group :apia do
|
24
|
+
counter :requests_total do
|
25
|
+
comment "The total number of requests handled by Apia"
|
26
|
+
tags([:method, :route, :status, :api, :endpoint, :authenticator, :error_code])
|
27
|
+
end
|
28
|
+
histogram :endpoint_duration do
|
29
|
+
comment "Time spent in Apia requests in seconds"
|
30
|
+
unit :seconds
|
31
|
+
tags([:status, :endpoint])
|
32
|
+
buckets ::Yabeda::Rails::LONG_RUNNING_REQUEST_BUCKETS
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def increment_request_total_metric(event)
|
41
|
+
tags = {
|
42
|
+
endpoint: event.payload[:request]&.endpoint&.name,
|
43
|
+
status: event.payload[:response]&.status,
|
44
|
+
api: event.payload[:request]&.api&.name,
|
45
|
+
route: "/#{event.payload[:request]&.route&.path}",
|
46
|
+
method: event.payload[:request]&.route&.request_method,
|
47
|
+
authenticator: event.payload[:request]&.authenticator&.name,
|
48
|
+
error_code: extract_error_code_from_response_body(event.payload[:response]&.body)
|
49
|
+
}
|
50
|
+
::Yabeda.apia.requests_total.increment(tags, by: 1)
|
51
|
+
end
|
52
|
+
|
53
|
+
def extract_error_code_from_response_body(body)
|
54
|
+
return "" unless body.is_a?(Hash)
|
55
|
+
|
56
|
+
body.dig(:error, :code).to_s
|
57
|
+
end
|
58
|
+
|
59
|
+
def observe_endpoint_duration_metric(event)
|
60
|
+
return if event.payload[:time].nil?
|
61
|
+
|
62
|
+
tags = {
|
63
|
+
endpoint: event.payload[:request]&.endpoint&.name,
|
64
|
+
status: event.payload[:response]&.status
|
65
|
+
}
|
66
|
+
::Yabeda.apia.endpoint_duration.measure(tags, event.payload[:time])
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/apia/yabeda.rb
ADDED
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: apia-yabeda
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Paul Sturgess
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-02-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: yabeda
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.12'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: yabeda-rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.9.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.9.0
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- Gemfile
|
62
|
+
- LICENSE.txt
|
63
|
+
- README.md
|
64
|
+
- Rakefile
|
65
|
+
- apia-yabeda.gemspec
|
66
|
+
- lib/apia/yabeda.rb
|
67
|
+
- lib/apia/yabeda/prometheus_collector.rb
|
68
|
+
- lib/apia/yabeda/version.rb
|
69
|
+
homepage: https://github.com/krystal/apia-yabeda
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata:
|
73
|
+
homepage_uri: https://github.com/krystal/apia-yabeda
|
74
|
+
source_code_uri: https://github.com/krystal/apia-yabeda
|
75
|
+
changelog_uri: https://github.com/krystal/apia-yabeda/changelog.md
|
76
|
+
rubygems_mfa_required: 'false'
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 2.7.0
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubygems_version: 3.4.19
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Apia Yabeda integration
|
96
|
+
test_files: []
|