Permalink
Browse files

basic current integration

Signed-off-by: Peter Edge <peter.edge@gmail.com>
  • Loading branch information...
peter-edge committed Feb 3, 2016
1 parent 3f34c5d commit d73c75ff28c3c6ff1883088de87435249e0fb385
Showing with 154 additions and 0 deletions.
  1. +35 −0 current/current.go
  2. +100 −0 current/marshaller.go
  3. +14 −0 env/env.go
  4. +5 −0 lion.go
View
@@ -0,0 +1,35 @@
package currentlion // import "go.pedge.io/lion/current"
import (
"log/syslog"
"go.pedge.io/lion"
"go.pedge.io/lion/syslog"
)
const (
syslogNetwork = "tcp"
)
// NewPusher returns a new Pusher for current.
func NewPusher(
appName string,
syslogAddress string,
token string,
) (lion.Pusher, error) {
writer, err := syslog.Dial(
syslogNetwork,
syslogAddress,
syslog.LOG_INFO,
appName,
)
if err != nil {
return nil, err
}
return sysloglion.NewPusher(
writer,
sysloglion.PusherWithMarshaller(
newMarshaller(token),
),
), nil
}
View
@@ -0,0 +1,100 @@
package currentlion
import (
"bytes"
"time"
"go.pedge.io/lion"
)
type marshaller struct {
token string
}
func newMarshaller(token string) *marshaller {
return &marshaller{token}
}
func (t *marshaller) Marshal(entry *lion.Entry) ([]byte, error) {
return jsonMarshalEntry(entry, t.token)
}
func jsonMarshalEntry(entry *lion.Entry, token string) ([]byte, error) {
jsonEntry, err := entryToJSONEntry(entry)
if err != nil {
return nil, err
}
if jsonEntry == nil {
return nil, nil
}
buffer := bytes.NewBuffer(nil)
_, _ = buffer.WriteString("@current:")
_, _ = buffer.WriteString(token)
_ = buffer.WriteByte(' ')
if err := lion.GlobalJSONMarshalFunc()(buffer, jsonEntry); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
type jsonEntry struct {
ID string `json:"id,omitempty"`
Timestamp string `json:"@timestamp,omitempty"`
Contexts []interface{} `json:"contexts,omitempty"`
Fields map[string]string `json:"fields,omitempty"`
Event interface{} `json:"event,omitempty"`
Message string `json:"message,omitempty"`
WriterOutput string `json:"writer_output,omitempty"`
}
type jsonEntryMessage struct {
Name string `json:"name,omitempty"`
Value interface{} `json:"value,omitempty"`
}
func entryToJSONEntry(entry *lion.Entry) (*jsonEntry, error) {
if entry == nil {
return nil, nil
}
jsonEntry := &jsonEntry{
ID: entry.ID,
Timestamp: entry.Time.Format(time.RFC3339),
Fields: entry.Fields,
Message: entry.Message,
WriterOutput: string(entry.WriterOutput),
}
if len(entry.Contexts) > 0 {
jsonEntry.Contexts = make([]interface{}, 0)
for _, context := range entry.Contexts {
jsonContext, err := entryMessageToJSONEntryMessage(context)
if err != nil {
return nil, err
}
if jsonContext != nil {
jsonEntry.Contexts = append(jsonEntry.Contexts, jsonContext)
}
}
}
if entry.Event != nil {
jsonEvent, err := entryMessageToJSONEntryMessage(entry.Event)
if err != nil {
return nil, err
}
jsonEntry.Event = jsonEvent
}
return jsonEntry, nil
}
func entryMessageToJSONEntryMessage(entryMessage *lion.EntryMessage) (*jsonEntryMessage, error) {
if entryMessage == nil {
return nil, nil
}
name, err := entryMessage.Name()
if err != nil {
return nil, nil
}
return &jsonEntryMessage{
Name: name,
Value: entryMessage.Value,
}, nil
}
View
@@ -12,6 +12,7 @@ import (
"go.pedge.io/env"
"go.pedge.io/lion"
"go.pedge.io/lion/current"
"go.pedge.io/lion/syslog"
"gopkg.in/natefinch/lumberjack.v2"
)
@@ -35,6 +36,12 @@ type Env struct {
// Must be set with SyslogNetwork.
// If not set and LogDir not set, logs will be to stderr.
SyslogAddress string `env:"SYSLOG_ADDRESS"`
// The current token.
// Must be set with CurrentSyslogNetwork.
CurrentToken string `env:"CURRENT_TOKEN"`
// The current syslog host:port.
// Must be set with CurrentToken.
CurrentSyslogAddress string `env:"CURRENT_SYSLOG_ADDRESS"`
}
// Setup gets the Env from the environment, and then calls SetupEnv.
@@ -66,6 +73,13 @@ func SetupEnv(env Env) error {
}
pushers = append(pushers, pusher)
}
if env.CurrentToken != "" && env.CurrentSyslogAddress != "" {
pusher, err := currentlion.NewPusher(logAppName, env.CurrentSyslogAddress, env.CurrentToken)
if err != nil {
return err
}
pushers = append(pushers, pusher)
}
switch len(pushers) {
case 0:
lion.SetLogger(lion.DiscardLogger)
View
@@ -59,6 +59,11 @@ func GlobalLogger() Logger {
return globalLogger
}
// GlobalJSONMarshalFunc returns the global JSONMarshalFunc instance.
func GlobalJSONMarshalFunc() JSONMarshalFunc {
return globalJSONMarshalFunc
}
// SetLogger sets the global Logger instance.
func SetLogger(logger Logger) {
globalLock.Lock()

0 comments on commit d73c75f

Please sign in to comment.