@@ -29,6 +29,22 @@ def type
2929 def payload
3030 { }
3131 end
32+
33+ # Override equality to allow for deduplication
34+ # The basic implementation is to check if the other object is an instance of the same class.
35+ # This works for events that have no attributes.
36+ # For events with attributes, you should override this method to compare the attributes.
37+ def ==( other )
38+ other . is_a? ( self . class )
39+ end
40+
41+ # @see #==
42+ alias eql? ==
43+
44+ # @see #==
45+ def hash
46+ self . class . hash
47+ end
3248 end
3349
3450 # Telemetry class for the 'app-started' event
@@ -263,6 +279,8 @@ def patch_error(integration)
263279
264280 # Telemetry class for the 'app-client-configuration-change' event
265281 class AppClientConfigurationChange < Base
282+ attr_reader :changes , :origin
283+
266284 def type
267285 'app-client-configuration-change'
268286 end
@@ -301,6 +319,16 @@ def configuration
301319
302320 res
303321 end
322+
323+ def ==( other )
324+ other . is_a? ( AppClientConfigurationChange ) && other . changes == @changes && other . origin == @origin
325+ end
326+
327+ alias eql? ==
328+
329+ def hash
330+ [ self . class , @changes , @origin ] . hash
331+ end
304332 end
305333
306334 # Telemetry class for the 'app-heartbeat' event
@@ -319,6 +347,8 @@ def type
319347
320348 # Telemetry class for the 'generate-metrics' event
321349 class GenerateMetrics < Base
350+ attr_reader :namespace , :metric_series
351+
322352 def type
323353 'generate-metrics'
324354 end
@@ -335,24 +365,54 @@ def payload
335365 series : @metric_series . map ( &:to_h )
336366 }
337367 end
368+
369+ def ==( other )
370+ other . is_a? ( GenerateMetrics ) && other . namespace == @namespace && other . metric_series == @metric_series
371+ end
372+
373+ alias eql? ==
374+
375+ def hash
376+ [ self . class , @namespace , @metric_series ] . hash
377+ end
338378 end
339379
340- # Telemetry class for the 'logs' event
380+ # Telemetry class for the 'logs' event.
381+ # Logs with the same content are deduplicated at flush time.
341382 class Log < Base
342383 LEVELS = {
343384 error : 'ERROR' ,
344385 warn : 'WARN' ,
345386 } . freeze
346387
388+ LEVELS_STRING = LEVELS . values . freeze
389+
390+ attr_reader :message , :level , :stack_trace , :count
391+
347392 def type
348393 'logs'
349394 end
350395
351- def initialize ( message :, level :, stack_trace : nil )
396+ # @param message [String] the log message
397+ # @param level [Symbol, String] the log level. Either :error, :warn, 'ERROR', or 'WARN'.
398+ # @param stack_trace [String, nil] the stack trace
399+ # @param count [Integer] the number of times the log was emitted. Used for deduplication.
400+ def initialize ( message :, level :, stack_trace : nil , count : 1 )
352401 super ( )
353402 @message = message
354403 @stack_trace = stack_trace
355- @level = LEVELS . fetch ( level ) { |k | raise ArgumentError , "Invalid log level :#{ k } " }
404+
405+ if level . is_a? ( String ) && LEVELS_STRING . include? ( level )
406+ # String level is used during object copy for deduplication
407+ @level = level
408+ elsif level . is_a? ( Symbol )
409+ # Symbol level is used by the regular log emitter user
410+ @level = LEVELS . fetch ( level ) { |k | raise ArgumentError , "Invalid log level :#{ k } " }
411+ else
412+ raise ArgumentError , "Invalid log level #{ level } "
413+ end
414+
415+ @count = count
356416 end
357417
358418 def payload
@@ -362,10 +422,24 @@ def payload
362422 message : @message ,
363423 level : @level ,
364424 stack_trace : @stack_trace ,
425+ count : @count ,
365426 } . compact
366427 ]
367428 }
368429 end
430+
431+ # override equality to allow for deduplication
432+ def ==( other )
433+ other . is_a? ( Log ) &&
434+ other . message == @message &&
435+ other . level == @level && other . stack_trace == @stack_trace && other . count == @count
436+ end
437+
438+ alias eql? ==
439+
440+ def hash
441+ [ self . class , @message , @level , @stack_trace , @count ] . hash
442+ end
369443 end
370444
371445 # Telemetry class for the 'distributions' event
@@ -395,6 +469,16 @@ def payload
395469 }
396470 end
397471 end
472+
473+ def ==( other )
474+ other . is_a? ( MessageBatch ) && other . events == @events
475+ end
476+
477+ alias eql? ==
478+
479+ def hash
480+ [ self . class , @events ] . hash
481+ end
398482 end
399483 end
400484 end
0 commit comments