module Lenjador::Utils

Constants

DECIMAL_FRACTION_OF_SECOND
DUMP_OPTIONS
NO_TRACE_INFORMATION

Public Class Methods

application_name(service_name) click to toggle source

Return application name

Returns lower snake case application name. This allows the application value to be used in the elasticsearch index name.

@param [String] service_name

@return [String]

# File lib/lenjador/utils.rb, line 41
def self.application_name(service_name)
  underscore(service_name)
end
build_event(metadata, level, application_name) click to toggle source

Build logstash json compatible event

@param [Hash] metadata @param [#to_s] level @param [String] service_name

@return [Hash]

# File lib/lenjador/utils.rb, line 23
def self.build_event(metadata, level, application_name)
  overwritable_params
    .merge!(metadata)
    .merge!(tracing_information)
    .merge!(
      application: application_name,
      level: level
    )
end
generate_json(obj) click to toggle source
# File lib/lenjador/utils.rb, line 52
def self.generate_json(obj)
  Oj.dump(obj, DUMP_OPTIONS)
end
underscore(input) click to toggle source
# File lib/lenjador/utils.rb, line 56
def self.underscore(input)
  word = input.to_s.dup
  word.gsub!(/::/, '/')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!('-', '_')
  word.downcase!
  word
end

Private Class Methods

overwritable_params() click to toggle source
# File lib/lenjador/utils.rb, line 45
def self.overwritable_params
  {
    :@timestamp => Time.now
  }
end
tracing_information() click to toggle source
# File lib/lenjador/utils.rb, line 66
def self.tracing_information
  tracing_information_from_opentelemetry ||
    tracing_information_from_opentracing ||
    NO_TRACE_INFORMATION
end
tracing_information_from_opentelemetry() click to toggle source
# File lib/lenjador/utils.rb, line 73
def self.tracing_information_from_opentelemetry
  return nil unless defined?(OpenTelemetry)

  current_span_context = OpenTelemetry::Trace.current_span.context
  return nil unless current_span_context.valid?

  {
    trace_id: current_span_context.hex_trace_id,
    span_id: current_span_context.hex_span_id
  }
end
tracing_information_from_opentracing() click to toggle source

Tracing information is included only if OpenTracing supports method called `active_span` (version >= 0.4.1). We use SpanContext#trace_id and SpanContext#span_id methods to retrieve tracing information. These methods are not yet supported by the OpenTracing API, so we first check if these methods exist. Popular tracing libraries already implement them. These methods are likely to be added to the API very soon: github.com/opentracing/specification/blob/master/rfc/trace_identifiers.md

@deprecated Use OpenTelemetry instead

# File lib/lenjador/utils.rb, line 95
def self.tracing_information_from_opentracing
  return nil if !defined?(OpenTracing) || !OpenTracing.respond_to?(:active_span)

  context = OpenTracing.active_span&.context
  if context && context.respond_to?(:trace_id) && context.respond_to?(:span_id)
    {
      trace_id: context.trace_id,
      span_id: context.span_id
    }
  end
end