Skip to content

Commit

Permalink
add target label
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Pana <8968914+acpana@users.noreply.github.com>
  • Loading branch information
acpana committed Feb 28, 2023
1 parent 16fd041 commit 1c67b47
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
9 changes: 9 additions & 0 deletions constraint/pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,15 @@ func (c *Client) Review(ctx context.Context, obj interface{}, opts ...drivers.Op

responses.ByTarget[target] = resp
if stats != nil {
// add the target label to these stats for future collation.
targetLabel := &instrumentation.Label{Name: "target", Value: target}
for _, stat := range stats {
if stat.Labels == nil || len(stat.Labels) == 0 {
stat.Labels = []*instrumentation.Label{targetLabel}
} else {
stat.Labels = append(stat.Labels, targetLabel)
}
}
responses.StatsEntries = append(responses.StatsEntries, stats...)
}
}
Expand Down
15 changes: 14 additions & 1 deletion constraint/pkg/client/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,8 @@ func TestE2E_Review_StatsEntries(t *testing.T) {
if err != nil {
t.Fatal(err)
}
c, err := client.NewClient(client.Targets(&handlertest.Handler{}), client.Driver(d))
testHandler := &handlertest.Handler{}
c, err := client.NewClient(client.Targets(testHandler), client.Driver(d))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -883,6 +884,18 @@ func TestE2E_Review_StatsEntries(t *testing.T) {
t.Errorf("expected labels but did not find any")
}

foundTargetLabel := false
for _, label := range se.Labels {
if label.Name == "target" && label.Value == testHandler.GetName() {
foundTargetLabel = true
break
}
}

if !foundTargetLabel {
t.Fatalf("did not find target label for: %s", testHandler.GetName())
}

delete(kindSet, se.StatsFor)
} else {
t.Fatalf("encountered an unknown Key: %s", reflect.ValueOf(se.Scope))
Expand Down

0 comments on commit 1c67b47

Please sign in to comment.