Skip to content

Commit

Permalink
fix: aws-sdk nitpicks (open-telemetry#1023)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertlaurin authored Nov 22, 2021
1 parent 6cba354 commit b375af1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 69 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,34 @@ module Instrumentation
module AwsSdk
# Generates Spans for all interactions with AwsSdk
class Handler < Seahorse::Client::Handler
def call(context)
span_name = get_span_name(context)
attributes = get_span_attributes(context)
def call(context) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
return super unless context

tracer.in_span(span_name, kind: OpenTelemetry::Trace::SpanKind::CLIENT, attributes: attributes) do |span|
execute = proc {
super(context).tap do |response|
if (err = response.error)
span.record_exception(err)
span.status = Trace::Status.error(err)
end
end
}
service_name = context.client.class.api.metadata['serviceId'] || context.client.class.to_s.split('::')[1]
operation = context.operation&.name
attributes = {
'aws.region' => context.config.region,
OpenTelemetry::SemanticConventions::Trace::RPC_SYSTEM => 'aws-api',
OpenTelemetry::SemanticConventions::Trace::RPC_METHOD => operation,
OpenTelemetry::SemanticConventions::Trace::RPC_SERVICE => service_name
}
attributes[SemanticConventions::Trace::DB_SYSTEM] = 'dynamodb' if service_name == 'DynamoDB'

tracer.in_span("#{service_name}.#{operation}", attributes: attributes, kind: OpenTelemetry::Trace::SpanKind::CLIENT) do |span|
if instrumentation_config[:suppress_internal_instrumentation]
OpenTelemetry::Common::Utilities.untraced(&execute)
OpenTelemetry::Common::Utilities.untraced { super }
else
execute.call
super
end.tap do |response|
if (err = response.error)
span.record_exception(err)
span.status = Trace::Status.error(err)
end
end
end
end

def get_span_attributes(context)
span_attributes = {
'aws.region' => context.config.region,
OpenTelemetry::SemanticConventions::Trace::RPC_SYSTEM => 'aws-api',
OpenTelemetry::SemanticConventions::Trace::RPC_METHOD => get_operation(context),
OpenTelemetry::SemanticConventions::Trace::RPC_SERVICE => get_service_name(context)
}

db_attributes = DbHelper.get_db_attributes(context, get_service_name(context), get_operation(context))
span_attributes.merge(db_attributes)
end

def get_service_name(context)
context&.client.class.api.metadata['serviceId'] || context&.client.class.to_s.split('::')[1]
end

def get_operation(context)
context&.operation&.name
end

def get_span_name(context)
"#{get_service_name(context)}.#{get_operation(context)}"
end
private

def tracer
AwsSdk::Instrumentation.instance.tracer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
MINIMUM_VERSION = Gem::Version.new('2.0')

install do |_config|
require_relative 'handler'
require_relative 'services'
require_relative 'db_helper'

require_dependencies
add_plugin(Seahorse::Client::Base, *loaded_constants)
end

Expand All @@ -29,6 +26,13 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base

option :suppress_internal_instrumentation, default: false, validate: :boolean

private

def require_dependencies
require_relative 'handler'
require_relative 'services'
end

def gem_version
if Gem.loaded_specs['aws-sdk']
Gem.loaded_specs['aws-sdk'].version
Expand Down

0 comments on commit b375af1

Please sign in to comment.