Class: Sbmt::KafkaConsumer::Instrumentation::SentryTracer

Inherits:
Tracer
  • Object
show all
Defined in:
lib/sbmt/kafka_consumer/instrumentation/sentry_tracer.rb

Constant Summary collapse

CONSUMER_ERROR_TYPES =
%w[
  consumer.base.consume_one
  consumer.base.consumed_batch
  consumer.inbox.consume_one
].freeze
EVENTS =
%w[
  consumer.consumed_one
  consumer.process_message
  consumer.mark_as_consumed
].freeze

Instance Method Summary collapse

Methods inherited from Tracer

#initialize

Constructor Details

This class inherits a constructor from Sbmt::KafkaConsumer::Instrumentation::Tracer

Instance Method Details

#handle_consumed_batchObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sbmt/kafka_consumer/instrumentation/sentry_tracer.rb', line 45

def handle_consumed_batch
  message_first = @payload[:messages].first
  message = {
    trace_id: @payload[:trace_id],
    topic: message_first.topic,
    first_offset: message_first.offset,
    last_offset: @payload[:messages].last.offset
  }

  with_sentry_transaction(
    @payload[:caller],
    message
  ) do
    yield
  end
end

#handle_consumed_oneObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sbmt/kafka_consumer/instrumentation/sentry_tracer.rb', line 30

def handle_consumed_one
  message = {
    trace_id: @payload[:trace_id],
    topic: @payload[:message].topic,
    offset: @payload[:message].offset
  }

  with_sentry_transaction(
    @payload[:caller],
    message
  ) do
    yield
  end
end

#handle_errorObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sbmt/kafka_consumer/instrumentation/sentry_tracer.rb', line 62

def handle_error
  return yield unless ::Sentry.initialized?

  exception = @payload[:error]
  return yield unless exception.respond_to?(:message)

  ::Sentry.with_scope do |scope|
    if detailed_logging_enabled?
      message = @payload[:message]
      if message.present?
        contexts = {
          payload: message_payload(message),
          metadata: message.
        }
        scope.set_contexts(contexts: contexts)
      end
    end
    ::Sentry.capture_exception(exception)
  end

  yield
end

#trace(&block) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/sbmt/kafka_consumer/instrumentation/sentry_tracer.rb', line 22

def trace(&block)
  return handle_consumed_one(&block) if EVENTS.include?(@event_id)
  return handle_consumed_batch(&block) if @event_id == "consumer.consumed_batch"
  return handle_error(&block) if @event_id == "error.occurred"

  yield
end