-
Notifications
You must be signed in to change notification settings - Fork 680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add simple Logstash adapter #64
Changes from 1 commit
77deffb
da21b03
64a4af7
18c2064
398bf0e
d92e811
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package logstash | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"net" | ||
"os" | ||
|
||
"github.com/gliderlabs/logspout/router" | ||
) | ||
|
||
var hostname string | ||
|
||
func init() { | ||
router.AdapterFactories.Register(NewLogstashAdapter, "logstash") | ||
|
||
hostname, _ = os.Hostname() | ||
} | ||
|
||
// LogstashAdapter is an adapter that streams UPD JSON to Logstash. | ||
type LogstashAdapter struct { | ||
conn net.Conn | ||
route *router.Route | ||
} | ||
|
||
// NewLogstashAdapter creates a LogstashAdapter with UDP transport. | ||
func NewLogstashAdapter(route *router.Route) (router.LogAdapter, error) { | ||
transport, found := router.AdapterTransports.Lookup(route.AdapterTransport("udp")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually saying "udp" is the default transport (by using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe TCP should work also, but I have not tried it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay. Your comments suggest otherwise, so maybe consider removing specific mention of UDP in them. |
||
if !found { | ||
return nil, errors.New("unable to find adapter: " + route.Adapter) | ||
} | ||
|
||
conn, err := transport.Dial(route.Address, route.Options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &LogstashAdapter{ | ||
route: route, | ||
conn: conn, | ||
}, nil | ||
} | ||
|
||
func (adapter *LogstashAdapter) Stream(logstream chan *router.Message) { | ||
for m := range logstream { | ||
msg := LogstashMessage{ | ||
Time: m.Time.Unix(), | ||
Message: m.Data, | ||
Hostname: hostname, | ||
Image: m.Container.Config.Image, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing logstash just supports arbitrary JSON keys? If so, don't you think some people might want more than just container image? Do you have ideas on how to make this more configurable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is true. At this point we are doing further parsing of the message (which is in logfmt format by logrus) in the filter section of logstash. Maybe the container ID and some other Docker metadata could be good to have as keys. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like this adapter is not as general as you think. Maybe it deserves to live as your own third-party module? I say this because if I were to do a general Logstash adapter, I probably wouldn't use JSON+UDP which is just one possible input format ... If I were to pick a canonical Logstash adapter machanism, I'd use the logstash-forwarder protocol, perhaps even use it directly as a library. Don't you agree? |
||
} | ||
js, _ := json.Marshal(msg) | ||
io.WriteString(adapter.conn, string(js)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to use |
||
} | ||
} | ||
|
||
type LogstashMessage struct { | ||
Time int64 `json:"time"` | ||
Message string `json:"message"` | ||
Hostname string `json:"hostname"` | ||
Image string `json:"image"` | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, it's a global. Maybe set this up as a field of the adapter object and do this in its constructor.