fluent-plugin-redis-counter 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.2.0
2
+ * count key can be built from JSON data contents.
3
+
1
4
  ## 0.1.1
2
5
  * add 'count_key_format' in order to make time-dependent redis keyname
3
6
  * bug fix
data/README.md CHANGED
@@ -35,7 +35,19 @@ fluent-plugin-redis-counter is hosted by [RubyGems.org](https://rubygems.org/).
35
35
  <pattern>
36
36
  match_status ^2[0-9][0-9]$
37
37
  count_key_format foo-statux2xx-%d
38
- localtime # time-zone(default: localtime, it can be "utc" or "localtime")
38
+ localtime # time-zone(default: localtime, it can be "utc" or "localtime")
39
+ </pattern>
40
+
41
+ # you can also use values from the matched JSON record in the key name, by using the
42
+ # syntax foo%_{key1}-%_{key2}, where key1 and key2 are keys in the JSON record that
43
+ # was received by fluentd.
44
+ # for example, "customer:%_{customer_id}:status2xx" will be formatted to
45
+ # "customer:123:status2xx" if the JSON record contains a key named "customer_id"
46
+ # with value 123, like so: {"status": 200, "customer_id": 123 ... }.
47
+ # these can be combined with the time formatting options in the previous example.
48
+ <pattern>
49
+ match_status ^2[0-9][0-9]$
50
+ count_key_format customer:%_{customer_id}:status2xx-%Y-%m-%d
39
51
  </pattern>
40
52
  </match>
41
53
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -3,11 +3,11 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "fluent-plugin-redis-counter"
6
- s.version = "0.1.1"
6
+ s.version = "0.2.0"
7
7
  s.description = "fluent-plugin-redis-counter is a fluent plugin to count-up/down redis keys."
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Buntaro Okada"]
10
- s.date = %q{2012-06-21}
10
+ s.date = %q{2013-02-19}
11
11
  s.email = "kbinani.bt@gmail.com"
12
12
  s.homepage = "https://github.com/kbinani/fluent-plugin-redis-counter"
13
13
  s.summary = "Redis counter plugin for fluent"
@@ -52,7 +52,7 @@ module Fluent
52
52
  @patterns.select { |pattern|
53
53
  pattern.is_match?(record)
54
54
  }.each{ |pattern|
55
- table[pattern.get_count_key(time)] += pattern.count_value
55
+ table[pattern.get_count_key(time, record)] += pattern.count_value
56
56
  }
57
57
  }
58
58
  rescue EOFError
@@ -69,6 +69,20 @@ module Fluent
69
69
  class RedisCounterException < Exception
70
70
  end
71
71
 
72
+ class RecordValueFormatter
73
+ attr_reader :format
74
+ def initialize(format)
75
+ @format = format
76
+ end
77
+
78
+ def key(record)
79
+ @format.gsub(/(%_\{[^\}]+\})/) do |s|
80
+ key = s.match(/\{([^\}]+)\}/)[1]
81
+ record[key]
82
+ end
83
+ end
84
+ end
85
+
72
86
  class Pattern
73
87
  attr_reader :matches, :count_value
74
88
 
@@ -90,7 +104,7 @@ module Fluent
90
104
  if conf_element.has_key?('utc')
91
105
  is_localtime = false
92
106
  end
93
- @count_key_format = TimeFormatter.new(conf_element['count_key_format'], is_localtime)
107
+ @count_key_format = [conf_element['count_key_format'], is_localtime]
94
108
  end
95
109
 
96
110
  @count_value = 1
@@ -120,11 +134,13 @@ module Fluent
120
134
  return true
121
135
  end
122
136
 
123
- def get_count_key(time)
137
+ def get_count_key(time, record)
124
138
  if @count_key_format == nil
125
139
  @count_key
126
140
  else
127
- @count_key_format.format(time)
141
+ count_key = RecordValueFormatter.new(@count_key_format[0]).key(record)
142
+ formatter = TimeFormatter.new(count_key, @count_key_format[1])
143
+ formatter.format(time)
128
144
  end
129
145
  end
130
146
  end
@@ -54,11 +54,11 @@ class RedisCounterTest < Test::Unit::TestCase
54
54
  assert_equal 2, driver.instance.patterns[0].matches.size
55
55
  assert_equal Regexp.new('^2[0-9]{2}$'), driver.instance.patterns[0].matches['status']
56
56
  assert_equal Regexp.new('^https'), driver.instance.patterns[0].matches['url']
57
- assert_equal 'status-normal', driver.instance.patterns[0].get_count_key(Time.now.to_i)
57
+ assert_equal 'status-normal', driver.instance.patterns[0].get_count_key(Time.now.to_i, {})
58
58
  assert_equal 1, driver.instance.patterns[0].count_value
59
59
 
60
60
  assert_equal 0, driver.instance.patterns[1].matches.size
61
- assert_equal 'foo', driver.instance.patterns[1].get_count_key(Time.now.to_i)
61
+ assert_equal 'foo', driver.instance.patterns[1].get_count_key(Time.now.to_i, {})
62
62
  assert_equal 2, driver.instance.patterns[1].count_value
63
63
  end
64
64
 
@@ -97,7 +97,7 @@ class RedisCounterTest < Test::Unit::TestCase
97
97
  </pattern>
98
98
  ]
99
99
  time = Time.parse('2011-06-21 03:12:01 UTC').to_i
100
- assert_equal 'foo-2011-06-21-03-12-01', driver.instance.patterns[0].get_count_key(time)
100
+ assert_equal 'foo-2011-06-21-03-12-01', driver.instance.patterns[0].get_count_key(time, {})
101
101
  end
102
102
 
103
103
  def test_configure_count_key_format_localtime
@@ -108,7 +108,7 @@ class RedisCounterTest < Test::Unit::TestCase
108
108
  </pattern>
109
109
  ]
110
110
  local_time = Time.parse('2012-06-21 03:12:00').to_i
111
- assert_equal 'foo-2012-06-21-03-12-00', driver.instance.patterns[0].get_count_key(local_time)
111
+ assert_equal 'foo-2012-06-21-03-12-00', driver.instance.patterns[0].get_count_key(local_time, {})
112
112
  end
113
113
 
114
114
  def test_configure_duplicated_timezone
@@ -126,6 +126,18 @@ class RedisCounterTest < Test::Unit::TestCase
126
126
  end
127
127
  end
128
128
 
129
+ def test_configure_count_key_format_with_record_value_formatter
130
+ driver = create_driver %[
131
+ <pattern>
132
+ count_key_format %_{prefix}-foo-%Y-%m-%_{type}-%_{customer_id}
133
+ localtime
134
+ </pattern>
135
+ ]
136
+ local_time = Time.parse('2012-06-21 03:12:00').to_i
137
+ record = {'prefix' => 'pre', 'type' => 'bar', 'customer_id' => 321}
138
+ assert_equal 'pre-foo-2012-06-bar-321', driver.instance.patterns[0].get_count_key(local_time, record)
139
+ end
140
+
129
141
  def test_configure_invalid_count_value
130
142
  begin
131
143
  create_driver %[
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-redis-counter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.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: 2012-06-21 00:00:00.000000000 Z
12
+ date: 2013-02-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fluentd
@@ -80,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
80
  version: '0'
81
81
  requirements: []
82
82
  rubyforge_project:
83
- rubygems_version: 1.8.24
83
+ rubygems_version: 1.8.25
84
84
  signing_key:
85
85
  specification_version: 3
86
86
  summary: Redis counter plugin for fluent