discourse / prometheus_exporter

A framework for collecting and aggregating prometheus metrics
MIT License
525 stars 153 forks source link

Client default labels for Rails related metrics? #259

Closed adamk9k closed 1 year ago

adamk9k commented 1 year ago

Apologies if this something documented, I couldn't figure it out. I'm trying to run the gem in a multi-container environment, with every container running its own exporter process. In the exporter initializer I have

  require 'prometheus_exporter/middleware'
  require 'prometheus_exporter/instrumentation'

  PrometheusExporter::Client.new(custom_labels: { hostname: `hostname`.strip })

  # This reports stats per request like HTTP status and timings
  Rails.application.middleware.unshift PrometheusExporter::Middleware

  # this reports basic process stats like RSS and GC info
  PrometheusExporter::Instrumentation::Process.start(labels: { hostname: `hostname`.strip })

this results in metrics like

# TYPE ruby_rss gauge
ruby_rss{hostname="0e33cfafe5bf",type="ruby",pid="10"} 97058816

# HELP ruby_major_gc_ops_total Major GC operations by process.
# TYPE ruby_major_gc_ops_total counter
ruby_major_gc_ops_total{hostname="0e33cfafe5bf",type="ruby",pid="10"} 9

# HELP ruby_minor_gc_ops_total Minor GC operations by process.
# TYPE ruby_minor_gc_ops_total counter
ruby_minor_gc_ops_total{hostname="0e33cfafe5bf",type="ruby",pid="10"} 29

where hostname appears as a label, but also:

# TYPE ruby_http_request_duration_seconds summary
ruby_http_request_duration_seconds{action="index",controller="books",quantile="0.99"} 2.308657499997935
ruby_http_request_duration_seconds{action="index",controller="books",quantile="0.9"} 2.308657499997935
ruby_http_request_duration_seconds{action="index",controller="books",quantile="0.5"} 2.308657499997935
ruby_http_request_duration_seconds{action="index",controller="books",quantile="0.1"} 2.308657499997935
ruby_http_request_duration_seconds{action="index",controller="books",quantile="0.01"} 2.308657499997935
ruby_http_request_duration_seconds_sum{action="index",controller="books"} 2.308657499997935
ruby_http_request_duration_seconds_count{action="index",controller="books"} 1

# HELP ruby_http_request_redis_duration_seconds Time spent in HTTP reqs in Redis, in seconds.
# TYPE ruby_http_request_redis_duration_seconds summary

# HELP ruby_http_request_sql_duration_seconds Time spent in HTTP reqs in SQL in seconds.
# TYPE ruby_http_request_sql_duration_seconds summary
ruby_http_request_sql_duration_seconds{action="index",controller="books",quantile="0.99"} 0.028103999989980366
ruby_http_request_sql_duration_seconds{action="index",controller="books",quantile="0.9"} 0.028103999989980366
ruby_http_request_sql_duration_seconds{action="index",controller="books",quantile="0.5"} 0.028103999989980366
ruby_http_request_sql_duration_seconds{action="index",controller="books",quantile="0.1"} 0.028103999989980366
ruby_http_request_sql_duration_seconds{action="index",controller="books",quantile="0.01"} 0.028103999989980366
ruby_http_request_sql_duration_seconds_sum{action="index",controller="books"} 0.028103999989980366
ruby_http_request_sql_duration_seconds_count{action="index",controller="books"} 1

where the host label is missing. Am I missing something in the configuration or do I need to add my custom collectors with explicit labels.

Thanks in advance

sosedoff commented 1 year ago

@adamk9k you can apply your custom labels to pretty much all collectors with:

PrometheusExporter::Client.default = PrometheusExporter::Client.new(
  custom_labels: {
    hostname: PrometheusExporter.hostname,
    env: Rails.env
  }
)