Skip to content

Commit

Permalink
Added encoding support for exposed metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Gosling committed Jul 29, 2024
1 parent ee4cfe1 commit 21f1c3f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion fluent-plugin-prometheus.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = "fluent-plugin-prometheus"
spec.version = "2.1.0"
spec.version = "2.1.1"
spec.authors = ["Masahiro Sano"]
spec.email = ["[email protected]"]
spec.summary = %q{A fluent plugin that collects metrics and exposes for Prometheus.}
Expand Down
29 changes: 26 additions & 3 deletions lib/fluent/plugin/in_prometheus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'net/http'
require 'openssl'


module Fluent::Plugin
class PrometheusInput < Fluent::Plugin::Input
Fluent::Plugin.register_input('prometheus', self)
Expand Down Expand Up @@ -32,6 +33,9 @@ class PrometheusInput < Fluent::Plugin::Input
config_param :extra_conf, :hash, default: nil, symbolize_keys: true, deprecated: 'See http helper config'
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"

def initialize
super
@registry = ::Prometheus::Client.registry
Expand All @@ -54,6 +58,8 @@ 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 @@ -184,7 +190,16 @@ def start_webrick
end

def all_metrics
[200, { 'Content-Type' => ::Prometheus::Client::Formats::Text::CONTENT_TYPE }, ::Prometheus::Client::Formats::Text.marshal(@registry)]
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]
rescue => e
[500, { 'Content-Type' => 'text/plain' }, e.to_s]
end
Expand All @@ -197,8 +212,16 @@ def all_workers_metrics
full_result.add_metrics(resp.body)
end
end

[200, { 'Content-Type' => ::Prometheus::Client::Formats::Text::CONTENT_TYPE }, full_result.get_metrics]
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]
rescue => e
[500, { 'Content-Type' => 'text/plain' }, e.to_s]
end
Expand Down

0 comments on commit 21f1c3f

Please sign in to comment.