Class: SemanticLogger::Formatters::Loki::Labels

Inherits:
Object
  • Object
show all
Defined in:
lib/semantic_logger/formatters/loki/labels.rb

Overview

The labels of the stream of one log entry, as Loki accepts them: Loki rejects the whole push, with every entry in it, for a label name it does not accept, or past one of its limits, and keeps only one value of a label name.

It knows the labels written so far, a Hash of their names, as Strings, and their values, to which the formatter has written its own labels first (see Loki#build_stream), whether a name may still be a Symbol, the names that the formatter writes to the structured metadata instead of as labels, the labels still to be written under another name, Loki's limits, and how many labels there were when it finished them. It writes each name as a String (see #string_names), adds the named tags and the context to the labels (see #add), and then writes those that need another name and takes out those past Loki's limits (see #finish), which the formatter writes to the structured metadata instead. It names them by the formatter's rules (see Loki.loki_name and Loki.free_name), and converts their values with the converter the formatter gives it.

Constant Summary collapse

LABEL_NAME =

Matches a label name that Loki accepts: an ASCII letter or an underscore, followed by ASCII letters, digits, and underscores. Loki rejects the whole push for any other, such as request-id, 1st, or an empty name, with "couldn't parse labels".

/\A[A-Za-z_][A-Za-z0-9_]*\z/
EMPTY_LABEL_NAME =

The name of a label whose key is empty, with the prefix that Loki puts before a name that starts with a digit.

"key_".freeze
SERVICE_NAME =

The label that Loki adds to a stream that does not have it, and does not count toward max_label_names_per_series, even when the stream has it.

"service_name".freeze

Instance Method Summary collapse

Constructor Details

#initialize(labels, loki_value, max_names, max_name_length, max_value_length, reserved:, symbols: false) ⇒ Labels

Parameters labels: [Hash] The labels written so far, which this adds to, and takes the labels past Loki's limits from. loki_value: [#call] Returns a value as Loki stores it, a String: the formatter's #loki_value, which a subclass of the formatter may override. max_names, max_name_length, max_value_length: [Integer] Loki's limits, see Loki#initialize. reserved: [Array] The names that the formatter writes to the structured metadata instead of as labels, such as thread (see Loki#initialize). A label of that name is written under another name, since Loki would show the structured metadata as thread_extracted beside it. symbols: [true|false] Whether a label may have a name that is not a String, such as a Symbol that a subclass of the formatter written for v5.2 writes its own label under, as v5.2 did. The names are then written as Strings (see #string_names) before the first label is added, and as the labels are finished. Default: false, as the formatter's own methods write every label under a String.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/semantic_logger/formatters/loki/labels.rb', line 51

def initialize(labels, loki_value, max_names, max_name_length, max_value_length, reserved:,
               symbols: false)
  @labels           = labels
  @loki_value       = loki_value
  @max_names        = max_names
  @max_name_length  = max_name_length
  @max_value_length = max_value_length
  @reserved         = reserved
  @symbols          = symbols
  @unchecked        = symbols
  @renamed          = nil
  @finished_size    = nil
end

Instance Method Details

#add(key, value) ⇒ Object

Adds the label of a named tag or a value of the context, with its value as Loki stores it (see #initialize), unless the value is nil, which Loki would store as the text "null".

Loki rejects the whole push for a label name it does not accept (see LABEL_NAME), so such a name is replaced (see #label_name_for). Loki keeps one value of a label name, so a label whose name another label already has is written once when that label has the same value, as Loki stores it, and otherwise under another name, as is a label of a reserved name. These are written after the others, by #finish, so that the name they get is not one that a later label has as its own.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/semantic_logger/formatters/loki/labels.rb', line 87

def add(key, value)
  return if value.nil?

  string_names if @unchecked
  name  = key.is_a?(Symbol) ? key.name : key.to_s
  value = @loki_value.call(value)
  if !label_name?(name)
    (@renamed ||= []) << [label_name_for(name), value]
  elsif @labels.key?(name) || @reserved.include?(name)
    (@renamed ||= []) << [name, value]
  else
    @labels[name] = value
  end
end

#finish ⇒ Object

Writes the labels whose names are taken, reserved, or were replaced (see #add), after the others, so that a label written under its own name keeps it (see #write_renamed), and then takes out the labels past Loki's limits (see #limit). A label written under a Symbol since, as by a subclass of the formatter, is first written under a String (see #string_names).

It can finish them again, after more labels were added (see #finished?).

Returns [Array<Array(String, String)>|nil] the labels taken out, each its name and its value as Loki stores it, or nil when there are none.



112
113
114
115
116
117
118
# File 'lib/semantic_logger/formatters/loki/labels.rb', line 112

def finish
  string_names if @symbols
  write_renamed if @renamed
  moved          = limit
  @finished_size = @labels.size
  moved
end

#finished? ⇒ Boolean

Whether #finish has written and limited these labels, and no label was added since, as a subclass of the formatter may add one after its #build_stream called super, even under the name of another label, such as :level beside "level". A label whose value was replaced since is not seen.

Returns:

  • (Boolean)


74
75
76
# File 'lib/semantic_logger/formatters/loki/labels.rb', line 74

def finished?
  @finished_size == @labels.size
end

#of?(labels) ⇒ Boolean

Whether these are the labels of the supplied Hash: the same Hash, not an equal one.

Returns:

  • (Boolean)


66
67
68
# File 'lib/semantic_logger/formatters/loki/labels.rb', line 66

def of?(labels)
  @labels.equal?(labels)
end