Skip to content

Commit fac27b1

Browse files
committed
v0.0.3 - fix payload shape, to conform to opbeat api
1 parent a286404 commit fac27b1

File tree

4 files changed

+67
-18
lines changed

4 files changed

+67
-18
lines changed

logbeat.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
// LogbeatVersion is used to identify notifications sent by Logbeat.
8-
const LogbeatVersion string = "0.0.2"
8+
const LogbeatVersion string = "0.0.3"
99

1010
// LogbeatHook delivers logs to the Opbeat service.
1111
type LogbeatHook struct {

logbeat_test.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type LogbeatTestSuite struct {
2626
Entry *logrus.Entry
2727
OpbeatHook *LogbeatHook
2828
OpbeatClient *OpbeatClient
29+
OpbeatExtra OpbeatExtra
30+
OpbeatMachine OpbeatMachine
2931
OpbeatPayload *OpbeatPayload
3032
OpbeatJSON *bytes.Buffer
3133
OpbeatRequest *http.Request
@@ -45,7 +47,7 @@ func (suite *LogbeatTestSuite) SetupTest() {
4547
suite.Token = "TEST_TOKEN"
4648

4749
suite.Entry = &logrus.Entry{
48-
Level: logrus.ErrorLevel,
50+
Level: logrus.PanicLevel,
4951
Message: "Example Logbeat Log Entry",
5052
Data: logrus.Fields{"example": "true"},
5153
Time: time.Date(1955, time.November, 05, 9, 11, 12, 13, time.UTC),
@@ -67,12 +69,18 @@ func (suite *LogbeatTestSuite) SetupTest() {
6769

6870
hostname, _ := os.Hostname()
6971
suite.Hostname = hostname
72+
suite.OpbeatMachine = NewOpbeatMachine()
73+
suite.OpbeatExtra = NewOpbeatExtra(suite.Entry)
7074
}
7175

7276
func (suite *LogbeatTestSuite) TestLogbeatHookType() {
7377
suite.IsType(&LogbeatHook{}, suite.Hook, "expects an instance of LogbeatHook")
7478
}
7579

80+
func (suite *LogbeatTestSuite) TestLogbeatHookInterface() {
81+
suite.Implements((*logrus.Hook)(nil), new(LogbeatHook), "expects OpbeatHook to implment logrus.Hook interface")
82+
}
83+
7684
func (suite *LogbeatTestSuite) TestNewOpbeatHook() {
7785
suite.IsType(&LogbeatHook{}, suite.OpbeatHook, "expects an instance of LogbeatHook.")
7886
suite.Equal(suite.OpbeatHook.AppId, "TEST_APP_ID", "expects the correct Opbeat App ID.")

opbeat.go

+41-14
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,22 @@ type OpbeatClient struct {
2424

2525
// OpbeatPayload structures log entries for Opbeat's API.
2626
type OpbeatPayload struct {
27-
Extra OpbeatExtra `json:"extra,omitempty"`
28-
Level string `json:"level"`
29-
Logger string `json:"logger"`
30-
Machine string `json:"machine,omitempty"`
31-
Message string `json:"message"`
32-
Timestamp string `json:"timestamp"`
27+
Extra OpbeatExtra `json:"extra,omitempty"`
28+
Level string `json:"level"`
29+
Logger string `json:"logger"`
30+
Machine OpbeatMachine `json:"machine,omitempty"`
31+
Message string `json:"message"`
32+
Timestamp string `json:"timestamp"`
3333
}
3434

35-
// OpbeatExtra structures Logrus Fields for for Opbeat's API.
35+
// OpbeatExtra structures Logrus Fields for Opbeat's API.
3636
type OpbeatExtra map[string]interface{}
3737

38+
// OpbeatMachine represents the hostname of the Machine the error occured on to Opbeat's API.
39+
type OpbeatMachine struct {
40+
Hostname string `json:"hostname"`
41+
}
42+
3843
// NewOpbeatClient returns an OpbeatClient used for commnicating with Opbeat's API.
3944
func NewOpbeatClient(org, app, token string) *OpbeatClient {
4045
client := &http.Client{
@@ -54,20 +59,42 @@ func NewOpbeatClient(org, app, token string) *OpbeatClient {
5459
}
5560
}
5661

57-
// NewOpbeatPayload returns an OpbeatPayload for the given log entry.
58-
func NewOpbeatPayload(entry *logrus.Entry) *OpbeatPayload {
62+
// NewOpbeatMachine returns an OpbeatMachine for the current machine.
63+
func NewOpbeatMachine() OpbeatMachine {
64+
var machine = OpbeatMachine{}
65+
hostname, err := os.Hostname()
66+
if err != nil {
67+
return machine
68+
}
69+
machine.Hostname = hostname
70+
return machine
71+
}
72+
73+
// OpbeatLevel returns the logrus.Level, as a string that Opbeat will accept, for the given logrus.Entry.
74+
func OpbeatLevel(entry *logrus.Entry) string {
75+
level := entry.Level.String()
76+
if level == "panic" {
77+
level = "critical"
78+
}
79+
return level
80+
}
81+
82+
// NewOpbeatExtra returns an OpbeatExtra for the given logrus.Entry.
83+
func NewOpbeatExtra(entry *logrus.Entry) OpbeatExtra {
5984
var extra = OpbeatExtra{}
6085
for k, v := range entry.Data {
6186
extra[k] = v
6287
}
88+
return extra
89+
}
6390

64-
hostname, _ := os.Hostname()
65-
91+
// NewOpbeatPayload returns an OpbeatPayload for the given logrus.Entry.
92+
func NewOpbeatPayload(entry *logrus.Entry) *OpbeatPayload {
6693
return &OpbeatPayload{
67-
Extra: extra,
68-
Level: entry.Level.String(),
94+
Extra: NewOpbeatExtra(entry),
95+
Level: OpbeatLevel(entry),
6996
Logger: fmt.Sprintf("logbeat-%s", LogbeatVersion),
70-
Machine: hostname,
97+
Machine: NewOpbeatMachine(),
7198
Message: entry.Message,
7299
Timestamp: entry.Time.UTC().Format(ISO8601),
73100
}

opbeat_test.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,27 @@ func (suite *LogbeatTestSuite) TestNewOpbeatClient() {
2626
suite.Equal(suite.OpbeatClient.Token, "TEST_TOKEN", "expects the correct Opbeat Token")
2727
}
2828

29+
func (suite *LogbeatTestSuite) TestNewOpbeatMachine() {
30+
suite.IsType(OpbeatMachine{}, NewOpbeatMachine(), "expects an instance of OpbeatMachine")
31+
suite.Equal(suite.OpbeatMachine.Hostname, suite.Hostname, "expects the correct hostname")
32+
}
33+
34+
func (suite *LogbeatTestSuite) TestNewOpbeatExtra() {
35+
suite.IsType(OpbeatExtra{}, NewOpbeatExtra(suite.Entry), "expects an instance of OpbeatExtra")
36+
suite.Equal(suite.OpbeatExtra["example"], suite.Entry.Data["example"], "expects OpbeatExtra to be the same as Log Entry Data")
37+
}
38+
39+
func (suite *LogbeatTestSuite) TestOpbeatLevel() {
40+
suite.Equal(OpbeatLevel(suite.Entry), "critical", "expects the correct OpbeatLevel")
41+
}
42+
2943
func (suite *LogbeatTestSuite) TestNewOpbeatPayload() {
3044
suite.IsType(&OpbeatPayload{}, suite.OpbeatPayload, "expects an instance of OpbeatPayload")
3145
suite.IsType(OpbeatExtra{}, suite.OpbeatPayload.Extra, "expects an instance of OpbeatExtra")
3246
suite.Equal(suite.OpbeatPayload.Extra["example"], suite.Entry.Data["example"], "expects OpbeatPayload Extra to be the same as Log Entry Data")
33-
suite.Equal(suite.OpbeatPayload.Level, suite.Entry.Level.String(), "expects OpbeatPayload Message to be the same as Log Entry")
47+
suite.Equal(suite.OpbeatPayload.Level, "critical", "expects OpbeatPayload Level to be correct")
3448
suite.Contains(suite.OpbeatPayload.Logger, LogbeatVersion, "expects OpbeatPayload Logger to contain Logbeat Version")
35-
suite.Equal(suite.OpbeatPayload.Machine, suite.Hostname, "expects OpbeatPayload Machine to be the Hostname")
49+
suite.Equal(suite.OpbeatPayload.Machine, suite.OpbeatMachine, "expects OpbeatPayload Machine to be the OpbeatMachine")
3650
suite.Equal(suite.OpbeatPayload.Message, suite.Entry.Message, "expects OpbeatPayload Message to be the same as Log Entry")
3751
suite.Equal(suite.OpbeatPayload.Timestamp, suite.Entry.Time.Format(ISO8601), "expects OpbeatPayload Timestamp to be the same as Log Entry")
3852
}

0 commit comments

Comments
 (0)