fluent-plugin-stackdriver-monitoring 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0152a3a88115ba576c4fcc2deff4acfc308e96e8
4
- data.tar.gz: 3caa5074ce36b5063bfd3645ca74ab4dbd230719
3
+ metadata.gz: 0840d73e9fb3fb65918eecaf78b02e6e969e2ad9
4
+ data.tar.gz: 48731da8b94611bb1fde363e3a9b8aaf82f04be6
5
5
  SHA512:
6
- metadata.gz: c26c808d1bfc6bab1310e91f6605d495ad02ce1e9343f4287447db306ceab75f16c3daa4d9e5fb1345e9a395e6f1208803f01091f49c8a0847775d02ccd30d3b
7
- data.tar.gz: de8ad272f8fc05772a52e01cade86e08f7dc232158a96cc458636e235bbf18235f642fb0461e1df97eeca263e0e8c34f59497812b6a6f8c3a4d4030fb2771668
6
+ metadata.gz: 35bd642a6ccd1883e438de46febffe778300c033efe3ee577cc411cef6438156990f92c347aa077fb298214cad97ee4145593cf7f917ae9c4ea4c7a95765adc5
7
+ data.tar.gz: 00ea14ff7803677ed925da418c3fc785f5381a3012c0dd0c4f614d269ee2b630692d307f0d2f514f1360fe76642acf8ecc5f096f4e70b4689562aabb1057864b
@@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
6
6
  gem.license = 'MIT'
7
7
  gem.homepage = 'https://github.com/mia-0032/fluent-plugin-stackdriver-monitoring'
8
8
  gem.summary = gem.description
9
- gem.version = '0.0.3'
9
+ gem.version = '0.0.4'
10
10
  gem.authors = ['Yoshihiro MIYAI']
11
11
  gem.email = 'msparrow17@gmail.com'
12
12
  gem.has_rdoc = false
@@ -1,8 +1,5 @@
1
1
  require 'fluent/output'
2
- require 'google/api/metric_pb'
3
- require 'google/cloud/monitoring/v3/metric_service_client'
4
- require 'google/protobuf/repeated_field'
5
- require 'google/protobuf/timestamp_pb'
2
+ require_relative 'stackdriver/client'
6
3
 
7
4
  module Fluent
8
5
  class StackdriverMonitoringOutput < BufferedOutput
@@ -18,6 +15,7 @@ module Fluent
18
15
  end
19
16
 
20
17
  TYPE_PREFIX = 'custom.googleapis.com/'.freeze
18
+ PAST_DATA_TIME_LIMIT = 60 * 60 * 24 # 24h
21
19
 
22
20
  def configure(conf)
23
21
  super
@@ -35,15 +33,12 @@ module Fluent
35
33
  end
36
34
  end
37
35
 
38
- @project_name = Google::Cloud::Monitoring::V3::MetricServiceClient.project_path @project
39
- @metric_name = Google::Cloud::Monitoring::V3::MetricServiceClient.metric_descriptor_path @project, @custom_metrics.type
36
+ @client = Fluent::StackdriverMonitoring::Writer.new @project, @custom_metrics, log
40
37
  end
41
38
 
42
39
  def start
43
40
  super
44
-
45
- @metric_service_client = Google::Cloud::Monitoring::V3::MetricServiceClient.new
46
- @metric_descriptor = create_metric_descriptor
41
+ @client.start
47
42
  end
48
43
 
49
44
  def format(tag, time, record)
@@ -51,18 +46,17 @@ module Fluent
51
46
  end
52
47
 
53
48
  def write(chunk)
54
- chunk.msgpack_each do |tag, time, record|
55
- time_series = create_time_series
56
- value = record[@custom_metrics.key]
49
+ current_time = Time.now.to_i
57
50
 
58
- point = Google::Monitoring::V3::Point.new
59
- point.interval = create_time_interval time, @custom_metrics.time_interval
60
- point.value = create_typed_value value
61
- time_series.points.push point
51
+ chunk.msgpack_each do |tag, time, record|
52
+ if (current_time - time) >= PAST_DATA_TIME_LIMIT
53
+ log.warn 'Drop data point because it cannot be written more than 24h in the past', time: Time.at(time).to_s, metric_name: @metric_name
54
+ next
55
+ end
62
56
 
63
- log.debug "Create time series", time: Time.at(time).to_s, value: value, metric_name: @metric_name
64
- # Only one point can be written per TimeSeries per request.
65
- @metric_service_client.create_time_series @project_name, [time_series]
57
+ value = record[@custom_metrics.key]
58
+ start_time = time - @custom_metrics.time_interval
59
+ @client.write start_time, time, value
66
60
  end
67
61
  end
68
62
 
@@ -70,61 +64,5 @@ module Fluent
70
64
  def is_custom_metric?(metric_type)
71
65
  metric_type.start_with? TYPE_PREFIX
72
66
  end
73
-
74
- def create_metric_descriptor
75
- begin
76
- metric_descriptor = @metric_service_client.get_metric_descriptor(@metric_name)
77
- log.info "Succeed to get metric descripter", metric_name: @metric_name
78
- return metric_descriptor
79
- rescue Google::Gax::RetryError
80
- log.info "Failed to get metric descripter", metric_name: @metric_name
81
- end
82
-
83
- metric_descriptor = Google::Api::MetricDescriptor.new
84
- metric_descriptor.type = @custom_metrics.type
85
- metric_descriptor.metric_kind = @custom_metrics.metric_kind
86
- metric_descriptor.value_type = @custom_metrics.value_type
87
- metric_descriptor = @metric_service_client.create_metric_descriptor(@project_name, metric_descriptor)
88
- log.info "Succeed to create metric descripter", metric_name: @metric_name
89
-
90
- metric_descriptor
91
- end
92
-
93
- def create_time_series
94
- time_series = Google::Monitoring::V3::TimeSeries.new
95
-
96
- metric = Google::Api::Metric.new
97
- metric.type = @metric_descriptor.type
98
- time_series.metric = metric
99
-
100
- time_series.metric_kind = @metric_descriptor.metric_kind
101
- time_series.value_type = @metric_descriptor.value_type
102
-
103
- time_series
104
- end
105
-
106
- def create_time_interval(time, interval)
107
- time_interval = Google::Monitoring::V3::TimeInterval.new
108
- time_interval.start_time = Google::Protobuf::Timestamp.new seconds: (time - interval)
109
- time_interval.end_time = Google::Protobuf::Timestamp.new seconds: time
110
-
111
- time_interval
112
- end
113
-
114
- def create_typed_value(value)
115
- typed_value = Google::Monitoring::V3::TypedValue.new
116
- case @metric_descriptor.value_type
117
- when :BOOL
118
- typed_value.bool_value = !!value
119
- when :INT64
120
- typed_value.int64_value = value.to_i
121
- when :DOUBLE
122
- typed_value.double_value = value.to_f
123
- else
124
- raise 'Unknown value_type!'
125
- end
126
-
127
- typed_value
128
- end
129
67
  end
130
68
  end
@@ -0,0 +1,97 @@
1
+ require 'google/api/metric_pb'
2
+ require 'google/cloud/monitoring/v3/metric_service_client'
3
+ require 'google/protobuf/repeated_field'
4
+ require 'google/protobuf/timestamp_pb'
5
+
6
+ module Fluent
7
+ module StackdriverMonitoring
8
+ class Error < StandardError
9
+ end
10
+
11
+ class Writer
12
+ def initialize(project, custom_metrics, log)
13
+ @custom_metrics = custom_metrics
14
+ @project_name = Google::Cloud::Monitoring::V3::MetricServiceClient.project_path project
15
+ @metric_name = Google::Cloud::Monitoring::V3::MetricServiceClient.metric_descriptor_path project, custom_metrics.type
16
+ @log = log
17
+ end
18
+
19
+ def start
20
+ @metric_service_client = Google::Cloud::Monitoring::V3::MetricServiceClient.new
21
+ @metric_descriptor = create_metric_descriptor
22
+ end
23
+
24
+ def write(start_time, end_time, value)
25
+ time_series = create_time_series
26
+ point = Google::Monitoring::V3::Point.new
27
+ point.interval = create_time_interval start_time, end_time
28
+ point.value = create_typed_value value
29
+ time_series.points.push point
30
+
31
+ log.debug "Create time series", start_time: Time.at(start_time).to_s, end_time: Time.at(end_time).to_s, value: value, metric_name: @metric_name
32
+ # Only one point can be written per TimeSeries per request.
33
+ @metric_service_client.create_time_series @project_name, [time_series]
34
+ end
35
+
36
+ private
37
+ def log
38
+ @log
39
+ end
40
+
41
+ def create_metric_descriptor
42
+ begin
43
+ metric_descriptor = @metric_service_client.get_metric_descriptor(@metric_name)
44
+ log.info "Succeed to get metric descripter", metric_name: @metric_name
45
+ return metric_descriptor
46
+ rescue Google::Gax::RetryError
47
+ log.info "Failed to get metric descripter", metric_name: @metric_name
48
+ end
49
+
50
+ metric_descriptor = Google::Api::MetricDescriptor.new
51
+ metric_descriptor.type = @custom_metrics.type
52
+ metric_descriptor.metric_kind = @custom_metrics.metric_kind
53
+ metric_descriptor.value_type = @custom_metrics.value_type
54
+ metric_descriptor = @metric_service_client.create_metric_descriptor(@project_name, metric_descriptor)
55
+ log.info "Succeed to create metric descripter", metric_name: @metric_name
56
+
57
+ metric_descriptor
58
+ end
59
+
60
+ def create_time_series
61
+ time_series = Google::Monitoring::V3::TimeSeries.new
62
+
63
+ metric = Google::Api::Metric.new
64
+ metric.type = @metric_descriptor.type
65
+
66
+ time_series.metric = metric
67
+ time_series.metric_kind = @metric_descriptor.metric_kind
68
+ time_series.value_type = @metric_descriptor.value_type
69
+
70
+ time_series
71
+ end
72
+
73
+ def create_time_interval(start_time, end_time)
74
+ time_interval = Google::Monitoring::V3::TimeInterval.new
75
+ time_interval.start_time = Google::Protobuf::Timestamp.new seconds: start_time
76
+ time_interval.end_time = Google::Protobuf::Timestamp.new seconds: end_time
77
+ time_interval
78
+ end
79
+
80
+ def create_typed_value(value)
81
+ typed_value = Google::Monitoring::V3::TypedValue.new
82
+ case @metric_descriptor.value_type
83
+ when :BOOL
84
+ typed_value.bool_value = !!value
85
+ when :INT64
86
+ typed_value.int64_value = value.to_i
87
+ when :DOUBLE
88
+ typed_value.double_value = value.to_f
89
+ else
90
+ raise Error.new 'Unknown value_type!'
91
+ end
92
+
93
+ typed_value
94
+ end
95
+ end
96
+ end
97
+ end
@@ -4,7 +4,7 @@ class StackdriverMonitoringOutputTest < Test::Unit::TestCase
4
4
  CONFIG = %[
5
5
  project project-test
6
6
  <custom_metrics>
7
- key hoge_key
7
+ key test_key
8
8
  type custom.googleapis.com/hoge_type
9
9
  metric_kind GAUGE
10
10
  value_type INT64
@@ -24,7 +24,7 @@ class StackdriverMonitoringOutputTest < Test::Unit::TestCase
24
24
  d = create_driver
25
25
 
26
26
  assert_equal('project-test', d.instance.project)
27
- assert_equal('hoge_key', d.instance.custom_metrics.key)
27
+ assert_equal('test_key', d.instance.custom_metrics.key)
28
28
  assert_equal('custom.googleapis.com/hoge_type', d.instance.custom_metrics.type)
29
29
  assert_equal(:GAUGE, d.instance.custom_metrics.metric_kind)
30
30
  assert_equal(:INT64, d.instance.custom_metrics.value_type)
@@ -36,7 +36,7 @@ class StackdriverMonitoringOutputTest < Test::Unit::TestCase
36
36
  create_driver(%[
37
37
  project project-test
38
38
  <custom_metrics>
39
- key hoge_key
39
+ key test_key
40
40
  type invalid_type
41
41
  metric_kind GAUGE
42
42
  value_type INT64
@@ -50,7 +50,7 @@ class StackdriverMonitoringOutputTest < Test::Unit::TestCase
50
50
  create_driver(%[
51
51
  project project-test
52
52
  <custom_metrics>
53
- key hoge_key
53
+ key test_key
54
54
  type invalid_type
55
55
  metric_kind CUMULATIVE
56
56
  value_type INT64
@@ -65,7 +65,7 @@ class StackdriverMonitoringOutputTest < Test::Unit::TestCase
65
65
  create_driver(%[
66
66
  project project-test
67
67
  <custom_metrics>
68
- key hoge_key
68
+ key test_key
69
69
  type invalid_type
70
70
  metric_kind CUMULATIVE
71
71
  value_type BOOL
@@ -74,4 +74,48 @@ class StackdriverMonitoringOutputTest < Test::Unit::TestCase
74
74
  end
75
75
  end
76
76
  end
77
+
78
+ sub_test_case 'write' do
79
+ setup do
80
+ @client = mock!
81
+ @client.start.once
82
+ stub(Fluent::StackdriverMonitoring::Writer).new(anything, anything, anything).once { @client }
83
+ end
84
+
85
+ test 'basic data' do
86
+ now = Time.now.to_i
87
+ @client.write(now, now, 1).once
88
+
89
+ d = create_driver
90
+ d.emit({'test_key' => 1}, now)
91
+ d.run
92
+ end
93
+
94
+ test 'data with time_interval' do
95
+ now = Time.now.to_i
96
+ @client.write(now - 10, now, 1).once
97
+
98
+ d = create_driver(%[
99
+ project project-test
100
+ <custom_metrics>
101
+ key test_key
102
+ type custom.googleapis.com/hoge_type
103
+ metric_kind GAUGE
104
+ value_type INT64
105
+ time_interval 10s
106
+ </custom_metrics>
107
+ ])
108
+ d.emit({'test_key' => 1}, now)
109
+ d.run
110
+ end
111
+
112
+ test 'past data are dropped' do
113
+ past_time = Time.now.to_i - (60 * 60 * 24 + 1)
114
+ @client.write.times(0)
115
+
116
+ d = create_driver
117
+ d.emit({'test_key' => 1}, past_time)
118
+ d.run
119
+ end
120
+ end
77
121
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-stackdriver-monitoring
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshihiro MIYAI
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-14 00:00:00.000000000 Z
11
+ date: 2017-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fluentd
@@ -115,6 +115,7 @@ files:
115
115
  - Rakefile
116
116
  - fluent-plugin-stackdriver-monitoring.gemspec
117
117
  - lib/fluent/plugin/out_stackdriver_monitoring.rb
118
+ - lib/fluent/plugin/stackdriver/client.rb
118
119
  - test/plugin/test_out_stackdriver_monitoring.rb
119
120
  - test/test_helper.rb
120
121
  homepage: https://github.com/mia-0032/fluent-plugin-stackdriver-monitoring