Skip to content
This repository has been archived by the owner on Jun 19, 2022. It is now read-only.

Limit the values of event_type in Broker metrics #1352

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
2 changes: 1 addition & 1 deletion pkg/broker/ingress/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const (

traceID = "4bf92f3577b34da6a3ce929d0e0e4736"

eventType = "test-event-type"
eventType = "google.cloud.scheduler.job.v1.executed"
pod = "testpod"
container = "testcontainer"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/metrics/ingress_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (r *IngressReporter) ReportEventCount(ctx context.Context, args IngressRepo
tag.Insert(ContainerNameKey, string(r.containerName)),
tag.Insert(NamespaceNameKey, args.Namespace),
tag.Insert(BrokerNameKey, args.Broker),
tag.Insert(EventTypeKey, args.EventType),
tag.Insert(EventTypeKey, EventTypeMetricValue(args.EventType)),
tag.Insert(ResponseCodeKey, strconv.Itoa(args.ResponseCode)),
tag.Insert(ResponseCodeClassKey, metrics.ResponseCodeClass(args.ResponseCode)),
)
Expand Down
36 changes: 35 additions & 1 deletion pkg/metrics/ingress_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,40 @@ import (
func TestStatsReporter(t *testing.T) {
reportertest.ResetIngressMetrics()

args := IngressReportArgs{
Namespace: "testns",
Broker: "testbroker",
EventType: "google.cloud.scheduler.job.v1.executed",
ResponseCode: 202,
}
wantTags := map[string]string{
metricskey.LabelNamespaceName: "testns",
metricskey.LabelBrokerName: "testbroker",
metricskey.LabelEventType: "google.cloud.scheduler.job.v1.executed",
metricskey.LabelResponseCode: "202",
metricskey.LabelResponseCodeClass: "2xx",
metricskey.ContainerName: "testcontainer",
metricskey.PodName: "testpod",
}

r, err := NewIngressReporter(PodName("testpod"), ContainerName("testcontainer"))
if err != nil {
t.Fatal(err)
}

// test ReportEventCount
reportertest.ExpectMetrics(t, func() error {
return r.ReportEventCount(context.Background(), args)
})
reportertest.ExpectMetrics(t, func() error {
return r.ReportEventCount(context.Background(), args)
})
metricstest.CheckCountData(t, "event_count", wantTags, 2)
}

func TestStatsReporterWithCustomEventType(t *testing.T) {
reportertest.ResetIngressMetrics()

args := IngressReportArgs{
Namespace: "testns",
Broker: "testbroker",
Expand All @@ -39,7 +73,7 @@ func TestStatsReporter(t *testing.T) {
wantTags := map[string]string{
metricskey.LabelNamespaceName: "testns",
metricskey.LabelBrokerName: "testbroker",
metricskey.LabelEventType: "testeventtype",
metricskey.LabelEventType: "custom",
metricskey.LabelResponseCode: "202",
metricskey.LabelResponseCodeClass: "2xx",
metricskey.ContainerName: "testcontainer",
Expand Down
25 changes: 25 additions & 0 deletions pkg/metrics/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ limitations under the License.
package metrics

import (
"strings"

"go.opencensus.io/tag"
"knative.dev/pkg/metrics/metricskey"
)

const defaultEventType = "custom"

type PodName string
type ContainerName string

Expand All @@ -43,3 +47,24 @@ var (
PodNameKey = tag.MustNewKey(metricskey.PodName)
ContainerNameKey = tag.MustNewKey(metricskey.ContainerName)
)

var (
allowedEventTypes = map[string]struct{} {
"e2e-dummy-event-type": {},
"e2e-testing-resp-event-type-dummy": {},
}
)

// Stackdriver has a limit on the cardinality of fields on a metric, and event types can be any custom string. We're
// reducing the allowable values on this field to be limited to GCP event types and defaulting everything else to
// "custom".
func EventTypeMetricValue(eventType string) string {
if strings.HasPrefix(eventType, "google.cloud") {
return eventType
}
if _, contains := allowedEventTypes[eventType]; contains {
return eventType
}

return defaultEventType
}
52 changes: 52 additions & 0 deletions pkg/metrics/tags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2020 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package metrics

import (
"testing"
)

func TestEventTypeMetricValue(t *testing.T) {
tests := []struct {
event string
want string
}{
// Current Supported GCP event types
{"google.cloud.audit.log.v1.written", "google.cloud.audit.log.v1.written"},
{"google.cloud.pubsub.topic.v1.messagePublished", "google.cloud.pubsub.topic.v1.messagePublished"},
{"google.cloud.scheduler.job.v1.executed", "google.cloud.scheduler.job.v1.executed"},
{"google.cloud.storage.object.v1.archived", "google.cloud.storage.object.v1.archived"},
{"google.cloud.storage.object.v1.deleted", "google.cloud.storage.object.v1.deleted"},
{"google.cloud.storage.object.v1.finalized", "google.cloud.storage.object.v1.finalized"},
{"google.cloud.storage.object.v1.metadataUpdated", "google.cloud.storage.object.v1.metadataUpdated"},

{"google.cloud.some.newly.added.event", "google.cloud.some.newly.added.event"},

{"some.custom.event", "custom"},
{"", "custom"},

// Used in E2E tests
{"e2e-dummy-event-type", "e2e-dummy-event-type"},
{"e2e-testing-resp-event-type-dummy", "e2e-testing-resp-event-type-dummy"},
}

for _, test := range tests {
if got := EventTypeMetricValue(test.event); got != test.want {
t.Errorf("EventTypeMetricValue(%q) = %v; want %v", test.event, got, test.want)
}
}
}