Class: Milemarker::Structured

Inherits:
Milemarker show all
Defined in:
lib/milemarker/structured.rb

Overview

Milemarker for structured logging

* #create_logger! creates a logger that spits out JSON lines instead of human-centered strings
* #batch_line and #final_line return hashes of count/time/rate data
  *...and are aliased to #batch_data and #final_data

Milemarker::Structured should be a drop-in replacement for Milemarker, with the above differences and of course the caveat that if you provide your own logger it should expect to deal with the hashes coming from #batch_data and #final_data

Constant Summary

Constants inherited from Milemarker

VERSION

Instance Attribute Summary

Attributes inherited from Milemarker

#batch_end_time, #batch_number, #batch_size, #batch_start_time, #count, #last_batch_seconds, #last_batch_size, #logger, #name, #prev_count, #start_time

Instance Method Summary collapse

Methods inherited from Milemarker

#_increment_and_on_batch, #batch_rate, #batch_rate_str, #batch_seconds_so_far, #final_batch_size, #incr, #increment_and_log_batch_line, #initialize, #log, #log_batch_line, #log_final_line, #on_batch, #reset_for_next_batch!, #set_milemarker!, #threadsafe_increment_and_on_batch, #threadsafify!, #total_rate, #total_rate_str, #total_seconds_so_far

Constructor Details

This class inherits a constructor from Milemarker

Instance Method Details

#batch_lineHash Also known as: batch_data

Returns hash with information about the last batch.

Returns:

  • (Hash)

    hash with information about the last batch



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/milemarker/structured.rb', line 41

def batch_line
  {
    name: name,
    batch_count: last_batch_size,
    batch_seconds: last_batch_seconds,
    batch_rate: batch_rate,
    total_count: count,
    total_seconds: total_seconds_so_far,
    total_rate: total_rate
  }
end

#create_logger!(*args, **kwargs) ⇒ Object

Create a logger that spits out JSON strings instead of human-oriented strings’ In addition to whatever message is passed, will always also include { level: severity, time: datetime }

The logger will try to deal intelligently with different types of arguments

* a Hash will just be passed
* a String;s return json will show up in the hash under the key 'msg'
* an Exception's return json will have the error's message, class, the first bit of the backtrace, and hostname
* Anything else will be treated like a hash if it responds to #to_h;
  otherwise use msg.inspect as a message string


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/milemarker/structured.rb', line 23

def create_logger!(*args, **kwargs)
  super
  @logger.formatter = proc do |severity, datetime, _progname, msg|
    case msg
    when Hash
      msg
    when String
      { msg: msg }
    when Exception
      exception_message_hash(msg)
    else
      other_message_hash(msg)
    end.merge({ level: severity, time: datetime }).to_json
  end
  self
end

#exception_message_hash(msg) ⇒ Object



68
69
70
# File 'lib/milemarker/structured.rb', line 68

def exception_message_hash(msg)
  { msg: msg.message, error: msg.class, at: msg.backtrace&.first, hostname: Socket.gethostname }
end

#final_lineHash Also known as: final_data

Returns hash with information about the last batch.

Returns:

  • (Hash)

    hash with information about the last batch



56
57
58
59
60
61
62
63
64
# File 'lib/milemarker/structured.rb', line 56

def final_line
  {
    name: name,
    final_batch_size: final_batch_size,
    total_count: count,
    total_seconds: total_seconds_so_far,
    total_rate: total_rate
  }
end

#other_message_hash(msg) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/milemarker/structured.rb', line 72

def other_message_hash(msg)
  if msg.respond_to? :to_h
    msg.to_h
  else
    { msg: msg.inspect }
  end
end