hg-api 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 707dd430082262283db96436e1b07a8c0cca779a
4
- data.tar.gz: aeaf032ff4ca32998942c0b928e1a30e0715070b
3
+ metadata.gz: b4d586a9dc6008c4b2850fb6222040b334fdd3c1
4
+ data.tar.gz: 0191a6b0e33a137be05b4044275297099bfd6ecc
5
5
  SHA512:
6
- metadata.gz: 0b60ca7188810a394c264785f9f20a2fab5bfd81f13f9eb401bb904439c649494c2ef1f70221f18e66ff8d17fed9a3d331e703c319b82217d6b6ab6b2df9038e
7
- data.tar.gz: b29693de3c2080a4faab643d20cb51eacbac2a1fe7e25604a395e879e1edbbe5d9c1e9b89497075b6d4a33f130398d8bd9c5bad9a8e94bb9de3203b16215921b
6
+ metadata.gz: ffb639122d85d49bbcefa9216d4325eb6c627e2a9000d99cba14a2fb3247179597c2ea2ae3c2c720d9b34aaf9dccd50fba520820a485f9f94209c7d07435164c
7
+ data.tar.gz: c2e75bdfb457a5408c53109030f6da6c6a0232595ca8646bd4e6c570ef98365738295cf68c5fb2513dd1478ef74a1a206d44dc55235e720d9319e8c6c89c0dd9
data/README.md CHANGED
@@ -24,7 +24,7 @@ client.metric("signup", 1)
24
24
  client.metric("load", 100, via: :tcp)
25
25
  ```
26
26
 
27
- Default transport is udp, it can be changed by passing options hash while
27
+ Default transport is UDP, it can be changed by passing options hash while
28
28
  constructing client:
29
29
 
30
30
  ```ruby
@@ -34,7 +34,21 @@ client.metric("this.metric.send.over.http", 5)
34
34
 
35
35
  Supported transports: udp, tcp, http.
36
36
 
37
- API KEY must be present as environmental variable HOSTED_GRAPHITE_API_KEY.
37
+ API KEY must be present as environmental variable `HOSTED_GRAPHITE_API_KEY`.
38
+
39
+ Host and port for UDP also can be configured via env vars, `HOSTED_GRAPHITE_HOST`
40
+ and `HOSTED_GRAPHITE_PORT` respectively, as well as URI for HTTP - `HOSTED_GRAPHITE_PORT`.
41
+
42
+ Default settings are `carbon.hostedgraphite.com` host and `2003` port, http uri `https://hostedgraphite.com/api/v1/sink`.
43
+
44
+ Also you can pass prefix, which will be added to each key:
45
+
46
+
47
+ ```ruby
48
+ client = HGAPI.new(prefix: ["apps", "yourappname"])
49
+ # or:
50
+ client = HGAPI.new(prefix: "apps.yourappname")
51
+ ```
38
52
 
39
53
  ## Contributing
40
54
 
data/lib/hg_api/client.rb CHANGED
@@ -8,19 +8,38 @@ module HGAPI
8
8
  HOST = 'carbon.hostedgraphite.com'
9
9
  PORT = 2003
10
10
 
11
+ attr_reader :disabled, :settings
12
+
11
13
  def initialize(options = {})
12
- @default_transport = check_transport!(options[:via]) || :udp
13
- @api_key = ENV["HOSTED_GRAPHITE_API_KEY"]
14
- @disabled = @api_key.nil?
14
+ @settings = build_settings(options)
15
+ @disabled = @settings[:api_key].nil?
15
16
  end
16
17
 
17
18
  def metric(key, value, options = {})
18
19
  return if @disabled
19
- send_metric(key, value, check_transport!(options[:via]) || @default_transport)
20
+ send_metric(key, value, check_transport!(options[:via]) || settings[:default_transport])
21
+ end
22
+
23
+ def time(key, options = {})
24
+ start = Time.now
25
+ result = yield
26
+ metric(key, ((Time.now - start) * 1000).round, options)
27
+ result
20
28
  end
21
29
 
22
30
  private
23
31
 
32
+ def build_settings(options)
33
+ {
34
+ :api_key => ENV["HOSTED_GRAPHITE_API_KEY"],
35
+ :host => ENV["HOSTED_GRAPHITE_HOST"] || HOST,
36
+ :port => ENV["HOSTED_GRAPHITE_PORT"] || PORT,
37
+ :http_uri => ENV["HOSTED_GRAPHITE_HTTP_URI"] || HTTP_URI,
38
+ :default_transport => check_transport!(options[:via]) || :udp,
39
+ :prefix => options[:prefix]
40
+ }
41
+ end
42
+
24
43
  def check_transport!(transport)
25
44
  if transport && !SUPPORTED_TRANSPORTS.include?(transport.to_sym)
26
45
  raise "#{transport} is unsupported transport"
@@ -34,26 +53,34 @@ module HGAPI
34
53
 
35
54
  def send_metric_udp(key, value)
36
55
  sock = UDPSocket.new
37
- sock.send "#{@api_key}.#{key} #{value}\n", 0, HOST, PORT
56
+ sock.send "#{@settings[:api_key]}.#{prefix}#{key} #{value}\n", 0, @settings[:host], @settings[:port]
38
57
  sock.close
39
58
  end
40
59
 
41
60
  def send_metric_tcp(key, value)
42
- conn = TCPSocket.new HOST, PORT
43
- conn.puts "#{@api_key}.#{key} #{value}\n"
61
+ conn = TCPSocket.new @settings[:host], @settings[:port]
62
+ conn.puts "#{@settings[:api_key]}.#{prefix}#{key} #{value}\n"
44
63
  conn.close
45
64
  end
46
65
 
47
66
  def send_metric_http(key, value)
48
- uri = URI(HTTP_URI)
67
+ uri = URI(@settings[:http_uri])
49
68
 
50
69
  req = Net::HTTP::Post.new(uri.request_uri)
51
- req.basic_auth @api_key, nil
52
- req.body = "#{key} #{value}"
70
+ req.basic_auth @settings[:api_key], nil
71
+ req.body = "#{prefix}#{key} #{value}"
53
72
 
54
73
  res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
55
74
  http.request(req)
56
75
  end
57
76
  end
77
+
78
+ def prefix
79
+ @prefix ||= if settings[:prefix] && !settings[:prefix].empty?
80
+ Array(settings[:prefix]).join('.') << '.'
81
+ else
82
+ ""
83
+ end
84
+ end
58
85
  end
59
86
  end
@@ -1,3 +1,3 @@
1
1
  module HGAPI
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hg-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artyom Keydunov