Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions sentry-ruby/lib/sentry/log_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Sentry
# Event type that represents a log entry with its attributes
#
# @see https://develop.sentry.dev/sdk/telemetry/logs/#log-envelope-item-payload
class LogEvent < Event
class LogEvent
TYPE = "log"

DEFAULT_PARAMETERS = [].freeze
Expand All @@ -15,8 +15,12 @@ class LogEvent < Event
level
body
timestamp
environment
release
server_name
trace_id
attributes
contexts
]

SENTRY_ATTRIBUTES = {
Expand All @@ -33,15 +37,20 @@ class LogEvent < Event

attr_accessor :level, :body, :template, :attributes

def initialize(configuration: Sentry.configuration, **options)
super(configuration: configuration)
attr_reader :configuration, *SERIALIZEABLE_ATTRIBUTES

def initialize(configuration: Sentry.configuration, **options)
@configuration = configuration
@type = TYPE
@server_name = configuration.server_name
@environment = configuration.environment
@release = configuration.release
@timestamp = Sentry.utc_now.iso8601
@level = options.fetch(:level)
@body = options[:body]
@template = @body if is_template?
@attributes = options[:attributes] || DEFAULT_ATTRIBUTES
@contexts = DEFAULT_CONTEXT
@contexts = {}
end

def to_hash
Expand Down Expand Up @@ -114,17 +123,15 @@ def attribute_hash(value)
{ value: value, type: value_type(value) }
end

VALUE_TYPES = Hash.new("string").merge!({

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotta be careful when using the default value

$ irb
irb(main):001> a = Hash.new("string")
=> {}
irb(main):002> a["foo"]
=> "string"
irb(main):003> a["bar"] << "bar"
=> "stringbar"
irb(main):004> a["foo"]
=> "stringbar"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@solnic 🙏

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dentarg @sl0thentr0py I believe we're good since there's frozen string literals magic comment at the top of this file.

Integer => "integer",
TrueClass => "boolean",
FalseClass => "boolean",
Float => "double"
}).freeze

def value_type(value)
case value
when Integer
"integer"
when TrueClass, FalseClass
"boolean"
when Float
"double"
else
"string"
end
VALUE_TYPES[value.class]
end

def parameters
Expand Down
14 changes: 10 additions & 4 deletions sentry-ruby/lib/sentry/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,32 @@ def clear
# @param hint [Hash] the hint data that'll be passed to event processors.
# @return [Event]
def apply_to_event(event, hint = nil)
unless event.is_a?(CheckInEvent)
unless event.is_a?(CheckInEvent) || event.is_a?(LogEvent)
event.tags = tags.merge(event.tags)
event.user = user.merge(event.user)
event.extra = extra.merge(event.extra)
event.contexts = contexts.merge(event.contexts)
event.transaction = transaction_name if transaction_name
event.transaction_info = { source: transaction_source } if transaction_source
event.fingerprint = fingerprint
event.level = level unless event.is_a?(LogEvent)
event.level = level
event.breadcrumbs = breadcrumbs
event.rack_env = rack_env if rack_env
event.attachments = attachments
end

if span
event.contexts[:trace] ||= span.get_trace_context
event.dynamic_sampling_context ||= span.get_dynamic_sampling_context

if event.respond_to?(:dynamic_sampling_context)
event.dynamic_sampling_context ||= span.get_dynamic_sampling_context
end
else
event.contexts[:trace] ||= propagation_context.get_trace_context
event.dynamic_sampling_context ||= propagation_context.get_dynamic_sampling_context

if event.respond_to?(:dynamic_sampling_context)
event.dynamic_sampling_context ||= propagation_context.get_dynamic_sampling_context
end
end

all_event_processors = self.class.global_event_processors + @event_processors
Expand Down