Skip to content

[WIP] initial implementation of skipping prefix hash #26

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

Open
wants to merge 1 commit 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
7 changes: 6 additions & 1 deletion lib/logstash/outputs/graphite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class LogStash::Outputs::Graphite < LogStash::Outputs::Base
# This config setting changes the separator from the '.' default.
config :nested_object_separator, :validate => :string, :default => "."

# TODO
# In the special case of using a hash field as metrics, one could want to skip the "metrics container" name by setting this config to `true`
# For example an event containing a field "mymetrics" => {a => 1, b => { c => 2 }} is normally converted into `mymetrics.a` ..., with this config it would simply write `a`...
config :skip_prefix_for_metrics_hash, :validate => :boolean, :default => false

def register
@include_metrics.collect!{|regexp| Regexp.new(regexp)}
@exclude_metrics.collect!{|regexp| Regexp.new(regexp)}
Expand Down Expand Up @@ -175,7 +180,7 @@ def event_timestamp(event)

def metrics_lines_for_event(event, metric, value, timestamp)
if event[metric].is_a?(Hash)
dotify(event[metric], metric).map do |k, v|
dotify(event[metric], @skip_prefix_for_metrics_hash ? nil: metric).map do |k, v|
metrics_line(event, k, v, timestamp)
end
else
Expand Down
26 changes: 26 additions & 0 deletions spec/outputs/graphite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,32 @@
subject.receive(event)
end

context "with a field hash" do

subject { LogStash::Outputs::Graphite.new("host" => "localhost", "port" => port, "metrics" => {"metrics" => "whatever"}) }
let(:event) { LogStash::Event.new("@meta" => {"metrics_field" => "metrics"}, "foo" => "fancy", "metrics" => { 'foo.bar' => 1.0,
'bar.foo'=> 2.0}, "bar" => 42) }

it "include all metrics" do
expect(server.size).to eq(2)
expect(server.pop).to match(/^metrics.bar.foo 2.0 \d{10,}\n$/)
expect(server.pop).to match(/^metrics.foo.bar 1.0 \d{10,}\n$/)
end
end

context "with a field hash" do

subject { LogStash::Outputs::Graphite.new("host" => "localhost", "port" => port, "metrics" => {"metrics" => "whatever"}, "skip_prefix_for_metrics_hash" => true) }
let(:event) { LogStash::Event.new("@meta" => {"metrics_field" => "metrics"}, "foo" => "fancy", "metrics" => { 'foo.bar' => 1.0,
'bar.foo'=> 2.0}, "bar" => 42) }

it "include all metrics" do
expect(server.size).to eq(2)
expect(server.pop).to match(/^bar.foo 2.0 \d{10,}\n$/)
expect(server.pop).to match(/^foo.bar 1.0 \d{10,}\n$/)
end
end

context "with a default run" do

subject { LogStash::Outputs::Graphite.new("host" => "localhost", "port" => port, "metrics" => [ "hurray.%{foo}", "%{bar}" ]) }
Expand Down