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

pass a JSON encoded message as JSON, string as is #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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