faraday-zipkin 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjJkNWExZDIwMmE4NDZlYzY4YmViNWFmNDIyZTJiYTVhMThkZWVlMw==
4
+ OGYxYzAzNzAzZTQ3OTVhMjcyOWUyZWFlOTQ2OWE2YjgzOTlmNzBjYQ==
5
5
  data.tar.gz: !binary |-
6
- Yzg0MTAyNDU0YmQ0NGJkMGI5NzUxMWFiZjQ3NzdhNWFjNGRiYzk0Nw==
6
+ YmRlM2VkNzIyNmQyYWNhMzQ5M2M2N2RkN2ZlMjI0YTM5YWVhYTdmOQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ODQ3ZDI4ZDM4NjQwMTQ0NWViOGNlOGJjZGMxZTFlYmMzMjI2NWI1YmQxNjlk
10
- OGQ4NTE3Y2JlMmI2Y2ExODU4YzAwZGM5YWFjMjNjYzcyYjA4MThlMmE3YjRk
11
- YzgwYjZkZDQ2ZDM2NGY3NWUyMWI3MzI2YmVhOTkwNmFiMmZkMjI=
9
+ OTdkMzY3MmJiNTQyOWVkNjZhZDA5ZGI0MDkyNWJlMGQxZTg2N2RhZmMxMDkx
10
+ MTJlOWUzNTlhMWIzMGFlNTcwYjJjMGQyODQ2YzM2MjRiM2RmYWFhNGNiY2Zm
11
+ ODg1ZjgwZmU5YmVmODU0ZWE1ZjJiMzcyYjQ3MWVhOWE5ZDIwOGQ=
12
12
  data.tar.gz: !binary |-
13
- NmRhZGRlN2U4OGVmMTM2NDEyYWZiOTA3NGUyMzFkNGUzZTQ3OWRmZGYxYWMy
14
- ODU0ODllNjhhMjM0NGNlM2VmNjZkZjkwMzUyNDMxZTEyOTI5YWU1ZDA3OGE2
15
- ZGYwYTI1NWZmMTU0MmE5MjBjYWRiMDQ4NTRmYjZhNjJhMTdjZDQ=
13
+ ZTZkZGQwZmU4NTRhYmRiYjdhOTJlMmQ2Nzg4MjU2ZWJhMGUyMWQ0NjI5ZjE1
14
+ NjYwZjhlYmM2ZjU2MmY5Mzk2YzcyMGNlMTIwYTcwN2ZjMTRmNDkwODVmNjU3
15
+ MGExOWI3NjcwYzkxYzkxMmMwZWRjNzhiMWVkNDNkODFmODIxZjA=
data/README.md CHANGED
@@ -24,12 +24,17 @@ Include Faraday::Zipkin::TraceHeaders as a Faraday middleware:
24
24
  require 'faraday/zipkin'
25
25
 
26
26
  conn = Faraday.new(:url => 'http://localhost:9292/') do |faraday|
27
- faraday.use Faraday::Zipkin::TraceHeaders
27
+ faraday.use Faraday::Zipkin::TraceHeaders [, 'service_name']
28
28
  # default Faraday stack
29
29
  faraday.request :url_encoded
30
30
  faraday.adapter Faraday.default_adapter
31
31
  end
32
-
32
+
33
+ Note that supplying the service name for the destination service is
34
+ optional; the tracing will default to a service name derived from the
35
+ first section of the destination URL (e.g. 'service.example.com' =>
36
+ 'service').
37
+
33
38
  ## Contributing
34
39
 
35
40
  1. Fork it ( https://github.com/Oscil8/faraday-zipkin/fork )
@@ -1,5 +1,5 @@
1
1
  module Faraday
2
2
  module Zipkin
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -15,23 +15,26 @@ module Faraday
15
15
  :sampled => "X-B3-Sampled"
16
16
  }.freeze
17
17
 
18
- def initialize(app)
18
+ def initialize(app, service_name=nil)
19
19
  @app = app
20
+ @service_name = service_name
20
21
  end
21
22
 
22
23
  def call(env)
23
24
  trace_id = ::Trace.id
24
25
 
25
26
  # handle either a URI object (passed by Faraday v0.8.x in testing), or something string-izable
26
- host = env[:url].respond_to?(:host) ? env[:url].host : URI.parse(env[:url].to_s).host
27
+ url = env[:url].respond_to?(:host) ? env[:url] : URI.parse(env[:url].to_s)
28
+ service_name = @service_name || url.host.split('.').first # default to url-derived service name
29
+ endpoint = ::Trace::Endpoint.new(::Trace::Endpoint.host_to_i32(url.host), url.port, service_name)
27
30
 
28
31
  ::Trace.push(trace_id.next_id) do
29
- ::Trace.record(::Trace::Annotation.new(::Trace::Annotation::CLIENT_SEND, host))
32
+ ::Trace.record(::Trace::Annotation.new(::Trace::Annotation::CLIENT_SEND, endpoint))
30
33
  B3_HEADERS.each do |method, header|
31
34
  env[:request_headers][header] = ::Trace.id.send(method).to_s
32
35
  end
33
36
  result = @app.call(env)
34
- ::Trace.record(::Trace::Annotation.new(::Trace::Annotation::CLIENT_RECV, host))
37
+ ::Trace.record(::Trace::Annotation.new(::Trace::Annotation::CLIENT_RECV, endpoint))
35
38
  result
36
39
  end
37
40
  end
@@ -1,6 +1,8 @@
1
1
  describe Faraday::Zipkin::TraceHeaders do
2
- let(:middleware) { described_class.new(lambda{|env| env}) }
2
+ let(:wrapped_app) { lambda{|env| env} }
3
+
3
4
  let(:hostname) { 'service.example.com' }
5
+ let(:host_ip) { 0x11223344 }
4
6
 
5
7
  def process(body, url, headers={})
6
8
  env = {
@@ -11,19 +13,29 @@ describe Faraday::Zipkin::TraceHeaders do
11
13
  middleware.call(env)
12
14
  end
13
15
 
14
- # custom matcher for trace annotation
15
- RSpec::Matchers.define :have_value_and_host do |v, h|
16
- match { |actual| actual.value == v && actual.host == h }
16
+ # custom matchers for trace annotation
17
+ RSpec::Matchers.define :have_value do |v|
18
+ match { |actual| actual.value == v }
19
+ end
20
+
21
+ RSpec::Matchers.define :have_endpoint do |ip, svc|
22
+ match { |actual| actual.host.kind_of?(::Trace::Endpoint) &&
23
+ actual.host.ipv4 == ip &&
24
+ actual.host.service_name == svc }
17
25
  end
18
26
 
27
+ before(:each) {
28
+ allow(::Trace::Endpoint).to receive(:host_to_i32).with(hostname).and_return(host_ip)
29
+ }
30
+
19
31
  shared_examples 'can make requests' do
20
32
  context 'with tracing id' do
21
33
  let(:trace_id) { ::Trace::TraceId.new(1, 2, 3, true) }
22
34
 
23
35
  it 'sets the X-B3 request headers' do
24
36
  # expect SEND then RECV
25
- expect(::Trace).to receive(:record).with(have_value_and_host(::Trace::Annotation::CLIENT_SEND, hostname)).ordered
26
- expect(::Trace).to receive(:record).with(have_value_and_host(::Trace::Annotation::CLIENT_RECV, hostname)).ordered
37
+ expect(::Trace).to receive(:record).with(have_value(::Trace::Annotation::CLIENT_SEND).and(have_endpoint(host_ip, service_name))).ordered
38
+ expect(::Trace).to receive(:record).with(have_value(::Trace::Annotation::CLIENT_RECV).and(have_endpoint(host_ip, service_name))).ordered
27
39
 
28
40
  result = nil
29
41
  ::Trace.push(trace_id) do
@@ -48,16 +60,33 @@ describe Faraday::Zipkin::TraceHeaders do
48
60
  end
49
61
  end
50
62
 
51
- context 'request with string URL' do
52
- let(:url) { "https://#{hostname}/some/path/here" }
63
+ context 'middleware configured (without service_name)' do
64
+ let(:middleware) { described_class.new(wrapped_app) }
65
+ let(:service_name) { 'service' }
66
+
67
+ context 'request with string URL' do
68
+ let(:url) { "https://#{hostname}/some/path/here" }
53
69
 
54
- include_examples 'can make requests'
70
+ include_examples 'can make requests'
71
+ end
72
+
73
+ # in testing, Faraday v0.8.x passes a URI object rather than a string
74
+ context 'request with pre-parsed URL' do
75
+ let(:url) { URI.parse("https://#{hostname}/some/path/here") }
76
+
77
+ include_examples 'can make requests'
78
+ end
55
79
  end
56
80
 
57
- # in testing, Faraday v0.8.x passes a URI object rather than a string
58
- context 'request with pre-parsed URL' do
59
- let(:url) { URI.parse("https://#{hostname}/some/path/here") }
81
+ context 'configured with service_name "foo"' do
82
+ let(:middleware) { described_class.new(wrapped_app, 'foo') }
83
+ let(:service_name) { 'foo' }
60
84
 
61
- include_examples 'can make requests'
85
+ # in testing, Faraday v0.8.x passes a URI object rather than a string
86
+ context 'request with pre-parsed URL' do
87
+ let(:url) { URI.parse("https://#{hostname}/some/path/here") }
88
+
89
+ include_examples 'can make requests'
90
+ end
62
91
  end
63
92
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-zipkin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ariel Salomon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-26 00:00:00.000000000 Z
11
+ date: 2014-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday