From bc978b78aa6fdbed17f7e43e61a2cba850ed2101 Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Thu, 19 Jul 2018 10:53:06 -0400 Subject: [PATCH 1/2] [WeCall Opentracing] Basic support for Jager / OpenTracing. Still in alpha. --- lib/we/call.rb | 1 + lib/we/call/connection.rb | 22 ++++++++++++++++++---- lib/we/call/version.rb | 2 +- spec/unit/we/call/connection_spec.rb | 28 +++++++++++++++++++++++++++- we-call.gemspec | 5 ++++- 5 files changed, 51 insertions(+), 7 deletions(-) diff --git a/lib/we/call.rb b/lib/we/call.rb index 20e9227..336ca4d 100755 --- a/lib/we/call.rb +++ b/lib/we/call.rb @@ -15,6 +15,7 @@ class << self end def self.configuration + # potentially add in tracer here as param @configuration ||= Configuration.new end diff --git a/lib/we/call/connection.rb b/lib/we/call/connection.rb index a8aa9cf..4c649eb 100644 --- a/lib/we/call/connection.rb +++ b/lib/we/call/connection.rb @@ -1,5 +1,10 @@ require 'typhoeus/adapters/faraday' +require 'opentracing' +require 'faraday/tracer' +require 'rack/tracer' +require 'jaeger/client' + module We module Call module Connection @@ -40,18 +45,20 @@ def get_adapter # @param [String] app # @param [String] env # @yieldparam [Faraday::Connection] Faraday connection object is yielded to a block - def new(host:, timeout: nil, open_timeout: OPEN_TIMEOUT, app: guess_app, env: guess_env, &block) + def new(host:, timeout: nil, open_timeout: OPEN_TIMEOUT, app: guess_app, env: guess_env, active_span: guess_span, &block) @host = host @app = app or raise_missing_app! @env = env or raise_missing_env! @timeout = timeout or raise_missing_timeout! @open_timeout = open_timeout or raise_missing_open_timeout! + @active_span = active_span + create(&block) end private - attr_reader :app, :env, :host, :timeout, :open_timeout + attr_reader :app, :env, :host, :timeout, :open_timeout, :active_span # @return [Faraday::Connection] Preconfigured Faraday Connection object, for hitting get, post, etc. def create @@ -63,12 +70,14 @@ def create config.app_env_header => env, } - request = { + options = { timeout: timeout, open_timeout: open_timeout } - Faraday.new(host, builder: builder, headers: headers, request: request) do |faraday| + Faraday.new(host, builder: builder, headers: headers, request: options) do |faraday| + faraday.use Faraday::Tracer, span: active_span + if config.detect_deprecations faraday.response :sunset, setup_sunset_middleware(faraday) end @@ -132,6 +141,11 @@ def guess_app ENV['APP_NAME'] || rails_app_name end + def guess_span + return rails_app_env['rack.span'] if rails_app_env + nil + end + def rails_app_env ::Rails.env if (defined? ::Rails) end diff --git a/lib/we/call/version.rb b/lib/we/call/version.rb index 474ba6b..7021f71 100755 --- a/lib/we/call/version.rb +++ b/lib/we/call/version.rb @@ -1,5 +1,5 @@ module We module Call - VERSION = "0.8.0" + VERSION = "0.9.a" end end diff --git a/spec/unit/we/call/connection_spec.rb b/spec/unit/we/call/connection_spec.rb index 565df14..645730d 100755 --- a/spec/unit/we/call/connection_spec.rb +++ b/spec/unit/we/call/connection_spec.rb @@ -1,7 +1,7 @@ require "spec_helper" RSpec.describe We::Call::Connection do - DEFAULT_MIDDLEWARES = [FaradayMiddleware::Gzip, Faraday::Sunset] + DEFAULT_MIDDLEWARES = [FaradayMiddleware::Gzip, Faraday::Sunset, Faraday::Tracer] describe '#initialize' do context 'when host is missing' do @@ -102,6 +102,32 @@ end end + context 'when active_span needs to be guessed' do + subject { described_class.new(host: 'http://foo.com', app: 'foo', env: env, timeout: 5, open_timeout: 10) } + + context 'rack.span is present' do + let(:env) do + { + 'test' => { + 'rack.span' => 'some_span' + } + } + end + + it 'contains X-App-Name header' do + expect(subject.headers['X-App-Env']['test']['rack.span']).to eq('some_span') + end + end + + context 'rack.span is not present' do + let(:env) { 'test' } + + it 'sets active_span to nil if rack.span is not present' do + expect(subject.headers['X-App-Env']['test']['rack.span']).to be_nil + end + end + end + context 'with custom block' do subject do described_class.new(**valid_arguments) do |faraday| diff --git a/we-call.gemspec b/we-call.gemspec index 78291a8..5d329ef 100644 --- a/we-call.gemspec +++ b/we-call.gemspec @@ -23,7 +23,10 @@ Gem::Specification.new do |spec| spec.add_dependency "typhoeus", "~> 1.3" spec.add_dependency "faraday", ">= 0.9.0", "< 1" spec.add_dependency "faraday_middleware", '~> 0.10' - spec.add_dependency "faraday-sunset", '~> 0.2' + spec.add_dependency "faraday-sunset", "~> 0.2" + spec.add_dependency 'faraday-tracer', "~> 0.6.0" + spec.add_dependency 'jaeger-client' + spec.add_dependency 'rack-tracer' spec.add_development_dependency "appraisal", "~> 2.0" spec.add_development_dependency "coveralls", '~> 0.7' From 5dca3f81529874631a9f4955a495be97288c62f5 Mon Sep 17 00:00:00 2001 From: Eric Swirsky Date: Mon, 10 Sep 2018 10:39:55 -0400 Subject: [PATCH 2/2] [Opentracing] update version to match semver --- lib/we/call/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/we/call/version.rb b/lib/we/call/version.rb index 7021f71..548e2bc 100755 --- a/lib/we/call/version.rb +++ b/lib/we/call/version.rb @@ -1,5 +1,5 @@ module We module Call - VERSION = "0.9.a" + VERSION = "0.9.0-alpha.1" end end