From afaf11baca5f79c4c88572764706247abe328053 Mon Sep 17 00:00:00 2001 From: Santosh Kumar Date: Tue, 25 Feb 2020 14:59:17 -0800 Subject: [PATCH 1/3] added histogram metric for response time and exposed bucket configurations --- .../server/web_collector.rb | 19 +++++++++++++++++-- lib/prometheus_exporter/version.rb | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/prometheus_exporter/server/web_collector.rb b/lib/prometheus_exporter/server/web_collector.rb index 95033614..2e2ff958 100644 --- a/lib/prometheus_exporter/server/web_collector.rb +++ b/lib/prometheus_exporter/server/web_collector.rb @@ -1,8 +1,16 @@ # frozen_string_literal: true +require 'yaml' + module PrometheusExporter::Server class WebCollector < TypeCollector def initialize + if ENV["RAIL_PROMETHEUS_EXPORTER_CONFIG"] then + @config = YAML.load(File.read(ENV["RAIL_PROMETHEUS_EXPORTER_CONFIG"])) + else + puts("Could not find env RAIL_PROMETHEUS_EXPORTER_CONFIG. Loading defualt configuration.") + @config = {"histogram_buckets"=>[ 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0, 20.0, 30.0 ]} + end @metrics = {} end @@ -28,9 +36,16 @@ def ensure_metrics "Total HTTP requests from web app." ) - @metrics["http_duration_seconds"] = @http_duration_seconds = PrometheusExporter::Metric::Summary.new( + # Commenting out request duration summary metrics + # @metrics["http_duration_seconds"] = @http_duration_seconds = PrometheusExporter::Metric::Summary.new( + # "http_duration_seconds", + # "Time spent in HTTP reqs in seconds." + # ) + + @metrics["http_duration_seconds"] = @http_duration_seconds = PrometheusExporter::Metric::Histogram.new( "http_duration_seconds", - "Time spent in HTTP reqs in seconds." + "Time spent in HTTP reqs in seconds.", + opts={:buckets=>@config["histogram_buckets"]} ) @metrics["http_redis_duration_seconds"] = @http_redis_duration_seconds = PrometheusExporter::Metric::Summary.new( diff --git a/lib/prometheus_exporter/version.rb b/lib/prometheus_exporter/version.rb index ba312c14..cfde6275 100644 --- a/lib/prometheus_exporter/version.rb +++ b/lib/prometheus_exporter/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module PrometheusExporter - VERSION = '0.5.1' + VERSION = '0.5.1.db' end From 03a512f2421150e4d0a37771fcaf34bac5729111 Mon Sep 17 00:00:00 2001 From: Santosh Kumar Date: Wed, 26 Feb 2020 13:08:44 -0800 Subject: [PATCH 2/3] replaced numbers with string digit and add prefix configuration --- .../server/web_collector.rb | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/prometheus_exporter/server/web_collector.rb b/lib/prometheus_exporter/server/web_collector.rb index 2e2ff958..dc26e0be 100644 --- a/lib/prometheus_exporter/server/web_collector.rb +++ b/lib/prometheus_exporter/server/web_collector.rb @@ -29,10 +29,17 @@ def metrics protected + def add_metric_prefix(metric_name) + if @config["metric_prefix"] + metric_name = @config["metric_prefix"]+"_"+metric_name + end + metric_name + end + def ensure_metrics unless @http_requests_total @metrics["http_requests_total"] = @http_requests_total = PrometheusExporter::Metric::Counter.new( - "http_requests_total", + add_metric_prefix("http_requests_total"), "Total HTTP requests from web app." ) @@ -43,32 +50,42 @@ def ensure_metrics # ) @metrics["http_duration_seconds"] = @http_duration_seconds = PrometheusExporter::Metric::Histogram.new( - "http_duration_seconds", + add_metric_prefix("http_duration_seconds"), "Time spent in HTTP reqs in seconds.", opts={:buckets=>@config["histogram_buckets"]} ) @metrics["http_redis_duration_seconds"] = @http_redis_duration_seconds = PrometheusExporter::Metric::Summary.new( - "http_redis_duration_seconds", + add_metric_prefix("http_redis_duration_seconds"), "Time spent in HTTP reqs in Redis, in seconds." ) @metrics["http_sql_duration_seconds"] = @http_sql_duration_seconds = PrometheusExporter::Metric::Summary.new( - "http_sql_duration_seconds", + add_metric_prefix("http_sql_duration_seconds"), "Time spent in HTTP reqs in SQL in seconds." ) @metrics["http_queue_duration_seconds"] = @http_queue_duration_seconds = PrometheusExporter::Metric::Summary.new( - "http_queue_duration_seconds", + add_metric_prefix("http_queue_duration_seconds"), "Time spent queueing the request in load balancer in seconds." ) end end + def replace_digits_in_controller(controller) + if controller + controller = controller.gsub(/\d+/, "digit") + end + controller + end + def observe(obj) + obj['controller'] = replace_digits_in_controller(obj['controller']) + default_labels = { - controller: obj['controller'] || 'other', - action: obj['action'] || 'other' + request_url: obj['controller'] || 'other', + action: obj['action'] || 'other', + status_code: obj["status"] } custom_labels = obj['custom_labels'] labels = custom_labels.nil? ? default_labels : default_labels.merge(custom_labels) From 16435f754b6e3637575e76ce1d464b47d425a403 Mon Sep 17 00:00:00 2001 From: Santosh Kumar Date: Wed, 26 Feb 2020 13:34:09 -0800 Subject: [PATCH 3/3] updated histogram bucket --- build.sh | 3 +++ .../server/web_collector.rb | 19 ++++++------------- 2 files changed, 9 insertions(+), 13 deletions(-) create mode 100755 build.sh diff --git a/build.sh b/build.sh new file mode 100755 index 00000000..7663a928 --- /dev/null +++ b/build.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +gem build prometheus_exporter.gemspec diff --git a/lib/prometheus_exporter/server/web_collector.rb b/lib/prometheus_exporter/server/web_collector.rb index dc26e0be..9bda8941 100644 --- a/lib/prometheus_exporter/server/web_collector.rb +++ b/lib/prometheus_exporter/server/web_collector.rb @@ -9,7 +9,7 @@ def initialize @config = YAML.load(File.read(ENV["RAIL_PROMETHEUS_EXPORTER_CONFIG"])) else puts("Could not find env RAIL_PROMETHEUS_EXPORTER_CONFIG. Loading defualt configuration.") - @config = {"histogram_buckets"=>[ 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0, 20.0, 30.0 ]} + @config = {"histogram_buckets"=>[ 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5.0, 7.5, 10, 20, 30 ]} end @metrics = {} end @@ -29,17 +29,10 @@ def metrics protected - def add_metric_prefix(metric_name) - if @config["metric_prefix"] - metric_name = @config["metric_prefix"]+"_"+metric_name - end - metric_name - end - def ensure_metrics unless @http_requests_total @metrics["http_requests_total"] = @http_requests_total = PrometheusExporter::Metric::Counter.new( - add_metric_prefix("http_requests_total"), + "http_requests_total", "Total HTTP requests from web app." ) @@ -50,23 +43,23 @@ def ensure_metrics # ) @metrics["http_duration_seconds"] = @http_duration_seconds = PrometheusExporter::Metric::Histogram.new( - add_metric_prefix("http_duration_seconds"), + "http_duration_seconds", "Time spent in HTTP reqs in seconds.", opts={:buckets=>@config["histogram_buckets"]} ) @metrics["http_redis_duration_seconds"] = @http_redis_duration_seconds = PrometheusExporter::Metric::Summary.new( - add_metric_prefix("http_redis_duration_seconds"), + "http_redis_duration_seconds", "Time spent in HTTP reqs in Redis, in seconds." ) @metrics["http_sql_duration_seconds"] = @http_sql_duration_seconds = PrometheusExporter::Metric::Summary.new( - add_metric_prefix("http_sql_duration_seconds"), + "http_sql_duration_seconds", "Time spent in HTTP reqs in SQL in seconds." ) @metrics["http_queue_duration_seconds"] = @http_queue_duration_seconds = PrometheusExporter::Metric::Summary.new( - add_metric_prefix("http_queue_duration_seconds"), + "http_queue_duration_seconds", "Time spent queueing the request in load balancer in seconds." ) end