Skip to content

Commit

Permalink
Merge pull request #139 from bacongobbler/136-fix-panic
Browse files Browse the repository at this point in the history
fix(log): fix panic error when building app log message
  • Loading branch information
Matthew Fisher authored Mar 23, 2017
2 parents ec3c26c + 2569391 commit ac50ac9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
14 changes: 10 additions & 4 deletions log/message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,17 @@ func buildControllerLogMessage(message *Message) string {
}

func buildApplicationLogMessage(message *Message) string {
return fmt.Sprintf("%s %s[%s.%s.%s]: %s",
p := podRegex.FindStringSubmatch(message.Kubernetes.PodName)
tag := fmt.Sprintf(
"%s.%s",
message.Kubernetes.Labels["type"],
message.Kubernetes.Labels["version"])
if len(p) > 0 {
tag = fmt.Sprintf("%s.%s", tag, p[len(p)-1])
}
return fmt.Sprintf("%s %s[%s]: %s",
message.Time.Format(timeFormat),
message.Kubernetes.Labels["app"],
message.Kubernetes.Labels["type"],
message.Kubernetes.Labels["version"],
podRegex.FindStringSubmatch(message.Kubernetes.PodName)[4],
tag,
message.Log)
}
13 changes: 13 additions & 0 deletions log/message_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ var (
validAppMessage = `{"log": "test message", "stream": "stderr", "time": "2016-10-18T20:29:38+00:00", "docker": {"container_id": "containerId"}, "kubernetes": {"namespace_name": "foo", "pod_id": "podId", "pod_name": "foo-web-845861952-nzf60", "container_name": "foo-web", "labels": {"app": "foo",
"heritage": "deis", "type": "web", "version": "v2"}, "host": "host"}}`

badPodNameMessage = `{"log": "test message", "stream": "stderr", "time": "2016-10-18T20:29:38+00:00", "docker": {"container_id": "containerId"}, "kubernetes": {"namespace_name": "foo", "pod_id": "podId", "pod_name": "foo-web-845861952", "container_name": "foo-web", "labels": {"app": "foo",
"heritage": "deis", "type": "web", "version": "v2"}, "host": "host"}}`

badjson = `{"log":}`
)

Expand Down Expand Up @@ -63,6 +66,16 @@ func TestBuildApplicationLogMessageFromValidMessage(t *testing.T) {
"failed to build application log")
}

func TestBuildApplicationLogMessageFromInvalidMessage(t *testing.T) {
message := new(Message)
err := json.Unmarshal([]byte(badPodNameMessage), message)
assert.NoError(t, err, "error occured parsing log message")
expected := buildApplicationLogMessage(message)
assert.Equal(t, expected,
"2016-10-18T20:29:38+00:00 foo[web.v2]: test message",
"failed to build application log")
}

func TestHandleValidAppMessage(t *testing.T) {
a, err := storage.NewRingBufferAdapter(1)
assert.NoError(t, err, "error creating ring buffer")
Expand Down

0 comments on commit ac50ac9

Please sign in to comment.