mirror of https://github.com/knative/pkg.git
adding retry event count metric for source (#2016)
* adding retry event count metric for source Bump a few assorted dependencies to their latest versions (#2013) * Bump a few assorted dependencies to their latest versions * Use new uuid helper * Some more slight adjustments fixing boiler plate and format * updates in format * updates in format * fixing minor errors * Merge branch 'master' of https://github.com/knative/pkg * fixing comment based on comment * Update source/source_stats_reporter.go Co-authored-by: Julian Friedman <julz.friedman@uk.ibm.com> * adding changes based on comments Co-authored-by: Julian Friedman <julz.friedman@uk.ibm.com>
This commit is contained in:
parent
17a36e7661
commit
b5bf07b2ba
|
|
@ -35,6 +35,12 @@ var (
|
||||||
stats.UnitDimensionless,
|
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.
|
// Create the tag keys that will be used to add tags to our measurements.
|
||||||
// Tag keys must conform to the restrictions described in
|
// Tag keys must conform to the restrictions described in
|
||||||
// go.opencensus.io/tag/validate.go. Currently those restrictions are:
|
// go.opencensus.io/tag/validate.go. Currently those restrictions are:
|
||||||
|
|
@ -51,6 +57,7 @@ var (
|
||||||
responseTimeout = tag.MustNewKey(metricskey.LabelResponseTimeout)
|
responseTimeout = tag.MustNewKey(metricskey.LabelResponseTimeout)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ReportArgs defines the arguments for reporting metrics.
|
||||||
type ReportArgs struct {
|
type ReportArgs struct {
|
||||||
Namespace string
|
Namespace string
|
||||||
EventType string
|
EventType string
|
||||||
|
|
@ -69,6 +76,7 @@ func init() {
|
||||||
type StatsReporter interface {
|
type StatsReporter interface {
|
||||||
// ReportEventCount captures the event count. It records one per call.
|
// ReportEventCount captures the event count. It records one per call.
|
||||||
ReportEventCount(args *ReportArgs, responseCode int) error
|
ReportEventCount(args *ReportArgs, responseCode int) error
|
||||||
|
ReportRetryEventCount(args *ReportArgs, responseCode int) error
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ StatsReporter = (*reporter)(nil)
|
var _ StatsReporter = (*reporter)(nil)
|
||||||
|
|
@ -98,6 +106,15 @@ func (r *reporter) ReportEventCount(args *ReportArgs, responseCode int) error {
|
||||||
return nil
|
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) {
|
func (r *reporter) generateTag(args *ReportArgs, responseCode int) (context.Context, error) {
|
||||||
return tag.New(
|
return tag.New(
|
||||||
r.ctx,
|
r.ctx,
|
||||||
|
|
@ -132,6 +149,12 @@ func register() {
|
||||||
Aggregation: view.Count(),
|
Aggregation: view.Count(),
|
||||||
TagKeys: tagKeys,
|
TagKeys: tagKeys,
|
||||||
},
|
},
|
||||||
|
&view.View{
|
||||||
|
Description: retryEventCountM.Description(),
|
||||||
|
Measure: retryEventCountM,
|
||||||
|
Aggregation: view.Count(),
|
||||||
|
TagKeys: tagKeys,
|
||||||
|
},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,14 +51,31 @@ func TestStatsReporter(t *testing.T) {
|
||||||
metricskey.LabelResponseCodeClass: "2xx",
|
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 {
|
expectSuccess(t, func() error {
|
||||||
return r.ReportEventCount(args, http.StatusAccepted)
|
return r.ReportEventCount(args, http.StatusAccepted)
|
||||||
})
|
})
|
||||||
expectSuccess(t, func() error {
|
expectSuccess(t, func() error {
|
||||||
return r.ReportEventCount(args, http.StatusAccepted)
|
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, "event_count", wantTags, 2)
|
||||||
|
metricstest.CheckCountData(t, "retry_event_count", retryWantTags, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func expectSuccess(t *testing.T, f func() error) {
|
func expectSuccess(t *testing.T, f func() error) {
|
||||||
|
|
@ -75,5 +92,6 @@ func setup() {
|
||||||
func resetMetrics() {
|
func resetMetrics() {
|
||||||
// OpenCensus metrics carry global state that need to be reset between unit tests.
|
// OpenCensus metrics carry global state that need to be reset between unit tests.
|
||||||
metricstest.Unregister("event_count")
|
metricstest.Unregister("event_count")
|
||||||
|
metricstest.Unregister("retry_event_count")
|
||||||
register()
|
register()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue