Skip to content

Commit 2366aa3

Browse files
committed
add idle_timeout between log messages for a given tag
1 parent a91e339 commit 2366aa3

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Renamed `cert` and `key` to `client_cert` and `client_key` respectively.
44
* Change to short timeouts on network calls so logging doesn't go dead for extended periods.
5+
* Added `idle_timeout` to force upstream reconnection after a period of time with no traffic for a particular tag. Useful for low-traffic senders. Not recommended for high-traffic.
56

67

78
#### 1.0.0

docs/configuration.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Host represents DNS name of endpoint where should be data sent. Example: `syslog
1515

1616
Example: `6514`
1717

18+
### idle_timeout
19+
20+
If a given tag has gone this many seconds between log messages, disconnect and reconnect before sending logs. Useful in low-traffic logging situations with remote hosts that disconnect after a period of time. Disabled by default. Example: `600`
21+
1822
### token
1923

2024
Some services require a token to identify the account. Example: `ABABABABABABA@99999`. Not required for Papertrail.
@@ -72,6 +76,7 @@ Optionally record key where to get msgid from the record. If not provided nil va
7276
@type syslog_tls
7377
host logs1.papertrailapp.com
7478
port 12345
79+
idle_timeout 720
7580
7681
hostname static-hostname
7782
facility SYSLOG

lib/fluent/plugin/out_syslog_tls.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class SyslogTlsOutput < Fluent::Output
2929

3030
config_param :host, :string
3131
config_param :port, :integer
32+
config_param :idle_timeout, :integer, default: nil
3233
config_param :token, :string, default: nil
3334
config_param :client_cert, :string, default: nil
3435
config_param :client_key, :string, default: nil
@@ -99,7 +100,7 @@ def logger(tag)
99100
end
100101

101102
def new_logger(tag)
102-
transport = ::SyslogTls::SSLTransport.new(host, port, client_cert: client_cert, client_key: client_key, max_retries: 3)
103+
transport = ::SyslogTls::SSLTransport.new(host, port, idle_timeout: idle_timeout, client_cert: client_cert, client_key: client_key, max_retries: 3)
103104
logger = ::SyslogTls::Logger.new(transport, token)
104105
logger.facility(facility)
105106
logger.hostname(hostname)

lib/syslog_tls/ssl_transport.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ class SSLTransport
2525

2626
attr_accessor :socket
2727

28-
attr_reader :host, :port, :client_cert, :client_key, :ssl_version
28+
attr_reader :host, :port, :idle_timeout, :client_cert, :client_key, :ssl_version
2929

3030
attr_writer :retries
3131

32-
def initialize(host, port, client_cert: nil, client_key: nil, ssl_version: :TLSv1_2, max_retries: 1)
32+
def initialize(host, port, idle_timeout: nil, client_cert: nil, client_key: nil, ssl_version: :TLSv1_2, max_retries: 1)
3333
@host = host
3434
@port = port
35+
@idle_timeout = idle_timeout
3536
@client_cert = client_cert
3637
@client_key = client_key
3738
@ssl_version = ssl_version
@@ -52,6 +53,7 @@ def connect
5253
rescue Errno::ETIMEDOUT
5354
raise 'Socket timeout during connect'
5455
end
56+
@last_write = Time.now if idle_timeout
5557
end
5658

5759
def get_tcp_connection
@@ -103,6 +105,14 @@ def get_ssl_connection
103105

104106
# Allow to retry on failed writes
105107
def write(s)
108+
if idle_timeout
109+
if (t=Time.now) > @last_write + idle_timeout
110+
@socket.close rescue nil
111+
connect
112+
else
113+
@last_write = t
114+
end
115+
end
106116
begin
107117
retry_id ||= 0
108118
do_write(s)

0 commit comments

Comments
 (0)