Implements #has_metric?(*path)

Add a new method that uses the `fast_lookup` has to find if a specific
metric exist instead of relying on exceptions.

Usage:

```ruby
metric_store.has_metric?(:node, :sashimi, :pipelines, :pipeline01, :plugins, :"logstash-output-elasticsearch", :event_in) # true
metric_store.has_metric?(:node, :sashimi, :pipelines, :pipeline01, :plugins, :"logstash-output-elasticsearch", :do_not_exist) # false
```
Fixes: #6533

Fixes #6759
This commit is contained in:
Pier-Hugues Pellerin 2017-02-27 10:23:56 -08:00
parent 469f451e99
commit 584a3860a0
2 changed files with 21 additions and 3 deletions

View file

@ -52,7 +52,7 @@ module LogStash module Instrument
# BUT. If the value is not present in the `@fast_lookup` the value will be inserted and
# `#puf_if_absent` will return nil. With this returned value of nil we assume that we don't
# have it in the `@metric_store` for structured search so we add it there too.
if found_value = @fast_lookup.put_if_absent([namespaces, key], provided_value)
if found_value = @fast_lookup.put_if_absent(namespaces.dup << key, provided_value)
return found_value
else
@structured_lookup_mutex.synchronize do
@ -162,6 +162,10 @@ module LogStash module Instrument
end
end
def has_metric?(*path)
@fast_lookup[path]
end
# Return all the individuals Metric,
# This call mimic a Enum's each if a block is provided
#
@ -179,9 +183,9 @@ module LogStash module Instrument
alias_method :all, :each
def prune(path)
key_paths = key_paths(path).map {|k| k.to_sym }
key_paths = key_paths(path).map(&:to_sym)
@structured_lookup_mutex.synchronize do
keys_to_delete = @fast_lookup.keys.select {|namespace, _| (key_paths - namespace).empty? }
keys_to_delete = @fast_lookup.keys.select {|namespace| (key_paths - namespace[0..-2]).empty? }
keys_to_delete.each {|k| @fast_lookup.delete(k) }
delete_from_map(@store, key_paths)
end

View file

@ -53,6 +53,20 @@ describe LogStash::Instrument::MetricStore do
end
end
context "#has_metric?" do
context "when the path exist" do
it "returns true" do
expect(subject.has_metric?(:node, :sashimi, :pipelines, :pipeline01, :plugins, :"logstash-output-elasticsearch", :event_in)).to be_truthy
end
end
context "when the path doesn't exist" do
it "returns false" do
expect(subject.has_metric?(:node, :sashimi, :pipelines, :pipeline01, :plugins, :"logstash-input-nothing")).to be_falsey
end
end
end
describe "#get" do
context "when the path exist" do
it "retrieves end of of a branch" do