Skip to content

Commit d7c6673

Browse files
author
ljalbertsimard
committed
feat(logspout): support sending log via tcp
1 parent 101bbba commit d7c6673

3 files changed

Lines changed: 36 additions & 10 deletions

File tree

docs/managing_deis/running-deis-without-ceph.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ than the :ref:`logger` component.
110110
111111
$ HOST=logs.somewhere.com
112112
$ PORT=98765
113-
$ deisctl config logs set host=${HOST} port=${PORT}
113+
$ PROTOCOL=udp # Supported protocols are udp and tcp
114+
$ deisctl config logs set host=${HOST} port=${PORT} protocol=${PROTOCOL}
114115
115116
Configure registry
116117
^^^^^^^^^^^^^^^^^^

logspout/logspout.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,26 @@ func syslogStreamer(target Target, types []string, logstream chan *Log) {
6464
continue
6565
}
6666
tag, pid := getLogName(logline.Name)
67-
addr, err := net.ResolveUDPAddr("udp", target.Addr)
68-
assert(err, "syslog")
69-
conn, err := net.DialUDP("udp", nil, addr)
70-
assert(err, "syslog")
71-
// bump up the packet size for large log lines
72-
assert(conn.SetWriteBuffer(1048576), "syslog")
67+
var conn net.Conn
68+
if strings.EqualFold(target.Protocol, "tcp") {
69+
addr, err := net.ResolveTCPAddr("tcp", target.Addr)
70+
assert(err, "syslog")
71+
tcpconn, err := net.DialTCP("tcp", nil, addr)
72+
assert(err, "syslog")
73+
assert(tcpconn.SetWriteBuffer(1048576), "syslog")
74+
conn = tcpconn
75+
} else if strings.EqualFold(target.Protocol, "udp") {
76+
addr, err := net.ResolveUDPAddr("udp", target.Addr)
77+
assert(err, "syslog")
78+
udpconn, err := net.DialUDP("udp", nil, addr)
79+
assert(err, "syslog")
80+
assert(udpconn.SetWriteBuffer(1048576), "syslog")
81+
conn = udpconn
82+
} else {
83+
assert(fmt.Errorf("%s is not a supported protocol, use either udp or tcp", target.Protocol), "syslog")
84+
}
7385
// HACK: Go's syslog package hardcodes the log format, so let's send our own message
74-
_, err = fmt.Fprintf(conn,
86+
_, err := fmt.Fprintf(conn,
7587
"%s %s[%s]: %s",
7688
time.Now().Format(getopt("DATETIME_FORMAT", dtime.DeisDatetimeFormat)),
7789
tag,
@@ -161,6 +173,17 @@ func httpStreamer(w http.ResponseWriter, req *http.Request, logstream chan *Log,
161173
}
162174
}
163175

176+
func getEtcdValueOrDefault(c *etcd.Client, key string, defaultValue string) string {
177+
resp, err := c.Get(key, false, false)
178+
if err != nil {
179+
if strings.Contains(fmt.Sprintf("%v", err), "Key not found") {
180+
return defaultValue
181+
}
182+
assert(err, "url")
183+
}
184+
return resp.Node.Value
185+
}
186+
164187
func main() {
165188
debugMode = getopt("DEBUG", "") != ""
166189
port := getopt("PORT", "8000")
@@ -183,9 +206,10 @@ func main() {
183206
assert(err, "url")
184207
portResp, err := etcd.Get("/deis/logs/port", false, false)
185208
assert(err, "url")
209+
protocol := getEtcdValueOrDefault(etcd, "/deis/logs/protocol", "udp")
186210
host := fmt.Sprintf("%s:%s", hostResp.Node.Value, portResp.Node.Value)
187-
log.Println("routing all to " + host)
188-
router.Add(&Route{Target: Target{Type: "syslog", Addr: host}})
211+
log.Printf("routing all to %s://%s", protocol, host)
212+
router.Add(&Route{Target: Target{Type: "syslog", Addr: host, Protocol: protocol}})
189213
}
190214

191215
if len(os.Args) > 1 {

logspout/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func (s *Source) All() bool {
4141
type Target struct {
4242
Type string `json:"type"`
4343
Addr string `json:"addr"`
44+
Protocol string `json:"protocol"`
4445
AppendTag string `json:"append_tag,omitempty"`
4546
}
4647

0 commit comments

Comments
 (0)