Skip to content

Commit aba353d

Browse files
committed
Setup Prometheus & Grafana
1 parent 3a5304f commit aba353d

6 files changed

Lines changed: 63 additions & 0 deletions

File tree

.dev_to/compose.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ x-backend: &backend
4646
IRB_HISTFILE: /usr/local/hist/.irb_history
4747
EDITOR: vi
4848
SKYLIGHT_AUTHENTICATION: $SKYLIGHT_AUTHENTICATION
49+
PROMETHEUS_EXPORTER_HOST: exporter
4950
depends_on: &backend_depends_on
5051
postgres:
5152
condition: service_healthy
@@ -72,6 +73,12 @@ services:
7273
# <<: *backend
7374
# command: bundle exec sidekiq -C config/sidekiq.yml
7475

76+
exporter:
77+
<<: *backend
78+
command: bundle exec prometheus_exporter -b 0.0.0.0 -a prometheus/custom_collector.rb
79+
ports:
80+
- "9394:9394"
81+
7582
postgres:
7683
image: postgres:14
7784
volumes:
@@ -99,6 +106,24 @@ services:
99106
timeout: 3s
100107
retries: 30
101108

109+
prometheus:
110+
image: dockerhub.timeweb.cloud/prom/prometheus
111+
volumes:
112+
- ./prometheus.yml:/etc/prometheus/prometheus.yml
113+
- prom_data:/prometheus
114+
command:
115+
- '--config.file=/etc/prometheus/prometheus.yml'
116+
ports:
117+
- '9090:9090'
118+
grafana:
119+
image: dockerhub.timeweb.cloud/grafana/grafana
120+
environment:
121+
- GF_SECURITY_ADMIN_PASSWORD=pass
122+
depends_on:
123+
- prometheus
124+
ports:
125+
- "3030:3000"
126+
102127
webpacker:
103128
<<: *app
104129
command: bundle exec ./bin/webpack-dev-server
@@ -124,3 +149,4 @@ volumes:
124149
redis:
125150
packs:
126151
packs-test:
152+
prom_data:

.dev_to/prometheus.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
global:
2+
scrape_interval: 5s
3+
external_labels:
4+
monitor: 'my-monitor'
5+
scrape_configs:
6+
- job_name: 'prometheus'
7+
static_configs:
8+
- targets: ['localhost:9090']
9+
- job_name: 'devdev'
10+
static_configs:
11+
- targets: ['exporter:9394']

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ gem "validate_url", "~> 1.0"
103103
gem "webpacker", "~> 3.5"
104104
gem "webpush", "~> 0.3"
105105
gem "rack-mini-profiler"
106+
gem "prometheus_exporter"
106107

107108
group :development do
108109
gem "better_errors", "~> 2.5"

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,8 @@ GEM
638638
ast (~> 2.4.0)
639639
pg (1.1.4)
640640
powerpack (0.1.2)
641+
prometheus_exporter (2.1.0)
642+
webrick
641643
pry (0.12.2)
642644
coderay (~> 1.1.0)
643645
method_source (~> 0.9.0)
@@ -917,6 +919,7 @@ GEM
917919
webpush (0.3.2)
918920
hkdf (~> 0.2)
919921
jwt
922+
webrick (1.8.1)
920923
websocket-driver (0.6.5)
921924
websocket-extensions (>= 0.1.0)
922925
websocket-extensions (0.1.3)
@@ -1005,6 +1008,7 @@ DEPENDENCIES
10051008
omniauth-twitter (~> 1.4)
10061009
parallel_tests (~> 2.27)
10071010
pg (~> 1.1)
1011+
prometheus_exporter
10081012
pry (~> 0.12)
10091013
pry-byebug (~> 3.7)
10101014
pry-rails (~> 0.3)

config/initializers/prometheus.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
unless Rails.env.test?
2+
require 'prometheus_exporter/middleware'
3+
4+
# This reports stats per request like HTTP status and timings
5+
Rails.application.middleware.unshift PrometheusExporter::Middleware
6+
end

prometheus/custom_collector.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class CustomCollector < PrometheusExporter::Server::TypeCollector
2+
unless defined? Rails
3+
require File.expand_path("../../config/environment", __FILE__)
4+
end
5+
6+
def type
7+
"mariela_posts"
8+
end
9+
10+
def metrics
11+
mariela_posts_gague = PrometheusExporter::Metric::Gauge.new('mariela_posts', 'number of mariela posts')
12+
mariela_posts_gague.observe User.find_by_name('Mariela Ledner').articles.count
13+
[mariela_posts_gague]
14+
end
15+
end

0 commit comments

Comments
 (0)