sematext-metrics 0.0.1.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YjBmNTFiNWNlODliZGU5NWFhODVkZDg2YTExNmUzOTJlMDExZDYyNw==
4
+ ODUxZjQ0NWQ1OWVjZDQ5YmY1ODg2OWI1Yzg2YjE3NGRhOTlkZWEwMw==
5
5
  data.tar.gz: !binary |-
6
- MGE1NTIzZDU0NmQ1YTY0Y2NmMGE3ZTIyNjdjNDY1OGE0ZGI5NmM3YQ==
6
+ NWY1YmI0YjE4YjRhZDU1ZDI4MDRlYzMwNDU5MDdlZGYxMmY0ODJkMQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZjI1ZmZlMzI4NmY2MzBjYWE1N2M3NTk4YzZlOTQ0NTFiZDZhYjhiMzc1YWM3
10
- YjMwN2QyMGI2OTAyZGYyNDdmY2FmMmFhOWMwYTE3MGE4ODA3MmM1MjA3Zjg0
11
- YjE3MGNlNDVjMzQyNzhkZjMyNGRmMmZlZmNkYWQ2YzYxNWY3ZDM=
9
+ ZmZhMWNhODQxNmE3NTYzYzZhMjBlZDZiZTY1MTE0MjQ4ZDhlOWFhZDM5ZmVj
10
+ NmUzNzAyYTI4NDg4ODkwZGU0MDY5M2EwODBlMGVkOWViMTZjYjhkZjQyM2Q4
11
+ ZDNjODZhNzlmNWYzNjE0N2QxODA3ZTY1MmNjZWIwOGYxNjkxNjc=
12
12
  data.tar.gz: !binary |-
13
- NzRiMWU4YzBiYmIwMjk5ZTJlNDc3ODUzZGQwNzNkNGY2MzhiYTVhY2U4MDcw
14
- ZmI2YmZiOWY3MGU3ZWUzMWJjNTc2MDFkYWEwNzBiODRlM2MwZGM2ZDBlY2Zk
15
- NjM5YWE0NTE2YzExOGY3YjFkNmVlZjRlY2M0MzVjNDBkMzdkM2Y=
13
+ YzViYzMxODMxY2Q4YTRmYWMyYzkxOWY5MGQyZDE2NmFkM2YwNWQ3NmEyNzhk
14
+ NjQwNDM5MjMzZThlODFmYzc3ZWUyZDdkMWMwY2FkZGNmYzAwZjJkOTc1MDY3
15
+ YTE3NzRlZDQ1MGIxZTQyNmEwODk0NmNmN2JhMjJhYjRiZDk3ZGY=
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sematext-metrics (0.0.1)
4
+ sematext-metrics (0.0.2)
5
5
  httparty (~> 0.11.0)
6
6
 
7
7
  GEM
@@ -11,9 +11,9 @@ GEM
11
11
  httparty (0.11.0)
12
12
  multi_json (~> 1.0)
13
13
  multi_xml (>= 0.5.2)
14
- multi_json (1.7.6)
15
- multi_xml (0.5.4)
16
- rake (10.0.4)
14
+ multi_json (1.8.2)
15
+ multi_xml (0.5.5)
16
+ rake (10.1.0)
17
17
  rspec (2.13.0)
18
18
  rspec-core (~> 2.13.0)
19
19
  rspec-expectations (~> 2.13.0)
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # sematext-metrics gem
1
+ # sematext-metrics gem[![Build Status](https://travis-ci.org/sematext/sematext-metrics-gem.png)](https://travis-ci.org/sematext/sematext-metrics-gem)
2
2
 
3
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
4
 
@@ -62,6 +62,14 @@ To use different tokens for different applications, use `sync` or `async` factor
62
62
  user_metrics.send_batch user_datapoints
63
63
  search_metrics.send_batch search_datapoints
64
64
 
65
+ To send metrics to on-premises SPM setup you can also configure endpoint url:
66
+
67
+ user_metrics = Sematext::Metrics::Client.sync("[token]", "http://spm-receiver.example.com/spm-receiver/custom/receive.raw")
68
+ search_metrics = Sematext::Metrics::Client.async("[token]", "http://spm-receiver.example.com/spm-receiver/custom/receive.raw")
69
+
70
+ or
71
+
72
+ Sematext::Metrics.initialize("[token]", :receiver_url => "http://spm-receiver.example.com/spm-receiver/custom/receive.raw")
65
73
 
66
74
  ## Asynchronous sending
67
75
 
@@ -72,7 +80,7 @@ In order to use this extension add `em-http-request` gem to your application ma
72
80
 
73
81
  Asynchronous data sending can be configured during initialization:
74
82
 
75
- Sematext::Metrics.initialize("[token]", async=true)
83
+ Sematext::Metrics.initialize("[token]", :async => true)
76
84
 
77
85
  Or use `async` factory method to create a client instance:
78
86
 
@@ -21,9 +21,16 @@ require "sematext/metrics/async_sender"
21
21
 
22
22
  module Sematext
23
23
  module Metrics
24
- def self.initialize(token, async = false)
24
+ def self.initialize(token, args = {})
25
25
  raise "Token should be defined" unless token
26
- @client = async ? Client.async(token) : Client.sync(token)
26
+
27
+ opts = { :async => false }
28
+ if !!args == args
29
+ opts[:async] = args
30
+ elsif args.kind_of?(Hash)
31
+ opts.merge!(args)
32
+ end
33
+ @client = opts[:async] ? Client.async(token, opts[:receiver_url]) : Client.sync(token, opts[:receiver_url])
27
34
  end
28
35
 
29
36
  def self.send datapoint
@@ -42,12 +49,20 @@ module Sematext
42
49
 
43
50
  class Client
44
51
  class << self
45
- def sync(token)
46
- Client.new(SyncSender.new(token))
52
+ def sync(token, receiver_url = nil)
53
+ url, path = parse_url(receiver_url) if receiver_url
54
+ Client.new(SyncSender.new(token, url, path))
47
55
  end
48
56
 
49
- def async(token)
50
- Client.new(AsyncSender.new(token))
57
+ def async(token, receiver_url = nil)
58
+ url, path = parse_url(receiver_url) if receiver_url
59
+ Client.new(AsyncSender.new(token, url, path))
60
+ end
61
+
62
+ private
63
+ def parse_url(url)
64
+ uri = URI.parse(url)
65
+ ["#{uri.scheme}://#{uri.host}:#{uri.port}", uri.path]
51
66
  end
52
67
  end
53
68
 
@@ -77,7 +92,7 @@ module Sematext
77
92
  @sender.send serialized
78
93
  end
79
94
  end
80
-
95
+
81
96
  def epoch
82
97
  Time.now.to_i * 1000
83
98
  end
@@ -15,8 +15,10 @@
15
15
  module Sematext
16
16
  module Metrics
17
17
  class AsyncSender
18
- def initialize(token)
18
+ def initialize(token, base_uri, path)
19
19
  @token = token
20
+ @path = path || Settings::RECEIVER_PATH
21
+ @base_uri = base_uri || Settings::RECEIVER_URI
20
22
  if defined?(EventMachine)
21
23
  require 'em-http-request'
22
24
  end
@@ -26,7 +28,7 @@ module Sematext
26
28
  deferrable = EventMachine::DefaultDeferrable.new
27
29
 
28
30
  post_options = {
29
- :path => Settings::RECEIVER_PATH,
31
+ :path => @path,
30
32
  :query => {:token => @token},
31
33
  :body => data,
32
34
  :head => {
@@ -35,7 +37,7 @@ module Sematext
35
37
  }
36
38
  }
37
39
 
38
- http = EventMachine::HttpRequest.new(Settings::RECEIVER_URI).post post_options
40
+ http = EventMachine::HttpRequest.new(@base_uri).post post_options
39
41
  http.errback {
40
42
  deferrable.fail{{:status => :failed}}
41
43
  }
@@ -19,9 +19,10 @@ module Sematext
19
19
  class SyncSender
20
20
  include HTTParty
21
21
 
22
- def initialize(token)
22
+ def initialize(token, base_uri, path)
23
23
  @token = token
24
- self.class.default_options[:base_uri] = Settings::RECEIVER_URI
24
+ self.class.default_options[:base_uri] = base_uri || Settings::RECEIVER_URI
25
+ @path = path || Settings::RECEIVER_PATH
25
26
  end
26
27
 
27
28
  def send data
@@ -33,7 +34,7 @@ module Sematext
33
34
  },
34
35
  :body => data
35
36
  }
36
- response = self.class.post(Settings::RECEIVER_PATH, post_options)
37
+ response = self.class.post(@path, post_options)
37
38
  if response.code == 201
38
39
  {:status => :succeed}
39
40
  else
@@ -30,6 +30,8 @@ module Sematext
30
30
  [:name, :value, :agg_type].each do |field|
31
31
  check_obligatory field, datapoint[field]
32
32
  end
33
+ raise "name can't be empty" if datapoint[:name].empty?
34
+ raise "value should be a number" unless datapoint[:value].kind_of?(Numeric)
33
35
  [:name, :filter1, :filter2].each do |field|
34
36
  value = datapoint[field]
35
37
  check_bounds field, datapoint[field] if value
@@ -14,6 +14,6 @@
14
14
 
15
15
  module Sematext
16
16
  module Metrics
17
- VERSION = "0.0.1.1"
17
+ VERSION = "0.0.2"
18
18
  end
19
19
  end
@@ -15,24 +15,97 @@
15
15
  require "spec_helper"
16
16
 
17
17
  describe Sematext::Metrics::RawValidator do
18
- let(:validator) { Sematext::Metrics::RawValidator }
18
+ let(:validator) { Sematext::Metrics::RawValidator.new }
19
+
20
+ it "raises exception when name is not set or empty" do
21
+ expect {
22
+ validator.validate(
23
+ :name => '',
24
+ :agg_type => :sum,
25
+ :value => 42
26
+ )
27
+ }.to raise_error
28
+
29
+ expect {
30
+ validator.validate(
31
+ :agg_type => :sum,
32
+ :value => 42
33
+ )
34
+ }.to raise_error
35
+ end
36
+
37
+ it "raises exception when value is not set" do
38
+ expect {
39
+ validator.validate(
40
+ :name => 'name',
41
+ :agg_type => :sum
42
+ )
43
+ }.to raise_error
44
+ end
19
45
 
20
- it "raises exception when datapoint is invalid" do
46
+ it "raises exceptionw when agg_type is not set or invalid" do
21
47
  expect {
22
48
  validator.validate(
23
- :timestamp => 123,
24
- :name => 'coffee_consumed',
25
- :value => 1234,
26
- :agg_type => :unknown
49
+ :name => 'name',
50
+ :value => 42
27
51
  )
28
52
  }.to raise_error
29
-
30
53
  expect {
31
54
  validator.validate(
32
- :name => 'n' * 256,
55
+ :name => 'name',
56
+ :value => 42,
57
+ :agg_type => :invalid
58
+ )
59
+ }.to raise_error
60
+ end
61
+
62
+ it "raises exception when name, filter1 or filter2 too log" do
63
+ expect {
64
+ validator.validate(
65
+ :name => 'a' * 256,
33
66
  :agg_type => :sum,
34
- :filter1 => 'filter'
67
+ :value => 42
68
+ )
69
+ }.to raise_error
70
+ expect {
71
+ validator.validate(
72
+ :name => 'name',
73
+ :agg_type => :sum,
74
+ :value => 42,
75
+ :filter1 => 'a' * 256
76
+ )
77
+ }.to raise_error
78
+ expect {
79
+ validator.validate(
80
+ :name => 'name',
81
+ :agg_type => :sum,
82
+ :value => 42,
83
+ :filter2 => 'a' * 256
84
+ )
85
+ }.to raise_error
86
+ end
87
+
88
+ it "raises exception when value is not a number" do
89
+ expect {
90
+ validator.validate(
91
+ :name => 'name',
92
+ :agg_type => :sum,
93
+ :value => 'foo'
35
94
  )
36
95
  }.to raise_error
96
+ expect {
97
+ validator.validate(
98
+ :name => 'name',
99
+ :agg_type => :sum,
100
+ :value => 100.2
101
+ )
102
+ }.to_not raise_error
103
+ expect {
104
+ validator.validate(
105
+ :name => 'name',
106
+ :agg_type => :sum,
107
+ :value => 100
108
+ )
109
+ }.to_not raise_error
37
110
  end
38
111
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sematext-metrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Zalunin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-11 00:00:00.000000000 Z
11
+ date: 2013-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler