Skip to content
Merged
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
4 changes: 4 additions & 0 deletions internal/actorlog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ func (al *ActorLogger) WrapContainerLogs(r io.Reader, actorRef resources.ActorRe
dec.UseNumber()

unmarshalErr := dec.Decode(&m)
if unmarshalErr == nil && m == nil {
unmarshalErr = errors.New("JSON value is not an object")
}
if unmarshalErr == nil {
var trailing any
if err := dec.Decode(&trailing); err != io.EOF {
Expand Down Expand Up @@ -155,6 +158,7 @@ func (al *ActorLogger) WrapContainerLogs(r io.Reader, actorRef resources.ActorRe
}
labels["ate.dev/actor_atespace"] = actorRef.Atespace
labels["ate.dev/actor_name"] = actorRef.Name
labels["ate.dev/actor_uid"] = actorUID
labels["ate.dev/actor_template_namespace"] = actorTemplateNamespace
labels["ate.dev/actor_template_name"] = actorTemplateName
labels["ate.dev/container_name"] = containerName
Expand Down
38 changes: 37 additions & 1 deletion internal/actorlog/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ func TestWrapContainerLogs_JSONInput(t *testing.T) {
if labels["ate.dev/actor_name"] != "act-1" {
t.Errorf("got actor_name = %v, want 'act-1'", labels["ate.dev/actor_name"])
}
if labels["ate.dev/actor_uid"] != "uid-1" {
t.Errorf("got actor_uid = %v, want 'uid-1'", labels["ate.dev/actor_uid"])
}
if labels["ate.dev/actor_template_namespace"] != "tmpl-ns" {
t.Errorf("got actor_template_namespace = %v, want 'tmpl-ns'", labels["ate.dev/actor_template_namespace"])
}
Expand Down Expand Up @@ -209,7 +212,7 @@ func TestWrapContainerLogs_MergeLabels(t *testing.T) {
}

func TestWrapContainerLogs_LabelCollision(t *testing.T) {
input := `{"level":"info","msg":"App log","labels":{"ate.dev/actor_name":"malicious-id","app":"my-app"}}` + "\n"
input := `{"level":"info","msg":"App log","labels":{"ate.dev/actor_name":"malicious-id","ate.dev/actor_uid":"malicious-uid","app":"my-app"}}` + "\n"
rdr := strings.NewReader(input)

var buf bytes.Buffer
Expand All @@ -236,6 +239,39 @@ func TestWrapContainerLogs_LabelCollision(t *testing.T) {
if labels["ate.dev/actor_name"] != "act-1" {
t.Errorf("got actor_name = %v, want 'act-1' (Substrate metadata should take precedence)", labels["ate.dev/actor_name"])
}
if labels["ate.dev/actor_uid"] != "uid-1" {
t.Errorf("got actor_uid = %v, want 'uid-1' (Substrate metadata should take precedence)", labels["ate.dev/actor_uid"])
}
}

func TestWrapContainerLogs_JSONNull(t *testing.T) {
input := "null\n"
rdr := strings.NewReader(input)

var buf bytes.Buffer
al := NewActorLogger(&buf, false)
al.WrapContainerLogs(rdr, resources.ActorRef{Atespace: "default", Name: "act-1"}, "uid-1", "tmpl-ns", "tmpl-1", "ctr-1")

var m map[string]any
if err := json.Unmarshal(buf.Bytes(), &m); err != nil {
t.Fatalf("failed to parse JSON output: %v", err)
}

if m["message"] != "null" {
t.Errorf("got message = %v, want 'null'", m["message"])
}

labelsAny, ok := m[al.labelsKey]
if !ok {
t.Fatal("missing labels group")
}
labels, ok := labelsAny.(map[string]any)
if !ok {
t.Fatal("labels group is not a map")
}
if labels["ate.dev/actor_uid"] != "uid-1" {
t.Errorf("got actor_uid = %v, want 'uid-1'", labels["ate.dev/actor_uid"])
}
}

func TestWrapContainerLogs_TrailingGarbage(t *testing.T) {
Expand Down
Loading