Class: SemanticLogger::Appender::SentryRuby

Inherits:
Subscriber
  • Object
show all
Defined in:
lib/semantic_logger/appender/sentry_ruby.rb

Constant Summary collapse

USER_KEYS =

Keys of the named tags or payload that set the Sentry user, mapped to its fields.

{user_id: :id, username: :username, user_email: :email, ip_address: :ip_address}.freeze
USER_VALUE_TYPES =

The types of value taken from the payload for a field of the Sentry user.

[String, Symbol, Integer].freeze
FINGERPRINT_PART_TYPES =

The types of value accepted in a fingerprint, which Sentry turns into Strings.

[String, Symbol, Numeric].freeze
SCOPE_CAPTURE_KEY =

The key of the Sentry scope of the thread that logged the entry, as captured with the entry.

:sentry_scope

Constants inherited from Subscriber

Subscriber::MESSAGE_ERRORS

Instance Attribute Summary

Attributes inherited from Subscriber

#application, #environment, #formatter, #host, #logger, #metrics

Instance Method Summary collapse

Methods inherited from Subscriber

#batch_all_or_nothing?, #batch_by_default?, #batch_refusal, #console_output?, #console_stream, #fails_any_batch?, #flush, #inspect, #inspect_details, #message_error?, #not_sent?, #rejected_entry, #should_log?, #wrote_nothing?

Constructor Details

#initialize(level: :error, **args, &block) ⇒ SentryRuby

Create Appender

Parameters level: [:trace | :debug | :info | :warn | :error | :fatal] Override the log level for this appender. Default: :error

formatter: [Object|Proc|Symbol|Hash]
An instance of a class that implements #call, or a Proc to be used to format
the output from this appender
Default: Use the built-in formatter (See: #call)

filter: [Regexp|Proc]
RegExp: Only include log messages where the class name matches the supplied.
regular expression. All other messages will be ignored.
Proc: Only include log messages where the supplied Proc returns true
      The Proc must return true or false.

host: [String]
Name of this host to appear in log messages.
Default: SemanticLogger.host

application: [String]
Name of this application to appear in log messages.
Default: SemanticLogger.application


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/semantic_logger/appender/sentry_ruby.rb', line 52

def initialize(level: :error, **args, &block)
  # Replace the Sentry Ruby logger so that we can identify its log
  # messages and not forward them to Sentry
  unless ::Sentry.initialized?
    ::Sentry.init do |config|
      logger = SemanticLogger[::Sentry]
      config.respond_to?(:sdk_logger=) ? config.sdk_logger = logger : config.logger = logger
    end
  end
  super
  # Sentry keeps the user, tags, contexts, breadcrumbs, and trace of a request in the Sentry
  # scope of the thread or fiber handling it, but #log runs on the appender thread.
  SemanticLogger::Logger.subscribe(method(:capture_scope))
end

Instance Method Details

#close ⇒ Object

Stops capturing the Sentry scope of each log entry.



68
69
70
# File 'lib/semantic_logger/appender/sentry_ruby.rb', line 68

def close
  SemanticLogger::Logger.unsubscribe(method(:capture_scope))
end

#log(log) ⇒ Object

Send an error notification to sentry



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/semantic_logger/appender/sentry_ruby.rb', line 73

def log(log)
  # Ignore logs coming from Sentry itself
  return false if log.name == "Sentry"

  # Sentry serializes the event with JSON.generate itself, which raises for a binary String
  # that holds bytes outside ASCII (json 3.1), or warns about it (json 2.8 to 3.0.x), and for
  # invalid UTF-8, and then loses the event. So the data is sent as a copy with its text
  # converted to valid UTF-8, as the JSON formatters write it, and a Hash or Array nested too
  # deeply, or that holds itself, cut off there as they cut it off (see Utils.encode_utf8).
  # Any other object is sent as it is, for Sentry to write as it writes it.
  context = format_log(log) { |data| Utils.encode_utf8(data) }
  # The payload and named tags are this appender's own copies (above), so the keys taken out
  # below are left out only of what is sent to Sentry.
  payload               = payload_extras(context.delete(:payload), log.payload)
  named_tags            = (context[:named_tags] = like_original(context[:named_tags] || {}, log.named_tags))
  transaction_name      = named_tags.delete(:transaction_name)
  fingerprint           = extract_fingerprint!(named_tags) || extract_fingerprint!(payload, from_payload: true)

  user = extract_user!(named_tags, payload)
  tags = extract_tags!(context)

  ::Sentry.with_scope do |scope|
    apply_captured_scope(scope, log)
    scope.set_user(merged_user(scope.user, user)) if user
    scope.set_level(context.delete(:level)) if context[:level]
    scope.set_fingerprint(fingerprint) if fingerprint
    scope.set_transaction_name(transaction_name) if transaction_name
    scope.set_tags(tags)
    scope.set_extras(context)
    scope.set_extras(payload)

    if log.exception
      ::Sentry.capture_exception(log.exception)
    elsif log.backtrace
      ::Sentry.capture_message(context[:message], backtrace: log.backtrace)
    else
      ::Sentry.capture_message(context[:message])
    end
  end

  true
end