Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility for setting prefix on custom metrics, solves #49 #163

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,30 @@ ruby_web_requests{hostname="app-server-01",route="test/route"} 1
ruby_web_requests{hostname="app-server-01"} 1
```

It is also possible to add a custom prefix to each counter instance.
Same as above but with a custom prefix overriding the global prefix.
```ruby
# Specify global prefix for metric names
PrometheusExporter::Metric::Base.default_prefix = "ruby"

# Specify default labels for metrics
PrometheusExporter::Metric::Base.default_labels = { "hostname" => "app-server-01" }

# Counter instance with custom prefix for this metric
counter = PrometheusExporter::Metric::Counter.new("web_requests", "number of web requests", "my_custom_prefix")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I think there is typo here?

PrometheusExporter::Metric::Counter.new("web_requests", "number of web requests",    prefix: "my_custom_prefix")


counter.observe(1, route: 'test/route')
counter.observe
```

This will output these metrics
```
# HELP my_custom_prefix_web_requests number of web requests
# TYPE my_custom_prefix_web_requests counter
my_custom_prefix_web_requests{hostname="app-server-01",route="test/route"} 1
my_custom_prefix_web_requests{hostname="app-server-01"} 1
```

### Exporter Process Configuration

When running the process for `prometheus_exporter` using `bin/prometheus_exporter`, there are several configurations that
Expand Down
6 changes: 4 additions & 2 deletions lib/prometheus_exporter/metric/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ def self.default_labels

attr_accessor :help, :name, :data

def initialize(name, help)
def initialize(name, help, opts = {})
@name = name
@help = help
@opts = opts
end

def type
Expand Down Expand Up @@ -62,7 +63,8 @@ def from_json(json)
end

def prefix(name)
Base.default_prefix + name
pfx = @opts && @opts.key?(:prefix) ? @opts[:prefix] : Base.default_prefix
pfx + name
end

def labels_text(labels)
Expand Down
2 changes: 1 addition & 1 deletion lib/prometheus_exporter/metric/counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module PrometheusExporter::Metric
class Counter < Base
attr_reader :data

def initialize(name, help)
def initialize(name, help, opts = {})
super
reset!
end
Expand Down
2 changes: 1 addition & 1 deletion lib/prometheus_exporter/metric/gauge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module PrometheusExporter::Metric
class Gauge < Base
attr_reader :data

def initialize(name, help)
def initialize(name, help, opts = {})
super
reset!
end
Expand Down
2 changes: 1 addition & 1 deletion lib/prometheus_exporter/metric/histogram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Histogram < Base
DEFAULT_BUCKETS = [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5.0, 10.0].freeze

def initialize(name, help, opts = {})
super(name, help)
super
@buckets = (opts[:buckets] || DEFAULT_BUCKETS).sort.reverse
reset!
end
Expand Down
2 changes: 1 addition & 1 deletion lib/prometheus_exporter/metric/summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Summary < Base
attr_reader :estimators, :count, :total

def initialize(name, help, opts = {})
super(name, help)
super
reset!
@quantiles = opts[:quantiles] || DEFAULT_QUANTILES
end
Expand Down
4 changes: 2 additions & 2 deletions lib/prometheus_exporter/server/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ def register_metric_unsafe(obj)
metric =
case obj["type"]
when "gauge"
PrometheusExporter::Metric::Gauge.new(name, help)
PrometheusExporter::Metric::Gauge.new(name, help, opts)
when "counter"
PrometheusExporter::Metric::Counter.new(name, help)
PrometheusExporter::Metric::Counter.new(name, help, opts)
when "summary"
PrometheusExporter::Metric::Summary.new(name, help, opts)
when "histogram"
Expand Down
17 changes: 17 additions & 0 deletions test/metric/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ module PrometheusExporter::Metric
assert_equal(counter.to_prometheus_text, text)
end

it "supports custom prefix" do
Base.default_prefix = 'notcustom_'
cp = Counter.new("a_counter", "my amazing counter", prefix: 'custom_prefix_')

cp.observe(2, baz: "bar")
cp.observe(223)

text = <<~TEXT
# HELP custom_prefix_a_counter my amazing counter
# TYPE custom_prefix_a_counter counter
custom_prefix_a_counter{baz="bar"} 2
custom_prefix_a_counter 223
TEXT

assert_equal(cp.to_prometheus_text, text)
end

it "supports reset! for Gauge" do

gauge = Gauge.new("test", "test")
Expand Down
46 changes: 46 additions & 0 deletions test/server/collector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,52 @@ def test_it_can_pass_options_to_histogram
assert_equal(text, collector.prometheus_metrics_text)
end

def test_it_can_pass_options_to_gauge
name = 'test_name'
help = 'test_help'
collector = PrometheusExporter::Server::Collector.new
json = {
type: :gauge,
help: help,
name: name,
keys: { key1: 'test1' },
opts: { prefix: 'custom_pfx_' },
value: 8
}
collector.process(json.to_json)

text = <<~TXT
# HELP custom_pfx_test_name test_help
# TYPE custom_pfx_test_name gauge
custom_pfx_test_name{key1=\"test1\"} 8
TXT

assert_equal(text, collector.prometheus_metrics_text)
end

def test_it_can_pass_options_to_counter
name = 'test_name'
help = 'test_help'
collector = PrometheusExporter::Server::Collector.new
json = {
type: :counter,
help: help,
name: name,
keys: { key1: 'test1' },
opts: { prefix: 'custom_pfx_' },
value: 18
}
collector.process(json.to_json)

text = <<~TXT
# HELP custom_pfx_test_name test_help
# TYPE custom_pfx_test_name counter
custom_pfx_test_name{key1=\"test1\"} 18
TXT

assert_equal(text, collector.prometheus_metrics_text)
end

def test_it_can_collect_sidekiq_metrics
collector = PrometheusExporter::Server::Collector.new
client = PipedClient.new(collector)
Expand Down