leafy-rack 0.4.0-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d1239867bb5be43b57a52dcce8f281d146b9edf1
4
+ data.tar.gz: 51137ff0563f9a8e922f4af160253f9b34a9c66c
5
+ SHA512:
6
+ metadata.gz: 90e49272d516440d2208bfd84d922d94fbfe0eb948c0e76ba228e104a1e21e2314f802e68ee9e26a4df0121eee008c02a5389ce1eadd4bcce207a547f90f13e1
7
+ data.tar.gz: 669522fc78cea7d8bee811485e82b15b8ca288f65d3ea8791dd9b3cf6cb198473e221780e147bf06cb719a94f95adb0324272031f626c73eaaab06a6c874d7bf
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .yardoc
2
+ doc
3
+ pkg
4
+ *.lock
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ #-*- mode: ruby -*-
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'leafy-metrics', :path => '../leafy-metrics'
6
+ gem 'leafy-health', :path => '../leafy-health'
7
+
8
+ gemspec
9
+
10
+ # vim: syntax=Ruby
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Lookout
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,155 @@
1
+ # Leafy-Rack
2
+
3
+ ## installation
4
+
5
+ via rubygems
6
+ ```
7
+ gem install leafy-rack
8
+ ```
9
+ or add to your Gemfile
10
+ ```
11
+ gem 'leafy-rack
12
+ ```
13
+
14
+ installing the gem also takes care of the jar dependencies with jruby-1.7.16+
15
+
16
+ ## intro
17
+
18
+ there actually three parts to this gem
19
+
20
+ * serializers to write out json data from the collected data of ```Leafy::Health::Registry``` and ```Leafy::Metrics::Registry```
21
+
22
+ * instrumented class which is almost like a rack middleware but is threadsafe and is meant to be shared with **ALL** requests. with this sharing in can count the number of active requests.
23
+
24
+ * a collection of middleware
25
+
26
+ ## serializers for health and metrics data
27
+
28
+ are using the internal API of ```Leafy::Health::Registry``` or ```Leafy::Metrics::Registry``` to ```run_health_checks``` or retrieve the collect metrics and produces the json representation of these data.
29
+
30
+ registry = Leafy::Health::Registry.new
31
+ json_writer = Leafy::Json::HealthWriter.new
32
+ json_writer.to_json( registry.health.run_health_checks )
33
+
34
+ or
35
+
36
+ registry = Leafy::Metrics::Registry.new
37
+ json_writer = Leafy::Json::MetricsWriter.new
38
+ json_writer.to_json( registry.metrics )
39
+
40
+ both json writers can take a second argument to generate pretty prints:
41
+
42
+ json_writer.to_json( registry.health.run_health_checks, true )
43
+ json_writer.to_json( registry.metrics, true )
44
+
45
+ ## instrumented http response
46
+
47
+ the class ```Leafy::Instrumented::Instrumented``` has a call method which expect a block. the block needs to return the usual rack middleware result ```[status, headers, body]```.
48
+
49
+ typical usage of this inside a rack-middleware
50
+
51
+ metrics = Leafy::Metrics::Registry.new
52
+ instrumented = Leafy::Instrumented::Instrumented.new( metrics, 'myapp' )
53
+ instrumented.call do
54
+ @app.call( env )
55
+ end
56
+
57
+ see the ```Leafy::Rack::Instrumented``` for an example.
58
+
59
+ ## rack middleware
60
+
61
+ * instrumented middleware collecting metrics on response status, response time, and active requests
62
+ * json data of metrics snapshot
63
+ * json data of current health
64
+ * ping
65
+ * java thread-dump
66
+ * admin page with links to metrics, health, ping and thread-dump data
67
+
68
+ ### instrumented middleware
69
+
70
+ metrics = Leafy::Metrics::Registry.new
71
+ use Leafy::Rack::Instrumented, Leafy::Instrumented::Instrumented.new( metrics, 'webapp' )
72
+
73
+ note: when this instrumented middleware gets configured **after** any of the admin middleware (see below) then those admin requests are not going into the instrumented metrics.
74
+
75
+ ### metrics middleware
76
+
77
+ json data of a snapshot of metrics are under the path **/metrics**
78
+
79
+ metrics = Leafy::Metrics::Registry.new
80
+ use Leafy::Rack::Metrics, metrics
81
+
82
+ or with custom path
83
+
84
+ metrics = Leafy::Metrics::Registry.new
85
+ use Leafy::Rack::Metrics, metrics, '/admin/metrics'
86
+
87
+ ### health-checks middleware
88
+
89
+ json data of current health are under the path **/health**
90
+
91
+ health = Leafy::Health::Registry.new
92
+ use Leafy::Rack::Health, health
93
+
94
+ or with custom path
95
+
96
+ health = Leafy::Health::Registry.new
97
+ use Leafy::Rack::Health, health, '/admin/health'
98
+
99
+ ### ping middleware
100
+
101
+ under the path **/ping**
102
+
103
+ use Leafy::Rack::Ping
104
+
105
+ or with custom path
106
+
107
+ use Leafy::Rack::Ping, '/admin/ping'
108
+
109
+ ### java thread-dump middleware
110
+
111
+ under the path **/threads**
112
+
113
+ use Leafy::Rack::ThreadDump
114
+
115
+ or with custom path
116
+
117
+ use Leafy::Rack::ThreadDump, '/admin/threads'
118
+
119
+
120
+ ### admin page middleware
121
+
122
+ a simple page with links to metrics, health, ping and thread-dump data under the path **/admin**
123
+
124
+ metrics = Leafy::Metrics::Registry.new
125
+ health = Leafy::Health::Registry.new
126
+
127
+ use Leafy::Rack::Admin, metrics, health
128
+
129
+ or with custom path
130
+
131
+ metrics = Leafy::Metrics::Registry.new
132
+ health = Leafy::Health::Registry.new
133
+
134
+ use Leafy::Rack::Admin, metrics, health, '/hidden/admin'
135
+
136
+ ## example sinatra app
137
+
138
+ there is an [example sinatra application](https://github.com/lookout/leafy/tree/master/examples/hellowarld) which uses admin and instrumented middleware and adds some extra metrics inside the application.
139
+
140
+ ## developement
141
+
142
+ get all the gems and jars in place
143
+
144
+ gem install jar-dependencies --development
145
+ bundle install
146
+
147
+ please make sure you are using jar-dependencies > 0.1.8 !
148
+
149
+ for running all specs
150
+
151
+ rake
152
+
153
+ or
154
+
155
+ rspec spec/reporter_spec.rb
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ #-*- mode: ruby -*-
2
+
3
+ require "rspec/core/rake_task"
4
+ RSpec::Core::RakeTask.new
5
+
6
+ require "yard"
7
+ YARD::Rake::YardocTask.new do |t|
8
+ t.files = ['lib/**/*.rb']
9
+ t.options += ["--title", "Leafy Rack Middleware"]
10
+ end
11
+
12
+ task :default => [ :spec ]
13
+
14
+ # vim: syntax=Ruby
@@ -0,0 +1,30 @@
1
+ #-*- mode: ruby -*-
2
+
3
+ require File.expand_path( '../lib/leafy/rack/version', __FILE__ )
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'leafy-rack'
7
+ s.version = Leafy::Rack::VERSION
8
+ s.author = 'christian meier'
9
+ s.email = [ 'christian.meier@lookout.com' ]
10
+
11
+ s.platform = 'java'
12
+ s.license = 'MIT'
13
+ s.summary = %q(rack middleware to expose leafy metrics/health data and more)
14
+ s.homepage = 'https://github.com/lookout/leafy'
15
+ s.description = %q(rack middleware to expose leafy metrics/health data as well a ping rack and a thread-dump rack)
16
+
17
+ s.files = `git ls-files`.split($/)
18
+
19
+ s.requirements << 'jar io.dropwizard.metrics:metrics-json, 3.1.0'
20
+ s.requirements << 'jar io.dropwizard.metrics:metrics-jvm, 3.1.0'
21
+
22
+ s.add_runtime_dependency 'jar-dependencies', '~> 0.1.8'
23
+ s.add_runtime_dependency 'leafy-metrics', "~> #{Leafy::Rack::VERSION}"
24
+ s.add_runtime_dependency 'leafy-health', "~> #{Leafy::Rack::VERSION}"
25
+ s.add_development_dependency 'rspec', '~> 3.1'
26
+ s.add_development_dependency 'yard', '~> 0.8.7'
27
+ s.add_development_dependency 'rake', '~> 10.2'
28
+ end
29
+
30
+ # vim: syntax=Ruby
data/lib/leafy-rack.rb ADDED
@@ -0,0 +1 @@
1
+ require 'leafy/rack'
@@ -0,0 +1,10 @@
1
+ # this is a generated file, to avoid over-writing it just delete this comment
2
+ require 'jar_dependencies'
3
+
4
+ require_jar( 'com.fasterxml.jackson.core', 'jackson-annotations', '2.4.0' )
5
+ require_jar( 'com.fasterxml.jackson.core', 'jackson-core', '2.4.2' )
6
+ require_jar( 'org.slf4j', 'slf4j-api', '1.7.7' )
7
+ require_jar( 'io.dropwizard.metrics', 'metrics-jvm', '3.1.0' )
8
+ require_jar( 'io.dropwizard.metrics', 'metrics-core', '3.1.0' )
9
+ require_jar( 'com.fasterxml.jackson.core', 'jackson-databind', '2.4.2' )
10
+ require_jar( 'io.dropwizard.metrics', 'metrics-json', '3.1.0' )
@@ -0,0 +1,35 @@
1
+ # basically a meter on all requests. but can customized by a status-code to meter map. see constructor.
2
+ module Leafy
3
+ module Instrumented
4
+ class BasicInstrumented
5
+
6
+ NAME_PREFIX = "responseCodes"
7
+
8
+ # creates the metrics. all none registered status codes are marked on the meter named 'other'
9
+ #
10
+ # @param [Leafy::Metrics::Registry] the registry on which register the metrics
11
+ # @param [String] name basename of the metrics
12
+ # @param [Hash] map of status code [Integer] to meter-name [String]. for each code there will be a meter.
13
+ def initialize( registry, name, meter_names_by_status_code = {} )
14
+ @meters_by_status_code = java.util.concurrent.ConcurrentHashMap.new
15
+ meter_names_by_status_code.each do |k,v|
16
+ @meters_by_status_code[ k ] = registry.register_meter( "#{name}.#{v}" )
17
+ end
18
+ @other = registry.register_meter( "#{name}.#{NAME_PREFIX}.other" )
19
+ end
20
+
21
+ # basically wraps a rack middleware call
22
+ def call( &block )
23
+ raise "block needed" unless block_given?
24
+ result = block.call
25
+ mark_meter_for_status_code result[0]
26
+ result
27
+ end
28
+
29
+ def mark_meter_for_status_code( status )
30
+ metric = @meters_by_status_code[ status ] || @other
31
+ metric.mark
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,30 @@
1
+ require 'leafy/instrumented/basic_instrumented'
2
+
3
+ module Leafy
4
+ module Instrumented
5
+ class CollectedInstrumented < BasicInstrumented
6
+
7
+ INFO = 1;
8
+ SUCCESS = 2;
9
+ REDIRECT = 3;
10
+ CLIENT = 4;
11
+ SERVER = 5;
12
+
13
+ # creates the some metrics for status code 2xx, 3xx, 4xx and 5xx. all none registered status codes are marked on the meter named 'other'.
14
+ #
15
+ # @param [Leafy::Metrics::Registry] the registry on which register the metrics
16
+ # @param [String] name basename of the metrics
17
+ def initialize( registry, name )
18
+ super( registry, name,
19
+ { SUCCESS => "#{NAME_PREFIX}.2xx",
20
+ REDIRECT => "#{NAME_PREFIX}.3xx",
21
+ CLIENT => "#{NAME_PREFIX}.4xx",
22
+ SERVER => "#{NAME_PREFIX}.5xx" } )
23
+ end
24
+
25
+ def mark_meter_for_status_code( status )
26
+ super( status/ 100 )
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,53 @@
1
+ require 'leafy/instrumented/basic_instrumented'
2
+
3
+ module Leafy
4
+ module Instrumented
5
+ class Instrumented < BasicInstrumented
6
+
7
+ OK = 200;
8
+ CREATED = 201;
9
+ NO_CONTENT = 204;
10
+ RESET_CONTENT = 205;
11
+ BAD_REQUEST = 400;
12
+ NOT_FOUND = 404;
13
+ SERVER_ERROR = 500;
14
+
15
+ # creates the some metrics for status code 200, 201, 204, 205, 400, 404 and 500. all none registered status codes are marked on the meter named 'other'. further the request time get measured and the active request counter is maintained.
16
+ #
17
+ # @param [Leafy::Metrics::Registry] the registry on which register the metrics
18
+ # @param [String] name basename of the metrics
19
+ def initialize( registry, name )
20
+ super( registry, name,
21
+ { OK => "#{NAME_PREFIX}.ok",
22
+ CREATED => "#{NAME_PREFIX}.created",
23
+ NO_CONTENT => "#{NAME_PREFIX}.noContent",
24
+ RESET_CONTENT => "#{NAME_PREFIX}.resetContent",
25
+ BAD_REQUEST => "#{NAME_PREFIX}.badRequest",
26
+ NOT_FOUND => "#{NAME_PREFIX}.notFound",
27
+ SERVER_ERROR => "#{NAME_PREFIX}.serverError" } )
28
+ @active = registry.register_counter( "#{name}.active_requests" )
29
+ @timer = registry.register_timer( "#{name}.requests" )
30
+ end
31
+
32
+ # basically wraps a rack middleware call. mesures the request time and
33
+ # keeps an active request counter.
34
+ def call( &block )
35
+ raise "block needed" unless block_given?
36
+ @active.inc
37
+
38
+ @timer.time do
39
+
40
+ super
41
+
42
+ end
43
+ ensure
44
+ @active.dec
45
+ end
46
+
47
+ def mark_meter_for_status_code( status )
48
+ metric = @meters_by_status_code[ status ] || @other
49
+ metric.mark
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,15 @@
1
+ require 'leafy/health'
2
+ require 'leafy/json/json_writer'
3
+
4
+ java_import com.codahale.metrics.json.HealthCheckModule
5
+
6
+ module Leafy
7
+ module Json
8
+ class HealthWriter < JsonWriter
9
+
10
+ def initialize
11
+ super( HealthCheckModule.new )
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ java_import com.fasterxml.jackson.databind.ObjectMapper
2
+
3
+ module Leafy
4
+ module Json
5
+ class JsonWriter
6
+
7
+ def initialize( jacksonModule )
8
+ @mapper = ObjectMapper.new.registerModule( jacksonModule )
9
+ end
10
+
11
+ def data
12
+ raise 'need implementation'
13
+ end
14
+
15
+ def to_json( data, pretty = false )
16
+ # TODO make this stream
17
+ output = java.io.ByteArrayOutputStream.new
18
+ writer( pretty ).writeValue(output, data);
19
+ output.to_s
20
+ end
21
+
22
+ private
23
+
24
+ def writer( pretty )
25
+ if pretty
26
+ @mapper.writerWithDefaultPrettyPrinter
27
+ else
28
+ @mapper.writer
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ require 'leafy/metrics'
2
+ require 'leafy/json/json_writer'
3
+
4
+ java_import com.codahale.metrics.MetricFilter
5
+ java_import com.codahale.metrics.json.MetricsModule
6
+
7
+ java_import java.util.concurrent.TimeUnit
8
+
9
+ module Leafy
10
+ module Json
11
+ class MetricsWriter < JsonWriter
12
+
13
+ def initialize
14
+ super( # make this configurable
15
+ MetricsModule.new(TimeUnit::SECONDS,
16
+ TimeUnit::SECONDS,
17
+ true,
18
+ MetricFilter::ALL) )
19
+ end
20
+ end
21
+ end
22
+ end