mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-10 17:21:38 -05:00
Expose prometheus metrics at /metrics
This commit is contained in:
parent
9631696231
commit
17340079ce
6 changed files with 63 additions and 1 deletions
|
|
@ -1 +1 @@
|
|||
0.30.3
|
||||
0.30.4
|
||||
|
|
|
|||
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -65,6 +65,7 @@
|
|||
.dotnet/
|
||||
.cursorrules
|
||||
.cursormemory.md
|
||||
.serena/project.yml
|
||||
|
||||
/config/credentials/production.key
|
||||
/config/credentials/production.yml.enc
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
17
app/controllers/metrics_controller.rb
Normal file
17
app/controllers/metrics_controller.rb
Normal file
|
|
@ -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
|
||||
34
app/services/prometheus_metrics.rb
Normal file
34
app/services/prometheus_metrics.rb
Normal file
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue