Skip to content
This repository was archived by the owner on Aug 27, 2021. It is now read-only.

[WeCall Opentracing] Basic support for Jager / OpenTracing #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/we/call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class << self
end

def self.configuration
# potentially add in tracer here as param
@configuration ||= Configuration.new
end

Expand Down
22 changes: 18 additions & 4 deletions lib/we/call/connection.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/we/call/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module We
module Call
VERSION = "0.8.0"
VERSION = "0.9.a"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gem is following SemVer, which suggests a format more like 0.9.0-alpha1 https://semver.org/

end
end
28 changes: 27 additions & 1 deletion spec/unit/we/call/connection_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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|
Expand Down
5 changes: 4 additions & 1 deletion we-call.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down