11# frozen_string_literal: true
22
3- require "json "
3+ require "sentry/utils/telemetry_attributes "
44
55module Sentry
66 # Event type that represents a log entry with its attributes
77 #
88 # @see https://develop.sentry.dev/sdk/telemetry/logs/#log-envelope-item-payload
99 class LogEvent
10+ include Sentry ::Utils ::TelemetryAttributes
11+
1012 TYPE = "log"
1113
1214 DEFAULT_PARAMETERS = [ ] . freeze
13- DEFAULT_ATTRIBUTES = { } . freeze
14-
15- SERIALIZEABLE_ATTRIBUTES = %i[
16- level
17- body
18- timestamp
19- environment
20- release
21- server_name
22- trace_id
23- attributes
24- contexts
25- ]
26-
27- SENTRY_ATTRIBUTES = {
28- "sentry.trace.parent_span_id" => :parent_span_id ,
29- "sentry.environment" => :environment ,
30- "sentry.release" => :release ,
31- "sentry.address" => :server_name ,
32- "sentry.sdk.name" => :sdk_name ,
33- "sentry.sdk.version" => :sdk_version ,
34- "sentry.message.template" => :template ,
35- "sentry.origin" => :origin
36- }
3715
3816 PARAMETER_PREFIX = "sentry.message.parameter"
3917
40- USER_ATTRIBUTES = {
41- "user.id" => :user_id ,
42- "user.name" => :user_username ,
43- "user.email" => :user_email
44- }
45-
4618 LEVELS = %i[ trace debug info warn error fatal ] . freeze
4719
48- attr_accessor :level , :body , :template , :attributes , :user , :origin
49-
50- attr_reader :configuration , *( SERIALIZEABLE_ATTRIBUTES - %i[ level body attributes ] )
51-
52- SERIALIZERS = %i[
53- attributes
54- body
55- level
56- parent_span_id
57- sdk_name
58- sdk_version
59- template
60- timestamp
61- trace_id
62- user_id
63- user_username
64- user_email
65- ] . map { |name | [ name , :"serialize_#{ name } " ] } . to_h
20+ attr_accessor :level , :body , :template , :attributes , :origin , :trace_id , :span_id
21+ attr_reader :timestamp
6622
6723 TOKEN_REGEXP = /%\{ (\w +)\} /
6824
69- def initialize ( configuration : Sentry . configuration , **options )
70- @configuration = configuration
25+ def initialize ( **options )
7126 @type = TYPE
72- @server_name = configuration . server_name
73- @environment = configuration . environment
74- @release = configuration . release
7527 @timestamp = Sentry . utc_now
7628 @level = options . fetch ( :level )
7729 @body = options [ :body ]
7830 @template = @body if is_template?
79- @attributes = options [ :attributes ] || DEFAULT_ATTRIBUTES
80- @user = options [ :user ] || { }
31+ @attributes = options [ :attributes ] || { }
8132 @origin = options [ :origin ]
82- @contexts = { }
33+ @trace_id = nil
34+ @span_id = nil
8335 end
8436
8537 def to_h
86- SERIALIZEABLE_ATTRIBUTES . each_with_object ( { } ) do |name , memo |
87- memo [ name ] = serialize ( name )
88- end
38+ {
39+ level : level . to_s ,
40+ timestamp : timestamp . to_f ,
41+ trace_id : @trace_id ,
42+ span_id : @span_id ,
43+ body : serialize_body ,
44+ attributes : serialize_attributes
45+ } . compact
8946 end
9047
9148 private
9249
93- def serialize ( name )
94- serializer = SERIALIZERS [ name ]
95-
96- if serializer
97- __send__ ( serializer )
98- else
99- public_send ( name )
100- end
101- end
102-
103- def serialize_level
104- level . to_s
105- end
106-
107- def serialize_sdk_name
108- Sentry . sdk_meta [ "name" ]
109- end
110-
111- def serialize_sdk_version
112- Sentry . sdk_meta [ "version" ]
113- end
114-
115- def serialize_timestamp
116- timestamp . to_f
117- end
118-
119- def serialize_trace_id
120- contexts . dig ( :trace , :trace_id )
121- end
122-
123- def serialize_parent_span_id
124- contexts . dig ( :trace , :parent_span_id )
125- end
126-
12750 def serialize_body
12851 if parameters . empty?
12952 body
@@ -134,61 +57,14 @@ def serialize_body
13457 end
13558 end
13659
137- def serialize_user_id
138- user [ :id ]
139- end
140-
141- def serialize_user_username
142- user [ :username ]
143- end
144-
145- def serialize_user_email
146- user [ :email ]
147- end
148-
149- def serialize_template
150- template if has_parameters?
151- end
152-
15360 def serialize_attributes
154- hash = { }
155-
156- attributes . each do |key , value |
157- hash [ key ] = attribute_hash ( value )
158- end
159-
160- SENTRY_ATTRIBUTES . each do |key , name |
161- if ( value = serialize ( name ) )
162- hash [ key ] = attribute_hash ( value )
163- end
164- end
165-
166- USER_ATTRIBUTES . each do |key , name |
167- if ( value = serialize ( name ) )
168- hash [ key ] = value
169- end
170- end
171-
172- hash
61+ populate_sentry_attributes!
62+ @attributes . transform_values! { |v | attribute_hash ( v ) }
17363 end
17464
175- def attribute_hash ( value )
176- case value
177- when String
178- { value : value , type : "string" }
179- when TrueClass , FalseClass
180- { value : value , type : "boolean" }
181- when Integer
182- { value : value , type : "integer" }
183- when Float
184- { value : value , type : "double" }
185- else
186- begin
187- { value : JSON . generate ( value ) , type : "string" }
188- rescue
189- { value : value , type : "string" }
190- end
191- end
65+ def populate_sentry_attributes!
66+ @attributes [ "sentry.origin" ] ||= @origin if @origin
67+ @attributes [ "sentry.message.template" ] ||= template if has_parameters?
19268 end
19369
19470 def parameters
0 commit comments