From 492c0eaa5b0ce8a2e51d10cfb0dcd0259534130e Mon Sep 17 00:00:00 2001 From: Nick Gauthier Date: Thu, 10 Nov 2016 10:28:41 -0500 Subject: [PATCH 1/5] inject and extract for http requests and rack requests --- Makefile | 1 + examples/rack/inject_extract.rb | 66 +++++++++++++++++++++++++++++++++ lib/lightstep/tracer.rb | 35 ++++++++++++++++- spec/lightstep_spec.rb | 29 +++++++++++++++ 4 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 examples/rack/inject_extract.rb diff --git a/Makefile b/Makefile index f661fc4..951e8ff 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ test: bundle exec rake spec ruby example.rb ruby examples/fork_children/main.rb + ruby examples/rack/inject_extract.rb benchmark: ruby benchmark/bench.rb diff --git a/examples/rack/inject_extract.rb b/examples/rack/inject_extract.rb new file mode 100644 index 0000000..7fa8bce --- /dev/null +++ b/examples/rack/inject_extract.rb @@ -0,0 +1,66 @@ +require 'bundler/setup' +require 'lightstep' + +require 'rack' +require 'rack/server' + +$token = '{your_access_token}' +$request_id = 'abc123' + +class Router + def initialize + @tracer = LightStep::Tracer.new(component_name: 'router', access_token: $token) + end + + def call(env) + span = @tracer.start_span("router_call").set_baggage_item("request-id", $request_id) + span.log(event: "router_request", env: env) + puts "parent #{span.trace_id}" + + client = Net::HTTP.new("localhost", "9002") + req = Net::HTTP::Post.new("/") + @tracer.inject(span, LightStep::Tracer::FORMAT_HTTP, req) + res = client.request(req) + + span.log(event: "application_response", response: res.to_s) + span.finish + @tracer.flush + puts "----> https://app.lightstep.com/#{$token}/trace?span_guid=#{span.id}&at_micros=#{span.start_micros} <----" + [200, {}, [res.body]] + end +end + +class App + def initialize + @tracer = LightStep::Tracer.new(component_name: 'app', access_token: $token) + end + + def call(env) + span = @tracer.extract("app_call", LightStep::Tracer::FORMAT_HTTP, env) + puts "child #{span.to_h[:trace_guid]}" + span.log(event: "application", env: env) + sleep 0.05 + span.finish + @tracer.flush + [200, {}, ["application"]] + end +end + +router_thread = Thread.new do + Thread.abort_on_exception = true + Rack::Server.start(app: Router.new, Port: 9001) +end + +app_thread = Thread.new do + Thread.abort_on_exception = true + Rack::Server.start(app: App.new, Port: 9002) +end + +loop do + begin + p Net::HTTP.get(URI("http://localhost:9001/")) + break + rescue Errno::ECONNREFUSED + sleep 0.05 + end +end diff --git a/lib/lightstep/tracer.rb b/lib/lightstep/tracer.rb index 3bf9771..30e65e0 100644 --- a/lib/lightstep/tracer.rb +++ b/lib/lightstep/tracer.rb @@ -11,6 +11,7 @@ module LightStep class Tracer FORMAT_TEXT_MAP = 1 FORMAT_BINARY = 2 + FORMAT_HTTP = 3 class Error < LightStep::Error; end class ConfigurationError < LightStep::Tracer::Error; end @@ -89,13 +90,15 @@ module LightStep # Inject a span into the given carrier # @param span [Span] # @param format [LightStep::Tracer::FORMAT_TEXT_MAP, LightStep::Tracer::FORMAT_BINARY] - # @param carrier [Hash-like] + # @param carrier [Hash, Net::HTTP::Request] def inject(span, format, carrier) case format when LightStep::Tracer::FORMAT_TEXT_MAP inject_to_text_map(span, carrier) when LightStep::Tracer::FORMAT_BINARY warn 'Binary inject format not yet implemented' + when LightStep::Tracer::FORMAT_HTTP + inject_to_http_headers(span, carrier) else warn 'Unknown inject format' end @@ -104,7 +107,9 @@ module LightStep # Extract a span from a carrier # @param operation_name [String] # @param format [LightStep::Tracer::FORMAT_TEXT_MAP, LightStep::Tracer::FORMAT_BINARY] - # @param carrier [Hash-like] + # @param carrier [Hash] This can be either a normal hash of http headers (uppercase and + # separated by underscores) or a Rack environment header. The Rack `HTTP_` prefix + # will automatically be stripped. # @return [Span] def extract(operation_name, format, carrier) case format @@ -113,6 +118,8 @@ module LightStep when LightStep::Tracer::FORMAT_BINARY warn 'Binary join format not yet implemented' nil + when LightStep::Tracer::FORMAT_HTTP + extract_from_rack_env(operation_name, carrier) else warn 'Unknown join format' nil @@ -175,6 +182,8 @@ module LightStep CARRIER_TRACER_STATE_PREFIX = 'ot-tracer-'.freeze CARRIER_BAGGAGE_PREFIX = 'ot-baggage-'.freeze + CARRIER_RACK_HTTP_TRACER_STATE_PREFIX = "#{CARRIER_TRACER_STATE_PREFIX.gsub("-", "_").upcase}" + CARRIER_RACK_HTTP_BAGGAGE_PREFIX = "#{CARRIER_BAGGAGE_PREFIX.gsub("-", "_").upcase}" DEFAULT_MAX_LOG_RECORDS = 1000 MIN_MAX_LOG_RECORDS = 1 @@ -213,5 +222,27 @@ module LightStep span end + + def inject_to_http_headers(span, carrier) + carrier[CARRIER_RACK_HTTP_TRACER_STATE_PREFIX + 'SPANID'] = span.id + carrier[CARRIER_RACK_HTTP_TRACER_STATE_PREFIX + 'TRACEID'] = span.trace_id unless span.trace_id.nil? + carrier[CARRIER_RACK_HTTP_TRACER_STATE_PREFIX + 'SAMPLED'] = 'true' + + span.baggage.each do |key, value| + carrier[CARRIER_RACK_HTTP_BAGGAGE_PREFIX + key.gsub("-", "_").upcase.gsub(/[^A-Z0-9_]/, '')] = value + end + end + + def extract_from_rack_env(operation_name, env) + extract_from_text_map(operation_name, env.reduce({}){|memo, tuple| + raw_header, value = tuple + header = raw_header.gsub(/^HTTP_/, '') + if header.start_with?(CARRIER_RACK_HTTP_TRACER_STATE_PREFIX) || + header.start_with?(CARRIER_RACK_HTTP_BAGGAGE_PREFIX) + memo[header.gsub("_", "-").downcase] = value + end + memo + }) + end end end diff --git a/spec/lightstep_spec.rb b/spec/lightstep_spec.rb index 446f0bc..86a456b 100644 --- a/spec/lightstep_spec.rb +++ b/spec/lightstep_spec.rb @@ -283,6 +283,35 @@ describe LightStep do span2.finish end + it 'should handle inject/extract for http requests and rack' do + tracer = init_test_tracer + span1 = tracer.start_span('test_span') + span1.set_baggage_item('footwear', 'cleats') + span1.set_baggage_item('umbrella', 'golf') + span1.set_baggage_item('unsafe!@#$%$^&header', 'value') + + carrier = {} + tracer.inject(span1, LightStep::Tracer::FORMAT_HTTP, carrier) + expect(carrier['OT_TRACER_TRACEID']).to eq(span1.trace_id) + expect(carrier['OT_TRACER_SPANID']).to eq(span1.id) + expect(carrier['OT_BAGGAGE_FOOTWEAR']).to eq('cleats') + expect(carrier['OT_BAGGAGE_UMBRELLA']).to eq('golf') + expect(carrier['OT_BAGGAGE_UNSAFEHEADER']).to eq('value') + + span2 = tracer.extract('test_span_2', LightStep::Tracer::FORMAT_HTTP, carrier) + expect(span2.trace_id).to eq(span1.trace_id) + expect(span2.tags[:parent_span_guid]).to eq(span1.id) + expect(span2.get_baggage_item('footwear')).to eq('cleats') + expect(span2.get_baggage_item('umbrella')).to eq('golf') + + span3 = tracer.extract('test_span_3', LightStep::Tracer::FORMAT_HTTP, {'HTTP_OT_TRACER_TRACEID' => 'abc123'}) + expect(span3.trace_id).to eq('abc123') + + span1.finish + span2.finish + span3.finish + end + it 'should handle concurrent spans' do result = nil tracer = init_callback_tracer(proc { |obj|; result = obj; }) From 4b9169f751e2168c4b834b0ad0c984ac55f259a1 Mon Sep 17 00:00:00 2001 From: Nick Gauthier Date: Thu, 10 Nov 2016 14:32:23 -0500 Subject: [PATCH 2/5] use -s in http headers and ignore case --- examples/rack/inject_extract.rb | 8 ++++---- lib/lightstep/tracer.rb | 26 ++++++++++++-------------- spec/lightstep_spec.rb | 22 +++++++++++----------- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/examples/rack/inject_extract.rb b/examples/rack/inject_extract.rb index 7fa8bce..9cbeef6 100644 --- a/examples/rack/inject_extract.rb +++ b/examples/rack/inject_extract.rb @@ -15,17 +15,17 @@ class Router def call(env) span = @tracer.start_span("router_call").set_baggage_item("request-id", $request_id) span.log(event: "router_request", env: env) - puts "parent #{span.trace_id}" + puts "parent #{span.span_context.trace_id}" client = Net::HTTP.new("localhost", "9002") req = Net::HTTP::Post.new("/") - @tracer.inject(span, LightStep::Tracer::FORMAT_HTTP, req) + @tracer.inject(span, LightStep::Tracer::FORMAT_HTTP_HEADERS, req) res = client.request(req) span.log(event: "application_response", response: res.to_s) span.finish @tracer.flush - puts "----> https://app.lightstep.com/#{$token}/trace?span_guid=#{span.id}&at_micros=#{span.start_micros} <----" + puts "----> https://app.lightstep.com/#{$token}/trace?span_guid=#{span.span_context.id}&at_micros=#{span.start_micros} <----" [200, {}, [res.body]] end end @@ -36,7 +36,7 @@ class App end def call(env) - span = @tracer.extract("app_call", LightStep::Tracer::FORMAT_HTTP, env) + span = @tracer.extract("app_call", LightStep::Tracer::FORMAT_HTTP_HEADERS, env) puts "child #{span.to_h[:trace_guid]}" span.log(event: "application", env: env) sleep 0.05 diff --git a/lib/lightstep/tracer.rb b/lib/lightstep/tracer.rb index 42845fc..5085660 100644 --- a/lib/lightstep/tracer.rb +++ b/lib/lightstep/tracer.rb @@ -11,7 +11,7 @@ module LightStep class Tracer FORMAT_TEXT_MAP = 1 FORMAT_BINARY = 2 - FORMAT_HTTP = 3 + FORMAT_HTTP_HEADERS = 3 class Error < LightStep::Error; end class ConfigurationError < LightStep::Tracer::Error; end @@ -97,7 +97,7 @@ module LightStep inject_to_text_map(span, carrier) when LightStep::Tracer::FORMAT_BINARY warn 'Binary inject format not yet implemented' - when LightStep::Tracer::FORMAT_HTTP + when LightStep::Tracer::FORMAT_HTTP_HEADERS inject_to_http_headers(span, carrier) else warn 'Unknown inject format' @@ -118,7 +118,7 @@ module LightStep when LightStep::Tracer::FORMAT_BINARY warn 'Binary join format not yet implemented' nil - when LightStep::Tracer::FORMAT_HTTP + when LightStep::Tracer::FORMAT_HTTP_HEADERS extract_from_rack_env(operation_name, carrier) else warn 'Unknown join format' @@ -182,8 +182,6 @@ module LightStep CARRIER_TRACER_STATE_PREFIX = 'ot-tracer-'.freeze CARRIER_BAGGAGE_PREFIX = 'ot-baggage-'.freeze - CARRIER_RACK_HTTP_TRACER_STATE_PREFIX = "#{CARRIER_TRACER_STATE_PREFIX.gsub("-", "_").upcase}" - CARRIER_RACK_HTTP_BAGGAGE_PREFIX = "#{CARRIER_BAGGAGE_PREFIX.gsub("-", "_").upcase}" DEFAULT_MAX_LOG_RECORDS = 1000 MIN_MAX_LOG_RECORDS = 1 @@ -224,22 +222,22 @@ module LightStep end def inject_to_http_headers(span, carrier) - carrier[CARRIER_RACK_HTTP_TRACER_STATE_PREFIX + 'SPANID'] = span.id - carrier[CARRIER_RACK_HTTP_TRACER_STATE_PREFIX + 'TRACEID'] = span.trace_id unless span.trace_id.nil? - carrier[CARRIER_RACK_HTTP_TRACER_STATE_PREFIX + 'SAMPLED'] = 'true' + carrier[CARRIER_TRACER_STATE_PREFIX + 'spanid'] = span.span_context.id + carrier[CARRIER_TRACER_STATE_PREFIX + 'traceid'] = span.span_context.trace_id unless span.span_context.trace_id.nil? + carrier[CARRIER_TRACER_STATE_PREFIX + 'sampled'] = 'true' - span.baggage.each do |key, value| - carrier[CARRIER_RACK_HTTP_BAGGAGE_PREFIX + key.gsub("-", "_").upcase.gsub(/[^A-Z0-9_]/, '')] = value + span.span_context.baggage.each do |key, value| + carrier[CARRIER_BAGGAGE_PREFIX + key.gsub(/[^A-Za-z0-9-]/, '')] = value end end def extract_from_rack_env(operation_name, env) extract_from_text_map(operation_name, env.reduce({}){|memo, tuple| raw_header, value = tuple - header = raw_header.gsub(/^HTTP_/, '') - if header.start_with?(CARRIER_RACK_HTTP_TRACER_STATE_PREFIX) || - header.start_with?(CARRIER_RACK_HTTP_BAGGAGE_PREFIX) - memo[header.gsub("_", "-").downcase] = value + header = raw_header.gsub(/^HTTP_/, '').gsub("_","-").downcase + if header.start_with?(CARRIER_TRACER_STATE_PREFIX) || + header.start_with?(CARRIER_BAGGAGE_PREFIX) + memo[header.downcase] = value end memo }) diff --git a/spec/lightstep_spec.rb b/spec/lightstep_spec.rb index 9a82c23..9ade56b 100644 --- a/spec/lightstep_spec.rb +++ b/spec/lightstep_spec.rb @@ -292,21 +292,21 @@ describe LightStep do span1.set_baggage_item('unsafe!@#$%$^&header', 'value') carrier = {} - tracer.inject(span1, LightStep::Tracer::FORMAT_HTTP, carrier) - expect(carrier['OT_TRACER_TRACEID']).to eq(span1.trace_id) - expect(carrier['OT_TRACER_SPANID']).to eq(span1.id) - expect(carrier['OT_BAGGAGE_FOOTWEAR']).to eq('cleats') - expect(carrier['OT_BAGGAGE_UMBRELLA']).to eq('golf') - expect(carrier['OT_BAGGAGE_UNSAFEHEADER']).to eq('value') + tracer.inject(span1, LightStep::Tracer::FORMAT_HTTP_HEADERS, carrier) + expect(carrier['ot-tracer-traceid']).to eq(span1.span_context.trace_id) + expect(carrier['ot-tracer-spanid']).to eq(span1.span_context.id) + expect(carrier['ot-baggage-footwear']).to eq('cleats') + expect(carrier['ot-baggage-umbrella']).to eq('golf') + expect(carrier['ot-baggage-unsafeheader']).to eq('value') - span2 = tracer.extract('test_span_2', LightStep::Tracer::FORMAT_HTTP, carrier) - expect(span2.trace_id).to eq(span1.trace_id) - expect(span2.tags[:parent_span_guid]).to eq(span1.id) + span2 = tracer.extract('test_span_2', LightStep::Tracer::FORMAT_HTTP_HEADERS, carrier) + expect(span2.span_context.trace_id).to eq(span1.span_context.trace_id) + expect(span2.tags[:parent_span_guid]).to eq(span1.span_context.id) expect(span2.get_baggage_item('footwear')).to eq('cleats') expect(span2.get_baggage_item('umbrella')).to eq('golf') - span3 = tracer.extract('test_span_3', LightStep::Tracer::FORMAT_HTTP, {'HTTP_OT_TRACER_TRACEID' => 'abc123'}) - expect(span3.trace_id).to eq('abc123') + span3 = tracer.extract('test_span_3', LightStep::Tracer::FORMAT_HTTP_HEADERS, {'HTTP_OT_TRACER_TRACEID' => 'abc123'}) + expect(span3.span_context.trace_id).to eq('abc123') span1.finish span2.finish From 9a443e4792ea90ac553f5489e8da37ae8a0a3646 Mon Sep 17 00:00:00 2001 From: Nick Gauthier Date: Fri, 11 Nov 2016 14:45:58 -0500 Subject: [PATCH 3/5] drop baggage keys that can't be used as http headers during extract additional tests cases around case and underscores --- lib/lightstep/tracer.rb | 15 +++++++++------ spec/lightstep_spec.rb | 7 ++++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/lightstep/tracer.rb b/lib/lightstep/tracer.rb index 5085660..8d6e1a8 100644 --- a/lib/lightstep/tracer.rb +++ b/lib/lightstep/tracer.rb @@ -227,18 +227,21 @@ module LightStep carrier[CARRIER_TRACER_STATE_PREFIX + 'sampled'] = 'true' span.span_context.baggage.each do |key, value| - carrier[CARRIER_BAGGAGE_PREFIX + key.gsub(/[^A-Za-z0-9-]/, '')] = value + key = key.downcase.gsub("_", "-") + if key =~ /[^a-z0-9\-]/ + # TODO: log the error internally + next + end + carrier[CARRIER_BAGGAGE_PREFIX + key] = value end end def extract_from_rack_env(operation_name, env) extract_from_text_map(operation_name, env.reduce({}){|memo, tuple| raw_header, value = tuple - header = raw_header.gsub(/^HTTP_/, '').gsub("_","-").downcase - if header.start_with?(CARRIER_TRACER_STATE_PREFIX) || - header.start_with?(CARRIER_BAGGAGE_PREFIX) - memo[header.downcase] = value - end + header = raw_header.gsub(/^HTTP_/, '').gsub("_", "-").downcase + + memo[header] = value if header.start_with?(CARRIER_TRACER_STATE_PREFIX, CARRIER_BAGGAGE_PREFIX) memo }) end diff --git a/spec/lightstep_spec.rb b/spec/lightstep_spec.rb index 9ade56b..4ccf347 100644 --- a/spec/lightstep_spec.rb +++ b/spec/lightstep_spec.rb @@ -290,6 +290,7 @@ describe LightStep do span1.set_baggage_item('footwear', 'cleats') span1.set_baggage_item('umbrella', 'golf') span1.set_baggage_item('unsafe!@#$%$^&header', 'value') + span1.set_baggage_item('CASE-Sensitivity_Underscores', 'value') carrier = {} tracer.inject(span1, LightStep::Tracer::FORMAT_HTTP_HEADERS, carrier) @@ -297,13 +298,17 @@ describe LightStep do expect(carrier['ot-tracer-spanid']).to eq(span1.span_context.id) expect(carrier['ot-baggage-footwear']).to eq('cleats') expect(carrier['ot-baggage-umbrella']).to eq('golf') - expect(carrier['ot-baggage-unsafeheader']).to eq('value') + expect(carrier['ot-baggage-unsafeheader']).to be_nil + expect(carrier['ot-baggage-case-sensitivity-underscores']).to eq('value') span2 = tracer.extract('test_span_2', LightStep::Tracer::FORMAT_HTTP_HEADERS, carrier) expect(span2.span_context.trace_id).to eq(span1.span_context.trace_id) expect(span2.tags[:parent_span_guid]).to eq(span1.span_context.id) expect(span2.get_baggage_item('footwear')).to eq('cleats') expect(span2.get_baggage_item('umbrella')).to eq('golf') + expect(span2.get_baggage_item('unsafe!@#$%$^&header')).to be_nil + expect(span2.get_baggage_item('unsafeheader')).to be_nil + expect(span2.get_baggage_item('case-sensitivity-underscores')).to eq('value') span3 = tracer.extract('test_span_3', LightStep::Tracer::FORMAT_HTTP_HEADERS, {'HTTP_OT_TRACER_TRACEID' => 'abc123'}) expect(span3.span_context.trace_id).to eq('abc123') From 430b03c333e042c9755fb4b10fe95fe9753d1730 Mon Sep 17 00:00:00 2001 From: Nick Gauthier Date: Mon, 14 Nov 2016 13:09:18 -0500 Subject: [PATCH 4/5] change to rack-specific format --- examples/rack/inject_extract.rb | 4 ++-- lib/lightstep/tracer.rb | 20 +++++++++----------- spec/lightstep_spec.rb | 12 +++++++++--- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/examples/rack/inject_extract.rb b/examples/rack/inject_extract.rb index 9cbeef6..1424efe 100644 --- a/examples/rack/inject_extract.rb +++ b/examples/rack/inject_extract.rb @@ -19,7 +19,7 @@ class Router client = Net::HTTP.new("localhost", "9002") req = Net::HTTP::Post.new("/") - @tracer.inject(span, LightStep::Tracer::FORMAT_HTTP_HEADERS, req) + @tracer.inject(span, LightStep::Tracer::FORMAT_RACK, req) res = client.request(req) span.log(event: "application_response", response: res.to_s) @@ -36,7 +36,7 @@ class App end def call(env) - span = @tracer.extract("app_call", LightStep::Tracer::FORMAT_HTTP_HEADERS, env) + span = @tracer.extract("app_call", LightStep::Tracer::FORMAT_RACK, env) puts "child #{span.to_h[:trace_guid]}" span.log(event: "application", env: env) sleep 0.05 diff --git a/lib/lightstep/tracer.rb b/lib/lightstep/tracer.rb index 8d6e1a8..d575985 100644 --- a/lib/lightstep/tracer.rb +++ b/lib/lightstep/tracer.rb @@ -11,7 +11,7 @@ module LightStep class Tracer FORMAT_TEXT_MAP = 1 FORMAT_BINARY = 2 - FORMAT_HTTP_HEADERS = 3 + FORMAT_RACK = 3 class Error < LightStep::Error; end class ConfigurationError < LightStep::Tracer::Error; end @@ -90,15 +90,15 @@ module LightStep # Inject a span into the given carrier # @param span [Span] # @param format [LightStep::Tracer::FORMAT_TEXT_MAP, LightStep::Tracer::FORMAT_BINARY] - # @param carrier [Hash, Net::HTTP::Request] + # @param carrier [Hash] def inject(span, format, carrier) case format when LightStep::Tracer::FORMAT_TEXT_MAP inject_to_text_map(span, carrier) when LightStep::Tracer::FORMAT_BINARY warn 'Binary inject format not yet implemented' - when LightStep::Tracer::FORMAT_HTTP_HEADERS - inject_to_http_headers(span, carrier) + when LightStep::Tracer::FORMAT_RACK + inject_to_rack(span, carrier) else warn 'Unknown inject format' end @@ -107,9 +107,7 @@ module LightStep # Extract a span from a carrier # @param operation_name [String] # @param format [LightStep::Tracer::FORMAT_TEXT_MAP, LightStep::Tracer::FORMAT_BINARY] - # @param carrier [Hash] This can be either a normal hash of http headers (uppercase and - # separated by underscores) or a Rack environment header. The Rack `HTTP_` prefix - # will automatically be stripped. + # @param carrier [Hash] # @return [Span] def extract(operation_name, format, carrier) case format @@ -118,8 +116,8 @@ module LightStep when LightStep::Tracer::FORMAT_BINARY warn 'Binary join format not yet implemented' nil - when LightStep::Tracer::FORMAT_HTTP_HEADERS - extract_from_rack_env(operation_name, carrier) + when LightStep::Tracer::FORMAT_RACK + extract_from_rack(operation_name, carrier) else warn 'Unknown join format' nil @@ -221,7 +219,7 @@ module LightStep span end - def inject_to_http_headers(span, carrier) + def inject_to_rack(span, carrier) carrier[CARRIER_TRACER_STATE_PREFIX + 'spanid'] = span.span_context.id carrier[CARRIER_TRACER_STATE_PREFIX + 'traceid'] = span.span_context.trace_id unless span.span_context.trace_id.nil? carrier[CARRIER_TRACER_STATE_PREFIX + 'sampled'] = 'true' @@ -236,7 +234,7 @@ module LightStep end end - def extract_from_rack_env(operation_name, env) + def extract_from_rack(operation_name, env) extract_from_text_map(operation_name, env.reduce({}){|memo, tuple| raw_header, value = tuple header = raw_header.gsub(/^HTTP_/, '').gsub("_", "-").downcase diff --git a/spec/lightstep_spec.rb b/spec/lightstep_spec.rb index 4ccf347..9925daa 100644 --- a/spec/lightstep_spec.rb +++ b/spec/lightstep_spec.rb @@ -293,7 +293,7 @@ describe LightStep do span1.set_baggage_item('CASE-Sensitivity_Underscores', 'value') carrier = {} - tracer.inject(span1, LightStep::Tracer::FORMAT_HTTP_HEADERS, carrier) + tracer.inject(span1, LightStep::Tracer::FORMAT_RACK, carrier) expect(carrier['ot-tracer-traceid']).to eq(span1.span_context.trace_id) expect(carrier['ot-tracer-spanid']).to eq(span1.span_context.id) expect(carrier['ot-baggage-footwear']).to eq('cleats') @@ -301,7 +301,13 @@ describe LightStep do expect(carrier['ot-baggage-unsafeheader']).to be_nil expect(carrier['ot-baggage-case-sensitivity-underscores']).to eq('value') - span2 = tracer.extract('test_span_2', LightStep::Tracer::FORMAT_HTTP_HEADERS, carrier) + carrier = carrier.reduce({}) do |memo, tuple| + key, value = tuple + memo["HTTP_#{key.gsub("-", "_").upcase}"] = value + memo + end + + span2 = tracer.extract('test_span_2', LightStep::Tracer::FORMAT_RACK, carrier) expect(span2.span_context.trace_id).to eq(span1.span_context.trace_id) expect(span2.tags[:parent_span_guid]).to eq(span1.span_context.id) expect(span2.get_baggage_item('footwear')).to eq('cleats') @@ -310,7 +316,7 @@ describe LightStep do expect(span2.get_baggage_item('unsafeheader')).to be_nil expect(span2.get_baggage_item('case-sensitivity-underscores')).to eq('value') - span3 = tracer.extract('test_span_3', LightStep::Tracer::FORMAT_HTTP_HEADERS, {'HTTP_OT_TRACER_TRACEID' => 'abc123'}) + span3 = tracer.extract('test_span_3', LightStep::Tracer::FORMAT_RACK, {'HTTP_OT_TRACER_TRACEID' => 'abc123'}) expect(span3.span_context.trace_id).to eq('abc123') span1.finish From d1f99c7f7f55ec587f716e7b8c1f5ade1f89e129 Mon Sep 17 00:00:00 2001 From: Nick Gauthier Date: Mon, 14 Nov 2016 13:11:47 -0500 Subject: [PATCH 5/5] allowe mixed case and underscores in rack inject --- lib/lightstep/tracer.rb | 3 +-- spec/lightstep_spec.rb | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/lightstep/tracer.rb b/lib/lightstep/tracer.rb index d575985..880648f 100644 --- a/lib/lightstep/tracer.rb +++ b/lib/lightstep/tracer.rb @@ -225,8 +225,7 @@ module LightStep carrier[CARRIER_TRACER_STATE_PREFIX + 'sampled'] = 'true' span.span_context.baggage.each do |key, value| - key = key.downcase.gsub("_", "-") - if key =~ /[^a-z0-9\-]/ + if key =~ /[^A-Za-z0-9\-_]/ # TODO: log the error internally next end diff --git a/spec/lightstep_spec.rb b/spec/lightstep_spec.rb index 9925daa..8c524bf 100644 --- a/spec/lightstep_spec.rb +++ b/spec/lightstep_spec.rb @@ -299,7 +299,7 @@ describe LightStep do expect(carrier['ot-baggage-footwear']).to eq('cleats') expect(carrier['ot-baggage-umbrella']).to eq('golf') expect(carrier['ot-baggage-unsafeheader']).to be_nil - expect(carrier['ot-baggage-case-sensitivity-underscores']).to eq('value') + expect(carrier['ot-baggage-CASE-Sensitivity_Underscores']).to eq('value') carrier = carrier.reduce({}) do |memo, tuple| key, value = tuple