Skip to content

Instantly share code, notes, and snippets.

@eht16
Created April 7, 2019 09:15
Show Gist options
  • Save eht16/ab255866316296cff999742bd13d0f39 to your computer and use it in GitHub Desktop.
Save eht16/ab255866316296cff999742bd13d0f39 to your computer and use it in GitHub Desktop.
Logstash TCP vs UDP newline vs no newline
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import socket
PORT = 3333
def send(proto, use_trailing_newline):
if proto == 'UDP':
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
else:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('127.0.0.1', PORT))
with_newline = 'with' if use_trailing_newline else 'without'
for msgnum in range(5):
# prepare data
data = dict(message=f'Message via {proto} {with_newline} newline #{msgnum}')
data = json.dumps(data)
if use_trailing_newline:
data += '\n'
data = bytes(data, 'utf-8')
# send
if proto == 'UDP':
sock.sendto(data, ('127.0.0.1', PORT))
else:
sock.sendall(data)
sock.close()
send(proto='UDP', use_trailing_newline=True)
send(proto='UDP', use_trailing_newline=False)
send(proto='TCP', use_trailing_newline=True)
send(proto='TCP', use_trailing_newline=False)
tcp {
host => "127.0.0.1"
port => 3333
mode => server
codec => json {}
}
udp {
host => "127.0.0.1"
port => 3333
codec => json {}
}
{"message":"Message via TCP with newline #0","@version":"1","@timestamp":"2019-04-07T09:14:14.082Z","host":"localhost.localdomain","port":44252}
{"message":"Message via TCP with newline #1","@version":"1","@timestamp":"2019-04-07T09:14:14.082Z","host":"localhost.localdomain","port":44252}
{"message":"Message via TCP with newline #2","@version":"1","@timestamp":"2019-04-07T09:14:14.082Z","host":"localhost.localdomain","port":44252}
{"message":"Message via TCP with newline #3","@version":"1","@timestamp":"2019-04-07T09:14:14.082Z","host":"localhost.localdomain","port":44252}
{"message":"Message via TCP with newline #4","@version":"1","@timestamp":"2019-04-07T09:14:14.082Z","host":"localhost.localdomain","port":44252}
{"message":"Message via TCP without newline #0","@version":"1","@timestamp":"2019-04-07T09:14:14.081Z","host":"localhost.localdomain","port":44254}
{"message":"Message via UDP with newline #0","@version":"1","@timestamp":"2019-04-07T09:14:14.064Z","host":"127.0.0.1"}
{"message":"Message via UDP with newline #1","@version":"1","@timestamp":"2019-04-07T09:14:14.064Z","host":"127.0.0.1"}
{"message":"Message via UDP with newline #2","@version":"1","@timestamp":"2019-04-07T09:14:14.066Z","host":"127.0.0.1"}
{"message":"Message via UDP with newline #3","@version":"1","@timestamp":"2019-04-07T09:14:14.068Z","host":"127.0.0.1"}
{"message":"Message via UDP with newline #4","@version":"1","@timestamp":"2019-04-07T09:14:14.066Z","host":"127.0.0.1"}
{"message":"Message via UDP without newline #0","@version":"1","@timestamp":"2019-04-07T09:14:14.067Z","host":"127.0.0.1"}
{"message":"Message via UDP without newline #1","@version":"1","@timestamp":"2019-04-07T09:14:14.067Z","host":"127.0.0.1"}
{"message":"Message via UDP without newline #2","@version":"1","@timestamp":"2019-04-07T09:14:14.068Z","host":"127.0.0.1"}
{"message":"Message via UDP without newline #3","@version":"1","@timestamp":"2019-04-07T09:14:14.068Z","host":"127.0.0.1"}
{"message":"Message via UDP without newline #4","@version":"1","@timestamp":"2019-04-07T09:14:14.069Z","host":"127.0.0.1"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment