Skip to content

Commit

Permalink
Addressed comments
Browse files Browse the repository at this point in the history
Signed-off-by: Athishpranav2003 <[email protected]>
  • Loading branch information
Athishpranav2003 committed Aug 1, 2024
1 parent 87189d9 commit 29d0dca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 16 additions & 23 deletions lib/fluent/plugin/in_prometheus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 29d0dca

Please sign in to comment.