Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add opentelemetry instrumentation #3414

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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 google-apis-core/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ group :development do
gem 'redis', '~> 3.2'
gem 'logging', '~> 2.2'
gem 'opencensus', '~> 0.4'
gem 'opentelemetry-sdk'
gem 'httparty'
end

Expand Down
1 change: 1 addition & 0 deletions google-apis-core/google-apis-core.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]

gem.required_ruby_version = '>= 2.5'
gem.add_runtime_dependency "opentelemetry-api", '~> 1.0.0.rc1'
gem.add_runtime_dependency "representable", "~> 3.0"
gem.add_runtime_dependency "retriable", ">= 2.0", "< 4.0"
gem.add_runtime_dependency "addressable", "~> 2.5", ">= 2.5.1"
Expand Down
33 changes: 19 additions & 14 deletions google-apis-core/lib/google/apis/core/http_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

require 'opentelemetry'
require 'addressable/uri'
require 'addressable/template'
require 'google/apis/options'
Expand All @@ -27,6 +28,7 @@ module Core
class HttpCommand
include Logging

OTEL_TRACER = OpenTelemetry.tracer_provider.tracer('Google::Apis::Core::HttpCommand', Google::Apis::Core::VERSION)
RETRIABLE_ERRORS = [Google::Apis::ServerError, Google::Apis::RateLimitError, Google::Apis::TransmissionError]

begin
Expand Down Expand Up @@ -99,20 +101,23 @@ def execute(client)
prepare!
opencensus_begin_span
begin
Retriable.retriable tries: options.retries + 1,
base_interval: 1,
multiplier: 2,
on: RETRIABLE_ERRORS do |try|
# This 2nd level retriable only catches auth errors, and supports 1 retry, which allows
# auth to be re-attempted without having to retry all sorts of other failures like
# NotFound, etc
auth_tries = (try == 1 && authorization_refreshable? ? 2 : 1)
Retriable.retriable tries: auth_tries,
on: [Google::Apis::AuthorizationError, Signet::AuthorizationError, Signet::RemoteServerError, Signet::UnexpectedStatusError],
on_retry: proc { |*| refresh_authorization } do
execute_once(client).tap do |result|
if block_given?
yield result, nil
url_host = url.host.to_s
OTEL_TRACER.in_span(url_host) do
Copy link
Author

Choose a reason for hiding this comment

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

We have instrumentation for the underlying http lib (http client), so I think it makes sense to not duplicate all the HTTP attributes on this span.

This span also serves the purpose of grouping retired network calls under a common parent span.

Retriable.retriable tries: options.retries + 1,
base_interval: 1,
multiplier: 2,
on: RETRIABLE_ERRORS do |try|
# This 2nd level retriable only catches auth errors, and supports 1 retry, which allows
# auth to be re-attempted without having to retry all sorts of other failures like
# NotFound, etc
auth_tries = (try == 1 && authorization_refreshable? ? 2 : 1)
Retriable.retriable tries: auth_tries,
on: [Google::Apis::AuthorizationError, Signet::AuthorizationError, Signet::RemoteServerError, Signet::UnexpectedStatusError],
on_retry: proc { |*| refresh_authorization } do
execute_once(client).tap do |result|
if block_given?
yield result, nil
end
end
end
end
Expand Down
38 changes: 38 additions & 0 deletions google-apis-core/spec/google/apis/core/http_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,44 @@ class DecryptResponse
end
end if Google::Apis::Core::HttpCommand::OPENCENSUS_AVAILABLE

context('with opentelemetry sdk configured') do
require 'opentelemetry-sdk'

EXPORTER = OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new
SPAN_PROCESSOR = OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(EXPORTER)

OpenTelemetry::SDK.configure do |c|
c.add_span_processor SPAN_PROCESSOR
end

let(:span) { EXPORTER.finished_spans.first }

before { EXPORTER.reset }

it 'should create an OpenTelemetry span for a successful call' do
stub_request(:get, 'https://www.googleapis.com/zoo/animals').to_return(status: [200, ''], body: "Hello world")
command = Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
command.execute(client)

expect(span.name).to eq('www.googleapis.com')
expect(span.status).to be_ok
expect(span.instrumentation_library.name).to eq('Google::Apis::Core::HttpCommand')
expect(span.instrumentation_library.version).to eq(Google::Apis::Core::VERSION)
end

it 'should create an OpenTelemetry span for a call failure' do
stub_request(:get, 'https://www.googleapis.com/zoo/animals').to_return(status: [403, ''])
command = Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
expect { command.execute(client) }.to raise_error(Google::Apis::ClientError)

expect(span.name).to eq('www.googleapis.com')
expect(span.status).not_to be_ok
expect(span.events.first.attributes['exception.type']).to eq('Google::Apis::ClientError')
expect(span.events.first.attributes['exception.message']).to eq('Invalid request')
expect(span.events.first.attributes['exception.stacktrace']).not_to be_empty
end
end

it 'should send repeated query parameters' do
stub_request(:get, 'https://www.googleapis.com/zoo/animals?a=1&a=2&a=3')
.to_return(status: [200, ''])
Expand Down