-
Notifications
You must be signed in to change notification settings - Fork 29
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
Support for multiple tags #31
Conversation
…-output-loggly # Conflicts: # logstash-output-loggly.gemspec
Thanks for taking the time to work on this. Currently it's already possible to specify multiple tags, as mentioned in the doc. What's the issue you're trying to fix? Interpolation like the following should work as well, even with multiple tags:
Or have you encountered issues where this didn't work as expected? |
That didn't work for me (defaut tag ("logstash") was overriding). |
The problem likely comes from your field If I start Logstash with the following: bin/logstash -e "input { stdin {} generator { count => 1 } }
output { loggly { key => \"$LOGGLY_KEY\" tag => 'logstash,tag_%{sequence}' } } The generator plugin will generate one event with field However if I type another message, the |
However I would like it if you could do a few things in your PR, prior to merging this.
If you're not familiar with RSpec, our testing framework, you can always check this site, the Expectations and Mocks sections will likely help you figure out how to start from our existing tests and add new ones. |
@webmat, thanks, I'll look into your your example. |
@chergey Also, could you make sure you sign our Contributor License Agreement, please? https://www.elastic.co/contributor-agreement |
@webmat, how about we remove default tag ('logstash') ? If no tags supplied or failed interpolation, then no tag will result, e.g. %{tag1},tag2 would be 'tag2' (field 'tag1' doesn't exist) |
Yes that makes sense. Make sure to still default to 'logstash' if ever the final tag is an empty string (one or more interpolations failed + no literal tag). An example to make sure I'm clear:
Should end up with tag |
In other words, inside the loop, just don't add the tag if it didn't interpolate correctly (e.g. if it still matches the regex). And only after the loop do you check whether it's necessary to default to 'logstash' (if array is empty). |
@webmat, why still default to 'logstash' (if it didn't interpolate correctly) ? We can send events without any tag at all i.e. http://logs-01.loggly.com/inputs/TOKEN. |
Ah I was not aware that the tag was optional. Their documentation doesn't clearly state whether the tag is required or can be omitted. If that is so, I don't mind sending with no tag. Make sure that the URL will be adjusted accordingly (I would assume that "...TOKEN/tag/" with no tags won't work. And document the new behaviour in the comments for the attribute and in docs/index.asciidoc. GitHub renders asciidoc just like markdown, so you can check that what you did is fine (even if GitHub doesn't render the includes & so on at the top of the file). |
@webmat, please comment on my commit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay. Hectic past few days over here :-)
Good work overall. I have a few requests, but once these are taken care of, I think we'll be ready to merge and release :-)
You did specifically confirm that it's fine to send API calls without the tag/#{tag}
, correct?
lib/logstash/outputs/loggly.rb
Outdated
# For those cases where %{somefield} doesn't exist we don't include it | ||
if !/%{\w+}/.match(t) | ||
tag_array.push(t) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will not add tags that failed interpolation, this is good.
However this would still add empty string tags to the array, in cases where an interpolated field contains an empty string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also minor code convention nitpick. Avoid if !
and use unless
instead.
lib/logstash/outputs/loggly.rb
Outdated
|
||
if expected_field = key[/%{(.*)}/, 1] | ||
@logger.warn "Skipping sending message to Loggly. No key provided (key='#{key}'). Make sure to set field '#{expected_field}'." | ||
@logger.debug "Dropped message", :event => event.to_json | ||
return nil | ||
end | ||
|
||
tag=tag_array.join(",") | ||
tag = nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Declaring the variable outside the if is not necessary outside of control blocks like if
. This is only necessary for blocks like do...end
.
spec/outputs/loggly_spec.rb
Outdated
it 'should set the default tag to logstash' do | ||
expect(output).to receive(:send_batch).with([{event: event.to_hash, key: 'abcdef123456', tag: 'logstash'}]) | ||
output.receive(event) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test for the default behaviour should not be removed. Rather it should be adapted to show the new default behaviour (no tag and the URI ending in /bulk/#{key}
without the /tag/#{tag}
).
lib/logstash/outputs/loggly.rb
Outdated
url = "#{@proto}://#{@host}/bulk/#{key}/tag/#{tag}" | ||
else | ||
url = "#{@proto}://#{@host}/bulk/#{key}" | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once again, avoid if !
. unless...else
is not better, though. So perhaps use something like this instead?
if tag.nil?
url = "#{@proto}://#{@host}/bulk/#{key}"
else
url = "#{@proto}://#{@host}/bulk/#{key}/tag/#{tag}"
end
spec/outputs/loggly_spec.rb
Outdated
@@ -60,9 +56,9 @@ def logger_for(plugin) | |||
output.receive(event) | |||
end | |||
|
|||
it 'should default tag to logstash if interpolated field for tag does not exist' do | |||
it 'should send no tag to logstash if interpolated field for tag does not exist' do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initial wording (before your contribution) was a bit wonky. Could you remove the inaccurate "to logstash", and perhaps have this instead?
it 'should have no tag if the interpolated field for the tag does not exist' do
spec/outputs/loggly_spec.rb
Outdated
@@ -110,6 +106,46 @@ def logger_for(plugin) | |||
end | |||
end | |||
|
|||
context 'prepare tags' do | |||
it 'failed interpolation' do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your context
and it
strings should read more like sentences that describe the situation you're setting up (context) and intended result (it).
Here's an example for the two lines above:
context 'when computing the tags'
it 'should not leave a failed interpolation in the tag list' do
then of course all following it
blocks should be clarified to describe expected behaviour of each test case as well.
spec/outputs/loggly_spec.rb
Outdated
meta_event = output.send :prepare_meta, event | ||
expect(meta_event[:tag]).to eq('some_value') | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good coverage of the various scenarios, however. Love it 💯
Yes, we have our api in java that works just fine. Besides, I re-checked it myself with rest client.
You sure, it's ok ? Intellij IDEA Ruby plugin complains. |
Excellent. Once again, thanks a lot for your contribution! I will merge and release the next version of the plugin. |
Ah I have a small bit of housekeeping for you first, though. I see you've signed our CLA with your email |
Done. |
Thanks, I will be releasing this shortly! |
@chergey Version 6.0.0 with your changes is finally out (after a few internal back & forths :-) ). Once again, thanks a lot for taking the time to contribute! |
I added support for multiple tags.