diff --git a/source/source_stats_reporter.go b/source/source_stats_reporter.go index b6d1ca93b..f343d9d75 100644 --- a/source/source_stats_reporter.go +++ b/source/source_stats_reporter.go @@ -35,6 +35,12 @@ var ( stats.UnitDimensionless, ) + // retryEventCountM is a counter which records the number of events sent by the source in retries. + retryEventCountM = stats.Int64( + "retry_event_count", + "Number of retry events sent", + stats.UnitDimensionless, + ) // Create the tag keys that will be used to add tags to our measurements. // Tag keys must conform to the restrictions described in // go.opencensus.io/tag/validate.go. Currently those restrictions are: @@ -51,6 +57,7 @@ var ( responseTimeout = tag.MustNewKey(metricskey.LabelResponseTimeout) ) +// ReportArgs defines the arguments for reporting metrics. type ReportArgs struct { Namespace string EventType string @@ -69,6 +76,7 @@ func init() { type StatsReporter interface { // ReportEventCount captures the event count. It records one per call. ReportEventCount(args *ReportArgs, responseCode int) error + ReportRetryEventCount(args *ReportArgs, responseCode int) error } var _ StatsReporter = (*reporter)(nil) @@ -98,6 +106,15 @@ func (r *reporter) ReportEventCount(args *ReportArgs, responseCode int) error { return nil } +func (r *reporter) ReportRetryEventCount(args *ReportArgs, responseCode int) error { + ctx, err := r.generateTag(args, responseCode) + if err != nil { + return err + } + metrics.Record(ctx, retryEventCountM.M(1)) + return nil +} + func (r *reporter) generateTag(args *ReportArgs, responseCode int) (context.Context, error) { return tag.New( r.ctx, @@ -132,6 +149,12 @@ func register() { Aggregation: view.Count(), TagKeys: tagKeys, }, + &view.View{ + Description: retryEventCountM.Description(), + Measure: retryEventCountM, + Aggregation: view.Count(), + TagKeys: tagKeys, + }, ); err != nil { panic(err) } diff --git a/source/source_stats_reporter_test.go b/source/source_stats_reporter_test.go index 4c7dedc24..bfeeb4918 100644 --- a/source/source_stats_reporter_test.go +++ b/source/source_stats_reporter_test.go @@ -51,14 +51,31 @@ func TestStatsReporter(t *testing.T) { metricskey.LabelResponseCodeClass: "2xx", } - // test ReportEventCount + retryWantTags := map[string]string{ + metricskey.LabelNamespaceName: "testns", + metricskey.LabelEventType: "dev.knative.event", + metricskey.LabelEventSource: "unit-test", + metricskey.LabelName: "testsource", + metricskey.LabelResourceGroup: "testresourcegroup", + metricskey.LabelResponseCode: "503", + metricskey.LabelResponseCodeClass: "5xx", + } + + // test ReportEventCount and ReportRetryEventCount expectSuccess(t, func() error { return r.ReportEventCount(args, http.StatusAccepted) }) expectSuccess(t, func() error { return r.ReportEventCount(args, http.StatusAccepted) }) + expectSuccess(t, func() error { + return r.ReportRetryEventCount(args, http.StatusServiceUnavailable) + }) + expectSuccess(t, func() error { + return r.ReportRetryEventCount(args, http.StatusServiceUnavailable) + }) metricstest.CheckCountData(t, "event_count", wantTags, 2) + metricstest.CheckCountData(t, "retry_event_count", retryWantTags, 2) } func expectSuccess(t *testing.T, f func() error) { @@ -75,5 +92,6 @@ func setup() { func resetMetrics() { // OpenCensus metrics carry global state that need to be reset between unit tests. metricstest.Unregister("event_count") + metricstest.Unregister("retry_event_count") register() }