Skip to content

Commit

Permalink
Update this plugin to the v2.0 of the plugin api
Browse files Browse the repository at this point in the history
  • Loading branch information
ph committed May 4, 2016
1 parent 0d425bc commit b0319ae
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 27 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
---
sudo: false
language: ruby
cache: bundler
rvm:
- jruby-1.7.24
- jruby-1.7.25
script: bundle exec rspec spec && bundle exec rspec spec --tag integration
jdk: oraclejdk8
before_install:
- git clone -b feature/event_interface https://github.com/elastic/logstash
6 changes: 5 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
source 'https://rubygems.org'
gemspec
gemspec
gem "logstash-core", :path => "./logstash/logstash-core"
gem "logstash-core-plugin-api", :path => "./logstash/logstash-core-plugin-api"
gem "logstash-core-event-java", :path => "./logstash/logstash-core-event-java"
gem "logstash-devutils", :github => "elastic/logstash-devutils", :branch => "feature/plugin-api-2_0"
22 changes: 11 additions & 11 deletions lib/logstash/filters/useragent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def register
end

def filter(event)
useragent = event[@source]
useragent = event.get(@source)
useragent = useragent.first if useragent.is_a?(Array)

return if useragent.nil? || useragent.empty?
Expand Down Expand Up @@ -135,31 +135,31 @@ def lookup_useragent(useragent)
def set_fields(event, ua_data)
# UserAgentParser outputs as US-ASCII.

event[@prefixed_name] = ua_data.name.dup.force_encoding(Encoding::UTF_8)
event.set(@prefixed_name, ua_data.name.dup.force_encoding(Encoding::UTF_8))

#OSX, Andriod and maybe iOS parse correctly, ua-agent parsing for Windows does not provide this level of detail

# Calls in here use #dup because there's potential for later filters to modify these values
# and corrupt the cache. See uap source here for details https://github.com/ua-parser/uap-ruby/tree/master/lib/user_agent_parser
if (os = ua_data.os)
# The OS is a rich object
event[@prefixed_os] = ua_data.os.to_s.dup.force_encoding(Encoding::UTF_8)
event[@prefixed_os_name] = os.name.dup.force_encoding(Encoding::UTF_8) if os.name
event.set(@prefixed_os, ua_data.os.to_s.dup.force_encoding(Encoding::UTF_8))
event.set(@prefixed_os_name, os.name.dup.force_encoding(Encoding::UTF_8)) if os.name

# These are all strings
if (os_version = os.version)
event[@prefixed_os_major] = os_version.major.dup.force_encoding(Encoding::UTF_8) if os_version.major
event[@prefixed_os_minor] = os_version.minor.dup.force_encoding(Encoding::UTF_8) if os_version.minor
event.set(@prefixed_os_major, os_version.major.dup.force_encoding(Encoding::UTF_8)) if os_version.major
event.set(@prefixed_os_minor, os_version.minor.dup.force_encoding(Encoding::UTF_8)) if os_version.minor
end
end

event[@prefixed_device] = ua_data.device.to_s.dup.force_encoding(Encoding::UTF_8) if ua_data.device
event.set(@prefixed_device, ua_data.device.to_s.dup.force_encoding(Encoding::UTF_8)) if ua_data.device

if (ua_version = ua_data.version)
event[@prefixed_major] = ua_version.major.dup.force_encoding(Encoding::UTF_8) if ua_version.major
event[@prefixed_minor] = ua_version.minor.dup.force_encoding(Encoding::UTF_8) if ua_version.minor
event[@prefixed_patch] = ua_version.patch.dup.force_encoding(Encoding::UTF_8) if ua_version.patch
event[@prefixed_build] = ua_version.patch_minor.dup.force_encoding(Encoding::UTF_8) if ua_version.patch_minor
event.set(@prefixed_major, ua_version.major.dup.force_encoding(Encoding::UTF_8)) if ua_version.major
event.set(@prefixed_minor, ua_version.minor.dup.force_encoding(Encoding::UTF_8)) if ua_version.minor
event.set(@prefixed_patch, ua_version.patch.dup.force_encoding(Encoding::UTF_8)) if ua_version.patch
event.set(@prefixed_build, ua_version.patch_minor.dup.force_encoding(Encoding::UTF_8)) if ua_version.patch_minor
end
end
end
2 changes: 1 addition & 1 deletion logstash-filter-useragent.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |s|
s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }

# Gem dependencies
s.add_runtime_dependency "logstash-core-plugin-api", "~> 1.0"
s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0"

s.add_runtime_dependency 'user_agent_parser', ['>= 2.0.0']
s.add_runtime_dependency 'lru_redux', "~> 1.1.0"
Expand Down
26 changes: 13 additions & 13 deletions spec/filters/useragent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

sample "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31" do
insist { subject }.include?("ua")
insist { subject["ua"]["name"] } == "Chrome"
insist { subject["ua"]["os"] } == "Linux"
insist { subject["ua"]["major"] } == "26"
insist { subject["ua"]["minor"] } == "0"
insist { subject.get("[ua][name]") } == "Chrome"
insist { subject.get("[ua][os]") } == "Linux"
insist { subject.get("[ua][major]") } == "26"
insist { subject.get("[ua][minor]") } == "0"
end
end

Expand All @@ -34,10 +34,10 @@
CONFIG

sample "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31" do
insist { subject["name"] } == "Chrome"
insist { subject["os"] } == "Linux"
insist { subject["major"] } == "26"
insist { subject["minor"] } == "0"
insist { subject.get("name") } == "Chrome"
insist { subject.get("os") } == "Linux"
insist { subject.get("major") } == "26"
insist { subject.get("minor") } == "0"
end
end

Expand Down Expand Up @@ -91,7 +91,7 @@
}.each do |field, uad_getter|
context "for the #{field} field" do
let(:value) {uad_getter.call(ua_data)}
let(:target_field) { target[field]}
let(:target_field) { target.get(field)}

it "should not have a nil value" do
expect(target_field).to be_truthy
Expand Down Expand Up @@ -120,10 +120,10 @@

sample "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31" do
insist { subject.to_hash }.include?("message")
insist { subject["message"]["name"] } == "Chrome"
insist { subject["message"]["os"] } == "Linux"
insist { subject["message"]["major"] } == "26"
insist { subject["message"]["minor"] } == "0"
insist { subject.get("[message][name]") } == "Chrome"
insist { subject.get("[message][os]") } == "Linux"
insist { subject.get("[message][major]") } == "26"
insist { subject.get("[message][minor]") } == "0"
end
end
end

0 comments on commit b0319ae

Please sign in to comment.