-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds a basic Logstash appender that can be used to send scraped logs to Logstash server. Package log is renamed to servicelog to better reflect its responsibility.
- Loading branch information
Showing
12 changed files
with
147 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package appender | ||
|
||
import "github.com/allegro/mesos-executor/servicelog" | ||
|
||
// Appender delivers service log entries to their destination. | ||
type Appender interface { | ||
Append(entries <-chan servicelog.Entry) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package appender | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net" | ||
"time" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
|
||
"github.com/allegro/mesos-executor/servicelog" | ||
) | ||
|
||
const ( | ||
logstashVersion = "1" | ||
logstashDefaultTimeFormat = time.RFC3339Nano | ||
) | ||
|
||
type logstash struct { | ||
conn net.Conn | ||
} | ||
|
||
func (l logstash) Append(entries <-chan servicelog.Entry) { | ||
for entry := range entries { | ||
if err := l.sendEntry(entry); err != nil { | ||
log.WithError(err).Warn("Error appending logs.") | ||
} | ||
} | ||
} | ||
|
||
func (l logstash) sendEntry(entry servicelog.Entry) error { | ||
// TODO(medzin): Move formatting logic to separate structure and extend it there | ||
entry["@timestamp"] = time.Now().Format(logstashDefaultTimeFormat) | ||
entry["@version"] = logstashVersion | ||
bytes, err := json.Marshal(entry) | ||
bytes = append(bytes, '\n') | ||
|
||
if err != nil { | ||
return fmt.Errorf("unable to marshal log entry: %s", err) | ||
} | ||
if _, err = l.conn.Write(bytes); err != nil { | ||
return fmt.Errorf("unable to write to Logstash server: %s", err) | ||
} | ||
return nil | ||
} | ||
|
||
// NewLogstash creates new appender that will send log entries to Logstash. | ||
func NewLogstash(protocol, address string) (Appender, error) { | ||
conn, err := net.Dial(protocol, address) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to connect to Logstash server: %s", err) | ||
} | ||
return &logstash{conn: conn}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package appender | ||
|
||
import ( | ||
"bufio" | ||
"net" | ||
"testing" | ||
|
||
"github.com/allegro/mesos-executor/servicelog" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestIfSendsLogsToLogstash(t *testing.T) { | ||
ln, err := net.Listen("tcp", "127.0.0.1:0") | ||
require.NoError(t, err) | ||
|
||
done := make(chan struct{}) | ||
go func() { | ||
defer ln.Close() | ||
conn, _ := ln.Accept() | ||
reader := bufio.NewReader(conn) | ||
bytes, _, err := reader.ReadLine() | ||
|
||
require.NoError(t, err) | ||
assert.Contains(t, string(bytes), "\"@version\":\"1\"") | ||
assert.Contains(t, string(bytes), "@timestamp") | ||
|
||
done <- struct{}{} | ||
}() | ||
|
||
entries := make(chan servicelog.Entry) | ||
logstash, err := NewLogstash("tcp", ln.Addr().String()) | ||
require.NoError(t, err) | ||
|
||
go logstash.Append(entries) | ||
|
||
entries <- servicelog.Entry{} | ||
<-done | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package servicelog | ||
|
||
// Entry represents one scraped log line in flat key-value store. | ||
type Entry map[string]string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package scraper | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/allegro/mesos-executor/servicelog" | ||
) | ||
|
||
// Scraper in an interface for various scrapers that support different log formats. | ||
type Scraper interface { | ||
StartScraping(io.Reader) <-chan servicelog.Entry | ||
} | ||
|
||
// Pipe returns writer that can be used as a data provider for given scraper. | ||
func Pipe(scraper Scraper) io.Writer { | ||
reader, writer := io.Pipe() | ||
scraper.StartScraping(reader) | ||
return writer | ||
} |