@@ -9,36 +9,32 @@ module Instrumentation
99 module AwsSdk
1010 # Generates Spans for all interactions with AwsSdk
1111 class Handler < Seahorse ::Client ::Handler
12- SQS_SEND_MESSAGE = 'SQS.SendMessage'
13- SQS_SEND_MESSAGE_BATCH = 'SQS.SendMessageBatch'
14- SQS_RECEIVE_MESSAGE = 'SQS.ReceiveMessage'
15- SNS_PUBLISH = 'SNS.Publish'
16-
1712 def call ( context )
1813 return super unless context
1914
20- service_name = service_name ( context )
15+ service_id = service_name ( context )
2116 operation = context . operation &.name
22- client_method = "#{ service_name } .#{ operation } "
23- attributes = {
24- 'aws.region' => context . config . region ,
25- OpenTelemetry ::SemanticConventions ::Trace ::RPC_SYSTEM => 'aws-api' ,
26- OpenTelemetry ::SemanticConventions ::Trace ::RPC_METHOD => operation ,
27- OpenTelemetry ::SemanticConventions ::Trace ::RPC_SERVICE => service_name
28- }
29- attributes [ SemanticConventions ::Trace ::DB_SYSTEM ] = 'dynamodb' if service_name == 'DynamoDB'
30- MessagingHelper . apply_sqs_attributes ( attributes , context , client_method ) if service_name == 'SQS'
31- MessagingHelper . apply_sns_attributes ( attributes , context , client_method ) if service_name == 'SNS'
17+ client_method = "#{ service_id } .#{ operation } "
18+
19+ tracer . in_span (
20+ span_name ( context , client_method , service_id ) ,
21+ attributes : attributes ( context , client_method , service_id , operation ) ,
22+ kind : span_kind ( client_method , service_id )
23+ ) do |span |
24+ if instrumentation_config [ :inject_messaging_context ] &&
25+ %w[ SQS SNS ] . include? ( service_id )
26+ MessagingHelper . inject_context ( context , client_method )
27+ end
3228
33- tracer . in_span ( span_name ( context , client_method ) , attributes : attributes , kind : span_kind ( client_method ) ) do |span |
34- inject_context ( context , client_method )
3529 if instrumentation_config [ :suppress_internal_instrumentation ]
3630 OpenTelemetry ::Common ::Utilities . untraced { super }
3731 else
3832 super
3933 end . tap do |response |
40- span . set_attribute ( OpenTelemetry ::SemanticConventions ::Trace ::HTTP_STATUS_CODE ,
41- context . http_response . status_code )
34+ span . set_attribute (
35+ OpenTelemetry ::SemanticConventions ::Trace ::HTTP_STATUS_CODE ,
36+ context . http_response . status_code
37+ )
4238
4339 if ( err = response . error )
4440 span . record_exception ( err )
@@ -65,48 +61,40 @@ def service_name(context)
6561 context . client . class . api . metadata [ 'serviceId' ] || context . client . class . to_s . split ( '::' ) [ 1 ]
6662 end
6763
68- SEND_MESSAGE_CLIENT_METHODS = [ SQS_SEND_MESSAGE , SQS_SEND_MESSAGE_BATCH , SNS_PUBLISH ] . freeze
69- def inject_context ( context , client_method )
70- return unless SEND_MESSAGE_CLIENT_METHODS . include? client_method
71- return unless instrumentation_config [ :inject_messaging_context ]
72-
73- if client_method == SQS_SEND_MESSAGE_BATCH
74- context . params [ :entries ] . each do |entry |
75- entry [ :message_attributes ] ||= { }
76- OpenTelemetry . propagation . inject ( entry [ :message_attributes ] , setter : MessageAttributeSetter )
77- end
64+ def span_kind ( client_method , service_id )
65+ case service_id
66+ when 'SQS' , 'SNS'
67+ MessagingHelper . span_kind ( client_method )
7868 else
79- context . params [ :message_attributes ] ||= { }
80- OpenTelemetry . propagation . inject ( context . params [ :message_attributes ] , setter : MessageAttributeSetter )
69+ OpenTelemetry ::Trace ::SpanKind ::CLIENT
8170 end
8271 end
8372
84- def span_kind ( client_method )
85- case client_method
86- when SQS_SEND_MESSAGE , SQS_SEND_MESSAGE_BATCH , SNS_PUBLISH
87- OpenTelemetry ::Trace ::SpanKind ::PRODUCER
88- when SQS_RECEIVE_MESSAGE
89- OpenTelemetry ::Trace ::SpanKind ::CONSUMER
73+ def span_name ( context , client_method , service_id )
74+ case service_id
75+ when 'SQS' , 'SNS'
76+ MessagingHelper . legacy_span_name ( context , client_method )
9077 else
91- OpenTelemetry :: Trace :: SpanKind :: CLIENT
78+ client_method
9279 end
9380 end
9481
95- def span_name ( context , client_method )
96- case client_method
97- when SQS_SEND_MESSAGE , SQS_SEND_MESSAGE_BATCH , SNS_PUBLISH
98- "#{ MessagingHelper . queue_name ( context ) } publish"
99- when SQS_RECEIVE_MESSAGE
100- "#{ MessagingHelper . queue_name ( context ) } receive"
101- else
102- client_method
82+ def attributes ( context , client_method , service_id , operation )
83+ {
84+ 'aws.region' => context . config . region ,
85+ OpenTelemetry ::SemanticConventions ::Trace ::RPC_SYSTEM => 'aws-api' ,
86+ OpenTelemetry ::SemanticConventions ::Trace ::RPC_METHOD => operation ,
87+ OpenTelemetry ::SemanticConventions ::Trace ::RPC_SERVICE => service_id
88+ } . tap do |attrs |
89+ attrs [ SemanticConventions ::Trace ::DB_SYSTEM ] = 'dynamodb' if service_id == 'DynamoDB'
90+ MessagingHelper . apply_span_attributes ( context , attrs , client_method , service_id ) if %w[ SQS SNS ] . include? ( service_id )
10391 end
10492 end
10593 end
10694
10795 # A Seahorse::Client::Plugin that enables instrumentation for all AWS services
10896 class Plugin < Seahorse ::Client ::Plugin
109- def add_handlers ( handlers , config )
97+ def add_handlers ( handlers , _config )
11098 # run before Seahorse::Client::Plugin::ParamValidator (priority 50)
11199 handlers . add Handler , step : :validate , priority : 49
112100 end
0 commit comments