viaduct_metric_catcher 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: efe9f3767f8a61f166f873b6a9a62fbbab64c476
4
+ data.tar.gz: d4acc7d0ba5644d00a1e15eadf8d99e2ac7d68db
5
+ SHA512:
6
+ metadata.gz: 2fcca53f5dc5686f4501ab8ac18e2af3a14ecb9c0d83d2942b654035cfcdc3fec8f7cefb797d1b29dde517fcb9362dfc5c925e6f5049c411f7271f28798d0024
7
+ data.tar.gz: 2d209baed32f52a0897e9e51bde9f34a2bb33655bd8dc1e6baa577d71397e650ff667f3abb69650702180df658b26956c9ec8f2d5d02c8d03195397180e3bbec
@@ -0,0 +1,57 @@
1
+ require 'viaduct/metric_catcher/railtie' if defined?(Rails)
2
+ require 'viaduct/metric_catcher/client'
3
+ require 'viaduct/metric_catcher/error'
4
+ require 'socket'
5
+ require 'logger'
6
+
7
+ module Viaduct
8
+ module MetricCatcher
9
+ class << self
10
+
11
+ attr_accessor :framework, :hostname
12
+ attr_reader :client
13
+ attr_writer :logger
14
+
15
+ def logger
16
+ @logger ||= Logger.new(STDOUT)
17
+ end
18
+
19
+ def token=(token)
20
+ if token =~ /\A[a-zA-Z0-9]{20}\z/
21
+ @client = Client.new(token)
22
+ elsif token.nil?
23
+ @client = nil
24
+ else
25
+ raise Error, "Invalid token provided. Tokens must be 20 characters and only include letters & numbers."
26
+ end
27
+ end
28
+
29
+ def hostname
30
+ hostname_for_viaduct_process || hostname_from_socket
31
+ end
32
+
33
+ def hostname_for_viaduct_process
34
+ @hostname_for_viaduct_process ||= ENV['VDT_PROC'] ? "#{ENV['VDT_PROC']}/#{ENV['VDT_DEPLOYMENT']}" : nil
35
+ end
36
+
37
+ def hostname_from_socket
38
+ @hostname_from_socket ||= Socket.gethostname rescue nil
39
+ end
40
+
41
+ def send_payload(event, payload)
42
+ if self.client
43
+ hash = {
44
+ 'event' => event.to_s,
45
+ 'framework' => self.framework,
46
+ 'hostname' => self.hostname,
47
+ 'payload' => payload
48
+ }
49
+ self.client.send_to_udp(hash)
50
+ else
51
+ false
52
+ end
53
+ end
54
+
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,43 @@
1
+ require 'socket'
2
+ require 'json'
3
+
4
+ module Viaduct
5
+ module MetricCatcher
6
+ class Client
7
+
8
+ class << self
9
+ attr_writer :host, :port
10
+
11
+ def host
12
+ @host ||= "metrics.viaduct.io"
13
+ end
14
+
15
+ def port
16
+ @port ||= 21100
17
+ end
18
+
19
+ def socket
20
+ @socket ||= UDPSocket.new
21
+ end
22
+ end
23
+
24
+ attr_reader :token
25
+
26
+ def initialize(token)
27
+ @token = token
28
+ end
29
+
30
+ def pack_hash_for_udp(hash)
31
+ time = Time.now.utc
32
+ [1, time.to_i, time.nsec, @token, hash.to_json].pack('NNNa20a*')
33
+ end
34
+
35
+ def send_to_udp(hash)
36
+ self.class.socket.send(pack_hash_for_udp(hash), 0, self.class.host, self.class.port)
37
+ rescue => e
38
+ Viaduct::MetricCatcher.logger.warn "[Viaduct Metrics] Exception while sending metrics. #{e.class} #{e.message}"
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,6 @@
1
+ module Viaduct
2
+ module MetricCatcher
3
+ class Error < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,42 @@
1
+ module Viaduct
2
+ module MetricCatcher
3
+ class Railtie < ::Rails::Railtie
4
+
5
+ initializer 'viaduct.metric-catcher.notifications' do
6
+ # Set the framework to 'rails'
7
+ Viaduct::MetricCatcher.framework = 'rails'
8
+ # Set the logger to use the standard Rails logger
9
+ Viaduct::MetricCatcher.logger = Rails.logger
10
+ # Subscribe to all requests and send request details whenever a request
11
+ # happens.
12
+ ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args|
13
+ event = ActiveSupport::Notifications::Event.new(*args)
14
+ Viaduct::MetricCatcher.send_payload(:request, {
15
+ 'method' => event.payload[:method],
16
+ 'path' => event.payload[:path],
17
+ 'status' => event.payload[:status],
18
+ 'time' => event.duration,
19
+ 'extra' => {
20
+ 'db_runtime' => event.payload[:db_runtime],
21
+ 'view_runtime' => event.payload[:view_runtime],
22
+ 'controller' => event.payload[:controller],
23
+ 'action' => event.payload[:action]
24
+ }
25
+ })
26
+ end
27
+ # Subscribe to all database queries and send quer details whenever a
28
+ # query is executed.
29
+ ActiveSupport::Notifications.subscribe "sql.active_record" do |*args|
30
+ event = ActiveSupport::Notifications::Event.new(*args)
31
+ Viaduct::MetricCatcher.send_payload(:database_query, {
32
+ 'query' => event.payload[:sql],
33
+ 'time' => event.duration,
34
+ 'extra' => {
35
+ 'name' => event.payload[:name]
36
+ }
37
+ })
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1 @@
1
+ require 'viaduct/metric_catcher'
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: viaduct_metric_catcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Cooke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Automatically catch metrics and send them to the Viaduct Metrics service
14
+ email:
15
+ - adam@viaduct.io
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/viaduct/metric_catcher.rb
21
+ - lib/viaduct/metric_catcher/client.rb
22
+ - lib/viaduct/metric_catcher/error.rb
23
+ - lib/viaduct/metric_catcher/railtie.rb
24
+ - lib/viaduct_metric_catcher.rb
25
+ homepage: https://github.com/viaduct/metric-catcher
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.4.5
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Automatically catch metrics and send them to the Viaduct Metrics service
49
+ test_files: []