class Loki::Batch

Attributes

streams[R]

Public Class Methods

new(e) click to toggle source
# File lib/logstash/outputs/loki/batch.rb, line 6
def initialize(e)
    @bytes = 0
    @createdAt = Time.now
    @streams = {}
    add(e)
end

Public Instance Methods

add(e) click to toggle source
# File lib/logstash/outputs/loki/batch.rb, line 17
def add(e)
    @bytes = @bytes + e.entry['line'].length

    # Append the entry to an already existing stream (if any)
    labels = e.labels.sort.to_h
    labelkey = labels.to_s
    if @streams.has_key?(labelkey)
        stream = @streams[labelkey]
        stream['entries'].append(e.entry)
        return
    else
        # Add the entry as a new stream
        @streams[labelkey] = {
            "labels" => labels,
            "entries" => [e.entry],
        }
    end
end
age() click to toggle source
# File lib/logstash/outputs/loki/batch.rb, line 40
def age()
    return Time.now - @createdAt
end
build_stream(stream) click to toggle source
# File lib/logstash/outputs/loki/batch.rb, line 52
def build_stream(stream)
    values = []
    stream['entries'].each { |entry|
        values.append([entry['ts'].to_s, entry['line']])
    }
    return {
        'stream'=>stream['labels'],
        'values' => values
    }
end
size_bytes() click to toggle source
# File lib/logstash/outputs/loki/batch.rb, line 13
def size_bytes
    return @bytes
end
size_bytes_after(line) click to toggle source
# File lib/logstash/outputs/loki/batch.rb, line 36
def size_bytes_after(line)
    return @bytes + line.length
end
to_json() click to toggle source
# File lib/logstash/outputs/loki/batch.rb, line 44
def to_json
    streams = []
    @streams.each { |_ , stream|
        streams.append(build_stream(stream))
    }
    return {"streams"=>streams}.to_json
end