metrics_adapter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6525fffc00cde5df061ba6ce5621b9db422ddd5d09393b754ce5919b7c536f87
4
+ data.tar.gz: 0663ee07b6c96170f4b4050e90821b64093ff9fe9e784d14b28c2478139ceda7
5
+ SHA512:
6
+ metadata.gz: 4ccad7b481311aaa2b2bc79eac01993f466e0a842ca5428d63b87aa53554da2a44148e08f5dec0305b374ee187272d64c21137becf530cde6e85c129fb4c46e4
7
+ data.tar.gz: 3f6bd8da506014433441d190aed87607800e2bd1d1a21ad2cb2c07bf9bba1b72349980db823fef09d8a38730c8bff39772b5736ef8fda57459ae1e859550315a
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 1.17.3
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Tomas D'Stefano
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,89 @@
1
+ # Metrics Adapter
2
+
3
+ A simple gem that collects application data to send to a metrics service.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'metrics_adapter'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install metrics_adapter
20
+
21
+ ## Setup
22
+
23
+ You can setup the following adapters/services:
24
+
25
+ 1. Mixpanel - https://mixpanel.com/
26
+ 2. Keen - https://keen.io/
27
+
28
+ ## Setup Mixpanel
29
+
30
+ Below we setup Mixpanel service using the slow request tracker:
31
+
32
+ ```ruby
33
+ # config/initializer/metrics_adapter.rb
34
+ require 'metrics_adapter/rails'
35
+
36
+ GeneralMetrics.configure do |config|
37
+ config.adapter = :mixpanel
38
+ config.adapter_options = { secret: 'my-secret' }
39
+ config.extra_attributes = { app: 'My-App' }
40
+ config.logger = Rails.logger
41
+ config.trackers = %i(slow_request)
42
+ config.thresholds = { slow_request: 1_000 }
43
+ end
44
+ ```
45
+
46
+ ## Setup Keen
47
+
48
+ Below we setup the Keen service using the slow request tracker:
49
+
50
+ ```ruby
51
+ # config/initializer/metrics_adapter.rb
52
+ require 'metrics_adapter/rails'
53
+
54
+ GeneralMetrics.configure do |config|
55
+ config.adapter = :keen
56
+ config.adapter_options = {
57
+ project_id: 'my-project-id',
58
+ write_key: 'my-write-key',
59
+ }
60
+ config.extra_attributes = { app: 'My-App' }
61
+ config.logger = Rails.logger
62
+ config.trackers = %i(slow_request)
63
+ config.thresholds = { slow_request: 1_000 }
64
+ end
65
+ ```
66
+
67
+ ## Metrics
68
+
69
+ For the first version, we support for now, the rails event 'process_action.action_controller'.
70
+
71
+ We will add more trackers as we go. We can pickup ideas from [rails docs](https://guides.rubyonrails.org/active_support_instrumentation.html#sql-active-record)
72
+
73
+ ### Slow request
74
+
75
+ If the request time is bigger than the slow request threshold, then the event
76
+ will be published by the adapter.
77
+
78
+ The threshold can be setup by:
79
+
80
+ ```ruby
81
+ config.thresholds = { slow_request: 1_000 }
82
+ ```
83
+
84
+ This will tell the gem to send metrics only if the request time are greater than
85
+ 1 second.
86
+
87
+ ## License
88
+
89
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "metrics_adapter"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,36 @@
1
+ require 'metrics_adapter/version'
2
+ require 'active_support/all'
3
+
4
+ module MetricsAdapter
5
+ mattr_accessor :adapter
6
+ mattr_accessor :logger
7
+
8
+ mattr_accessor :adapter_options
9
+ self.adapter_options = {}
10
+
11
+ mattr_accessor :extra_attributes
12
+ self.extra_attributes = {}
13
+
14
+ mattr_accessor :trackers
15
+ self.trackers = []
16
+
17
+ mattr_accessor :thresholds
18
+ self.thresholds = {}
19
+
20
+ mattr_accessor :conditionals
21
+ self.conditionals = {}
22
+
23
+ module Adapters
24
+ autoload :Mixpanel, 'metrics_adapter/adapters/mixpanel'
25
+ autoload :Keen, 'metrics_adapter/adapters/keen'
26
+ end
27
+
28
+ module Trackers
29
+ autoload :Base, 'metrics_adapter/trackers/base'
30
+ autoload :SlowRequest, 'metrics_adapter/trackers/slow_request'
31
+ end
32
+
33
+ def self.configure(&block)
34
+ yield self
35
+ end
36
+ end
@@ -0,0 +1,19 @@
1
+ require 'keen'
2
+
3
+ class MetricsAdapter::Adapters::Keen
4
+ attr_reader :project_id, :write_key
5
+
6
+ def initialize(options)
7
+ @project_id = options.fetch(:project_id)
8
+ @write_key = options.fetch(:write_key)
9
+ end
10
+
11
+ def publish(data)
12
+ name = data.delete(:name)
13
+
14
+ ::Keen.project_id = project_id
15
+ ::Keen.write_key = write_key
16
+
17
+ ::Keen.publish(name, data)
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ require 'mixpanel-ruby'
2
+
3
+ class MetricsAdapter::Adapters::Mixpanel
4
+ attr_reader :tracker
5
+
6
+ def initialize(options)
7
+ @tracker = ::Mixpanel::Tracker.new(options.fetch(:secret))
8
+ @logger = ::MetricsAdapter.logger
9
+ end
10
+
11
+ def publish(data)
12
+ id = data.delete(:id)
13
+ name = data.delete(:name)
14
+ tracker.track(id, name, data)
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ ## Request trackers:
2
+ ## 1. Tracks slow request given a threshold
3
+ ##
4
+ ActiveSupport::Notifications.subscribe('process_action.action_controller') do |event|
5
+ trackers = MetricsAdapter.trackers
6
+ conditional = MetricsAdapter.conditionals[:slow_request]
7
+
8
+ if conditional.present? && conditional.call.blank?
9
+ Rails.logger.info('Metrics Adapter wont publish slow request metric.')
10
+ else
11
+ MetricsAdapter::Trackers::SlowRequest.new(event).call if trackers.include?(:slow_request)
12
+ end
13
+ end
@@ -0,0 +1,26 @@
1
+ module MetricsAdapter
2
+ module Trackers
3
+ class Base
4
+ attr_reader :adapter,
5
+ :event,
6
+ :extra_attributes,
7
+ :logger,
8
+ :metric_name
9
+
10
+ def initialize(event)
11
+ adapter_name = MetricsAdapter.adapter.to_s.classify
12
+ @adapter = "MetricsAdapter::Adapters::#{adapter_name}".constantize.new(
13
+ MetricsAdapter.adapter_options
14
+ )
15
+ @event = event
16
+ @extra_attributes = ::MetricsAdapter.extra_attributes
17
+ @logger = ::MetricsAdapter.logger
18
+ @metric_name = self.class.name.demodulize.underscore
19
+ end
20
+
21
+ def call
22
+ raise NotImplementedError
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ module MetricsAdapter
2
+ module Trackers
3
+ class SlowRequest < Base
4
+ attr_reader :slow_request_threshold
5
+
6
+ def initialize(event)
7
+ @slow_request_threshold = ::MetricsAdapter.thresholds.fetch(:slow_request)
8
+ super
9
+ end
10
+
11
+ def call
12
+ if event.duration > slow_request_threshold
13
+ data = {
14
+ id: event.transaction_id,
15
+ name: metric_name,
16
+ duration: (event.duration / 1000),
17
+ method: event.payload[:method].to_s.downcase,
18
+ cpu_time_in_ms: event.cpu_time,
19
+ idle_time_in_ms: event.idle_time,
20
+ path: event.payload[:path]
21
+ }.merge(extra_attributes)
22
+ adapter.publish(data)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module MetricsAdapter
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'metrics_adapter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'metrics_adapter'
8
+ spec.version = MetricsAdapter::VERSION
9
+ spec.authors = ['Form builder developers']
10
+ spec.email = ['form-builder-team@digital.justice.gov.uk']
11
+
12
+ spec.summary = %q{A gem that collect application metrics.}
13
+ spec.description = %q{Collect custom metrics from your application.}
14
+ spec.homepage = 'https://github.com/ministryofjustice/metrics_adapter'
15
+ spec.license = 'MIT'
16
+
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'activesupport'
26
+ spec.add_dependency 'mixpanel-ruby'
27
+ spec.add_dependency 'keen'
28
+
29
+ spec.add_development_dependency 'bundler', '~> 1.17'
30
+ spec.add_development_dependency 'rake'
31
+ spec.add_development_dependency 'rspec'
32
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metrics_adapter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Form builder developers
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-06-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mixpanel-ruby
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: keen
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.17'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.17'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Collect custom metrics from your application.
98
+ email:
99
+ - form-builder-team@digital.justice.gov.uk
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - lib/metrics_adapter.rb
114
+ - lib/metrics_adapter/adapters/keen.rb
115
+ - lib/metrics_adapter/adapters/mixpanel.rb
116
+ - lib/metrics_adapter/rails.rb
117
+ - lib/metrics_adapter/trackers/base.rb
118
+ - lib/metrics_adapter/trackers/slow_request.rb
119
+ - lib/metrics_adapter/version.rb
120
+ - metrics_adapter.gemspec
121
+ homepage: https://github.com/ministryofjustice/metrics_adapter
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubygems_version: 3.0.6
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: A gem that collect application metrics.
144
+ test_files: []