sematext-metrics 0.0.1.1

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 ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YjBmNTFiNWNlODliZGU5NWFhODVkZDg2YTExNmUzOTJlMDExZDYyNw==
5
+ data.tar.gz: !binary |-
6
+ MGE1NTIzZDU0NmQ1YTY0Y2NmMGE3ZTIyNjdjNDY1OGE0ZGI5NmM3YQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZjI1ZmZlMzI4NmY2MzBjYWE1N2M3NTk4YzZlOTQ0NTFiZDZhYjhiMzc1YWM3
10
+ YjMwN2QyMGI2OTAyZGYyNDdmY2FmMmFhOWMwYTE3MGE4ODA3MmM1MjA3Zjg0
11
+ YjE3MGNlNDVjMzQyNzhkZjMyNGRmMmZlZmNkYWQ2YzYxNWY3ZDM=
12
+ data.tar.gz: !binary |-
13
+ NzRiMWU4YzBiYmIwMjk5ZTJlNDc3ODUzZGQwNzNkNGY2MzhiYTVhY2U4MDcw
14
+ ZmI2YmZiOWY3MGU3ZWUzMWJjNTc2MDFkYWEwNzBiODRlM2MwZGM2ZDBlY2Zk
15
+ NjM5YWE0NTE2YzExOGY3YjFkNmVlZjRlY2M0MzVjNDBkMzdkM2Y=
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - ree
4
+ - 1.9.2
5
+ - 1.9.3
6
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sematext-metrics-ruby.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,33 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sematext-metrics (0.0.1)
5
+ httparty (~> 0.11.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.4)
11
+ httparty (0.11.0)
12
+ multi_json (~> 1.0)
13
+ multi_xml (>= 0.5.2)
14
+ multi_json (1.7.6)
15
+ multi_xml (0.5.4)
16
+ rake (10.0.4)
17
+ rspec (2.13.0)
18
+ rspec-core (~> 2.13.0)
19
+ rspec-expectations (~> 2.13.0)
20
+ rspec-mocks (~> 2.13.0)
21
+ rspec-core (2.13.1)
22
+ rspec-expectations (2.13.0)
23
+ diff-lcs (>= 1.1.3, < 2.0)
24
+ rspec-mocks (2.13.1)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bundler (~> 1.3)
31
+ rake
32
+ rspec (~> 2.13.0)
33
+ sematext-metrics!
data/LICENSE.txt ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # sematext-metrics gem
2
+
3
+ Ruby gem for sending [Custom Metrics](https://sematext.atlassian.net/wiki/display/PUBSPM/Custom+Metrics) to [SPM](http://sematext.com/spm/index.html).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sematext-metrics'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sematext-metrics
18
+
19
+ ## Usage
20
+
21
+ Initialize client:
22
+
23
+ Sematext::Metrics.initialize("[token]")
24
+
25
+ Send datapoint:
26
+
27
+ Sematext::Metrics.send(
28
+ :timestamp => 1370969400767,
29
+ :name => 'coffee-consumed',
30
+ :value => 1000,
31
+ :agg_type => :sum,
32
+ :filter1 => 'floor=1',
33
+ :filter2 => 'machine=1'
34
+ )
35
+
36
+ Batch sending:
37
+
38
+ datapoints = [{
39
+ :name => 'coffee-consumed',
40
+ :value => 10,
41
+ :agg_type => :sum,
42
+ :filter1 => 'floor=1',
43
+ :filter2 => 'machine=1'
44
+ }, {
45
+ :name => 'coffee-consumed',
46
+ :value => 11,
47
+ :agg_type => :sum,
48
+ :filter1 => 'floor=1',
49
+ :filter2 => 'machine=2'
50
+ }]
51
+
52
+ Sematext::Metrics.send_batch datapoints
53
+
54
+ ## Configuration
55
+
56
+ To use different tokens for different applications, use `sync` or `async` factory method to instantiate a new client:
57
+
58
+ user_metrics = Sematext::Metrics::Client.sync("[elasticseach_app_token]")
59
+ search_metrics = Sematext::Metrics::Client.sync("[web_app_token]")
60
+
61
+ #send data
62
+ user_metrics.send_batch user_datapoints
63
+ search_metrics.send_batch search_datapoints
64
+
65
+
66
+ ## Asynchronous sending
67
+
68
+ Client can send data asynchronously using `em-http-request` extension for [EventMachine](http://http://rubyeventmachine.com/).
69
+ In order to use this extension add `em-http-request` gem to your application manually. Ensure EventMachine's reactor loop is running:
70
+
71
+ Thread.new { EventMachine.run }
72
+
73
+ Asynchronous data sending can be configured during initialization:
74
+
75
+ Sematext::Metrics.initialize("[token]", async=true)
76
+
77
+ Or use `async` factory method to create a client instance:
78
+
79
+ client = Sematext::Metrics::Client.async("[token]")
80
+
81
+ ## Further reading
82
+
83
+ [SPM Custom Metrics Wiki](https://sematext.atlassian.net/wiki/display/PUBSPM/Custom+Metrics).
84
+
85
+ ## License
86
+
87
+ Copyright 2013 Sematext Group, Inc.
88
+
89
+ Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
90
+
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,86 @@
1
+ # Copyright 2013 Sematext Group, Inc.
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 "sematext/metrics/version"
16
+ require "sematext/metrics/serializer"
17
+ require "sematext/metrics/settings"
18
+ require "sematext/metrics/validator"
19
+ require "sematext/metrics/sync_sender"
20
+ require "sematext/metrics/async_sender"
21
+
22
+ module Sematext
23
+ module Metrics
24
+ def self.initialize(token, async = false)
25
+ raise "Token should be defined" unless token
26
+ @client = async ? Client.async(token) : Client.sync(token)
27
+ end
28
+
29
+ def self.send datapoint
30
+ client.send datapoint
31
+ end
32
+
33
+ def self.send_batch datapoints
34
+ client.send_batch datapoints
35
+ end
36
+
37
+ private
38
+ def self.client
39
+ raise "Client should be initialized (forgot to call Sematext::Metrics.initialize?)" unless @client
40
+ @client
41
+ end
42
+
43
+ class Client
44
+ class << self
45
+ def sync(token)
46
+ Client.new(SyncSender.new(token))
47
+ end
48
+
49
+ def async(token)
50
+ Client.new(AsyncSender.new(token))
51
+ end
52
+ end
53
+
54
+ def initialize(sender)
55
+ @sender = sender
56
+ @serializer = RawSerializer.new
57
+ @validator = RawValidator.new
58
+ end
59
+
60
+ def send datapoint
61
+ return nil if datapoint.nil?
62
+
63
+ result = send_batch [datapoint]
64
+ result.first
65
+ end
66
+
67
+ def send_batch datapoints
68
+ return nil if datapoints.empty?
69
+
70
+ datapoints.each do |dp|
71
+ @validator.validate(dp)
72
+ dp[:timestamp] = epoch unless dp[:timestamp]
73
+ end
74
+
75
+ datapoints.each_slice(Settings::MAX_DP_PER_REQUEST).map do |slice|
76
+ serialized = @serializer.serialize_datapoints(slice)
77
+ @sender.send serialized
78
+ end
79
+ end
80
+
81
+ def epoch
82
+ Time.now.to_i * 1000
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,53 @@
1
+ # Copyright 2013 Sematext Group, Inc.
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
+ module Sematext
16
+ module Metrics
17
+ class AsyncSender
18
+ def initialize(token)
19
+ @token = token
20
+ if defined?(EventMachine)
21
+ require 'em-http-request'
22
+ end
23
+ end
24
+
25
+ def send data
26
+ deferrable = EventMachine::DefaultDeferrable.new
27
+
28
+ post_options = {
29
+ :path => Settings::RECEIVER_PATH,
30
+ :query => {:token => @token},
31
+ :body => data,
32
+ :head => {
33
+ 'Content-Type' => 'text/plain',
34
+ 'User-Agent' => Settings::USER_AGENT
35
+ }
36
+ }
37
+
38
+ http = EventMachine::HttpRequest.new(Settings::RECEIVER_URI).post post_options
39
+ http.errback {
40
+ deferrable.fail{{:status => :failed}}
41
+ }
42
+ http.callback {
43
+ if http.response_header.status == 201 then
44
+ deferrable.succeed({:status => :succeed})
45
+ else
46
+ deferrable.fail({:status => :failed, :response => http.response})
47
+ end
48
+ }
49
+ deferrable
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,35 @@
1
+ # Copyright 2013 Sematext Group, Inc.
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
+ module Sematext
16
+ module Metrics
17
+ class RawSerializer
18
+ def serialize_datapoints datapoint
19
+ datapoint.map{|m| self.serialize_datapoint(m)}.join("\n")
20
+ end
21
+
22
+ def serialize_datapoint datapoint
23
+ format = [
24
+ datapoint[:timestamp],
25
+ datapoint[:name],
26
+ datapoint[:value],
27
+ datapoint[:agg_type],
28
+ datapoint[:filter1],
29
+ datapoint[:filter2]
30
+ ]
31
+ format.join("\t")
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,24 @@
1
+ # Copyright 2013 Sematext Group, Inc.
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
+ module Sematext
16
+ module Metrics
17
+ module Settings
18
+ USER_AGENT = "Sematext-metrics-ruby #{VERSION}"
19
+ RECEIVER_URI = "http://spm-receiver.sematext.com"
20
+ RECEIVER_PATH = "/receiver/custom/receive.raw"
21
+ MAX_DP_PER_REQUEST = 100
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,45 @@
1
+ # Copyright 2013 Sematext Group, Inc.
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 'httparty'
16
+
17
+ module Sematext
18
+ module Metrics
19
+ class SyncSender
20
+ include HTTParty
21
+
22
+ def initialize(token)
23
+ @token = token
24
+ self.class.default_options[:base_uri] = Settings::RECEIVER_URI
25
+ end
26
+
27
+ def send data
28
+ post_options = {
29
+ :query => {:token => @token},
30
+ :headers => {
31
+ 'Content-Type' => 'text/plain',
32
+ 'User-Agent' => Settings::USER_AGENT
33
+ },
34
+ :body => data
35
+ }
36
+ response = self.class.post(Settings::RECEIVER_PATH, post_options)
37
+ if response.code == 201
38
+ {:status => :succeed}
39
+ else
40
+ {:status => :failed, :response => response.parsed_response}
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,43 @@
1
+ # Copyright 2013 Sematext Group, Inc.
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
+ module Sematext
16
+ module Metrics
17
+ class RawValidator
18
+ MAX_STRING_LENGTH = 255
19
+ VALID_AGG_TYPES = %w(sum avg min max)
20
+
21
+ def check_bounds name, value
22
+ raise "'#{name}' value can't be longer than 255 characters" unless value.size <= 255
23
+ end
24
+
25
+ def check_obligatory name, value
26
+ raise "'#{name}' should be defined" unless value
27
+ end
28
+
29
+ def validate datapoint
30
+ [:name, :value, :agg_type].each do |field|
31
+ check_obligatory field, datapoint[field]
32
+ end
33
+ [:name, :filter1, :filter2].each do |field|
34
+ value = datapoint[field]
35
+ check_bounds field, datapoint[field] if value
36
+ end
37
+ unless VALID_AGG_TYPES.include? datapoint[:agg_type].to_s
38
+ raise "Invalid aggregation type, valid values are: #{VALID_AGG_TYPES.join(', ')}"
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ # Copyright 2013 Sematext Group, Inc.
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
+ module Sematext
16
+ module Metrics
17
+ VERSION = "0.0.1.1"
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sematext/metrics/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sematext-metrics"
8
+ spec.version = Sematext::Metrics::VERSION
9
+ spec.authors = ["Pavel Zalunin"]
10
+ spec.email = ["info@sematext.com"]
11
+ spec.description = %q{Gem for talking to SPM}
12
+ spec.summary = %q{Gem for sending custom metrics to SPM}
13
+ spec.homepage = "http://github.com/sematext/sematext-metrics-gem"
14
+ spec.license = "Apache 2.0"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rspec", "~> 2.13.0"
23
+ spec.add_development_dependency "rake"
24
+
25
+ spec.add_runtime_dependency "httparty", "~> 0.11.0"
26
+ end
@@ -0,0 +1,39 @@
1
+ # Copyright 2013 Sematext Group, Inc.
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 "spec_helper"
16
+
17
+ describe Sematext::Metrics::Client do
18
+ let(:sender) { double("sender") }
19
+ let(:client) { Sematext::Metrics::Client.new(sender) }
20
+
21
+ it "sends serialized datapoints using sender" do
22
+ sender.should_receive(:send).with("123\tdp-1\t43\tsum\t\t")
23
+ client.send :timestamp => 123, :name => 'dp-1', :value => 43, :agg_type => :sum
24
+ end
25
+
26
+ it "sends large datapoints list by chunks" do
27
+ datapoints = [{:name => 'dp-1', :value => 43, :agg_type => :sum}] * 102
28
+
29
+ sender.should_receive(:send) do |value|
30
+ expect(value.split("\n")).to have(100).items
31
+ end
32
+
33
+ sender.should_receive(:send) do |value|
34
+ expect(value.split("\n")).to have(2).items
35
+ end
36
+
37
+ client.send_batch datapoints
38
+ end
39
+ end
@@ -0,0 +1,66 @@
1
+ # Copyright 2013 Sematext Group, Inc.
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 "spec_helper"
16
+
17
+ describe Sematext::Metrics::RawSerializer do
18
+ let(:ser) { Sematext::Metrics::RawSerializer.new }
19
+
20
+ it "serializes datapoint to raw format" do
21
+ ser = Sematext::Metrics::RawSerializer.new
22
+ expect(ser.serialize_datapoint({
23
+ :timestamp => 1370888200668,
24
+ :name => 'coffee_consumed',
25
+ :value => 42,
26
+ :agg_type => :sum,
27
+ :filter1 => 'building-1',
28
+ :filter2 => 'kitchen-1'
29
+ })).to eq("1370888200668\tcoffee_consumed\t42\tsum\tbuilding-1\tkitchen-1")
30
+
31
+ expect(ser.serialize_datapoint({
32
+ :timestamp => 1370888200668,
33
+ :name => 'coffee_consumed',
34
+ :value => 42,
35
+ :agg_type => :sum,
36
+ :filter2 => 'kitchen-1'
37
+ })).to eq("1370888200668\tcoffee_consumed\t42\tsum\t\tkitchen-1")
38
+
39
+ expect(ser.serialize_datapoint({})).to eq("\t\t\t\t\t")
40
+ end
41
+
42
+ it "serializes datapoints to raw format" do
43
+ ser = Sematext::Metrics::RawSerializer.new
44
+ dp1 = {
45
+ :timestamp => 1370889515001,
46
+ :name => 'unicorns_found',
47
+ :value => 101,
48
+ :agg_type => :sum,
49
+ :filter1 => 'Missouri',
50
+ :filter2 => 'Springfield'
51
+ }
52
+ dp2 = {
53
+ :timestamp => 1370889515001,
54
+ :name => 'uptime',
55
+ :value => 36,
56
+ :agg_type => :max,
57
+ :filter1 => 'rack-1',
58
+ :filter2 => 'host-1'
59
+ }
60
+
61
+ expect(ser.serialize_datapoints([dp1, dp2])).to eq(
62
+ "1370889515001\tunicorns_found\t101\tsum\tMissouri\tSpringfield\n"+
63
+ "1370889515001\tuptime\t36\tmax\track-1\thost-1"
64
+ )
65
+ end
66
+ end
@@ -0,0 +1,38 @@
1
+ # Copyright 2013 Sematext Group, Inc.
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 "spec_helper"
16
+
17
+ describe Sematext::Metrics::RawValidator do
18
+ let(:validator) { Sematext::Metrics::RawValidator }
19
+
20
+ it "raises exception when datapoint is invalid" do
21
+ expect {
22
+ validator.validate(
23
+ :timestamp => 123,
24
+ :name => 'coffee_consumed',
25
+ :value => 1234,
26
+ :agg_type => :unknown
27
+ )
28
+ }.to raise_error
29
+
30
+ expect {
31
+ validator.validate(
32
+ :name => 'n' * 256,
33
+ :agg_type => :sum,
34
+ :filter1 => 'filter'
35
+ )
36
+ }.to raise_error
37
+ end
38
+ end
@@ -0,0 +1,22 @@
1
+ # Copyright 2013 Sematext Group, Inc.
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 'rubygems'
16
+ require 'bundler/setup'
17
+
18
+ require 'sematext/metrics'
19
+
20
+ RSpec.configure do |config|
21
+
22
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sematext-metrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Pavel Zalunin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.13.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 2.13.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.11.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.11.0
69
+ description: Gem for talking to SPM
70
+ email:
71
+ - info@sematext.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .travis.yml
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/sematext/metrics.rb
84
+ - lib/sematext/metrics/async_sender.rb
85
+ - lib/sematext/metrics/serializer.rb
86
+ - lib/sematext/metrics/settings.rb
87
+ - lib/sematext/metrics/sync_sender.rb
88
+ - lib/sematext/metrics/validator.rb
89
+ - lib/sematext/metrics/version.rb
90
+ - sematext-metrics.gemspec
91
+ - spec/lib/sematext/client_spec.rb
92
+ - spec/lib/sematext/metrics/serializer_spec.rb
93
+ - spec/lib/sematext/metrics/validator_spec.rb
94
+ - spec/spec_helper.rb
95
+ homepage: http://github.com/sematext/sematext-metrics-gem
96
+ licenses:
97
+ - Apache 2.0
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.0.3
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Gem for sending custom metrics to SPM
119
+ test_files:
120
+ - spec/lib/sematext/client_spec.rb
121
+ - spec/lib/sematext/metrics/serializer_spec.rb
122
+ - spec/lib/sematext/metrics/validator_spec.rb
123
+ - spec/spec_helper.rb