Module: Logtastic

Defined in:
lib/logtastic.rb,
lib/logtastic/ecs.rb,
lib/logtastic/setup.rb,
lib/logtastic/version.rb

Defined Under Namespace

Classes: ECS, Setup

Constant Summary collapse

TIMER_EXECUTION_INTERVAL =

seconds

3
MAX_SLICE_EVENTS =
100
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.bulk_index(limit = MAX_SLICE_EVENTS) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/logtastic.rb', line 54

def bulk_index(limit = MAX_SLICE_EVENTS)
  events = @events.slice!(0, limit)
  grouped_events = events.group_by { |event| event.fetch(:output) }
  grouped_events.each do |output, group_events|
    bulk_body = group_events.map do |event|
      {
        index: {
          _index: event.fetch(:index),
          data: event.fetch(:body)
        }
      }
    end

    client(output).bulk(body: bulk_body)
  end
end

.client(output = :default) ⇒ Object



37
38
39
# File 'lib/logtastic.rb', line 37

def client(output = :default)
  @elasticsearch.fetch(output)
end

.ecs(output = :default, **args) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/logtastic.rb', line 27

def ecs(output = :default, **args)
  if output == :default && !@elasticsearch.key?(output)
    elasticsearch(output, client: Elasticsearch::Client.new)
  else
    elasticsearch(output)
  end

  ECS.new(**args, output: output)
end

.elasticsearch(output, options: nil, client: nil) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/logtastic.rb', line 19

def elasticsearch(output, options: nil, client: nil)
  if options || client
    @elasticsearch[output] = client || Elasticsearch::Client.new(options)
  else
    @elasticsearch.fetch(output)
  end
end

.watch(execution_interval: TIMER_EXECUTION_INTERVAL) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/logtastic.rb', line 46

def watch(execution_interval: TIMER_EXECUTION_INTERVAL)
  return unless @watching.make_true

  Concurrent::TimerTask.execute(execution_interval: execution_interval, run_now: true) do
    bulk_index
  end
end

.write(output, index:, body:) ⇒ Object



41
42
43
44
# File 'lib/logtastic.rb', line 41

def write(output, index:, body:)
  @events << { output: output, index: index, body: body }
  watch
end