Class: LogStashLogger::Device::Connectable

Inherits:
Base
  • Object
show all
Includes:
Buffer
Defined in:
lib/logstash-logger/device/connectable.rb

Direct Known Subclasses

AwsStream, Kafka, Redis, Socket, Unix

Instance Attribute Summary collapse

Attributes inherited from Base

#error_logger, #io, #sync

Instance Method Summary collapse

Methods included from Buffer

#buffer_flush, #buffer_full?, #buffer_initialize, #buffer_receive, #reset_buffer

Methods inherited from Base

#close!, #unrecoverable_error?

Constructor Details

#initialize(opts = {}) ⇒ Connectable

Returns a new instance of Connectable



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/logstash-logger/device/connectable.rb', line 10

def initialize(opts = {})
  super

  if opts[:batch_events]
    warn "The :batch_events option is deprecated. Please use :buffer_max_items instead"
  end

  if opts[:batch_timeout]
    warn "The :batch_timeout option is deprecated. Please use :buffer_max_interval instead"
  end

  @buffer_group = nil
  @buffer_max_items = opts[:batch_events] || opts[:buffer_max_items]
  @buffer_max_interval = opts[:batch_timeout] || opts[:buffer_max_interval]
  @drop_messages_on_flush_error =
    if opts.key?(:drop_messages_on_flush_error)
      opts.delete(:drop_messages_on_flush_error)
    else
      false
    end

  @drop_messages_on_full_buffer =
    if opts.key?(:drop_messages_on_full_buffer)
      opts.delete(:drop_messages_on_full_buffer)
    else
      true
    end

  @buffer_flush_at_exit =
    if opts.key?(:buffer_flush_at_exit)
      opts.delete(:buffer_flush_at_exit)
    else
      true
    end

  @buffer_logger = opts[:buffer_logger]

  buffer_initialize(
    max_items: @buffer_max_items,
    max_interval: @buffer_max_interval,
    logger: buffer_logger,
    autoflush: @sync,
    drop_messages_on_flush_error: @drop_messages_on_flush_error,
    drop_messages_on_full_buffer: @drop_messages_on_full_buffer,
    flush_at_exit: @buffer_flush_at_exit
  )
end

Instance Attribute Details

#buffer_loggerObject

Returns the value of attribute buffer_logger



8
9
10
# File 'lib/logstash-logger/device/connectable.rb', line 8

def buffer_logger
  @buffer_logger
end

Instance Method Details

#close(opts = {}) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/logstash-logger/device/connectable.rb', line 75

def close(opts = {})
  if opts.fetch(:flush, true)
    buffer_flush(final: true)
  end

  super
end

#connectObject

Implemented by subclasses



106
107
108
# File 'lib/logstash-logger/device/connectable.rb', line 106

def connect
  fail NotImplementedError
end

#connected?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/logstash-logger/device/connectable.rb', line 89

def connected?
  !!@io
end

#flush(*args) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/logstash-logger/device/connectable.rb', line 62

def flush(*args)
  if args.empty?
    buffer_flush
  else
    messages, group = *args
    write_batch(messages, group)
  end
end

#on_full_buffer_receive(data) ⇒ Object



71
72
73
# File 'lib/logstash-logger/device/connectable.rb', line 71

def on_full_buffer_receive(data)
  log_warning("Buffer Full - #{data}")
end

#reconnectObject



110
111
112
113
# File 'lib/logstash-logger/device/connectable.rb', line 110

def reconnect
  close(flush: false)
  connect
end

#to_ioObject



83
84
85
86
87
# File 'lib/logstash-logger/device/connectable.rb', line 83

def to_io
  with_connection do
    super
  end
end

#with_connection(&block) ⇒ Object

Ensure the block is executed with a valid connection



116
117
118
119
120
121
122
123
# File 'lib/logstash-logger/device/connectable.rb', line 116

def with_connection(&block)
  connect unless connected?
  yield
rescue => e
  log_error(e)
  close(flush: false)
  raise
end

#write(message) ⇒ Object



58
59
60
# File 'lib/logstash-logger/device/connectable.rb', line 58

def write(message)
  buffer_receive message, @buffer_group
end

#write_batch(messages, group = nil) ⇒ Object



99
100
101
102
103
# File 'lib/logstash-logger/device/connectable.rb', line 99

def write_batch(messages, group = nil)
  with_connection do
    super
  end
end

#write_one(message) ⇒ Object



93
94
95
96
97
# File 'lib/logstash-logger/device/connectable.rb', line 93

def write_one(message)
  with_connection do
    super
  end
end