datadog_rails 0.1.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 48f1bb76428653e4d7194f2e32f02565e47056fa
4
- data.tar.gz: 21120e0e20705f90a69cd815bc34970680dfe5d7
3
+ metadata.gz: 6052d1d544fcafc214dfd11756c58686c7344dd5
4
+ data.tar.gz: 6648e0ef9258937c998f4cacfbd5865bda341df7
5
5
  SHA512:
6
- metadata.gz: b56c25ec68f93429209eee78cfb365780b59dfd93b830c907721de0dde4cf926aad3b3a6b75486f5ef7ebd61d061f07a0e9f5a5330b6eabe32660198608f5c21
7
- data.tar.gz: bc0444d4c159ad1d9c8086fda3520e4e4e23314d2caefa45747698f4bb417f5b1ccbf9dfc4e27d697b9cfb92975d1228ec1632360b90e625ca7971abf3706f88
6
+ metadata.gz: 1967e90ef10456fa9e75930a31726887cc5f17549d7f09d526252730ea03430cc9bd272806cf546134e081c4033f7c2a726e9ddbe8fa5fe6af84ca4b92ebce70
7
+ data.tar.gz: a3e9566fb828f0aa063fe3768f70050af379eaaa33e19f46f1ad37d4c1071171f91416449ba6abfddafc55521943a61cd186bc6b28eb894fceca825db724cab3
@@ -1 +1,67 @@
1
- require 'datadog_rails/railtie' if defined?(Rails)
1
+ require "datadog_rails/configuration"
2
+
3
+ require 'datadog_rails/railtie' if defined?(Rails::Railtie)
4
+
5
+ module DatadogRails
6
+ class << self
7
+ attr_writer :configuration
8
+ attr_writer :client
9
+
10
+ # Embed in a String to clear all previous ANSI sequences.
11
+ CLEAR = "\e[0m"
12
+ BOLD = "\e[1m"
13
+
14
+ # Colors
15
+ BLACK = "\e[30m"
16
+ RED = "\e[31m"
17
+ GREEN = "\e[32m"
18
+ YELLOW = "\e[33m"
19
+ BLUE = "\e[34m"
20
+ MAGENTA = "\e[35m"
21
+ CYAN = "\e[36m"
22
+ WHITE = "\e[37m"
23
+
24
+ def configuration
25
+ @configuration ||= DatadogRails::Configuration.new
26
+ end
27
+
28
+ def configure
29
+ yield(configuration)
30
+
31
+ self.client = nil
32
+ end
33
+
34
+ def client
35
+ @client ||= Statsd.new configuration.dogstatsd_host, configuration.dogstatsd_port
36
+ end
37
+
38
+ def send_event_to_statsd(payload)
39
+ key_name = "#{configuration.application_name}.#{payload[:measurement]}"
40
+
41
+ if payload[:action] == :increment
42
+ client.increment key_name, :tags => payload[:tags]
43
+ else
44
+ client.histogram key_name, payload[:value], :tags => payload[:tags]
45
+ end
46
+
47
+ log_event(payload) if configuration.debug?
48
+ end
49
+
50
+ def log_event(payload)
51
+ key_name = "#{configuration.application_name}.#{payload[:measurement]}"
52
+
53
+ info = "\n"
54
+ info << " \tmethod => #{payload[:action]}\n"
55
+ info << " \tmetric => #{key_name}\n"
56
+ info << " \tvalue => #{payload[:value]}\n"
57
+ info << " \ttags => #{payload[:tags]}"
58
+
59
+ Rails.logger.info color(" DatadogRails", GREEN, true) + color(info, WHITE, true)
60
+ end
61
+
62
+ def color(text, color, bold=false)
63
+ bold = bold ? BOLD : ""
64
+ "#{bold}#{color}#{text}#{CLEAR}"
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,34 @@
1
+ module DatadogRails
2
+ class Configuration
3
+ attr_accessor :application_name
4
+ attr_accessor :dogstatsd_host
5
+ attr_accessor :dogstatsd_port
6
+ attr_accessor :instrumentation_environments
7
+ attr_accessor :instrumentation_enabled
8
+ attr_accessor :debug
9
+
10
+ DEFAULTS = {
11
+ :dogstatsd_host => "localhost",
12
+ :dogstatsd_port => 8125,
13
+ :instrumentation_environments => ["production"],
14
+ :instrumentation_enabled => true,
15
+ :debug => false
16
+ }
17
+
18
+ def initialize
19
+ @dogstatsd_host = DEFAULTS[:dogstatsd_host]
20
+ @dogstatsd_port = DEFAULTS[:dogstatsd_port]
21
+ @instrumentation_environments = DEFAULTS[:instrumentation_environments]
22
+ @instrumentation_enabled = DEFAULTS[:instrumentation_enabled]
23
+ @debug = DEFAULTS[:debug]
24
+ end
25
+
26
+ def debug?
27
+ !!@debug
28
+ end
29
+
30
+ def instrumentation_enabled?
31
+ !!@instrumentation_enabled
32
+ end
33
+ end
34
+ end
@@ -3,47 +3,47 @@ require 'statsd'
3
3
 
4
4
  module DatadogRails
5
5
  class Railtie < Rails::Railtie
6
- initializer "datadog_rails.configure_rails_initialization" do
7
- # Create a stats instance.
8
- $statsd = Statsd.new('localhost', 8125)
6
+ config.datadog_rails = ActiveSupport::OrderedOptions.new
9
7
 
8
+ initializer "datadog_rails.configure" do |app|
9
+ DatadogRails.configure do |config|
10
+ config.application_name = app.config.datadog_rails[:application_name] || ::Rails.application.class.parent_name.underscore
11
+ config.dogstatsd_host = app.config.datadog_rails[:dogstatsd_host] || config.dogstatsd_host
12
+ config.dogstatsd_port = app.config.datadog_rails[:dogstatsd_port] || config.dogstatsd_port
13
+ config.instrumentation_environments = app.config.datadog_rails[:instrumentation_environments] || config.instrumentation_environments
14
+ config.instrumentation_enabled = app.config.datadog_rails[:instrumentation_enabled] || config.instrumentation_enabled
15
+ config.debug = app.config.datadog_rails[:debug] || config.debug
16
+ end
17
+ end
18
+
19
+ initializer "datadog_rails.configure_rails_initialization" do |app|
10
20
  # Setting up a subscriber to /process_action.action_controller/
11
21
  ActiveSupport::Notifications.subscribe /process_action.action_controller/ do |*args|
12
22
  event = ActiveSupport::Notifications::Event.new(*args)
23
+
13
24
  controller = "controller:#{event.payload[:controller]}"
14
- action = "action:#{event.payload[:action]}"
15
- format = "format:#{event.payload[:format] || 'all'}"
16
- format = "format:all" if format == "format:*/*"
17
- host = "host:#{ENV['INSTRUMENTATION_HOSTNAME']}"
18
- status = event.payload[:status]
19
- tags = [controller, action, format, host]
25
+ action = "action:#{event.payload[:action]}"
26
+ format = "format:#{event.payload[:format] || 'all'}"
27
+ format = "format:all" if format == "format:*/*"
28
+ status = "status:#{event.payload[:status]}"
29
+
30
+ tags = [controller, action, format, status]
20
31
 
21
- ActiveSupport::Notifications.instrument :performance, :action => :timing, :tags => tags, :measurement => "request.total_duration", :value => event.duration
22
- ActiveSupport::Notifications.instrument :performance, :action => :timing, :tags => tags, :measurement => "database.query.time", :value => event.payload[:db_runtime]
23
- ActiveSupport::Notifications.instrument :performance, :action => :timing, :tags => tags, :measurement => "web.view.time", :value => event.payload[:view_runtime]
24
- ActiveSupport::Notifications.instrument :performance, :tags => tags, :measurement => "request.status.#{status}"
32
+ ActiveSupport::Notifications.instrument :performance, :action => :increment, :tags => tags, :measurement => "request.number", :value => 1
33
+ ActiveSupport::Notifications.instrument :performance, :action => :histogram, :tags => tags, :measurement => "request.total_duration", :value => event.duration
34
+ ActiveSupport::Notifications.instrument :performance, :action => :histogram, :tags => tags, :measurement => "database.query.time", :value => event.payload[:db_runtime]
35
+ ActiveSupport::Notifications.instrument :performance, :action => :histogram, :tags => tags, :measurement => "web.view.time", :value => event.payload[:view_runtime]
25
36
  end
26
37
 
27
38
  # Setting up a subscriber to /performance/
28
39
  ActiveSupport::Notifications.subscribe /performance/ do |name, start, finish, id, payload|
29
- send_event_to_statsd(name, payload) if DatadogRails.configuration.environments.include?(Rails.env)
30
- end
31
-
32
- # Send metric to DogStatsD
33
- def send_event_to_statsd(name, payload)
34
- action = payload[:action] || :increment
35
- measurement = payload[:measurement]
36
- value = payload[:value]
37
- tags = payload[:tags]
38
- key_name = "#{name.to_s.capitalize}.#{measurement}"
39
-
40
- if action == :increment
41
- $statsd.increment key_name, :tags => tags
42
- else
43
- $statsd.histogram key_name, value, :tags => tags
40
+ if DatadogRails.configuration.instrumentation_enabled? and
41
+ DatadogRails.configuration.instrumentation_environments.include?(Rails.env)
42
+ DatadogRails.send_event_to_statsd(payload)
43
+ elsif DatadogRails.configuration.instrumentation_enabled?
44
+ DatadogRails.log_event(payload)
44
45
  end
45
46
  end
46
-
47
47
  end
48
48
  end
49
- end
49
+ end
@@ -1,3 +1,3 @@
1
1
  module DatadogRails
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datadog_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrique Menezes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-30 00:00:00.000000000 Z
11
+ date: 2016-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -45,10 +45,9 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
- - MIT-LICENSE
49
- - README.rdoc
50
48
  - Rakefile
51
49
  - lib/datadog_rails.rb
50
+ - lib/datadog_rails/configuration.rb
52
51
  - lib/datadog_rails/railtie.rb
53
52
  - lib/datadog_rails/version.rb
54
53
  - lib/tasks/datadog_rails_tasks.rake
@@ -1,20 +0,0 @@
1
- Copyright 2016 Henrique Menezes
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,3 +0,0 @@
1
- = DatadogRails
2
-
3
- This project rocks and uses MIT-LICENSE.