From 29f9544b1497358888cd93175e5579cab83fd247 Mon Sep 17 00:00:00 2001 From: Daniel Azuma Date: Tue, 26 Nov 2019 11:53:36 -0800 Subject: [PATCH] fix: Prevent OpenCensus integration from getting out of sync on upload commands --- lib/google/apis/core/http_command.rb | 18 ++++++++++-------- spec/google/apis/core/upload_spec.rb | 10 ++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/google/apis/core/http_command.rb b/lib/google/apis/core/http_command.rb index b9fc5f779..67c0d195b 100644 --- a/lib/google/apis/core/http_command.rb +++ b/lib/google/apis/core/http_command.rb @@ -385,14 +385,16 @@ module Google return unless @opencensus_span return unless OpenCensus::Trace.span_context - if @http_res.body.respond_to? :bytesize - @opencensus_span.put_message_event \ - OpenCensus::Trace::SpanBuilder::RECEIVED, 1, @http_res.body.bytesize - end - status = @http_res.status.to_i - if status > 0 - @opencensus_span.set_status map_http_status status - @opencensus_span.put_attribute "http.status_code", status + if @http_res + if @http_res.body.respond_to? :bytesize + @opencensus_span.put_message_event \ + OpenCensus::Trace::SpanBuilder::RECEIVED, 1, @http_res.body.bytesize + end + status = @http_res.status.to_i + if status > 0 + @opencensus_span.set_status map_http_status status + @opencensus_span.put_attribute "http.status_code", status + end end OpenCensus::Trace.end_span @opencensus_span diff --git a/spec/google/apis/core/upload_spec.rb b/spec/google/apis/core/upload_spec.rb index de0cf1571..097eb893e 100644 --- a/spec/google/apis/core/upload_spec.rb +++ b/spec/google/apis/core/upload_spec.rb @@ -177,6 +177,16 @@ RSpec.describe Google::Apis::Core::ResumableUploadCommand do expect(a_request(:post, 'https://www.googleapis.com/zoo/animals') .with(body: 'Hello world')).to have_been_made end + + it 'should generate a proper opencensus span' do + OpenCensus::Trace.start_request_trace do |span_context| + command.execute(client) + spans = span_context.build_contained_spans + expect(spans.size).to eql 1 + span = spans.first + expect(span.name.value).to eql '/zoo/animals' + end + end end context 'with retriable error on start' do