suricate-newrelic 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bb75a73abd2936ea46fab36bf94f76fbdf46640a
4
+ data.tar.gz: f0b40d28fcc576865f6720cfa55f1f3ed2cd5fb1
5
+ SHA512:
6
+ metadata.gz: adbe43c312db5440b586846dfa2168f8416e3bf579db99f54088841815770e4fac3f07b8e5d588d279e780105cef3af2f8c5fd21d7b0a81b70e82cb84f506fd7
7
+ data.tar.gz: 7b6c4d8bdd93f20e91c81c1dd766d40276ee6221489eeabf8ccc2e12cb3993e659fabf05a59752239447b6fd2f56fcd7766da64ee7f138f1b4b694c7ad3094ed
@@ -0,0 +1,12 @@
1
+ module Suricate
2
+ module Newrelic
3
+ autoload :Client, 'suricate/newrelic/client'
4
+ autoload :ResponseTimeMetric, 'suricate/newrelic/response_time_metric'
5
+ autoload :Application, 'suricate/newrelic/application'
6
+ autoload :Status, 'suricate/newrelic/status'
7
+
8
+ # Collectors
9
+ autoload :ResponseTimesLineChartCollector, 'suricate/newrelic/collectors/response_times_line_chart_collector'
10
+ autoload :ApplicationStatusCollector, 'suricate/newrelic/collectors/application_status_collector'
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module Suricate::Newrelic
2
+ class Application
3
+ attr_reader :id, :name, :status
4
+
5
+ def initialize(options = {})
6
+ @id = options[:id]
7
+ @name = options[:name]
8
+ @status = options[:status]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,63 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'date'
4
+
5
+ module Suricate::Newrelic
6
+ class Client
7
+ def initialize(api_key)
8
+ @api_key = api_key
9
+ @url = 'https://api.newrelic.com/v2'
10
+ end
11
+
12
+ def get_app_response_times(app_id, options = {})
13
+ params = {
14
+ from: (options[:from] || (Time.now - 3600 * 14)),
15
+ to: (options[:to] || Time.now),
16
+ period: (options[:period] || 60 * 30),
17
+ 'values[]' => %w(average_call_time call_count),
18
+ 'names[]' => %w(HttpDispatcher)
19
+ }
20
+
21
+ path = "/applications/#{app_id}/metrics/data.json"
22
+ json = get(path, params)
23
+ metrics = json['metric_data']['metrics'][0]['timeslices']
24
+ build_response_times(metrics)
25
+ end
26
+
27
+ def get_application(app_id)
28
+ path = "/applications/#{app_id}.json"
29
+ json = get(path)
30
+ build_application(json['application'])
31
+ end
32
+
33
+ private
34
+ def get(path, params = {})
35
+ uri = URI.parse(@url + path)
36
+ uri.query = URI.encode_www_form(params)
37
+
38
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
39
+ request = Net::HTTP::Get.new(uri)
40
+ request['X-Api-Key'] = @api_key
41
+ http.request(request)
42
+ end
43
+
44
+ JSON.parse(response.body)
45
+ end
46
+
47
+
48
+
49
+ def build_response_times(json)
50
+ json.map do |entry|
51
+ ResponseTimeMetric.new(DateTime.parse(entry['to']).to_time.getlocal,
52
+ entry['values']['average_call_time'],
53
+ entry['values']['call_count'])
54
+ end
55
+ end
56
+
57
+ def build_application(json)
58
+ Application.new(id: json['id'],
59
+ status: Status.new(json['health_status']),
60
+ name: json['name'])
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,24 @@
1
+ module Suricate::Newrelic
2
+ class ApplicationStatusCollector
3
+ def initialize(options = {})
4
+ @client = Client.new(options[:api_key])
5
+ @app_id = options[:app_id]
6
+ end
7
+
8
+ def populate(response, options = {})
9
+ status = application.status
10
+ if status.ok?
11
+ response.ok!
12
+ elsif status.warning?
13
+ response.warning!
14
+ elsif status.alert?
15
+ response.alert!
16
+ end
17
+ end
18
+
19
+ private
20
+ def application
21
+ @client.get_application(@app_id)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,28 @@
1
+ module Suricate::Newrelic
2
+ class ResponseTimesLineChartCollector
3
+ def initialize(options = {})
4
+ @client = Client.new(options.delete(:api_key))
5
+ @app_id = options.delete(:app_id)
6
+ @color = options.delete(:color)
7
+ @options = options
8
+ end
9
+
10
+ def populate(response, options = {})
11
+ metrics = response_times
12
+ dates = metrics.map { |metric| metric.at.strftime('%d/%m - %T') }
13
+ response.chart do |chart|
14
+ chart.labels dates
15
+ chart.serie do |serie|
16
+ serie.name 'Response time'
17
+ serie.values metrics.map(&:value)
18
+ serie.color @color
19
+ end
20
+ end
21
+ end
22
+
23
+ private
24
+ def response_times
25
+ @client.get_app_response_times(@app_id, @options)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,11 @@
1
+ module Suricate::Newrelic
2
+ class ResponseTimeMetric
3
+ attr_reader :at, :value, :calls_count
4
+
5
+ def initialize(at, value, calls_count)
6
+ @at = at
7
+ @value = value
8
+ @calls_count = calls_count
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ module Suricate::Newrelic
2
+ class Status
3
+ def initialize(color)
4
+ @color = color
5
+ end
6
+
7
+ def ok?
8
+ @color == 'green'
9
+ end
10
+
11
+ def warning?
12
+ @color == 'yellow'
13
+ end
14
+
15
+ def alert?
16
+ @color == 'red'
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ module Suricate
2
+ module Newrelic
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: suricate-newrelic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Aurélien AMILIN
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: suricate
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.10.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.10.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: codeclimate-test-reporter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.4.7
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.4.7
111
+ description:
112
+ email:
113
+ - aurelien.amilin@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - lib/suricate/newrelic.rb
119
+ - lib/suricate/newrelic/application.rb
120
+ - lib/suricate/newrelic/client.rb
121
+ - lib/suricate/newrelic/collectors/application_status_collector.rb
122
+ - lib/suricate/newrelic/collectors/response_times_line_chart_collector.rb
123
+ - lib/suricate/newrelic/response_time_metric.rb
124
+ - lib/suricate/newrelic/status.rb
125
+ - lib/suricate/newrelic/version.rb
126
+ homepage: https://github.com/holinnn/suricate-newrelic
127
+ licenses:
128
+ - MIT
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.4.6
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Newrelic plugin for Suricate
150
+ test_files: []