Class: Lapsoss::Event

Inherits:
Data
  • Object
show all
Defined in:
lib/lapsoss/event.rb

Overview

Immutable event structure using Ruby 3.3 Data class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#backtrace_framesObject (readonly)

Returns the value of attribute backtrace_frames

Returns:

  • (Object)

    the current value of backtrace_frames



10
11
12
# File 'lib/lapsoss/event.rb', line 10

def backtrace_frames
  @backtrace_frames
end

#contextObject (readonly)

Returns the value of attribute context

Returns:

  • (Object)

    the current value of context



10
11
12
# File 'lib/lapsoss/event.rb', line 10

def context
  @context
end

#environmentObject (readonly)

Returns the value of attribute environment

Returns:

  • (Object)

    the current value of environment



10
11
12
# File 'lib/lapsoss/event.rb', line 10

def environment
  @environment
end

#exceptionObject (readonly)

Returns the value of attribute exception

Returns:

  • (Object)

    the current value of exception



10
11
12
# File 'lib/lapsoss/event.rb', line 10

def exception
  @exception
end

#fingerprintObject (readonly)

Returns the value of attribute fingerprint

Returns:

  • (Object)

    the current value of fingerprint



10
11
12
# File 'lib/lapsoss/event.rb', line 10

def fingerprint
  @fingerprint
end

#levelObject (readonly)

Returns the value of attribute level

Returns:

  • (Object)

    the current value of level



10
11
12
# File 'lib/lapsoss/event.rb', line 10

def level
  @level
end

#messageObject (readonly)

Returns the value of attribute message

Returns:

  • (Object)

    the current value of message



10
11
12
# File 'lib/lapsoss/event.rb', line 10

def message
  @message
end

#timestampObject (readonly)

Returns the value of attribute timestamp

Returns:

  • (Object)

    the current value of timestamp



10
11
12
# File 'lib/lapsoss/event.rb', line 10

def timestamp
  @timestamp
end

#transactionObject (readonly)

Returns the value of attribute transaction

Returns:

  • (Object)

    the current value of transaction



10
11
12
# File 'lib/lapsoss/event.rb', line 10

def transaction
  @transaction
end

#typeObject (readonly)

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



10
11
12
# File 'lib/lapsoss/event.rb', line 10

def type
  @type
end

Class Method Details

.build(type:, level: :info, **attributes) ⇒ Object

Factory method with smart defaults



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lapsoss/event.rb', line 23

def self.build(type:, level: :info, **attributes)
  timestamp = attributes[:timestamp] || Time.now
  environment = attributes[:environment].presence || Lapsoss.configuration.environment
  context = attributes[:context] || {}

  # Process exception if present
  exception = attributes[:exception]
  message = attributes[:message].presence || exception&.message
  backtrace_frames = process_backtrace(exception) if exception

  # Generate fingerprint
  fingerprint = attributes.fetch(:fingerprint) {
    generate_fingerprint(type, message, exception, environment)
  }

  new(
    type: type,
    level: level,
    timestamp: timestamp,
    message: message,
    exception: exception,
    context: context,
    environment: environment,
    fingerprint: fingerprint,
    backtrace_frames: backtrace_frames,
    transaction: attributes[:transaction]
  )
end

.generate_fingerprint(type, message, exception, environment) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/lapsoss/event.rb', line 104

def self.generate_fingerprint(type, message, exception, environment)
  return nil unless Lapsoss.configuration.fingerprint_callback ||
                   Lapsoss.configuration.fingerprint_patterns.present?

  fingerprinter = Fingerprinter.new(
    custom_callback: Lapsoss.configuration.fingerprint_callback,
    patterns: Lapsoss.configuration.fingerprint_patterns,
    normalize_paths: Lapsoss.configuration.normalize_fingerprint_paths,
    normalize_ids: Lapsoss.configuration.normalize_fingerprint_ids,
    include_environment: Lapsoss.configuration.fingerprint_include_environment
  )

  # Create a temporary event-like object for fingerprinting
  temp_event = Struct.new(:type, :message, :exception, :environment).new(
    type,
    message,
    exception,
    environment
  )

  fingerprinter.generate_fingerprint(temp_event)
end

.process_backtrace(exception) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/lapsoss/event.rb', line 96

def self.process_backtrace(exception)
  return nil unless exception&.backtrace.present?

  config = Lapsoss.configuration
  processor = BacktraceProcessor.new(config)
  processor.process_exception_backtrace(exception)
end

Instance Method Details

#as_json(options = nil) ⇒ Object

ActiveSupport::JSON serialization



53
54
55
# File 'lib/lapsoss/event.rb', line 53

def as_json(options = nil)
  to_h.compact_blank.as_json(options)
end

#backtraceObject



70
# File 'lib/lapsoss/event.rb', line 70

def backtrace = exception&.backtrace


80
# File 'lib/lapsoss/event.rb', line 80

def breadcrumbs = context[:breadcrumbs] || []

#exception_messageObject



64
# File 'lib/lapsoss/event.rb', line 64

def exception_message = exception&.message

#exception_typeObject

Helper methods



62
# File 'lib/lapsoss/event.rb', line 62

def exception_type = exception&.class&.name

#extraObject



78
# File 'lib/lapsoss/event.rb', line 78

def extra = context[:extra] || {}

#has_backtrace?Boolean

Returns:

  • (Boolean)


68
# File 'lib/lapsoss/event.rb', line 68

def has_backtrace? = backtrace_frames.present?

#has_exception?Boolean

Returns:

  • (Boolean)


66
# File 'lib/lapsoss/event.rb', line 66

def has_exception? = exception.present?

#request_contextObject



72
# File 'lib/lapsoss/event.rb', line 72

def request_context = context.dig(:extra, :request) || context.dig(:extra, "request")

#scrubbedObject

Apply data scrubbing



83
84
85
86
87
88
89
90
91
92
# File 'lib/lapsoss/event.rb', line 83

def scrubbed
  scrubber = Scrubber.new(
    scrub_fields: Lapsoss.configuration.scrub_fields,
    scrub_all: Lapsoss.configuration.scrub_all,
    whitelist_fields: Lapsoss.configuration.whitelist_fields,
    randomize_scrub_length: Lapsoss.configuration.randomize_scrub_length
  )

  with(context: scrubber.scrub(context))
end

#tagsObject



76
# File 'lib/lapsoss/event.rb', line 76

def tags = context[:tags] || {}

#to_json(options = nil) ⇒ Object



57
58
59
# File 'lib/lapsoss/event.rb', line 57

def to_json(options = nil)
  ActiveSupport::JSON.encode(as_json(options))
end

#user_contextObject



74
# File 'lib/lapsoss/event.rb', line 74

def user_context = context[:user]