diff --git a/README.md b/README.md index 7a0e241..d9e7bef 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ More configuration parameters: - `port`: listen port (default: 24231) - `metrics_path`: metrics HTTP endpoint (default: /metrics) - `aggregated_metrics_path`: metrics HTTP endpoint (default: /aggregated_metrics) -- `content_encoding`: encoding format for the exposed metrics (default: identity) +- `content_encoding`: encoding format for the exposed metrics (default: identity). Supported formats are {identity, gzip} When using multiple workers, each worker binds to port + `fluent_worker_id`. To scrape metrics from all workers at once, you can access http://localhost:24231/aggregated_metrics. diff --git a/lib/fluent/plugin/in_prometheus.rb b/lib/fluent/plugin/in_prometheus.rb index 09f4875..9bc01d6 100644 --- a/lib/fluent/plugin/in_prometheus.rb +++ b/lib/fluent/plugin/in_prometheus.rb @@ -34,7 +34,7 @@ class PrometheusInput < Fluent::Plugin::Input end desc 'Content encoding of the exposed metrics, Currently supported encoding is identity, gzip. Ref: https://prometheus.io/docs/instrumenting/exposition_formats/#basic-info' - config_param :content_encoding, :string, default: "identity" + config_param :content_encoding, :enum, list: [:identity, :gzip], default: :identity def initialize super @@ -58,8 +58,6 @@ def configure(conf) @base_port = @port @port += fluentd_worker_id - - raise "Invalid content encoding for the exposed metrics endpoint" unless @content_encoding="identity" || @content_encoding="gzip" end def multi_workers_ready? @@ -190,16 +188,7 @@ def start_webrick end def all_metrics - body = nil - case @content_encoding - when 'gzip' - gzip = Zlib::GzipWriter.new(StringIO.new) - gzip << ::Prometheus::Client::Formats::Text.marshal(@registry) - body = gzip.close.string - when 'identity' - body = ::Prometheus::Client::Formats::Text.marshal(@registry) - end - [200, { 'Content-Type' => ::Prometheus::Client::Formats::Text::CONTENT_TYPE, 'Content-Encoding' => @content_encoding }, body] + response_headers(::Prometheus::Client::Formats::Text.marshal(@registry)) rescue => e [500, { 'Content-Type' => 'text/plain' }, e.to_s] end @@ -212,16 +201,7 @@ def all_workers_metrics full_result.add_metrics(resp.body) end end - body = nil - case @content_encoding - when 'gzip' - gzip = Zlib::GzipWriter.new(StringIO.new) - gzip << full_result.get_metrics - body = gzip.close.string - when 'identity' - body = full_result.get_metrics - end - [200, { 'Content-Type' => ::Prometheus::Client::Formats::Text::CONTENT_TYPE, 'Content-Encoding' => @content_encoding }, body] + response_headers(full_result.get_metrics) rescue => e [500, { 'Content-Type' => 'text/plain' }, e.to_s] end @@ -249,5 +229,18 @@ def do_request(host:, port:, secure:) yield(http) end end + + def response_headers(metrics) + body = nil + case @content_encoding + when :gzip + gzip = Zlib::GzipWriter.new(StringIO.new) + gzip << metrics + body = gzip.close.string + when :identity + body = metrics + end + [200, { 'Content-Type' => ::Prometheus::Client::Formats::Text::CONTENT_TYPE, 'Content-Encoding' => @content_encoding.to_s }, body] + end end end