The socket
plugin helper creates various types of socket instances.
Here is an example:
require 'fluent/plugin/output'​module Fluent::Pluginclass ExampleOutput < OutputFluent::Plugin.register_output('example', self)​# 1. Load socket helperhelpers :socket​config_param :host, :stringconfig_param :port, :integer​# Omit `configure`, `shutdown` and other plugin APIs​def try_write(chunk)# 2. Create socketsocket = socket_create(:tcp, @host, @port)chunk.each do |time, record|# 3. Write data to socketsocket.write(record.to_json)endensure# 4. Close socketsocket.close if socketendendend
The socket
plugin helper does not manage the lifecycle of the socket. User must close the socket when it is no longer needed.
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:
# TCPsocket = socket_create(:tcp, 'example.com', 12340)socket.write(data)socket.close​# UDPsocket = socket_create(:udp, 'example.com', 12341)socket.write(data)socket.close​# TLSsocket = socket_create(:tls, 'example.com', 12342, insecure: true)socket.write(data)socket.close​# close socket automaticallysocket_create(:udp, 'example.com', 12341) do |sock|sock.write(data)end
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.
If you set true
to send_keepavlie_packet
, you also need to configure keep-alive related kernel parameters:
net.ipv4.tcp_keepalive_intvl = 75net.ipv4.tcp_keepalive_probes = 5net.ipv4.tcp_keepalive_time = 7200
This parameter mitigates half-open connection issue with load balancers. Check also this issue for AWS NLB case.
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
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
​out_forward
​
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.