fluent-plugin-google-cloud 0.8.2 → 0.8.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,75 @@
1
+ # Copyright 2020 Google Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # Additional assert functions.
16
+ module Asserts
17
+ # For an optional field with default values, Protobuf omits the field when it
18
+ # is deserialized to json. So we need to add an extra check for gRPC which
19
+ # uses Protobuf.
20
+ #
21
+ # An optional block can be passed in if we need to assert something other than
22
+ # a plain equal. e.g. assert_in_delta.
23
+ def assert_equal_with_default(_field, _expected_value, _default_value, _entry)
24
+ _undefined
25
+ end
26
+
27
+ # Compare the timestamp seconds and nanoseconds with the expected timestamp.
28
+ def assert_timestamp_matches(expected_ts, ts_secs, ts_nanos, entry)
29
+ assert_equal expected_ts.tv_sec, ts_secs, entry
30
+ # Fluentd v0.14 onwards supports nanosecond timestamp values.
31
+ # Added in 600 ns delta to avoid flaky tests introduced
32
+ # due to rounding error in double-precision floating-point numbers
33
+ # (to account for the missing 9 bits of precision ~ 512 ns).
34
+ # See http://wikipedia.org/wiki/Double-precision_floating-point_format.
35
+ assert_in_delta expected_ts.tv_nsec, ts_nanos, 600, entry
36
+ end
37
+
38
+ def assert_prometheus_metric_value(metric_name, expected_value, labels = {})
39
+ metric = Prometheus::Client.registry.get(metric_name)
40
+ assert_not_nil(metric)
41
+ metric_value = if labels == :aggregate
42
+ # Sum up all metric values regardless of the labels.
43
+ metric.values.values.reduce(0.0, :+)
44
+ else
45
+ metric.get(labels)
46
+ end
47
+ assert_equal(expected_value, metric_value)
48
+ end
49
+
50
+ def assert_opencensus_metric_value(metric_name, expected_value, labels = {})
51
+ translator = Monitoring::MetricTranslator.new(metric_name, labels)
52
+ metric_name = translator.name
53
+ labels = translator.translate_labels(labels)
54
+ # The next line collapses the labels to assert against the aggregated data,
55
+ # which can have some labels removed. Without this, it would test against
56
+ # the raw data. The view is more representative of the user experience, even
57
+ # though both tests should work because currently we only aggregate away one
58
+ # label that never changes during runtime.
59
+ labels.select! { |k, _| translator.view_labels.include? k }
60
+ labels = labels.map { |k, v| [k.to_s, v.to_s] }.to_h
61
+ stats_recorder = OpenCensus::Stats.ensure_recorder
62
+ view_data = stats_recorder.view_data metric_name
63
+ assert_not_nil(view_data)
64
+ # For now assume all metrics are counters.
65
+ assert_kind_of(OpenCensus::Stats::Aggregation::Sum,
66
+ view_data.view.aggregation)
67
+ assert_true(view_data.view.measure.int64?)
68
+ tag_values = view_data.view.columns.map { |column| labels[column] }
69
+ metric_value = 0
70
+ if view_data.data.key? tag_values
71
+ metric_value = view_data.data[tag_values].value
72
+ end
73
+ assert_equal(expected_value, metric_value)
74
+ end
75
+ end
@@ -22,6 +22,7 @@ require 'mocha/test_unit'
22
22
  require 'webmock/test_unit'
23
23
  require 'prometheus/client'
24
24
 
25
+ require_relative 'asserts'
25
26
  require_relative 'constants'
26
27
 
27
28
  module Monitoring
@@ -35,6 +36,7 @@ end
35
36
 
36
37
  # Unit tests for Google Cloud Logging plugin
37
38
  module BaseTest
39
+ include Asserts
38
40
  include Constants
39
41
 
40
42
  def setup
@@ -105,7 +107,7 @@ module BaseTest
105
107
 
106
108
  def test_configure_metadata_missing_parts_on_other_platforms
107
109
  setup_no_metadata_service_stubs
108
- Fluent::GoogleCloudOutput::CredentialsInfo.stubs(:project_id).returns(nil)
110
+ Common::Utils::CredentialsInfo.stubs(:project_id).returns(nil)
109
111
  [[CONFIG_MISSING_METADATA_PROJECT_ID, ['project_id'], false],
110
112
  [CONFIG_MISSING_METADATA_ZONE, [], true],
111
113
  [CONFIG_MISSING_METADATA_VM_ID, [], true],
@@ -2534,65 +2536,6 @@ module BaseTest
2534
2536
  _undefined
2535
2537
  end
2536
2538
 
2537
- # For an optional field with default values, Protobuf omits the field when it
2538
- # is deserialized to json. So we need to add an extra check for gRPC which
2539
- # uses Protobuf.
2540
- #
2541
- # An optional block can be passed in if we need to assert something other than
2542
- # a plain equal. e.g. assert_in_delta.
2543
- def assert_equal_with_default(_field, _expected_value, _default_value, _entry)
2544
- _undefined
2545
- end
2546
-
2547
- # Compare the timestamp seconds and nanoseconds with the expected timestamp.
2548
- def assert_timestamp_matches(expected_ts, ts_secs, ts_nanos, entry)
2549
- assert_equal expected_ts.tv_sec, ts_secs, entry
2550
- # Fluentd v0.14 onwards supports nanosecond timestamp values.
2551
- # Added in 600 ns delta to avoid flaky tests introduced
2552
- # due to rounding error in double-precision floating-point numbers
2553
- # (to account for the missing 9 bits of precision ~ 512 ns).
2554
- # See http://wikipedia.org/wiki/Double-precision_floating-point_format.
2555
- assert_in_delta expected_ts.tv_nsec, ts_nanos, 600, entry
2556
- end
2557
-
2558
- def assert_prometheus_metric_value(metric_name, expected_value, labels = {})
2559
- metric = Prometheus::Client.registry.get(metric_name)
2560
- assert_not_nil(metric)
2561
- metric_value = if labels == :aggregate
2562
- # Sum up all metric values regardless of the labels.
2563
- metric.values.values.reduce(0.0, :+)
2564
- else
2565
- metric.get(labels)
2566
- end
2567
- assert_equal(expected_value, metric_value)
2568
- end
2569
-
2570
- def assert_opencensus_metric_value(metric_name, expected_value, labels = {})
2571
- translator = Monitoring::MetricTranslator.new(metric_name, labels)
2572
- metric_name = translator.name
2573
- labels = translator.translate_labels(labels)
2574
- # The next line collapses the labels to assert against the aggregated data,
2575
- # which can have some labels removed. Without this, it would test against
2576
- # the raw data. The view is more representative of the user experience, even
2577
- # though both tests should work because currently we only aggregate away one
2578
- # label that never changes during runtime.
2579
- labels.select! { |k, _| translator.view_labels.include? k }
2580
- labels = labels.map { |k, v| [k.to_s, v.to_s] }.to_h
2581
- stats_recorder = OpenCensus::Stats.ensure_recorder
2582
- view_data = stats_recorder.view_data metric_name
2583
- assert_not_nil(view_data)
2584
- # For now assume all metrics are counters.
2585
- assert_kind_of(OpenCensus::Stats::Aggregation::Sum,
2586
- view_data.view.aggregation)
2587
- assert_true(view_data.view.measure.int64?)
2588
- tag_values = view_data.view.columns.map { |column| labels[column] }
2589
- metric_value = 0
2590
- if view_data.data.key? tag_values
2591
- metric_value = view_data.data[tag_values].value
2592
- end
2593
- assert_equal(expected_value, metric_value)
2594
- end
2595
-
2596
2539
  # Defined in specific gRPC or REST files.
2597
2540
  def expected_operation_message2
2598
2541
  _undefined
@@ -45,7 +45,7 @@ end
45
45
 
46
46
  # Constants used by unit tests for Google Cloud Logging plugin.
47
47
  module Constants
48
- include Fluent::GoogleCloudOutput::ServiceConstants
48
+ include Common::ServiceConstants
49
49
  include Fluent::GoogleCloudOutput::ConfigConstants
50
50
  include Fluent::GoogleCloudOutput::InternalConstants
51
51
 
@@ -471,6 +471,14 @@ module Constants
471
471
  zone asia-east2
472
472
  ).freeze
473
473
 
474
+ # For analyze_config.
475
+ CONFIG_ANALYZE_CONFIG = %(
476
+ google_fluentd_config_path \
477
+ test/plugin/data/google-fluentd-custom.conf
478
+ google_fluentd_baseline_config_path \
479
+ test/plugin/data/google-fluentd-baseline.conf
480
+ ).freeze
481
+
474
482
  # Service configurations for various services.
475
483
 
476
484
  # GCE.
@@ -0,0 +1,24 @@
1
+ <source>
2
+ @type syslog
3
+ port 514
4
+ protocol_type tcp
5
+ bind 127.0.0.1
6
+ tag syslog
7
+ </source>
8
+
9
+ <source>
10
+ @type tail
11
+ path /var/log/apache*/access.log,/var/log/apache*/access_log,/var/log/httpd/access.log,/var/log/httpd/access_log
12
+ pos_file /var/lib/google-fluentd/pos/apache-access.pos
13
+ tag apache-access
14
+ </source>
15
+
16
+ <filter **>
17
+ @type add_insert_ids
18
+ </filter>
19
+
20
+ <match **>
21
+ @type google_cloud
22
+ adjust_invalid_timestamps true
23
+ autoformat_stackdriver_trace true
24
+ </match>
@@ -0,0 +1,40 @@
1
+ <source>
2
+ @type syslog
3
+ port 514
4
+ protocol_type tcp
5
+ bind 127.0.0.1
6
+ tag syslog
7
+ </source>
8
+
9
+ <source>
10
+ @type tail
11
+ path /var/log/apache*/access.log,/var/log/apache*/access_log,/var/log/httpd/access.log,/var/log/httpd/access_log
12
+ pos_file /var/lib/google-fluentd/pos/apache-access.pos
13
+ tag apache-access
14
+ </source>
15
+
16
+ <filter **>
17
+ @type add_insert_ids
18
+ </filter>
19
+
20
+ <match **>
21
+ @type google_cloud
22
+ adjust_invalid_timestamps true
23
+ autoformat_stackdriver_trace false
24
+ coerce_to_utf8 true
25
+ </match>
26
+
27
+ <filter **>
28
+ @type some_custom_filter
29
+ </filter>
30
+
31
+ <filter **>
32
+ @type record_transformer
33
+ <record>
34
+ host_param "#{Socket.gethostname}"
35
+ </record>
36
+ </filter>
37
+
38
+ <match "app.#{ENV['FLUENTD_TAG']}">
39
+ @type stdout
40
+ </match>
@@ -0,0 +1,187 @@
1
+ # Copyright 2020 Google Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require_relative '../helper'
16
+ require_relative 'asserts'
17
+ require_relative 'constants'
18
+
19
+ require 'fluent/test/driver/filter'
20
+ require 'fluent/plugin/filter_analyze_config'
21
+
22
+ # Unit tests for filter_analyze_config plugin.
23
+ class FilterAnalyzeConfigTest < Test::Unit::TestCase
24
+ include Asserts
25
+ include Constants
26
+ include Fluent::AnalyzeConfigFilter::Constants
27
+
28
+ APPLICATION_DEFAULT_CONFIG = ''.freeze
29
+
30
+ def setup
31
+ Fluent::Test.setup
32
+ end
33
+
34
+ def test_config_file_does_not_exist
35
+ # By default, the FilterTestDriver.new does not set up a config file at:
36
+ # /etc/google-fluentd/google-fluentd.conf. The plugin should still proceed.
37
+ create_driver
38
+ # No exceptions were thrown.
39
+ end
40
+
41
+ def test_analyze_config
42
+ create_driver(CONFIG_ANALYZE_CONFIG)
43
+
44
+ # Default plugins, with default config.
45
+ assert_prometheus_metric_value(
46
+ :stackdriver_enabled_plugins,
47
+ 1,
48
+ plugin_name: 'source/syslog/tcp',
49
+ is_default_plugin: true,
50
+ has_default_value: true,
51
+ has_ruby_snippet: false)
52
+ assert_prometheus_metric_value(
53
+ :stackdriver_enabled_plugins,
54
+ 1,
55
+ plugin_name: 'source/tail/apache-access',
56
+ is_default_plugin: true,
57
+ has_default_value: true,
58
+ has_ruby_snippet: false)
59
+ assert_prometheus_metric_value(
60
+ :stackdriver_enabled_plugins,
61
+ 1,
62
+ plugin_name: 'filter/add_insert_ids',
63
+ is_default_plugin: true,
64
+ has_default_value: true,
65
+ has_ruby_snippet: false)
66
+
67
+ # Default plugins, with custom config.
68
+ assert_prometheus_metric_value(
69
+ :stackdriver_enabled_plugins,
70
+ 1,
71
+ plugin_name: 'match/google_cloud',
72
+ is_default_plugin: true,
73
+ has_default_value: false,
74
+ has_ruby_snippet: false)
75
+
76
+ # Custom plugins, some with embedded Ruby.
77
+ assert_prometheus_metric_value(
78
+ :stackdriver_enabled_plugins,
79
+ 1,
80
+ plugin_name: 'filter',
81
+ is_default_plugin: false,
82
+ has_default_value: false,
83
+ has_ruby_snippet: false)
84
+ assert_prometheus_metric_value(
85
+ :stackdriver_enabled_plugins,
86
+ 1,
87
+ plugin_name: 'filter/record_transformer',
88
+ is_default_plugin: false,
89
+ has_default_value: false,
90
+ has_ruby_snippet: true)
91
+ assert_prometheus_metric_value(
92
+ :stackdriver_enabled_plugins,
93
+ 1,
94
+ plugin_name: 'match/stdout',
95
+ is_default_plugin: false,
96
+ has_default_value: false,
97
+ has_ruby_snippet: true)
98
+
99
+ # For out_google_cloud, 3 params are present.
100
+ assert_prometheus_metric_value(
101
+ :stackdriver_config_usage,
102
+ 1,
103
+ plugin_name: 'google_cloud',
104
+ param: 'adjust_invalid_timestamps',
105
+ is_present: true,
106
+ has_default_value: true)
107
+ assert_prometheus_metric_value(
108
+ :stackdriver_config_usage,
109
+ 1,
110
+ plugin_name: 'google_cloud',
111
+ param: 'autoformat_stackdriver_trace',
112
+ is_present: true,
113
+ has_default_value: false)
114
+ assert_prometheus_metric_value(
115
+ :stackdriver_config_usage,
116
+ 1,
117
+ plugin_name: 'google_cloud',
118
+ param: 'coerce_to_utf8',
119
+ is_present: true,
120
+ has_default_value: false)
121
+ # The remaining "google_cloud" params are not present.
122
+ # The are no params for "detect_exceptions".
123
+ %w(
124
+ auth_method
125
+ detect_json
126
+ enable_monitoring
127
+ gcm_service_address
128
+ grpc_compression_algorithm
129
+ http_request_key
130
+ insert_id_key
131
+ label_map
132
+ labels
133
+ labels_key
134
+ logging_api_url
135
+ monitoring_type
136
+ non_utf8_replacement_string
137
+ operation_key
138
+ private_key_email
139
+ private_key_passphrase
140
+ private_key_path
141
+ project_id
142
+ source_location_key
143
+ span_id_key
144
+ statusz_port
145
+ trace_key
146
+ trace_sampled_key
147
+ use_grpc
148
+ use_metadata_service
149
+ vm_id
150
+ vm_name
151
+ zone
152
+ ).each do |p|
153
+ assert_prometheus_metric_value(
154
+ :stackdriver_config_usage,
155
+ 1,
156
+ plugin_name: 'google_cloud',
157
+ param: p,
158
+ is_present: false,
159
+ has_default_value: false)
160
+ end
161
+
162
+ # We also export values for the bools.
163
+ assert_prometheus_metric_value(
164
+ :stackdriver_config_bool_values,
165
+ 1,
166
+ plugin_name: 'google_cloud',
167
+ param: 'adjust_invalid_timestamps',
168
+ value: true)
169
+ assert_prometheus_metric_value(
170
+ :stackdriver_config_bool_values,
171
+ 1,
172
+ plugin_name: 'google_cloud',
173
+ param: 'autoformat_stackdriver_trace',
174
+ value: false)
175
+ assert_prometheus_metric_value(
176
+ :stackdriver_config_bool_values,
177
+ 1,
178
+ plugin_name: 'google_cloud',
179
+ param: 'coerce_to_utf8',
180
+ value: true)
181
+ end
182
+
183
+ def create_driver(conf = APPLICATION_DEFAULT_CONFIG)
184
+ Fluent::Test::FilterTestDriver.new(
185
+ Fluent::AnalyzeConfigFilter).configure(conf, true)
186
+ end
187
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-google-cloud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.8.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stackdriver Agents Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-16 00:00:00.000000000 Z
11
+ date: 2020-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fluentd
@@ -266,21 +266,27 @@ files:
266
266
  - README.rdoc
267
267
  - Rakefile
268
268
  - fluent-plugin-google-cloud.gemspec
269
+ - lib/fluent/plugin/common.rb
269
270
  - lib/fluent/plugin/filter_add_insert_ids.rb
271
+ - lib/fluent/plugin/filter_analyze_config.rb
270
272
  - lib/fluent/plugin/in_object_space_dump.rb
271
273
  - lib/fluent/plugin/monitoring.rb
272
274
  - lib/fluent/plugin/out_google_cloud.rb
273
275
  - lib/fluent/plugin/statusz.rb
274
276
  - test/helper.rb
277
+ - test/plugin/asserts.rb
275
278
  - test/plugin/base_test.rb
276
279
  - test/plugin/constants.rb
277
280
  - test/plugin/data/c31e573fd7f62ed495c9ca3821a5a85cb036dee1-privatekey.p12
278
281
  - test/plugin/data/credentials.json
282
+ - test/plugin/data/google-fluentd-baseline.conf
283
+ - test/plugin/data/google-fluentd-custom.conf
279
284
  - test/plugin/data/iam-credentials.json
280
285
  - test/plugin/data/invalid_credentials.json
281
286
  - test/plugin/data/new-style-credentials.json
282
287
  - test/plugin/test_driver.rb
283
288
  - test/plugin/test_filter_add_insert_ids.rb
289
+ - test/plugin/test_filter_analyze_config.rb
284
290
  - test/plugin/test_out_google_cloud.rb
285
291
  - test/plugin/test_out_google_cloud_grpc.rb
286
292
  homepage: https://github.com/GoogleCloudPlatform/fluent-plugin-google-cloud
@@ -302,20 +308,24 @@ required_rubygems_version: !ruby/object:Gem::Requirement
302
308
  - !ruby/object:Gem::Version
303
309
  version: '0'
304
310
  requirements: []
305
- rubygems_version: 3.0.6
311
+ rubygems_version: 3.0.8
306
312
  signing_key:
307
313
  specification_version: 4
308
314
  summary: fluentd plugins for the Stackdriver Logging API
309
315
  test_files:
310
316
  - test/helper.rb
317
+ - test/plugin/asserts.rb
311
318
  - test/plugin/base_test.rb
312
319
  - test/plugin/constants.rb
313
320
  - test/plugin/data/c31e573fd7f62ed495c9ca3821a5a85cb036dee1-privatekey.p12
314
321
  - test/plugin/data/credentials.json
322
+ - test/plugin/data/google-fluentd-baseline.conf
323
+ - test/plugin/data/google-fluentd-custom.conf
315
324
  - test/plugin/data/iam-credentials.json
316
325
  - test/plugin/data/invalid_credentials.json
317
326
  - test/plugin/data/new-style-credentials.json
318
327
  - test/plugin/test_driver.rb
319
328
  - test/plugin/test_filter_add_insert_ids.rb
329
+ - test/plugin/test_filter_analyze_config.rb
320
330
  - test/plugin/test_out_google_cloud.rb
321
331
  - test/plugin/test_out_google_cloud_grpc.rb