fluent-plugin-geoip 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -56,7 +56,6 @@ $ sudo td-agent-gem install fluent-plugin-geoip
56
56
  latitude ${latitude["host"]}
57
57
  longitude ${longitude["host"]}
58
58
  country_code3 ${country_code3["host"]}
59
- country_code2 ${country_code2["host"]}
60
59
  country ${country_code["host"]}
61
60
  country_name ${country_name["host"]}
62
61
  dma ${dma_code["host"]}
@@ -189,7 +188,6 @@ Provides these placeholders for adding field of geolocate results.
189
188
  * ${latitude[lookup_field]}
190
189
  * ${longitude[lookup_field]}
191
190
  * ${country_code3[lookup_field]}
192
- * ${country_code2[lookup_field]}
193
191
  * ${country_code[lookup_field]}
194
192
  * ${country_name[lookup_field]}
195
193
  * ${dma_code[lookup_field]}
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "fluent-plugin-geoip"
7
- spec.version = "0.3.1"
7
+ spec.version = "0.4.0"
8
8
  spec.authors = ["Kentaro Yoshida"]
9
9
  spec.email = ["y.ken.studio@gmail.com"]
10
10
  spec.summary = %q{Fluentd Output plugin to add information about geographical location of IP addresses with Maxmind GeoIP databases.}
@@ -4,8 +4,9 @@ class Fluent::GeoipOutput < Fluent::BufferedOutput
4
4
  Fluent::Plugin.register_output('geoip', self)
5
5
 
6
6
  REGEXP_PLACEHOLDER_SINGLE = /^\$\{(?<geoip_key>-?[^\[]+)\[['"](?<record_key>-?[^'"]+)['"]\]\}$/
7
- REGEXP_PLACEHOLDER_SCAN = /(\$\{[^\}]+?\})/
8
- GEOIP_KEYS = %w(city latitude longitude country_code3 country_code2 country_code country_name dma_code area_code region)
7
+ REGEXP_PLACEHOLDER_SCAN = /['"]?(\$\{[^\}]+?\})['"]?/
8
+
9
+ GEOIP_KEYS = %w(city latitude longitude country_code3 country_code country_name dma_code area_code region)
9
10
 
10
11
  config_param :geoip_database, :string, :default => File.dirname(__FILE__) + '/../../../data/GeoLiteCity.dat'
11
12
  config_param :geoip_lookup_key, :string, :default => 'host'
@@ -59,7 +60,7 @@ class Fluent::GeoipOutput < Fluent::BufferedOutput
59
60
  conf.elements.select { |element| element.name == 'record' }.each { |element|
60
61
  element.each_pair { |k, v|
61
62
  element.has_key?(k) # to suppress unread configuration warning
62
- v = v[1..v.size-2] if quoted_json?(v)
63
+ v = v[1..v.size-2] if quoted_value?(v)
63
64
  @map[k] = v
64
65
  validate_json = Proc.new {
65
66
  begin
@@ -107,21 +108,13 @@ class Fluent::GeoipOutput < Fluent::BufferedOutput
107
108
  private
108
109
 
109
110
  def json?(text)
110
- text.match(/^\[.+\]$/) || text.match(/^\{.+\}$/) || text.match(/^[\d\.\-]+$/)
111
+ text.match(/^\[.+\]$/) || text.match(/^\{.+\}$/)
111
112
  end
112
113
 
113
- def quoted_json?(text)
114
+ def quoted_value?(text)
114
115
  # to improbe compatibility with fluentd v1-config
115
116
  trim_quote = text[1..text.size-2]
116
- text.match(/(^'.+'$|^".+"$)/) && (json_array_block?(trim_quote) || json_hash_block?(trim_quote))
117
- end
118
-
119
- def json_array_block?(text)
120
- text.match(/^\[[^\{]*?\{.+\}[^\}]*?\]$/)
121
- end
122
-
123
- def json_hash_block?(text)
124
- text.match(/^\{[^\{]*?\{.+\}[^\}]*?\}$/)
117
+ text.match(/(^'.+'$|^".+"$)/)
125
118
  end
126
119
 
127
120
  def add_geoip_field(record)
@@ -131,6 +124,7 @@ class Fluent::GeoipOutput < Fluent::BufferedOutput
131
124
  rewrited = placeholder[value]
132
125
  elsif json?(value)
133
126
  rewrited = value.gsub(REGEXP_PLACEHOLDER_SCAN) {|match|
127
+ match = match[1..match.size-2] if quoted_value?(match)
134
128
  Yajl::Encoder.encode(placeholder[match])
135
129
  }
136
130
  rewrited = parse_json(rewrited)
@@ -12,8 +12,8 @@ class GeoipOutputTest < Test::Unit::TestCase
12
12
  tag geoip.${tag}
13
13
  ]
14
14
 
15
- def create_driver(conf=CONFIG,tag='test')
16
- Fluent::Test::OutputTestDriver.new(Fluent::GeoipOutput, tag).configure(conf)
15
+ def create_driver(conf=CONFIG,tag='test',use_v1=false)
16
+ Fluent::Test::OutputTestDriver.new(Fluent::GeoipOutput, tag).configure(conf, use_v1)
17
17
  end
18
18
 
19
19
  def test_configure
@@ -327,35 +327,76 @@ class GeoipOutputTest < Test::Unit::TestCase
327
327
  assert_equal [nil, nil], emits[1][2]['string_array']
328
328
  end
329
329
 
330
+ CONFIG_QUOTED_RECORD = %[
331
+ geoip_lookup_key host
332
+ <record>
333
+ location_properties '{ "country_code" : "${country_code["host"]}", "lat": ${latitude["host"]}, "lon": ${longitude["host"]} }'
334
+ location_string ${latitude['host']},${longitude['host']}
335
+ location_string2 ${country_code["host"]}
336
+ location_array "[${longitude['host']},${latitude['host']}]"
337
+ location_array2 '[${longitude["host"]},${latitude["host"]}]'
338
+ peculiar_pattern '[GEOIP] message => {"lat":${latitude["host"]}, "lon":${longitude["host"]}}'
339
+ </record>
340
+ remove_tag_prefix input.
341
+ tag geoip.${tag}
342
+ ]
343
+
344
+ def test_emit_quoted_record
345
+ d1 = create_driver(CONFIG_QUOTED_RECORD, 'input.access')
346
+ d1.run do
347
+ d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
348
+ end
349
+ emits = d1.emits
350
+ assert_equal 1, emits.length
351
+ assert_equal 'geoip.access', emits[0][0] # tag
352
+ location_properties = { "country_code" => "US", "lat" => 37.4192008972168, "lon"=> -122.05740356445312 }
353
+ assert_equal location_properties, emits[0][2]['location_properties']
354
+ assert_equal '37.4192008972168,-122.05740356445312', emits[0][2]['location_string']
355
+ assert_equal 'US', emits[0][2]['location_string2']
356
+ assert_equal [-122.05740356445312, 37.4192008972168], emits[0][2]['location_array']
357
+ assert_equal [-122.05740356445312, 37.4192008972168], emits[0][2]['location_array2']
358
+ assert_equal '[GEOIP] message => {"lat":37.4192008972168, "lon":-122.05740356445312}', emits[0][2]['peculiar_pattern']
359
+ end
330
360
 
361
+ def test_emit_v1_config_compatibility
362
+ d1 = create_driver(CONFIG_QUOTED_RECORD, 'input.access', true)
363
+ d1.run do
364
+ d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
365
+ end
366
+ emits = d1.emits
367
+ assert_equal 1, emits.length
368
+ assert_equal 'geoip.access', emits[0][0] # tag
369
+ location_properties = { "country_code" => "US", "lat" => 37.4192008972168, "lon"=> -122.05740356445312 }
370
+ assert_equal location_properties, emits[0][2]['location_properties']
371
+ assert_equal '37.4192008972168,-122.05740356445312', emits[0][2]['location_string']
372
+ assert_equal 'US', emits[0][2]['location_string2']
373
+ assert_equal [-122.05740356445312, 37.4192008972168], emits[0][2]['location_array']
374
+ assert_equal [-122.05740356445312, 37.4192008972168], emits[0][2]['location_array2']
375
+ assert_equal '[GEOIP] message => {"lat":37.4192008972168, "lon":-122.05740356445312}', emits[0][2]['peculiar_pattern']
376
+ end
331
377
 
332
- def test_emit_v1_parser
378
+ def test_emit_multiline_v1_config
333
379
  d1 = create_driver(%[
334
380
  geoip_lookup_key host
335
381
  <record>
336
- location_properties '{ "lat": ${latitude["host"]}, "lon": ${longitude["host"]} }'
337
- location_string ${latitude['host']},${longitude['host']}
338
- location_string2 ${latitude["host"]},${longitude["host"]}
339
- location_array "[${longitude['host']},${latitude['host']}]"
340
- location_array2 '[${longitude["host"]},${latitude["host"]}]'
341
- peculiar_pattern [GEOIP] message => {"lat":${latitude['host']}, "lon":${longitude['host']}}
382
+ location_properties {
383
+ "city": "${city['host']}",
384
+ "country_code": "${country_code['host']}",
385
+ "latitude": "${latitude['host']}",
386
+ "longitude": "${longitude['host']}"
387
+ }
342
388
  </record>
343
389
  remove_tag_prefix input.
344
390
  tag geoip.${tag}
345
- ], 'input.access')
391
+ ], 'input.access', true)
346
392
  d1.run do
347
393
  d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
348
394
  end
349
395
  emits = d1.emits
350
396
  assert_equal 1, emits.length
351
397
  assert_equal 'geoip.access', emits[0][0] # tag
352
- location_properties = { "lat" => 37.4192008972168, "lon"=> -122.05740356445312 }
398
+ location_properties = { "city"=>"Mountain View", "country_code"=>"US", "latitude"=>37.4192008972168, "longitude"=>-122.05740356445312 }
353
399
  assert_equal location_properties, emits[0][2]['location_properties']
354
- assert_equal '37.4192008972168,-122.05740356445312', emits[0][2]['location_string']
355
- assert_equal '37.4192008972168,-122.05740356445312', emits[0][2]['location_string2']
356
- assert_equal [-122.05740356445312, 37.4192008972168], emits[0][2]['location_array']
357
- assert_equal [-122.05740356445312, 37.4192008972168], emits[0][2]['location_array2']
358
- assert_equal '[GEOIP] message => {"lat":37.4192008972168, "lon":-122.05740356445312}', emits[0][2]['peculiar_pattern']
359
400
  end
360
401
  end
361
402
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-geoip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-25 00:00:00.000000000 Z
12
+ date: 2014-11-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler