@@ -24,17 +24,22 @@ type OpbeatClient struct {
24
24
25
25
// OpbeatPayload structures log entries for Opbeat's API.
26
26
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"`
33
33
}
34
34
35
- // OpbeatExtra structures Logrus Fields for for Opbeat's API.
35
+ // OpbeatExtra structures Logrus Fields for Opbeat's API.
36
36
type OpbeatExtra map [string ]interface {}
37
37
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
+
38
43
// NewOpbeatClient returns an OpbeatClient used for commnicating with Opbeat's API.
39
44
func NewOpbeatClient (org , app , token string ) * OpbeatClient {
40
45
client := & http.Client {
@@ -54,20 +59,42 @@ func NewOpbeatClient(org, app, token string) *OpbeatClient {
54
59
}
55
60
}
56
61
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 {
59
84
var extra = OpbeatExtra {}
60
85
for k , v := range entry .Data {
61
86
extra [k ] = v
62
87
}
88
+ return extra
89
+ }
63
90
64
- hostname , _ := os . Hostname ()
65
-
91
+ // NewOpbeatPayload returns an OpbeatPayload for the given logrus.Entry.
92
+ func NewOpbeatPayload ( entry * logrus. Entry ) * OpbeatPayload {
66
93
return & OpbeatPayload {
67
- Extra : extra ,
68
- Level : entry . Level . String ( ),
94
+ Extra : NewOpbeatExtra ( entry ) ,
95
+ Level : OpbeatLevel ( entry ),
69
96
Logger : fmt .Sprintf ("logbeat-%s" , LogbeatVersion ),
70
- Machine : hostname ,
97
+ Machine : NewOpbeatMachine () ,
71
98
Message : entry .Message ,
72
99
Timestamp : entry .Time .UTC ().Format (ISO8601 ),
73
100
}
0 commit comments