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

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



48
49
50
51
52
53
54
55
# File 'lib/opentelemetry/instrumentation/aws_sdk/messaging_helper.rb', line 48

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



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/opentelemetry/instrumentation/aws_sdk/messaging_helper.rb', line 68

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

.legacy_span_name(context, client_method) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/opentelemetry/instrumentation/aws_sdk/messaging_helper.rb', line 37

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/opentelemetry/instrumentation/aws_sdk/messaging_helper.rb', line 19

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



57
58
59
60
61
62
63
64
65
66
# File 'lib/opentelemetry/instrumentation/aws_sdk/messaging_helper.rb', line 57

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