diff --git a/.dev_to/compose.yml b/.dev_to/compose.yml index dfb76e9c..00ce450c 100644 --- a/.dev_to/compose.yml +++ b/.dev_to/compose.yml @@ -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 @@ -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: @@ -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 @@ -124,3 +149,4 @@ volumes: redis: packs: packs-test: + prom_data: diff --git a/.dev_to/prometheus.yml b/.dev_to/prometheus.yml new file mode 100644 index 00000000..6fc70d5d --- /dev/null +++ b/.dev_to/prometheus.yml @@ -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'] diff --git a/Gemfile b/Gemfile index 7d7eb416..c9ec8008 100644 --- a/Gemfile +++ b/Gemfile @@ -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" diff --git a/Gemfile.lock b/Gemfile.lock index ac3243e0..e05609de 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) diff --git a/config/initializers/prometheus.rb b/config/initializers/prometheus.rb new file mode 100644 index 00000000..e7a9245e --- /dev/null +++ b/config/initializers/prometheus.rb @@ -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 diff --git a/prometheus/custom_collector.rb b/prometheus/custom_collector.rb new file mode 100644 index 00000000..bc6ea53c --- /dev/null +++ b/prometheus/custom_collector.rb @@ -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