Skip to content
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

Leave fields unmutated. #2

Merged
merged 1 commit into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions output.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,29 @@ func getAndDelRequest(fields map[string]interface{}, key string) (*http.Request,
return req, true
}

func cloneFields(src map[string]interface{}) map[string]interface{} {
retval := make(map[string]interface{})
for k, v := range src {
retval[k] = v
}
return retval
}

// Write implements xlog.Output interface
func (o Output) Write(fields map[string]interface{}) error {
fields = cloneFields(fields)
level := xlogSeverityMap[fields[xlog.KeyLevel].(string)]

packet := raven.NewPacket(fields[xlog.KeyMessage].(string))
packet.Timestamp = raven.Timestamp(fields[xlog.KeyTime].(time.Time))
packet.Level = severityMap[level]
packet.Logger = "xlog"


delete(fields, xlog.KeyMessage)
delete(fields, xlog.KeyTime)
delete(fields, xlog.KeyLevel)
delete(fields, xlog.KeyFile)


if serverName, ok := getAndDel(fields, "host"); ok {
packet.ServerName = serverName
} else if serverName, ok := getAndDel(fields, "server_name"); ok {
Expand Down
87 changes: 87 additions & 0 deletions output_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package xlogsentry

import (
"github.com/getsentry/raven-go"
"github.com/rs/xlog"
"strings"
"testing"
"time"
)

type dummyTransportArgs struct {
url string
authHeader string
packet *raven.Packet
}

type dummyTransport struct {
roundtripChan chan<- dummyTransportArgs
}

func (dtx *dummyTransport) Send(url, authHeader string, packet *raven.Packet) error {
dtx.roundtripChan <- dummyTransportArgs{url, authHeader, packet}
return nil
}

func dummyClient(roundtripChan chan<- dummyTransportArgs) *raven.Client {
client, err := raven.New("http://secret@localhost/id")
if err != nil {
panic(err)
}
client.Transport = &dummyTransport{roundtripChan}
return client
}

func TestWriteBasic(t *testing.T) {
fields := map[string]interface{}{
xlog.KeyMessage: "message",
xlog.KeyTime: time.Date(2017, time.January, 1, 0, 0, 0, 0, time.UTC),
xlog.KeyLevel: "info",
xlog.KeyFile: "file",
}
roundtripChan := make(chan dummyTransportArgs, 1)
target := Output{
Timeout: 1 * time.Second,
StacktraceConfiguration: StackTraceConfiguration{
true,
xlog.LevelInfo,
0,
5,
[]string{},
},
client: dummyClient(roundtripChan),
host: "host",
}
target.Write(fields)
args, ok := <-roundtripChan
if !ok {
t.Log("no data has been sent")
t.Fail()
}
if "http://localhost/api/id/store/" != args.url {
t.Logf(`%+v != args.url (got %+v)`, "http://localhost/api/id/store/", args.url)
t.Fail()
}
if strings.Index(args.authHeader, "secret") < 0 {
t.Logf(`%+v not in args.authHeader (got %+v)`, "secret", args.authHeader)
t.Fail()
}
if "2017-01-01T00:00:00Z" != time.Time(args.packet.Timestamp).Format(time.RFC3339) {
t.Logf(`%+v != args.packet.Timestamp (got %+v)`, "2017-01-01T00:00:00Z", args.packet.Timestamp.Format(time.RFC3339))
t.Fail()
}
if raven.INFO != args.packet.Level {
t.Logf(`%+v != args.packet.Level (got %+v)`, "raven.INFO", args.packet.Level)
t.Fail()
}
for _, k := range []string{xlog.KeyMessage, xlog.KeyTime, xlog.KeyLevel, xlog.KeyFile} {
if _, ok := args.packet.Extra[k]; ok {
t.Logf(`key %+v exists in args.packet.Extra`, k)
t.Fail()
}
if _, ok := fields[k]; !ok {
t.Logf(`key %+v does not exist in fields`, k)
t.Fail()
}
}
}