Skip to content

Commit b375af1

Browse files
authored
fix: aws-sdk nitpicks (#1023)
1 parent 6cba354 commit b375af1

File tree

3 files changed

+28
-69
lines changed

3 files changed

+28
-69
lines changed

instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/db_helper.rb

Lines changed: 0 additions & 28 deletions
This file was deleted.

instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/handler.rb

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,34 @@ module Instrumentation
99
module AwsSdk
1010
# Generates Spans for all interactions with AwsSdk
1111
class Handler < Seahorse::Client::Handler
12-
def call(context)
13-
span_name = get_span_name(context)
14-
attributes = get_span_attributes(context)
12+
def call(context) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
13+
return super unless context
1514

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

25+
tracer.in_span("#{service_name}.#{operation}", attributes: attributes, kind: OpenTelemetry::Trace::SpanKind::CLIENT) do |span|
2626
if instrumentation_config[:suppress_internal_instrumentation]
27-
OpenTelemetry::Common::Utilities.untraced(&execute)
27+
OpenTelemetry::Common::Utilities.untraced { super }
2828
else
29-
execute.call
29+
super
30+
end.tap do |response|
31+
if (err = response.error)
32+
span.record_exception(err)
33+
span.status = Trace::Status.error(err)
34+
end
3035
end
3136
end
3237
end
3338

34-
def get_span_attributes(context)
35-
span_attributes = {
36-
'aws.region' => context.config.region,
37-
OpenTelemetry::SemanticConventions::Trace::RPC_SYSTEM => 'aws-api',
38-
OpenTelemetry::SemanticConventions::Trace::RPC_METHOD => get_operation(context),
39-
OpenTelemetry::SemanticConventions::Trace::RPC_SERVICE => get_service_name(context)
40-
}
41-
42-
db_attributes = DbHelper.get_db_attributes(context, get_service_name(context), get_operation(context))
43-
span_attributes.merge(db_attributes)
44-
end
45-
46-
def get_service_name(context)
47-
context&.client.class.api.metadata['serviceId'] || context&.client.class.to_s.split('::')[1]
48-
end
49-
50-
def get_operation(context)
51-
context&.operation&.name
52-
end
53-
54-
def get_span_name(context)
55-
"#{get_service_name(context)}.#{get_operation(context)}"
56-
end
39+
private
5740

5841
def tracer
5942
AwsSdk::Instrumentation.instance.tracer

instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/instrumentation.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
1212
MINIMUM_VERSION = Gem::Version.new('2.0')
1313

1414
install do |_config|
15-
require_relative 'handler'
16-
require_relative 'services'
17-
require_relative 'db_helper'
18-
15+
require_dependencies
1916
add_plugin(Seahorse::Client::Base, *loaded_constants)
2017
end
2118

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

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

29+
private
30+
31+
def require_dependencies
32+
require_relative 'handler'
33+
require_relative 'services'
34+
end
35+
3236
def gem_version
3337
if Gem.loaded_specs['aws-sdk']
3438
Gem.loaded_specs['aws-sdk'].version

0 commit comments

Comments
 (0)