lhc 6.5.0 → 6.6.0.zipkin.pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a1870e7f765bd88ab8d601bf98828dec1dad3578
4
- data.tar.gz: 2ee4018796f6db877e144888e3211c7cae3cd72c
3
+ metadata.gz: 9ec061c1dd54c6c48e6d3053eeaa5cfde466cbf9
4
+ data.tar.gz: 9b01c8475dd3c7002f4fb7f1a23c1e2eb5711566
5
5
  SHA512:
6
- metadata.gz: 8868211d499b67b58d67abbeadfd4fd12aabbf61d2a99f18a7f4e06ff5f51856b0203997e914e43143503c02315448913253b914b87c51c830ecbc1955b95aec
7
- data.tar.gz: 8f558506283ca904cda4c0db565f24f1728dd9976778523dcd7b74ed155aa82dce6eb4a28a5156dd4bcaea5f9b301dd2d4ee720beae1fca6a8aeee2ad615251c
6
+ metadata.gz: 3332b1305c96357cb69617ae27945b2a09f1621a5733e96d8366d4d07dbebf35fe428937e1e5ca34567f3a3007ff7ae649302afd946f37b7b77138933ec84637
7
+ data.tar.gz: bbbebb1d677c39e8f875b25bc494833043cacfa7b15013928153678f10ebf9d0b7dcf0083e12087ba5775c58da98870bd9f8f096970fab65a9057b7ea413c3f4
data/README.md CHANGED
@@ -106,7 +106,7 @@ You can configure global endpoints, placeholders and interceptors.
106
106
  LHC.configure do |c|
107
107
  c.placeholder :datastore, 'http://datastore/v2'
108
108
  c.endpoint :feedbacks, ':datastore/feedbacks', params: { has_reviews: true }
109
- c.interceptors = [CacheInterceptor]
109
+ c.interceptors = [LHC::Caching]
110
110
  end
111
111
  ```
112
112
 
@@ -185,7 +185,12 @@ To monitor and manipulate the http communication done with LHC, you can define i
185
185
  → [Read more about interceptors](docs/interceptors.md)
186
186
 
187
187
  A set of core interceptors is part of LHC,
188
- like [Caching](/docs/interceptors/caching.md), [Monitoring](/docs/interceptors/monitoring.md), [Authentication](/docs/interceptors/authentication.md), [Rollbar](/docs/interceptors/rollbar.md).
188
+ like
189
+ [Caching](/docs/interceptors/caching.md),
190
+ [Monitoring](/docs/interceptors/monitoring.md),
191
+ [Authentication](/docs/interceptors/authentication.md),
192
+ [Rollbar](/docs/interceptors/rollbar.md),
193
+ [Prometheus](/docs/interceptors/prometheus.md).
189
194
 
190
195
  → [Read more about core interceptors](docs/interceptors.md#core-interceptors)
191
196
 
@@ -67,7 +67,7 @@ Inside an interceptor, you are able to provide a response, rather then doing a r
67
67
  This is usefull for implementing an interceptor for caching.
68
68
 
69
69
  ```ruby
70
- class CacheInterceptor < LHC::Interceptor
70
+ class LHC::Cache < LHC::Interceptor
71
71
 
72
72
  def before_request(request)
73
73
  cached_response = Rails.cache.fetch(request.url)
@@ -34,6 +34,13 @@ Only GET requests are cached by default. If you want to cache any other request
34
34
  LHC.get('http://local.ch', cache: { methods: [:get] })
35
35
  ```
36
36
 
37
+ Responses served from cache are marked as served from cache:
38
+
39
+ ```ruby
40
+ response = LHC.get('http://local.ch', cache: true)
41
+ response.from_cache? # true
42
+ ```
43
+
37
44
  ## Options
38
45
 
39
46
  ```ruby
@@ -0,0 +1,18 @@
1
+ # Prometheus Interceptor
2
+
3
+ Logs basic request/response information to prometheus.
4
+
5
+ ```ruby
6
+ require 'prometheus/client'
7
+ LHC::Prometheus.client = Prometheus::Client
8
+ LHC::Prometheus.namespace = 'web_location_app'
9
+ LHC.config.interceptors = [LHC::Prometheus]
10
+ ```
11
+
12
+ ```ruby
13
+ LHC.get('http://local.ch')
14
+ ```
15
+
16
+ - Creates a promethues counter that receives additional meta information for: `:code`, `:success` and `:timeout`.
17
+
18
+ - Creates a promethues histogram for response times in milliseconds.
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
30
30
  s.add_development_dependency 'pry'
31
31
  s.add_development_dependency 'rubocop', '~> 0.36.0'
32
32
  s.add_development_dependency 'rubocop-rspec'
33
+ s.add_development_dependency 'prometheus-client', '~> 0.7.1'
33
34
 
34
35
  s.license = 'GPL-3'
35
36
  end
data/lib/lhc.rb CHANGED
@@ -16,6 +16,8 @@ module LHC
16
16
  'lhc/interceptors/auth'
17
17
  autoload :Caching,
18
18
  'lhc/interceptors/caching'
19
+ autoload :Prometheus,
20
+ 'lhc/interceptors/prometheus'
19
21
  autoload :Config,
20
22
  'lhc/config'
21
23
  autoload :Endpoint,
@@ -0,0 +1,38 @@
1
+ class LHC::Prometheus < LHC::Interceptor
2
+ include ActiveSupport::Configurable
3
+
4
+ config_accessor :client, :namespace
5
+
6
+ class << self
7
+ attr_accessor :registered
8
+ end
9
+
10
+ def self.request_key
11
+ [LHC::Prometheus.namespace, 'lhc_requests'].join('_').to_sym
12
+ end
13
+
14
+ def self.times_key
15
+ [LHC::Prometheus.namespace, 'lhc_times'].join('_').to_sym
16
+ end
17
+
18
+ def initialize
19
+ return if LHC::Prometheus.registered || LHC::Prometheus.client.blank?
20
+ LHC::Prometheus.client.registry.counter(LHC::Prometheus.request_key, 'Counter of all LHC requests.')
21
+ LHC::Prometheus.client.registry.histogram(LHC::Prometheus.times_key, 'Times for all LHC requests.')
22
+ LHC::Prometheus.registered = true
23
+ end
24
+
25
+ def after_response(response)
26
+ return if !LHC::Prometheus.registered || LHC::Prometheus.client.blank?
27
+ LHC::Prometheus.client.registry
28
+ .get(LHC::Prometheus.request_key)
29
+ .increment(
30
+ code: response.code,
31
+ success: response.success?,
32
+ timeout: response.timeout?
33
+ )
34
+ LHC::Prometheus.client.registry
35
+ .get(LHC::Prometheus.times_key)
36
+ .observe({}, response.time_ms)
37
+ end
38
+ end
@@ -0,0 +1,11 @@
1
+ class LHC::Zipkin < LHC::Interceptor
2
+
3
+ def before_request(request)
4
+ return unless defined?(ZipkinTracer::TraceContainer)
5
+ container = ZipkinTracer::TraceContainer.current
6
+ request.headers['X-B3-TraceId'] = container.trace_id.to_s
7
+ request.headers['X-B3-ParentSpanId'] = container.parent_id.to_s if container.parent_id
8
+ request.headers['X-B3-Sampled'] = container.sampled
9
+ request.headers['X-B3-Flags'] = container.flags
10
+ end
11
+ end
@@ -32,9 +32,14 @@ class LHC::Response
32
32
  body_replacement || raw.body.presence
33
33
  end
34
34
 
35
- # Provides response time in ms.
35
+ # Provides response time in seconds
36
36
  def time
37
- (raw.time || 0) * 1000
37
+ time_ms * 1000
38
+ end
39
+
40
+ # Provides response time in milliseconds
41
+ def time_ms
42
+ raw.time || 0
38
43
  end
39
44
 
40
45
  def timeout?
@@ -1,3 +1,3 @@
1
1
  module LHC
2
- VERSION ||= "6.5.0"
2
+ VERSION ||= "6.6.0.zipkin.pre"
3
3
  end
@@ -0,0 +1,49 @@
1
+ require 'rails_helper'
2
+ require 'prometheus/client'
3
+
4
+ describe LHC::Prometheus do
5
+ before(:each) do
6
+ LHC.config.interceptors = [LHC::Prometheus]
7
+ LHC::Prometheus.client = Prometheus::Client
8
+ LHC::Prometheus.namespace = 'test_app'
9
+ stub_request(:get, 'http://local.ch')
10
+ expect(Prometheus::Client).to receive(:registry).and_call_original.at_least(:once)
11
+ end
12
+
13
+ let(:client) { double("prometheus/client") }
14
+
15
+ context 'registering' do
16
+ it 'creates a counter and histogram registry in the prometheus client' do
17
+ expect(Prometheus::Client.registry).to receive(:counter).and_call_original.once
18
+ .with(:test_app_lhc_requests, 'Counter of all LHC requests.')
19
+ expect(Prometheus::Client.registry).to receive(:histogram).and_call_original.once
20
+ .with(:test_app_lhc_times, 'Times for all LHC requests.')
21
+
22
+ LHC.get('http://local.ch')
23
+ LHC.get('http://local.ch') # second request, registration should happen only once
24
+ end
25
+ end
26
+
27
+ context 'logging' do
28
+ let(:requests_registry_double) { double('requests_registry_double') }
29
+ let(:times_registry_double) { double('times_registry_double') }
30
+
31
+ it 'logs monitoring information to the created registries' do
32
+ expect(Prometheus::Client.registry).to receive(:get).and_return(requests_registry_double).once
33
+ .with(:test_app_lhc_requests)
34
+ expect(Prometheus::Client.registry).to receive(:get).and_return(times_registry_double).once
35
+ .with(:test_app_lhc_times)
36
+
37
+ expect(requests_registry_double).to receive(:increment).once
38
+ .with(
39
+ code: 200,
40
+ success: true,
41
+ timeout: false
42
+ )
43
+ expect(times_registry_double).to receive(:observe).once
44
+ .with({}, 0)
45
+
46
+ LHC.get('http://local.ch')
47
+ end
48
+ end
49
+ end
@@ -6,9 +6,14 @@ describe LHC::Response do
6
6
 
7
7
  let(:raw_response) { OpenStruct.new(time: time) }
8
8
 
9
- it 'provides response time in milliseconds' do
9
+ it 'provides response time in seconds' do
10
10
  response = LHC::Response.new(raw_response, nil)
11
11
  expect(response.time).to eq time * 1000
12
12
  end
13
+
14
+ it 'provides response time in seconds' do
15
+ response = LHC::Response.new(raw_response, nil)
16
+ expect(response.time_ms).to eq time
17
+ end
13
18
  end
14
19
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhc
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.5.0
4
+ version: 6.6.0.zipkin.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhc/contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-06 00:00:00.000000000 Z
11
+ date: 2017-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus
@@ -136,6 +136,20 @@ dependencies:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: prometheus-client
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 0.7.1
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 0.7.1
139
153
  description: 'Rails gem for HTTP: Wraps typhoeus and provides additional features
140
154
  (like interceptors)'
141
155
  email:
@@ -167,6 +181,7 @@ files:
167
181
  - docs/interceptors/authentication.md
168
182
  - docs/interceptors/caching.md
169
183
  - docs/interceptors/monitoring.md
184
+ - docs/interceptors/prometheus.md
170
185
  - docs/interceptors/rollbar.md
171
186
  - docs/request.md
172
187
  - docs/response.md
@@ -190,7 +205,9 @@ files:
190
205
  - lib/lhc/interceptors/auth.rb
191
206
  - lib/lhc/interceptors/caching.rb
192
207
  - lib/lhc/interceptors/monitoring.rb
208
+ - lib/lhc/interceptors/prometheus.rb
193
209
  - lib/lhc/interceptors/rollbar.rb
210
+ - lib/lhc/interceptors/zipkin.rb
194
211
  - lib/lhc/railtie.rb
195
212
  - lib/lhc/request.rb
196
213
  - lib/lhc/response.rb
@@ -274,6 +291,7 @@ files:
274
291
  - spec/interceptors/default_interceptors_spec.rb
275
292
  - spec/interceptors/define_spec.rb
276
293
  - spec/interceptors/monitoring/main_spec.rb
294
+ - spec/interceptors/prometheus_spec.rb
277
295
  - spec/interceptors/response_competition_spec.rb
278
296
  - spec/interceptors/return_response_spec.rb
279
297
  - spec/interceptors/rollbar/main_spec.rb
@@ -318,13 +336,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
318
336
  version: 2.0.0
319
337
  required_rubygems_version: !ruby/object:Gem::Requirement
320
338
  requirements:
321
- - - ">="
339
+ - - ">"
322
340
  - !ruby/object:Gem::Version
323
- version: '0'
341
+ version: 1.3.1
324
342
  requirements:
325
343
  - Ruby >= 2.0.0
326
344
  rubyforge_project:
327
- rubygems_version: 2.6.12
345
+ rubygems_version: 2.6.8
328
346
  signing_key:
329
347
  specification_version: 4
330
348
  summary: LocalHttpClient
@@ -402,6 +420,7 @@ test_files:
402
420
  - spec/interceptors/default_interceptors_spec.rb
403
421
  - spec/interceptors/define_spec.rb
404
422
  - spec/interceptors/monitoring/main_spec.rb
423
+ - spec/interceptors/prometheus_spec.rb
405
424
  - spec/interceptors/response_competition_spec.rb
406
425
  - spec/interceptors/return_response_spec.rb
407
426
  - spec/interceptors/rollbar/main_spec.rb