sensu-transport-snssqs 2.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/sensu/transport/snssqs.rb +174 -0
  3. metadata +5 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a825c80ab314cedefd551d4960c5922bda96a618
4
- data.tar.gz: d97c573f4356d8032614c70701b865d61af71956
3
+ metadata.gz: d5c8cbded309051beefe6b54d1f24cb03d4ffc86
4
+ data.tar.gz: 5f6b15bd2c9e180280f8908e842622d395e4a682
5
5
  SHA512:
6
- metadata.gz: 688227148a7821e3ae420697b3056db3823fa9ab52f6e76d57052d8604707fc414b80becd740680a862b60d341131cdb2386c4b026c6d3c1c2387578cc743d6f
7
- data.tar.gz: 83daa1a61c26f42fe40b3c64bd92d01a6dcf63144d42b78d656b68c10b2416d51e00a3419d21c6a012affb7552a5d4af55a69af001925754b1e091c8f581002a
6
+ metadata.gz: 7823b085e8a8504b3024bd8d88134e0ea59b7931566189362acf1ce54ee72224fb2f78b7269ad703eb3dc2894010e28070d4a1c0b66d1bf3bf02d957f2df47be
7
+ data.tar.gz: 36bf0b6222f0386ccad49d7f5b74ed6f131f1da3461890bef49dde8ac587584d6c9b62782154f81b6f64e304221270a40156e66ae65dab6e14b8fe31075280fa
@@ -0,0 +1,174 @@
1
+ require 'sensu/transport/base'
2
+ require 'aws-sdk'
3
+ require 'statsd-ruby'
4
+
5
+ module Sensu
6
+ module Transport
7
+ class SNSSQS < Sensu::Transport::Base
8
+ attr_accessor :logger
9
+
10
+ STRING_STR = "String".freeze
11
+ KEEPALIVES_STR = "keepalives".freeze
12
+ PIPE_STR = "pipe".freeze
13
+ TYPE_STR = "type".freeze
14
+
15
+ def initialize
16
+ @connected = false
17
+ @subscribing = false
18
+
19
+ # as of sensu 0.23.0 we need to call succeed when we have
20
+ # successfully connected to SQS.
21
+ #
22
+ # we already have our own logic to maintain the connection to
23
+ # SQS, so we can always say we're connected.
24
+ #
25
+ # See:
26
+ # https://github.com/sensu/sensu/blob/cdc25b29169ef2dcd2e056416eab0e83dbe000bb/CHANGELOG.md#0230---2016-04-04
27
+ succeed()
28
+ end
29
+
30
+ def connected?; @connected; end
31
+
32
+ def connect(settings)
33
+ @settings = settings
34
+ @connected = true
35
+ @results_callback = proc {}
36
+ @keepalives_callback = proc {}
37
+ @sqs = Aws::SQS::Client.new(region: @settings[:region])
38
+ @sns = Aws::SNS::Client.new(region: @settings[:region])
39
+
40
+ # connect to statsd, if necessary
41
+ @statsd = nil
42
+ if @settings[:statsd_addr] != ""
43
+ pieces = @settings[:statsd_addr].split(':')
44
+ @statsd = Statsd.new(pieces[0], pieces[1].to_i).tap { |sd|
45
+ sd.namespace = @settings[:statsd_namespace]
46
+ }
47
+ @statsd_sample_rate = @settings[:statsd_sample_rate].to_f
48
+ end
49
+ end
50
+
51
+ def statsd_incr(stat)
52
+ @statsd.increment(stat, @statsd_sample_rate) unless @statsd.nil?
53
+ end
54
+
55
+ def statsd_time(stat)
56
+ # always measure + run the block, but only if @statsd is set
57
+ # do we actually report it.
58
+ start = Time.now
59
+ result = yield
60
+ if !@statsd.nil?
61
+ @statsd.timing(stat, ((Time.now - start) * 1000).round(5), @statsd_sample_rate)
62
+ end
63
+ result
64
+ end
65
+
66
+ # subscribe will begin "subscribing" to the consuming sqs queue.
67
+ #
68
+ # What this really means is that we will start polling for
69
+ # messages from the SQS queue, and, depending on the message
70
+ # type, it will call the appropriate callback.
71
+ #
72
+ # This assumes that the SQS Queue is consuming "Raw" messages
73
+ # from SNS.
74
+ #
75
+ # "subscribing" means that the "callback" parameter will be
76
+ # called when there is a message for you to consume.
77
+ #
78
+ # "funnel" and "type" parameters are completely ignored.
79
+ def subscribe(type, pipe, funnel = nil, options = {}, &callback)
80
+ self.logger.info("subscribing to type=#{type}, pipe=#{pipe}, funnel=#{funnel}")
81
+
82
+ if pipe == KEEPALIVES_STR
83
+ @keepalives_callback = callback
84
+ else
85
+ @results_callback = callback
86
+ end
87
+
88
+ unless @subscribing
89
+ do_all_the_time {
90
+ EM::Iterator.new(receive_messages, 10).each do |msg, iter|
91
+ statsd_time("sqs.#{@settings[:consuming_sqs_queue_url]}.process_timing") {
92
+ if msg.message_attributes[PIPE_STR].string_value == KEEPALIVES_STR
93
+ @keepalives_callback.call(msg, msg.body)
94
+ else
95
+ @results_callback.call(msg, msg.body)
96
+ end
97
+ }
98
+ iter.next
99
+ end
100
+ }
101
+ @subscribing = true
102
+ end
103
+ end
104
+
105
+ # acknowledge will delete the given message from the SQS queue.
106
+ def acknowledge(info, &callback)
107
+ EM.defer {
108
+ @sqs.delete_message(
109
+ queue_url: @settings[:consuming_sqs_queue_url],
110
+ receipt_handle: info.receipt_handle,
111
+ )
112
+ statsd_incr("sqs.#{@settings[:consuming_sqs_queue_url]}.message.deleted")
113
+ callback.call(info) if callback
114
+ }
115
+ end
116
+
117
+ # publish publishes a message to the SNS topic.
118
+ #
119
+ # The type, pipe, and options are transformed into SNS message
120
+ # attributes and included with the message.
121
+ def publish(type, pipe, message, options = {}, &callback)
122
+ attributes = {
123
+ TYPE_STR => str_attr(type),
124
+ PIPE_STR => str_attr(pipe)
125
+ }
126
+ options.each do |k, v|
127
+ attributes[k.to_s] = str_attr(v.to_s)
128
+ end
129
+ EM.defer { send_message(message, attributes, &callback) }
130
+ end
131
+
132
+ private
133
+
134
+ def str_attr(str)
135
+ { :data_type => STRING_STR, :string_value => str }
136
+ end
137
+
138
+ def do_all_the_time(&blk)
139
+ callback = proc {
140
+ do_all_the_time(&blk)
141
+ }
142
+ EM.defer(blk, callback)
143
+ end
144
+
145
+ def send_message(msg, attributes, &callback)
146
+ resp = @sns.publish(
147
+ target_arn: @settings[:publishing_sns_topic_arn],
148
+ message: msg,
149
+ message_attributes: attributes
150
+ )
151
+ statsd_incr("sns.#{@settings[:publishing_sns_topic_arn]}.message.published")
152
+ callback.call({ :response => resp }) if callback
153
+ end
154
+
155
+ PIPE_ARR = [PIPE_STR]
156
+
157
+ # receive_messages returns an array of SQS messages
158
+ # for the consuming queue
159
+ def receive_messages
160
+ begin
161
+ resp = @sqs.receive_message(
162
+ message_attribute_names: PIPE_ARR,
163
+ queue_url: @settings[:consuming_sqs_queue_url],
164
+ wait_time_seconds: @settings[:wait_time_seconds],
165
+ max_number_of_messages: @settings[:max_number_of_messages],
166
+ )
167
+ resp.messages
168
+ rescue Aws::SQS::Errors::ServiceError => e
169
+ self.logger.info(e)
170
+ end
171
+ end
172
+ end
173
+ end
174
+ end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensu-transport-snssqs
3
3
  version: !ruby/object:Gem::Version
4
- version: '2.0'
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
- - ''
7
+ - Tom Wanielista
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
@@ -57,7 +57,8 @@ email:
57
57
  executables: []
58
58
  extensions: []
59
59
  extra_rdoc_files: []
60
- files: []
60
+ files:
61
+ - lib/sensu/transport/snssqs.rb
61
62
  homepage: https://github.com/SimpleFinance/sensu-transport-snssqs
62
63
  licenses: []
63
64
  metadata: {}
@@ -80,6 +81,6 @@ rubyforge_project:
80
81
  rubygems_version: 2.6.4
81
82
  signing_key:
82
83
  specification_version: 4
83
- summary: ''
84
+ summary: Sensu transport over Amazon SNS & SQS
84
85
  test_files: []
85
86
  has_rdoc: