Skip to content

Commit f952a1a

Browse files
authored
4518 – Create metrics class abstraction using Prometheus ruby client (#455)
- Added the gem prometheus-client. - Created a class abstraction MetricsService, so it's easy for us to change between prometheus, otel, or whatever else we might think is best. - Created some very basic methods to create and increment a custom counter. - Created one metric media_request_total so we can start testing things. - We create and register custom metrics once on initialization, and then call increment wherever it's needed - The gem adds http_server_request_duration_seconds histogram, http_server_requests_total counter and http_server_exceptions_total counter by default. References: 4396, 4518 PR: 455 - Unrelated to the current changes: It seems sometimes we get 'How to use the Public folder.rtf' and sometimes 'How-to-use-the-Public-folder.rtf' I don't think it matters if it has whistespaces or hifens.
1 parent aa7d993 commit f952a1a

File tree

8 files changed

+72
-1
lines changed

8 files changed

+72
-1
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,4 @@ gem 'addressable', '2.8.1'
7676
# Adding this removes some deprecation warnings, caused by double-loading of the net-protocol library
7777
# (see https://github.com/ruby/net-imap/issues/16). We *might* be able to remove this after upgrading to Ruby 3
7878
gem 'net-http'
79+
gem 'prometheus-client'

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ GEM
267267
parallel_tests (4.2.1)
268268
parallel
269269
pg (1.1.0)
270+
prometheus-client (4.2.2)
270271
public_suffix (4.0.7)
271272
puma (5.6.8)
272273
nio4r (~> 2.0)
@@ -454,6 +455,7 @@ DEPENDENCIES
454455
parallel_tests
455456
pg (= 1.1)
456457
postrank-uri!
458+
prometheus-client
457459
puma (= 5.6.8)
458460
rack (>= 1.6.11)
459461
rack-cors (>= 2.0.2)

app/models/media.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def as_json(options = {})
8686
end
8787
archive_if_conditions_are_met(options, id, cache)
8888
Metrics.schedule_fetching_metrics_from_facebook(self.data, self.url, ApiKey.current&.id)
89+
MetricsService.increment_counter(:media_request_total, labels: { service: 'pender', parser: self.data['provider'] })
8990
cache.read(id, :json) || cleanup_data_encoding(data)
9091
end
9192

config.ru

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# This file is used by Rack-based servers to start the application.
22

33
require ::File.expand_path('../config/environment', __FILE__)
4+
require 'rack'
5+
require 'prometheus/middleware/collector'
6+
require 'prometheus/middleware/exporter'
7+
8+
use Rack::Deflater
9+
use Prometheus::Middleware::Collector
10+
use Prometheus::Middleware::Exporter
411

512
if Rails.env.profile?
613
use Rack::RubyProf, path: 'tmp/profile'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Rails.application.reloader.to_prepare do
2+
MetricsService.custom_counter(:media_request_total, 'Count every request made', labels: [:service, :parser])
3+
end

lib/metrics_service.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'prometheus/client'
2+
3+
class MetricsService
4+
class << self
5+
def custom_counter(name, description, labels: [])
6+
counter = Prometheus::Client::Counter.new(
7+
name,
8+
docstring: description,
9+
labels: labels
10+
)
11+
prometheus_registry.register(counter)
12+
end
13+
14+
def increment_counter(name, labels: [])
15+
counter = prometheus_registry.get(name)
16+
return if counter.nil?
17+
18+
counter.increment(labels: labels)
19+
end
20+
21+
def get_counter(counter, labels: [])
22+
counter.get(labels: labels)
23+
end
24+
25+
private
26+
27+
def prometheus_registry
28+
Prometheus::Client.registry
29+
end
30+
31+
end
32+
end

test/lib/metrics_service_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'test_helper'
2+
3+
class MetricsServiceTest < ActiveSupport::TestCase
4+
test "custom_counter works for real" do
5+
assert_nothing_raised do
6+
MetricsService.custom_counter(:custom_counter, 'Custom counter test', labels: [:test])
7+
end
8+
end
9+
10+
test "increment_counter works for real" do
11+
MetricsService.custom_counter(:custom_counter_2, 'Custom counter test', labels: [:test])
12+
13+
assert_nothing_raised do
14+
MetricsService.increment_counter(:custom_counter_2, labels: [:test])
15+
end
16+
end
17+
18+
test "get_counter works for real" do
19+
custom_counter = MetricsService.custom_counter(:custom_counter_3, 'Custom counter test', labels: [:test])
20+
21+
assert_nothing_raised do
22+
MetricsService.get_counter(custom_counter, labels: [:test])
23+
end
24+
end
25+
end

test/models/parser/dropbox_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class DropboxIntegrationTest < ActiveSupport::TestCase
77

88
assert_equal 'item', data['type']
99
assert_equal 'dropbox', data['provider']
10-
assert_equal 'How to use the Public folder.rtf', data['title']
1110
assert_equal 'Shared with Dropbox', data['description']
11+
assert_match /How.to.use.the.Public.folder.rtf/, data['title']
1212
assert_not_nil data['picture']
1313
assert_nil data['error']
1414
end

0 commit comments

Comments
 (0)