Skip to content

Commit

Permalink
Setup Prometheus & Grafana
Browse files Browse the repository at this point in the history
  • Loading branch information
beglov committed May 30, 2024
1 parent 3a5304f commit aba353d
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .dev_to/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ x-backend: &backend
IRB_HISTFILE: /usr/local/hist/.irb_history
EDITOR: vi
SKYLIGHT_AUTHENTICATION: $SKYLIGHT_AUTHENTICATION
PROMETHEUS_EXPORTER_HOST: exporter
depends_on: &backend_depends_on
postgres:
condition: service_healthy
Expand All @@ -72,6 +73,12 @@ services:
# <<: *backend
# command: bundle exec sidekiq -C config/sidekiq.yml

exporter:
<<: *backend
command: bundle exec prometheus_exporter -b 0.0.0.0 -a prometheus/custom_collector.rb
ports:
- "9394:9394"

postgres:
image: postgres:14
volumes:
Expand Down Expand Up @@ -99,6 +106,24 @@ services:
timeout: 3s
retries: 30

prometheus:
image: dockerhub.timeweb.cloud/prom/prometheus
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
- prom_data:/prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
ports:
- '9090:9090'
grafana:
image: dockerhub.timeweb.cloud/grafana/grafana
environment:
- GF_SECURITY_ADMIN_PASSWORD=pass
depends_on:
- prometheus
ports:
- "3030:3000"

webpacker:
<<: *app
command: bundle exec ./bin/webpack-dev-server
Expand All @@ -124,3 +149,4 @@ volumes:
redis:
packs:
packs-test:
prom_data:
11 changes: 11 additions & 0 deletions .dev_to/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
global:
scrape_interval: 5s
external_labels:
monitor: 'my-monitor'
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'devdev'
static_configs:
- targets: ['exporter:9394']
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ gem "validate_url", "~> 1.0"
gem "webpacker", "~> 3.5"
gem "webpush", "~> 0.3"
gem "rack-mini-profiler"
gem "prometheus_exporter"

group :development do
gem "better_errors", "~> 2.5"
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,8 @@ GEM
ast (~> 2.4.0)
pg (1.1.4)
powerpack (0.1.2)
prometheus_exporter (2.1.0)
webrick
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
Expand Down Expand Up @@ -917,6 +919,7 @@ GEM
webpush (0.3.2)
hkdf (~> 0.2)
jwt
webrick (1.8.1)
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
Expand Down Expand Up @@ -1005,6 +1008,7 @@ DEPENDENCIES
omniauth-twitter (~> 1.4)
parallel_tests (~> 2.27)
pg (~> 1.1)
prometheus_exporter
pry (~> 0.12)
pry-byebug (~> 3.7)
pry-rails (~> 0.3)
Expand Down
6 changes: 6 additions & 0 deletions config/initializers/prometheus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
unless Rails.env.test?
require 'prometheus_exporter/middleware'

# This reports stats per request like HTTP status and timings
Rails.application.middleware.unshift PrometheusExporter::Middleware
end
15 changes: 15 additions & 0 deletions prometheus/custom_collector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CustomCollector < PrometheusExporter::Server::TypeCollector
unless defined? Rails
require File.expand_path("../../config/environment", __FILE__)
end

def type
"mariela_posts"
end

def metrics
mariela_posts_gague = PrometheusExporter::Metric::Gauge.new('mariela_posts', 'number of mariela posts')
mariela_posts_gague.observe User.find_by_name('Mariela Ledner').articles.count
[mariela_posts_gague]
end
end

0 comments on commit aba353d

Please sign in to comment.