Class: OpenTelemetry::Instrumentation::AwsSdk::MessagingHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/instrumentation/aws_sdk/messaging_helper.rb

Overview

An utility class to help SQS/SNS-related span attributes/context injection

Constant Summary collapse

SUPPORTED_SERVICES =
%w[SQS SNS].freeze
SQS_SEND_MESSAGE =
'SQS.SendMessage'
SQS_SEND_MESSAGE_BATCH =
'SQS.SendMessageBatch'
SQS_RECEIVE_MESSAGE =
'SQS.ReceiveMessage'
SNS_PUBLISH =
'SNS.Publish'
SEND_MESSAGE_CLIENT_METHODS =
[SQS_SEND_MESSAGE, SQS_SEND_MESSAGE_BATCH, SNS_PUBLISH].freeze

Class Method Summary collapse

Class Method Details

.apply_span_attributes(context, attrs, client_method, service_id) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/opentelemetry/instrumentation/aws_sdk/messaging_helper.rb', line 64

def apply_span_attributes(context, attrs, client_method, service_id)
  case service_id
  when 'SQS'
    apply_sqs_attributes(attrs, context, client_method)
  when 'SNS'
    apply_sns_attributes(attrs, context, client_method)
  end
end

.inject_context(context, client_method) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/opentelemetry/instrumentation/aws_sdk/messaging_helper.rb', line 91

def inject_context(context, client_method)
  return unless SEND_MESSAGE_CLIENT_METHODS.include?(client_method)

  if client_method == SQS_SEND_MESSAGE_BATCH
    context.params[:entries].each do |entry|
      entry[:message_attributes] ||= {}
      OpenTelemetry.propagation.inject(entry[:message_attributes], setter: MessageAttributeSetter)
    end
  else
    context.params[:message_attributes] ||= {}
    OpenTelemetry.propagation.inject(context.params[:message_attributes], setter: MessageAttributeSetter)
  end
end

.inject_context_if_supported(context, client_method, service_id) ⇒ Object



84
85
86
87
88
89
# File 'lib/opentelemetry/instrumentation/aws_sdk/messaging_helper.rb', line 84

def inject_context_if_supported(context, client_method, service_id)
  if HandlerHelper.instrumentation_config[:inject_messaging_context] &&
     SUPPORTED_SERVICES.include?(service_id)
    inject_context(context, client_method)
  end
end

.legacy_span_name(context, client_method) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/opentelemetry/instrumentation/aws_sdk/messaging_helper.rb', line 53

def legacy_span_name(context, client_method)
  case client_method
  when SQS_SEND_MESSAGE, SQS_SEND_MESSAGE_BATCH, SNS_PUBLISH
    "#{MessagingHelper.queue_name(context)} publish"
  when SQS_RECEIVE_MESSAGE
    "#{MessagingHelper.queue_name(context)} receive"
  else
    client_method
  end
end

.queue_name(context) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/opentelemetry/instrumentation/aws_sdk/messaging_helper.rb', line 24

def queue_name(context)
  topic_arn = context.params[:topic_arn]
  target_arn = context.params[:target_arn]

  if topic_arn || target_arn
    arn = topic_arn || target_arn
    return arn.split(':')[-1]
  end

  phone_number = context.params[:phone_number]
  return 'phone_number' if phone_number

  queue_url = context.params[:queue_url]
  return queue_url.split('/')[-1] if queue_url

  'unknown'
end

.span_kind(client_method) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/opentelemetry/instrumentation/aws_sdk/messaging_helper.rb', line 73

def span_kind(client_method)
  case client_method
  when SQS_SEND_MESSAGE, SQS_SEND_MESSAGE_BATCH, SNS_PUBLISH
    OpenTelemetry::Trace::SpanKind::PRODUCER
  when SQS_RECEIVE_MESSAGE
    OpenTelemetry::Trace::SpanKind::CONSUMER
  else
    OpenTelemetry::Trace::SpanKind::CLIENT
  end
end

.span_name(context, client_method) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/opentelemetry/instrumentation/aws_sdk/messaging_helper.rb', line 42

def span_name(context, client_method)
  case client_method
  when SQS_SEND_MESSAGE, SQS_SEND_MESSAGE_BATCH, SNS_PUBLISH
    "#{client_method}.#{queue_name(context)}.Publish"
  when SQS_RECEIVE_MESSAGE
    "#{client_method}.#{queue_name(context)}.Receive"
  else
    client_method
  end
end

.supported_servicesObject



20
21
22
# File 'lib/opentelemetry/instrumentation/aws_sdk/messaging_helper.rb', line 20

def supported_services
  SUPPORTED_SERVICES
end