faraday-zipkin 0.2.2 → 0.2.3

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
- ZmM0ZTc5MjhmMTJkODNiYjg1MjEyODM2MDJiMGI0YmNjMDJhMjRhZg==
4
+ Y2QxNTk2MzUxOTIwZTBkNTI0OTJmNzAxNzYzOTJmNTQ2MTgwZWE4Ng==
5
5
  data.tar.gz: !binary |-
6
- NjI3MmUxMTFkNjVjODc5NTZhOThlODVkNTg4M2U5N2I1MmFkNTc4Yw==
6
+ NjZhNTUyYWY0YmQyOTZjNDM5ZTVhNDE2MzRmZTYxYWE1Yzc3MDhjYQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MWNmNDA4ZGFjMWU5MzVlNDI4YjFmNDRkNzkyOGQ4NTZhY2Q0Y2IxZmQzMDZl
10
- M2UyNTQ4ZTMzYWQ3MDEyNzRjMjMwYjFkODg3MGY4ZDkzYTdiNDYwM2FjMWEz
11
- NzBiYTAxMjY2NzJiODhkOTk4YTM1ZDk3NzQ2MzkyZmY1ZWM4ZDQ=
9
+ NWI5NDExYTlmYWNmOWU5NGVkMzhjM2M2MGUxZjk3ZGFkYjRlYjE2YjFmOWY3
10
+ OTdiZDBjNjA4M2U3ZTczNTUyMzkzZjBiYmIzYmU0M2VmYzI3Y2JlMGRlMDgx
11
+ ZmZkZTRkNjk0M2RkMzUzMmZiNTU4MmVmYTQ0MWY3MTY4Y2Q4YWQ=
12
12
  data.tar.gz: !binary |-
13
- NzNlNzBmZWEzNmQ5Njg1OTA1OWU1OTA4NjRiMmFmMGExZDkxN2VmMDQ3MTYx
14
- ODY4MzI3YmQ0OWVjMzRmODVmY2ZiZjQ1Y2JkZDM0MTVlZDU3MTlmNjEwNGRi
15
- MzQ2MzFkOTEzZDg4OGJlZTVkYTU3M2JiN2MzNDg1YjU4MWI2OGI=
13
+ MGRlZmU0ZWU5N2VjNWRkYzcyMmZhZjQyMjk1NTc0MmU4MTczOGYxMTYwNTcw
14
+ NDc4MmZhZWUwZGZjZjIyMGQ2YTA4NTRjNzkxMDRmNTY2ZTk3NzRlN2I1Nzdl
15
+ OWNjMjAyMDQ2N2RkYmJjNmFhOGRjNzVmZWI4ZjZjYjFhMjhmNmE=
data/.gitignore CHANGED
@@ -12,3 +12,5 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ .ruby-gemset
16
+ .ruby-version
data/CHANGELOG ADDED
@@ -0,0 +1,10 @@
1
+
2
+ Supporting finagle-thrift v1.2.0
3
+ v0.1.0 initial implementation of client trace code
4
+ v0.2.0 added the optional service_name parameter, annotate correctly
5
+ v0.2.1 add annotation for http uri, method, response code
6
+ v0.2.2 fix serialization of response code
7
+ v0.2.3 minor gem cleanup, travis support; fix raise on DNS
8
+
9
+ TODO:
10
+ v0.3.0 - Upgrade to finagle-thrift v1.3.0
data/Gemfile CHANGED
@@ -2,3 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in faraday-zipkin.gemspec
4
4
  gemspec
5
+
6
+ # 0.9.1 adds thin dependency, prevents test on jruby
7
+ gem 'thrift', '0.9.0'
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Faraday::Zipkin
2
2
 
3
+ [![Build Status](https://travis-ci.org/Oscil8/faraday-zipkin.svg?branch=master)](https://travis-ci.org/Oscil8/faraday-zipkin)
4
+
3
5
  Faraday middleware to generate Zipkin tracing headers.
4
6
 
5
7
  For more information about Zipkin, go to
@@ -21,10 +23,11 @@ https://github.com/twitter/zipkin/blob/master/doc/collector-api.md
21
23
  Include Faraday::Zipkin::TraceHeaders as a Faraday middleware:
22
24
 
23
25
  require 'faraday'
24
- require 'faraday/zipkin'
26
+ require 'faraday-zipkin'
25
27
 
26
28
  conn = Faraday.new(:url => 'http://localhost:9292/') do |faraday|
27
- faraday.use Faraday::Zipkin::TraceHeaders [, 'service_name']
29
+ # 'service_name' is optional (but recommended)
30
+ faraday.use Faraday::Zipkin::TraceHeaders, 'service_name'
28
31
  # default Faraday stack
29
32
  faraday.request :url_encoded
30
33
  faraday.adapter Faraday.default_adapter
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'faraday/zipkin/version'
4
+ require 'faraday-zipkin/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "faraday-zipkin"
@@ -24,6 +24,5 @@ Gem::Specification.new do |spec|
24
24
 
25
25
  spec.add_development_dependency "bundler", "~> 1.6"
26
26
  spec.add_development_dependency "rake", "~> 10.0"
27
- spec.add_development_dependency "rspec"
28
- spec.add_development_dependency "thin" # required for finagle-thrift
27
+ spec.add_development_dependency "rspec", "~> 3.0"
29
28
  end
@@ -1,49 +1 @@
1
- require 'faraday'
2
- require 'finagle-thrift'
3
- require 'finagle-thrift/trace'
4
- require 'uri'
5
-
6
- require 'faraday/zipkin/version'
7
-
8
- module Faraday
9
- module Zipkin
10
- class TraceHeaders < ::Faraday::Middleware
11
- B3_HEADERS = {
12
- :trace_id => "X-B3-TraceId",
13
- :parent_id => "X-B3-ParentSpanId",
14
- :span_id => "X-B3-SpanId",
15
- :sampled => "X-B3-Sampled"
16
- }.freeze
17
-
18
- def initialize(app, service_name=nil)
19
- @app = app
20
- @service_name = service_name
21
- end
22
-
23
- def call(env)
24
- # handle either a URI object (passed by Faraday v0.8.x in testing), or something string-izable
25
- url = env[:url].respond_to?(:host) ? env[:url] : URI.parse(env[:url].to_s)
26
- service_name = @service_name || url.host.split('.').first # default to url-derived service name
27
- endpoint = ::Trace::Endpoint.new(::Trace::Endpoint.host_to_i32(url.host), url.port, service_name)
28
-
29
- trace_id = ::Trace.id
30
- ::Trace.push(trace_id.next_id) do
31
- B3_HEADERS.each do |method, header|
32
- env[:request_headers][header] = ::Trace.id.send(method).to_s
33
- end
34
-
35
- # annotate with method (GET/POST/etc.) and uri path
36
- ::Trace.set_rpc_name(env[:method].to_s.upcase)
37
- ::Trace.record(::Trace::BinaryAnnotation.new("http.uri", url.path, "STRING", endpoint))
38
- ::Trace.record(::Trace::Annotation.new(::Trace::Annotation::CLIENT_SEND, endpoint))
39
- result = @app.call(env).on_complete do |renv|
40
- # record HTTP status code on response
41
- ::Trace.record(::Trace::BinaryAnnotation.new("http.status", [renv[:status]].pack('n'), "I16", endpoint))
42
- end
43
- ::Trace.record(::Trace::Annotation.new(::Trace::Annotation::CLIENT_RECV, endpoint))
44
- result
45
- end
46
- end
47
- end
48
- end
49
- end
1
+ require 'faraday-zipkin'
@@ -0,0 +1,56 @@
1
+ require 'faraday'
2
+ require 'finagle-thrift'
3
+ require 'finagle-thrift/trace'
4
+ require 'uri'
5
+
6
+ module Faraday
7
+ module Zipkin
8
+ class TraceHeaders < ::Faraday::Middleware
9
+ B3_HEADERS = {
10
+ :trace_id => "X-B3-TraceId",
11
+ :parent_id => "X-B3-ParentSpanId",
12
+ :span_id => "X-B3-SpanId",
13
+ :sampled => "X-B3-Sampled"
14
+ }.freeze
15
+
16
+ def initialize(app, service_name=nil)
17
+ @app = app
18
+ @service_name = service_name
19
+ end
20
+
21
+ def call(env)
22
+ # handle either a URI object (passed by Faraday v0.8.x in testing), or something string-izable
23
+ url = env[:url].respond_to?(:host) ? env[:url] : URI.parse(env[:url].to_s)
24
+ service_name = @service_name || url.host.split('.').first # default to url-derived service name
25
+ endpoint = ::Trace::Endpoint.new(host_ip_for(url.host), url.port, service_name)
26
+
27
+ trace_id = ::Trace.id
28
+ ::Trace.push(trace_id.next_id) do
29
+ B3_HEADERS.each do |method, header|
30
+ env[:request_headers][header] = ::Trace.id.send(method).to_s
31
+ end
32
+
33
+ # annotate with method (GET/POST/etc.) and uri path
34
+ ::Trace.set_rpc_name(env[:method].to_s.upcase)
35
+ ::Trace.record(::Trace::BinaryAnnotation.new("http.uri", url.path, "STRING", endpoint))
36
+ ::Trace.record(::Trace::Annotation.new(::Trace::Annotation::CLIENT_SEND, endpoint))
37
+ result = @app.call(env).on_complete do |renv|
38
+ # record HTTP status code on response
39
+ ::Trace.record(::Trace::BinaryAnnotation.new("http.status", [renv[:status]].pack('n'), "I16", endpoint))
40
+ end
41
+ ::Trace.record(::Trace::Annotation.new(::Trace::Annotation::CLIENT_RECV, endpoint))
42
+ result
43
+ end
44
+ end
45
+
46
+ # get host IP for specified hostname, catching exceptions
47
+ def host_ip_for(hostname)
48
+ ::Trace::Endpoint.host_to_i32(hostname)
49
+ rescue
50
+ # default to 0.0.0.0 if lookup fails
51
+ 0x00000000
52
+ end
53
+ private :host_ip_for
54
+ end
55
+ end
56
+ end
@@ -1,5 +1,5 @@
1
1
  module Faraday
2
2
  module Zipkin
3
- VERSION = "0.2.2"
3
+ VERSION = "0.2.3"
4
4
  end
5
5
  end
@@ -0,0 +1,2 @@
1
+ require 'faraday-zipkin/version'
2
+ require 'faraday-zipkin/trace_headers'
data/spec/spec_helper.rb CHANGED
@@ -1,80 +1,5 @@
1
- require 'faraday/zipkin'
1
+ require 'faraday-zipkin'
2
2
 
3
- # This file was generated by the `rspec --init` command. Conventionally, all
4
- # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
- # The generated `.rspec` file contains `--require spec_helper` which will cause this
6
- # file to always be loaded, without a need to explicitly require it in any files.
7
- #
8
- # Given that it is always loaded, you are encouraged to keep this file as
9
- # light-weight as possible. Requiring heavyweight dependencies from this file
10
- # will add to the boot time of your test suite on EVERY test run, even for an
11
- # individual file that may not need all of that loaded. Instead, make a
12
- # separate helper file that requires this one and then use it only in the specs
13
- # that actually need it.
14
- #
15
- # The `.rspec` file also contains a few flags that are not defaults but that
16
- # users commonly want.
17
- #
18
- # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
3
  RSpec.configure do |config|
20
- # The settings below are suggested to provide a good initial experience
21
- # with RSpec, but feel free to customize to your heart's content.
22
- =begin
23
- # These two settings work together to allow you to limit a spec run
24
- # to individual examples or groups you care about by tagging them with
25
- # `:focus` metadata. When nothing is tagged with `:focus`, all examples
26
- # get run.
27
- config.filter_run :focus
28
- config.run_all_when_everything_filtered = true
29
-
30
- # Many RSpec users commonly either run the entire suite or an individual
31
- # file, and it's useful to allow more verbose output when running an
32
- # individual spec file.
33
- if config.files_to_run.one?
34
- # Use the documentation formatter for detailed output,
35
- # unless a formatter has already been configured
36
- # (e.g. via a command-line flag).
37
- config.default_formatter = 'doc'
38
- end
39
-
40
- # Print the 10 slowest examples and example groups at the
41
- # end of the spec run, to help surface which specs are running
42
- # particularly slow.
43
- config.profile_examples = 10
44
-
45
- # Run specs in random order to surface order dependencies. If you find an
46
- # order dependency and want to debug it, you can fix the order by providing
47
- # the seed, which is printed after each run.
48
- # --seed 1234
49
4
  config.order = :random
50
-
51
- # Seed global randomization in this process using the `--seed` CLI option.
52
- # Setting this allows you to use `--seed` to deterministically reproduce
53
- # test failures related to randomization by passing the same `--seed` value
54
- # as the one that triggered the failure.
55
- Kernel.srand config.seed
56
-
57
- # rspec-expectations config goes here. You can use an alternate
58
- # assertion/expectation library such as wrong or the stdlib/minitest
59
- # assertions if you prefer.
60
- config.expect_with :rspec do |expectations|
61
- # Enable only the newer, non-monkey-patching expect syntax.
62
- # For more details, see:
63
- # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
64
- expectations.syntax = :expect
65
- end
66
-
67
- # rspec-mocks config goes here. You can use an alternate test double
68
- # library (such as bogus or mocha) by changing the `mock_with` option here.
69
- config.mock_with :rspec do |mocks|
70
- # Enable only the newer, non-monkey-patching expect syntax.
71
- # For more details, see:
72
- # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
73
- mocks.syntax = :expect
74
-
75
- # Prevents you from mocking or stubbing a method that does not exist on
76
- # a real object. This is generally recommended.
77
- mocks.verify_partial_doubles = true
78
- end
79
- =end
80
5
  end
@@ -31,31 +31,36 @@ describe Faraday::Zipkin::TraceHeaders do
31
31
  middleware.call(env)
32
32
  end
33
33
 
34
- # custom matchers for trace annotation
35
- RSpec::Matchers.define :have_value do |v|
36
- match { |actual| actual.value == v }
37
- end
38
-
39
- RSpec::Matchers.define :have_endpoint do |ip, svc|
40
- match { |actual| actual.host.kind_of?(::Trace::Endpoint) &&
41
- actual.host.ipv4 == ip &&
42
- actual.host.service_name == svc }
43
- end
44
-
45
34
  before(:each) {
46
35
  ::Trace.sample_rate = 0.1 # make sure initialized
47
36
  allow(::Trace::Endpoint).to receive(:host_to_i32).with(hostname).and_return(host_ip)
48
37
  }
49
38
 
50
39
  shared_examples 'can make requests' do
40
+ # helper to check host component of annotation
41
+ def expect_host(host, host_ip, service_name)
42
+ expect(host).to be_a_kind_of(::Trace::Endpoint)
43
+ expect(host.ipv4).to eq(host_ip)
44
+ expect(host.service_name).to eq(service_name)
45
+ end
46
+
51
47
  def expect_tracing
52
48
  # expect SEND then RECV
53
49
  expect(::Trace).to receive(:set_rpc_name).with('POST')
54
50
  expect(::Trace).to receive(:record).with(instance_of(::Trace::BinaryAnnotation)).twice # http.uri, http.status
55
- expect(::Trace).to receive(:record).with(have_value(::Trace::Annotation::CLIENT_SEND).and(have_endpoint(host_ip, service_name))).ordered
56
- expect(::Trace).to receive(:record).with(have_value(::Trace::Annotation::CLIENT_RECV).and(have_endpoint(host_ip, service_name))).ordered
51
+ expect(::Trace).to receive(:record).with(instance_of(::Trace::Annotation)) do |ann|
52
+ expect(ann.value).to eq(::Trace::Annotation::CLIENT_SEND)
53
+ expect_host(ann.host, host_ip, service_name)
54
+ end.ordered
55
+ expect(::Trace).to receive(:record).with(instance_of(::Trace::Annotation)) do |ann|
56
+ expect(ann.value).to eq(::Trace::Annotation::CLIENT_RECV)
57
+ expect_host(ann.host, host_ip, service_name)
58
+ end.ordered
57
59
  end
58
60
 
61
+ # Ruby 1.8 didn't support \h
62
+ HEX_REGEX = RUBY_VERSION >= "1.9.2" ? '\h' : '[0-9a-fA-F]'
63
+
59
64
  context 'with tracing id' do
60
65
  let(:trace_id) { ::Trace::TraceId.new(1, 2, 3, true) }
61
66
 
@@ -68,7 +73,7 @@ describe Faraday::Zipkin::TraceHeaders do
68
73
  expect(result[:request_headers]['X-B3-TraceId']).to eq('0000000000000001')
69
74
  expect(result[:request_headers]['X-B3-ParentSpanId']).to eq('0000000000000003')
70
75
  expect(result[:request_headers]['X-B3-SpanId']).not_to eq('0000000000000003')
71
- expect(result[:request_headers]['X-B3-SpanId']).to match(/^\h{16}$/)
76
+ expect(result[:request_headers]['X-B3-SpanId']).to match(/^#{HEX_REGEX}{16}$/)
72
77
  expect(result[:request_headers]['X-B3-Sampled']).to eq('true')
73
78
  end
74
79
  end
@@ -79,12 +84,25 @@ describe Faraday::Zipkin::TraceHeaders do
79
84
  it 'generates a new ID, and sets the X-B3 request headers' do
80
85
  expect_tracing
81
86
  result = process('', url).env
82
- expect(result[:request_headers]['X-B3-TraceId']).to match(/^\h{16}$/)
83
- expect(result[:request_headers]['X-B3-ParentSpanId']).to match(/^\h{16}$/)
84
- expect(result[:request_headers]['X-B3-SpanId']).to match(/^\h{16}$/)
87
+ expect(result[:request_headers]['X-B3-TraceId']).to match(/^#{HEX_REGEX}{16}$/)
88
+ expect(result[:request_headers]['X-B3-ParentSpanId']).to match(/^#{HEX_REGEX}{16}$/)
89
+ expect(result[:request_headers]['X-B3-SpanId']).to match(/^#{HEX_REGEX}{16}$/)
85
90
  expect(result[:request_headers]['X-B3-Sampled']).to match(/(true|false)/)
86
91
  end
87
92
  end
93
+
94
+ context 'when looking up hostname raises' do
95
+ let(:host_ip) { 0x00000000 } # expect stubbed 'null' IP
96
+
97
+ before(:each) {
98
+ allow(::Trace::Endpoint).to receive(:host_to_i32).with(hostname).and_raise(SocketError)
99
+ }
100
+
101
+ it 'traces with stubbed endpoint address' do
102
+ expect_tracing
103
+ process('', url)
104
+ end
105
+ end
88
106
  end
89
107
 
90
108
  context 'middleware configured (without service_name)' do
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.2.2
4
+ version: 0.2.3
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-10-06 00:00:00.000000000 Z
11
+ date: 2014-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -84,30 +84,16 @@ dependencies:
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
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
- - !ruby/object:Gem::Dependency
98
- name: thin
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ! '>='
87
+ - - ~>
102
88
  - !ruby/object:Gem::Version
103
- version: '0'
89
+ version: '3.0'
104
90
  type: :development
105
91
  prerelease: false
106
92
  version_requirements: !ruby/object:Gem::Requirement
107
93
  requirements:
108
- - - ! '>='
94
+ - - ~>
109
95
  - !ruby/object:Gem::Version
110
- version: '0'
96
+ version: '3.0'
111
97
  description: Faraday middleware to generate Zipkin tracing headers.
112
98
  email:
113
99
  - asalomon@lookout.com
@@ -118,13 +104,16 @@ files:
118
104
  - .gitignore
119
105
  - .rspec
120
106
  - .travis.yml
107
+ - CHANGELOG
121
108
  - Gemfile
122
109
  - LICENSE.txt
123
110
  - README.md
124
111
  - Rakefile
125
112
  - faraday-zipkin.gemspec
113
+ - lib/faraday-zipkin.rb
114
+ - lib/faraday-zipkin/trace_headers.rb
115
+ - lib/faraday-zipkin/version.rb
126
116
  - lib/faraday/zipkin.rb
127
- - lib/faraday/zipkin/version.rb
128
117
  - spec/spec_helper.rb
129
118
  - spec/zipkin_trace_headers_spec.rb
130
119
  homepage: https://github.com/Oscil8/faraday-zipkin
@@ -147,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
136
  version: '0'
148
137
  requirements: []
149
138
  rubyforge_project:
150
- rubygems_version: 2.2.2
139
+ rubygems_version: 2.4.4
151
140
  signing_key:
152
141
  specification_version: 4
153
142
  summary: Faraday middleware to generate Zipkin tracing headers.