Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
pass a JSON encoded message as JSON, string as is
Browse files Browse the repository at this point in the history
  • Loading branch information
faridco committed Nov 11, 2017
1 parent df8eb69 commit 83bd012
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
3 changes: 2 additions & 1 deletion reader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -136,6 +137,6 @@ func synthRecord(err error) Record {
return Record{
Command: "journald-cloudwatch-logs",
Priority: ERROR,
Message: err.Error(),
Message: json.RawMessage(err.Error()),
}
}
46 changes: 24 additions & 22 deletions record.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import "encoding/json"

type Priority int

var (
Expand All @@ -25,28 +27,28 @@ var PriorityJSON = map[Priority][]byte{
}

type Record struct {
InstanceId string `json:"instanceId,omitempty"`
TimeUsec int64 `json:"-"`
PID int `json:"pid" journald:"_PID"`
UID int `json:"uid" journald:"_UID"`
GID int `json:"gid" journald:"_GID"`
Command string `json:"cmdName,omitempty" journald:"_COMM"`
Executable string `json:"exe,omitempty" journald:"_EXE"`
CommandLine string `json:"cmdLine,omitempty" journald:"_CMDLINE"`
SystemdUnit string `json:"systemdUnit,omitempty" journald:"_SYSTEMD_UNIT"`
BootId string `json:"bootId,omitempty" journald:"_BOOT_ID"`
MachineId string `json:"machineId,omitempty" journald:"_MACHINE_ID"`
Hostname string `json:"hostname,omitempty" journald:"_HOSTNAME"`
Transport string `json:"transport,omitempty" journald:"_TRANSPORT"`
Priority Priority `json:"priority" journald:"PRIORITY"`
Message string `json:"message" journald:"MESSAGE"`
MessageId string `json:"messageId,omitempty" journald:"MESSAGE_ID"`
Errno int `json:"machineId,omitempty" journald:"ERRNO"`
Syslog RecordSyslog `json:"syslog,omitempty"`
Kernel RecordKernel `json:"kernel,omitempty"`
Container_Name string `json:"containerName,omitempty" journald:"CONTAINER_NAME"`
Container_Tag string `json:"containerTag,omitempty" journald:"CONTAINER_TAG"`
Container_ID string `json:"containerID,omitempty" journald:"CONTAINER_ID"`
InstanceId string `json:"instanceId,omitempty"`
TimeUsec int64 `json:"-"`
PID int `json:"pid" journald:"_PID"`
UID int `json:"uid" journald:"_UID"`
GID int `json:"gid" journald:"_GID"`
Command string `json:"cmdName,omitempty" journald:"_COMM"`
Executable string `json:"exe,omitempty" journald:"_EXE"`
CommandLine string `json:"cmdLine,omitempty" journald:"_CMDLINE"`
SystemdUnit string `json:"systemdUnit,omitempty" journald:"_SYSTEMD_UNIT"`
BootId string `json:"bootId,omitempty" journald:"_BOOT_ID"`
MachineId string `json:"machineId,omitempty" journald:"_MACHINE_ID"`
Hostname string `json:"hostname,omitempty" journald:"_HOSTNAME"`
Transport string `json:"transport,omitempty" journald:"_TRANSPORT"`
Priority Priority `json:"priority" journald:"PRIORITY"`
Message json.RawMessage `json:"message" journald:"MESSAGE"`
MessageId string `json:"messageId,omitempty" journald:"MESSAGE_ID"`
Errno int `json:"machineId,omitempty" journald:"ERRNO"`
Syslog RecordSyslog `json:"syslog,omitempty"`
Kernel RecordKernel `json:"kernel,omitempty"`
Container_Name string `json:"containerName,omitempty" journald:"CONTAINER_NAME"`
Container_Tag string `json:"containerTag,omitempty" journald:"CONTAINER_TAG"`
Container_ID string `json:"containerID,omitempty" journald:"CONTAINER_ID"`
}

type RecordSyslog struct {
Expand Down
13 changes: 13 additions & 0 deletions unmarshal.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"time"

"github.com/coreos/go-systemd/sdjournal"
Expand Down Expand Up @@ -54,6 +56,17 @@ func unmarshalRecord(journal *sdjournal.Journal, toVal reflect.Value) error {
// the front, so we'll trim those off.
value = value[len(jdKey)+1:]

if fieldType.Name() == "RawMessage" {
if !strings.HasPrefix(value, `{"`) {
value = `"` + value + `"`
}
// fix unwanted characters to JSON message
value = strings.Replace(value, "\n", `\n`, -1)
value = strings.Replace(value, "\t", `\t`, -1)
fieldVal.SetBytes(json.RawMessage(value))
continue
}

switch fieldTypeKind {
case reflect.Int:
intVal, err := strconv.Atoi(value)
Expand Down

0 comments on commit 83bd012

Please sign in to comment.