Skip to content

Commit b9d920b

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

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed

.dev_to/compose.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ services:
7272
# <<: *backend
7373
# command: bundle exec sidekiq -C config/sidekiq.yml
7474

75+
exporter:
76+
<<: *backend
77+
command: bundle exec prometheus_exporter -b 0.0.0.0 -a prometheus/custom_collector.rb
78+
ports:
79+
- "9394:9394"
80+
7581
postgres:
7682
image: postgres:14
7783
volumes:
@@ -99,6 +105,24 @@ services:
99105
timeout: 3s
100106
retries: 30
101107

108+
prometheus:
109+
image: dockerhub.timeweb.cloud/prom/prometheus
110+
volumes:
111+
- ./prometheus.yml:/etc/prometheus/prometheus.yml
112+
- prom_data:/prometheus
113+
command:
114+
- '--config.file=/etc/prometheus/prometheus.yml'
115+
ports:
116+
- '9090:9090'
117+
grafana:
118+
image: dockerhub.timeweb.cloud/grafana/grafana
119+
environment:
120+
- GF_SECURITY_ADMIN_PASSWORD=pass
121+
depends_on:
122+
- prometheus
123+
ports:
124+
- "3030:3000"
125+
102126
webpacker:
103127
<<: *app
104128
command: bundle exec ./bin/webpack-dev-server
@@ -124,3 +148,4 @@ volumes:
124148
redis:
125149
packs:
126150
packs-test:
151+
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)