Class: Datadog::Core::Telemetry::Event::Log

Inherits:
Base
  • Object
show all
Defined in:
lib/datadog/core/telemetry/event.rb

Overview

Telemetry class for the ‘logs’ event. Logs with the same content are deduplicated at flush time.

Constant Summary collapse

LEVELS =
{
  error: 'ERROR',
  warn: 'WARN',
}.freeze
LEVELS_STRING =
LEVELS.values.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message:, level:, stack_trace: nil, count: 1) ⇒ Log

Returns a new instance of Log.

Parameters:

  • message (String)

    the log message

  • level (Symbol, String)

    the log level. Either :error, :warn, ‘ERROR’, or ‘WARN’.

  • stack_trace (String, nil) (defaults to: nil)

    the stack trace

  • count (Integer) (defaults to: 1)

    the number of times the log was emitted. Used for deduplication.



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/datadog/core/telemetry/event.rb', line 400

def initialize(message:, level:, stack_trace: nil, count: 1)
  super()
  @message = message
  @stack_trace = stack_trace

  if level.is_a?(String) && LEVELS_STRING.include?(level)
    # String level is used during object copy for deduplication
    @level = level
  elsif level.is_a?(Symbol)
    # Symbol level is used by the regular log emitter user
    @level = LEVELS.fetch(level) { |k| raise ArgumentError, "Invalid log level :#{k}" }
  else
    raise ArgumentError, "Invalid log level #{level}"
  end

  @count = count
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



390
391
392
# File 'lib/datadog/core/telemetry/event.rb', line 390

def count
  @count
end

#levelObject (readonly)

Returns the value of attribute level.



390
391
392
# File 'lib/datadog/core/telemetry/event.rb', line 390

def level
  @level
end

#messageObject (readonly)

Returns the value of attribute message.



390
391
392
# File 'lib/datadog/core/telemetry/event.rb', line 390

def message
  @message
end

#stack_traceObject (readonly)

Returns the value of attribute stack_trace.



390
391
392
# File 'lib/datadog/core/telemetry/event.rb', line 390

def stack_trace
  @stack_trace
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

override equality to allow for deduplication



432
433
434
435
436
# File 'lib/datadog/core/telemetry/event.rb', line 432

def ==(other)
  other.is_a?(Log) &&
    other.message == @message &&
    other.level == @level && other.stack_trace == @stack_trace && other.count == @count
end

#hashObject



440
441
442
# File 'lib/datadog/core/telemetry/event.rb', line 440

def hash
  [self.class, @message, @level, @stack_trace, @count].hash
end

#payloadObject



418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/datadog/core/telemetry/event.rb', line 418

def payload
  {
    logs: [
      {
        message: @message,
        level: @level,
        stack_trace: @stack_trace,
        count: @count,
      }.compact
    ]
  }
end

#typeObject



392
393
394
# File 'lib/datadog/core/telemetry/event.rb', line 392

def type
  'logs'
end