fluent-plugin-gcloud-pubsub-custom 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 76c67cd4a1690b17057f8961cc06136afe5ae3d56adfc6dc37c49190c845368b
4
- data.tar.gz: f0cf742945f94fcfc05e2364ad4881dce89c1bc6dfc723143236390ae8a90a53
3
+ metadata.gz: 940568295dd0772bb0e7f0fb2537c6fff1b95f6fcc2e8716b578c512529e808f
4
+ data.tar.gz: 9f2b817300f920541942d03a207290a833a945613611b0a25e72db52f39f9a16
5
5
  SHA512:
6
- metadata.gz: 671c29ebbef19b4155100aa6bedfdb686ee2bb79310e8c5ccb553806fde095cbd6c3ea5ff225eecfc29f6607ffc270205490873e564d3b2dfc713ce190cb26c0
7
- data.tar.gz: 158b3b2aa6f5f59e5fe0d8e8cfe6e1dbb2cf1e49541a6d0b8d9e21e15f0d4f8a536ea120bc76ef255a8b66c250ca1c92351bfa087c82eac00851a2f2b033307d
6
+ metadata.gz: 2911ef120d13bf8258981afa08d3124a01c04e88bf6af25f873c54f2eec1220f22b6473b24cfc6bbf750321933b28488c2f80a7a1b7459f9faa846044dca5e30
7
+ data.tar.gz: 8ce43148345570ef849d32097da05648daeae198feac5794c15cabb9272e0471d0add6409ae2a1889d2eb5e812786618eeab5c4d6317ae7eff6e05ad4d333ce0
@@ -1,5 +1,9 @@
1
1
  ## ChangeLog
2
2
 
3
+ ### Release 1.3.0 - 2018/05/18
4
+
5
+ - Make attributes available
6
+
3
7
  ### Release 1.2.0 - 2018/04/24
4
8
 
5
9
  - Output plugin
data/README.md CHANGED
@@ -91,6 +91,8 @@ Use `gcloud_pubsub` output plugin.
91
91
  - See https://cloud.google.com/pubsub/quotas#other_limits
92
92
  - `max_message_size` (optional, default: `4000000` = `4MB`)
93
93
  - Messages exceeding `max_message_size` are not published because Pub/Sub clients cannot receive it.
94
+ - `attribute_keys` (optional, default: `[]`)
95
+ - Publishing the set fields as attributes.
94
96
 
95
97
  ### Pull messages
96
98
 
@@ -144,6 +146,8 @@ Use `gcloud_pubsub` input plugin.
144
146
  - Pulling messages by intervals of specified seconds.
145
147
  - `pull_threads` (optional, default: `1`)
146
148
  - Set number of threads to pull messages.
149
+ - `attribute_keys` (optional, default: `[]`)
150
+ - Specify the key of the attribute to be emitted as the field of record.
147
151
  - `parse_error_action` (optional, default: `exception`)
148
152
  - Set error type when parsing messages fails.
149
153
  - `exception`: Raise exception. Messages are not acknowledged.
@@ -7,7 +7,7 @@ Gem::Specification.new do |gem|
7
7
  gem.license = "MIT"
8
8
  gem.homepage = "https://github.com/mia-0032/fluent-plugin-gcloud-pubsub-custom"
9
9
  gem.summary = gem.description
10
- gem.version = "1.2.0"
10
+ gem.version = "1.3.0"
11
11
  gem.authors = ["Yoshihiro MIYAI"]
12
12
  gem.email = "msparrow17@gmail.com"
13
13
  gem.has_rdoc = false
@@ -7,6 +7,22 @@ module Fluent
7
7
  class RetryableError < Error
8
8
  end
9
9
 
10
+ class Message
11
+ attr_reader :message, :attributes
12
+ def initialize(message, attributes={})
13
+ @message = message
14
+ @attributes = attributes
15
+ end
16
+
17
+ def bytesize()
18
+ attr_size = 0
19
+ @attributes.each do |key, val|
20
+ attr_size += key.bytesize + val.bytesize
21
+ end
22
+ @message.bytesize + attr_size
23
+ end
24
+ end
25
+
10
26
  class Publisher
11
27
  def initialize(project, key, autocreate_topic)
12
28
  @pubsub = Google::Cloud::Pubsub.new project_id: project, credentials: key
@@ -32,7 +48,7 @@ module Fluent
32
48
  def publish(topic_name, messages)
33
49
  topic(topic_name).publish do |batch|
34
50
  messages.each do |m|
35
- batch.publish m
51
+ batch.publish m.message, m.attributes
36
52
  end
37
53
  end
38
54
  rescue Google::Cloud::UnavailableError, Google::Cloud::DeadlineExceededError, Google::Cloud::InternalError => ex
@@ -37,6 +37,8 @@ module Fluent::Plugin
37
37
  config_param :return_immediately, :bool, default: true
38
38
  desc 'Set number of threads to pull messages.'
39
39
  config_param :pull_threads, :integer, default: 1
40
+ desc 'Specify the key of the attribute to be acquired as a record'
41
+ config_param :attribute_keys, :array, default: []
40
42
  desc 'Set error type when parsing messages fails.'
41
43
  config_param :parse_error_action, :enum, default: :exception, list: [:exception, :warning]
42
44
  # for HTTP RPC
@@ -220,8 +222,13 @@ module Fluent::Plugin
220
222
 
221
223
  messages.each do |m|
222
224
  line = m.message.data.chomp
225
+ attributes = m.attributes
223
226
  @parser.parse(line) do |time, record|
224
227
  if time && record
228
+ @attribute_keys.each do |key|
229
+ record[key] = attributes[key]
230
+ end
231
+
225
232
  event_streams[@extract_tag.call(record)].add(time, record)
226
233
  else
227
234
  case @parse_error_action
@@ -27,6 +27,8 @@ module Fluent::Plugin
27
27
  config_param :max_total_size, :integer, :default => 9800000 # 9.8MB
28
28
  desc 'Limit bytesize per message.'
29
29
  config_param :max_message_size, :integer, :default => 4000000 # 4MB
30
+ desc 'Publishing the set field as an attribute'
31
+ config_param :attribute_keys, :array, :default => []
30
32
 
31
33
  config_section :buffer do
32
34
  config_set_default :@type, DEFAULT_BUFFER_TYPE
@@ -50,7 +52,11 @@ module Fluent::Plugin
50
52
 
51
53
  def format(tag, time, record)
52
54
  record = inject_values_to_record(tag, time, record)
53
- @formatter.format(tag, time, record).to_msgpack
55
+ attributes = {}
56
+ @attribute_keys.each do |key|
57
+ attributes[key] = record.delete(key)
58
+ end
59
+ [@formatter.format(tag, time, record), attributes].to_msgpack
54
60
  end
55
61
 
56
62
  def formatted_to_msgpack_binary?
@@ -67,7 +73,8 @@ module Fluent::Plugin
67
73
  messages = []
68
74
  size = 0
69
75
 
70
- chunk.msgpack_each do |msg|
76
+ chunk.msgpack_each do |msg, attr|
77
+ msg = Fluent::GcloudPubSub::Message.new(msg, attr)
71
78
  if msg.bytesize > @max_message_size
72
79
  log.warn 'Drop a message because its size exceeds `max_message_size`', size: msg.bytesize
73
80
  next
@@ -18,13 +18,16 @@ class GcloudPubSubInputTest < Test::Unit::TestCase
18
18
 
19
19
  class DummyInvalidMsgData
20
20
  def data
21
- return 'foo:bar'
21
+ 'foo:bar'
22
22
  end
23
23
  end
24
24
  class DummyInvalidMessage
25
25
  def message
26
26
  DummyInvalidMsgData.new
27
27
  end
28
+ def attributes
29
+ {"attr_1" => "a", "attr_2" => "b"}
30
+ end
28
31
  end
29
32
 
30
33
  def create_driver(conf=CONFIG)
@@ -53,6 +56,7 @@ class GcloudPubSubInputTest < Test::Unit::TestCase
53
56
  return_immediately true
54
57
  pull_interval 2
55
58
  pull_threads 3
59
+ attribute_keys attr-test
56
60
  enable_rpc true
57
61
  rpc_bind 127.0.0.1
58
62
  rpc_port 24681
@@ -67,6 +71,7 @@ class GcloudPubSubInputTest < Test::Unit::TestCase
67
71
  assert_equal(1000, d.instance.max_messages)
68
72
  assert_equal(true, d.instance.return_immediately)
69
73
  assert_equal(3, d.instance.pull_threads)
74
+ assert_equal(['attr-test'], d.instance.attribute_keys)
70
75
  assert_equal(true, d.instance.enable_rpc)
71
76
  assert_equal('127.0.0.1', d.instance.rpc_bind)
72
77
  assert_equal(24681, d.instance.rpc_port)
@@ -78,6 +83,7 @@ class GcloudPubSubInputTest < Test::Unit::TestCase
78
83
  assert_equal(100, d.instance.max_messages)
79
84
  assert_equal(true, d.instance.return_immediately)
80
85
  assert_equal(1, d.instance.pull_threads)
86
+ assert_equal([], d.instance.attribute_keys)
81
87
  assert_equal(false, d.instance.enable_rpc)
82
88
  assert_equal('0.0.0.0', d.instance.rpc_bind)
83
89
  assert_equal(24680, d.instance.rpc_port)
@@ -126,13 +132,16 @@ class GcloudPubSubInputTest < Test::Unit::TestCase
126
132
  sub_test_case 'emit' do
127
133
  class DummyMsgData
128
134
  def data
129
- return '{"foo": "bar"}'
135
+ '{"foo": "bar"}'
130
136
  end
131
137
  end
132
138
  class DummyMessage
133
139
  def message
134
140
  DummyMsgData.new
135
141
  end
142
+ def attributes
143
+ {"attr_1" => "a", "attr_2" => "b"}
144
+ end
136
145
  end
137
146
 
138
147
  class DummyMsgDataWithTagKey
@@ -140,7 +149,7 @@ class GcloudPubSubInputTest < Test::Unit::TestCase
140
149
  @tag = tag
141
150
  end
142
151
  def data
143
- return '{"foo": "bar", "test_tag_key": "' + @tag + '"}'
152
+ '{"foo": "bar", "test_tag_key": "' + @tag + '"}'
144
153
  end
145
154
  end
146
155
  class DummyMessageWithTagKey
@@ -150,6 +159,9 @@ class GcloudPubSubInputTest < Test::Unit::TestCase
150
159
  def message
151
160
  DummyMsgDataWithTagKey.new @tag
152
161
  end
162
+ def attributes
163
+ {"attr_1" => "a", "attr_2" => "b"}
164
+ end
153
165
  end
154
166
 
155
167
  setup do
@@ -235,6 +247,22 @@ class GcloudPubSubInputTest < Test::Unit::TestCase
235
247
  assert_true d.events.empty?
236
248
  end
237
249
 
250
+ test 'with attributes' do
251
+ messages = Array.new(1, DummyMessage.new)
252
+ @subscriber.pull(immediate: true, max: 100).at_least(1) { messages }
253
+ @subscriber.acknowledge(messages).at_least(1)
254
+
255
+ d = create_driver("#{CONFIG}\nattribute_keys attr_1")
256
+ d.run(expect_emits: 1, timeout: 3)
257
+ emits = d.events
258
+
259
+ assert(1 <= emits.length)
260
+ emits.each do |tag, time, record|
261
+ assert_equal("test", tag)
262
+ assert_equal({"foo" => "bar", "attr_1" => "a"}, record)
263
+ end
264
+ end
265
+
238
266
  test 'invalid messages with parse_error_action warning' do
239
267
  messages = Array.new(1, DummyInvalidMessage.new)
240
268
  @subscriber.pull(immediate: true, max: 100).at_least(1) { messages }
@@ -220,7 +220,7 @@ class GcloudPubSubOutputTest < Test::Unit::TestCase
220
220
  d.run(default_tag: 'test') do
221
221
  d.feed({"foo" => "bar"})
222
222
  end
223
- assert_equal({"tag" => 'test', "foo" => "bar"}, JSON.parse(MessagePack.unpack(d.formatted.first)))
223
+ assert_equal({"tag" => 'test', "foo" => "bar"}, JSON.parse(MessagePack.unpack(d.formatted.first)[0]))
224
224
  end
225
225
  end
226
226
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-gcloud-pubsub-custom
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshihiro MIYAI
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-24 00:00:00.000000000 Z
11
+ date: 2018-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fluentd