Skip to content

Commit

Permalink
parse non-standard JSON fields as attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
Novgorodov Igor, PMK-TV-OP authored and Novgorodov Igor, PMK-TV-OP committed Jul 1, 2019
1 parent d923a7a commit fa1890c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.5
1.4.6
41 changes: 39 additions & 2 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"time"

pb "github.com/golang/protobuf/proto"
Expand All @@ -25,6 +27,20 @@ type eventJSON struct {
TTL float32
}

var (
eventJSONFields = map[string]bool{
"host": true,
"service": true,
"state": true,
"description": true,
"time": true,
"metric": true,
"tags": true,
"attributes": true,
"ttl": true,
}
)

func eventFromJSON(msg []byte) (ev *Event, err error) {
evJS := &eventJSON{}
if err = json.Unmarshal(msg, evJS); err != nil {
Expand All @@ -41,10 +57,31 @@ func eventFromJSON(msg []byte) (ev *Event, err error) {
Ttl: pb.Float32(evJS.TTL),
}

var tm time.Time
if !evJS.Time.IsZero() {
ev.TimeMicros = pb.Int64(evJS.Time.UnixNano() / 1000)
tm = evJS.Time
} else {
ev.TimeMicros = pb.Int64(time.Now().UnixNano() / 1000)
tm = time.Now()
}

ev.TimeMicros = pb.Int64(tm.UnixNano() / 1000)

// Unmarshal again to a map
m := map[string]interface{}{}
if err = json.Unmarshal(msg, &m); err != nil {
return
}

// Put any non-standard fields into attributes
for k, v := range m {
klc := strings.ToLower(k)

if !eventJSONFields[klc] {
ev.Attributes = append(ev.Attributes, &Attribute{
Key: pb.String(klc),
Value: pb.String(fmt.Sprintf("%v", v)),
})
}
}

for _, attr := range evJS.Attributes {
Expand Down
2 changes: 2 additions & 0 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,11 @@ func (i *input) handleHTTPEvent(w http.ResponseWriter, r *http.Request) {
evs, err := eventsFromMultipleJSONs(buf)
if err != nil {
i.Errorf("%s: Unable to parse event JSON: %s", r.RemoteAddr, err)
http.Error(w, err.Error(), 400)
return
}

i.Infof("%s: %d events parsed", r.RemoteAddr, len(evs))
i.sendEvents(evs)
}

Expand Down

0 comments on commit fa1890c

Please sign in to comment.