diff --git a/.app_version b/.app_version index e8262eb5..db287d4a 100644 --- a/.app_version +++ b/.app_version @@ -1 +1 @@ -0.30.3 +0.30.4 diff --git a/.gitignore b/.gitignore index 1510b45b..ce9010e1 100644 --- a/.gitignore +++ b/.gitignore @@ -65,6 +65,7 @@ .dotnet/ .cursorrules .cursormemory.md +.serena/project.yml /config/credentials/production.key /config/credentials/production.yml.enc diff --git a/CHANGELOG.md b/CHANGELOG.md index 2046bacf..410a8730 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +# [0.30.4] - 2025-07-25 + +## Added + +- Prometheus metrics are now available at `/metrics`. Configure `METRICS_USERNAME` and `METRICS_PASSWORD` environment variables for basic authentication. All other prometheus-related environment variables are also necessary. + + + # [0.30.3] - 2025-07-23 ## Changed diff --git a/app/controllers/metrics_controller.rb b/app/controllers/metrics_controller.rb new file mode 100644 index 00000000..4ff77603 --- /dev/null +++ b/app/controllers/metrics_controller.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class MetricsController < ApplicationController + http_basic_authenticate_with name: ENV['METRICS_USERNAME'], password: ENV['METRICS_PASSWORD'], only: :index + + def index + result = PrometheusMetrics.fetch_data + + if result[:success] + render plain: result[:data], content_type: 'text/plain' + elsif result[:error] == 'Prometheus exporter not enabled' + head :not_found + else + head :service_unavailable + end + end +end diff --git a/app/services/prometheus_metrics.rb b/app/services/prometheus_metrics.rb new file mode 100644 index 00000000..a4077b97 --- /dev/null +++ b/app/services/prometheus_metrics.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require 'net/http' +require 'uri' + +class PrometheusMetrics + class << self + def fetch_data + return { success: false, error: 'Prometheus exporter not enabled' } unless prometheus_enabled? + + host = ENV.fetch('PROMETHEUS_EXPORTER_HOST', 'localhost') + port = ENV.fetch('PROMETHEUS_EXPORTER_PORT', 9394) + + begin + response = Net::HTTP.get_response(URI("http://#{host}:#{port}/metrics")) + + if response.code == '200' + { success: true, data: response.body } + else + { success: false, error: "Prometheus server returned #{response.code}" } + end + rescue => e + Rails.logger.error "Failed to fetch Prometheus metrics: #{e.message}" + { success: false, error: e.message } + end + end + + private + + def prometheus_enabled? + DawarichSettings.prometheus_exporter_enabled? + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 93ceb12d..0c8026fb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -87,6 +87,8 @@ Rails.application.routes.draw do devise_for :users end + resources :metrics, only: [:index] + get 'map', to: 'map#index' namespace :api do