Fluentd
Search…
Plugin Helper: Socket
The socket plugin helper creates various types of socket instances.
Here is an example:
1
require 'fluent/plugin/output'
2
3
module Fluent::Plugin
4
class ExampleOutput < Output
5
Fluent::Plugin.register_output('example', self)
6
7
# 1. Load socket helper
8
helpers :socket
9
10
config_param :host, :string
11
config_param :port, :integer
12
13
# Omit `configure`, `shutdown` and other plugin APIs
14
15
def try_write(chunk)
16
# 2. Create socket
17
socket = socket_create(:tcp, @host, @port)
18
chunk.each do |time, record|
19
# 3. Write data to socket
20
socket.write(record.to_json)
21
end
22
ensure
23
# 4. Close socket
24
socket.close if socket
25
end
26
end
27
end
Copied!
The socket plugin helper does not manage the lifecycle of the socket. User must close the socket when it is no longer needed.

Methods

socket_create(proto, host, port, **kwargs, &block)

This method creates a socket instance with the given protocol type.
If the block is given, it will be invoked with the socket instance as a parameter, and the socket will automatically be closed when the block terminates.
    proto: protocol type. { :tcp, :udp, :tls }
    host: host name or IP address
    port: port number
    kwargs: extra options. For more details, see methods below.
    block: customize socket
Code example:
1
# TCP
2
socket = socket_create(:tcp, 'example.com', 12340)
3
socket.write(data)
4
socket.close
5
6
# UDP
7
socket = socket_create(:udp, 'example.com', 12341)
8
socket.write(data)
9
socket.close
10
11
# TLS
12
socket = socket_create(:tls, 'example.com', 12342, insecure: true)
13
socket.write(data)
14
socket.close
15
16
# close socket automatically
17
socket_create(:udp, 'example.com', 12341) do |sock|
18
sock.write(data)
19
end
Copied!

socket_create_tcp(host, port, **kwargs, &block)

This method creates socket instance for TCP.
If the block is given, it will be invoked with the socket instance as a parameter, and the socket will automatically be closed when the block terminates.
    host: hostname or IP address
    port: port number
    kwargs: extra options
      resolve_name: if true, resolve the hostname
      nonblock: if true, use non-blocking I/O
      linger_timeout: the timeout (seconds) to set SO_LINGER
      recv_timeout: the timeout (seconds) to set SO_RECVTIMEO
      send_timeout: the timeout (seconds) to set SO_SNDTIMEO
      send_keepalive_packet: if true, enable TCP keep-alive via SO_KEEPALIVE
      connect_timeout: the timeout for socket connect. When the connection
      timed out during establishment, Errno::ETIMEDOUT is raised.

send_keepalive_packet Use Case

If you set true to send_keepalive_packet, you also need to configure keep-alive related kernel parameters:
1
net.ipv4.tcp_keepalive_intvl = 75
2
net.ipv4.tcp_keepalive_probes = 5
3
net.ipv4.tcp_keepalive_time = 7200
Copied!
This parameter mitigates half-open connection issue with load balancers. Check also this issue for AWS NLB case.

socket_create_udp(host, port, **kwargs, &block)

This method creates socket instance for UDP.
If block is given, it will be invoked with the socket instance as a parameter, and socket will automatically be closed when the block terminates.
    host: host name or IP address
    port: port number
    kwargs: extra options
      resolve_name: if true, resolve the hostname
      connect: if true, connect to host
      nonblock: if true, use non-blocking I/O
      linger_timeout: the timeout (seconds) to set SO_LINGER
      recv_timeout: the timeout (seconds) to set SO_RECVTIMEO
      send_timeout: the timeout (seconds) to set SO_SNDTIMEO

socket_create_tls(host, port, **kwargs, &block)

This method creates socket instance for TLS.
If block is given, it will be invoked with the socket instance as a parameter, and socket will automatically be closed when the block terminates.
    host: host name or IP address
    port: port number
    kwargs: extra options
      version: set TLS version :TLSv1_1 or :TLSv1_2. (default: :TLSv1_2)
      ciphers: set the list of available cipher suites. (default:
      "ALL:!aNULL:!eNULL:!SSLv2")
      insecure: if true, set TLS verify mode NONE
      verify_fqdn: if true, validate the server certificate for the hostname
      fqdn: set FQDN
      enable_system_cert_store: if true, enable system default cert store
      allow_self_signed_cert: if true, allow self-signed certificate
      cert_paths: files contain PEM-encoded certificates
      private_key_path: set the client private key path
      private_key_passphrase: set the client private key passphrase
      cert_thumbprint: set the certificate thumbprint for searching from Windows system certstore
      cert_logical_store_name: set the certificate logical store name on Windows system certstore
      cert_use_enterprise_store: if true, enable to use certificate enterprise store on Windows system certstore
      Support more parameters same as socket_create_tcp's kwargs

Plugins using socket

If this article is incorrect or outdated, or omits critical information, please let us know. Fluentd is an open-source project under Cloud Native Computing Foundation (CNCF). All components are available under the Apache 2 License.
Last modified 4mo ago