do not record for empty metric config

This commit is contained in:
Yanwei Guo 2020-04-29 13:49:28 -07:00
parent 47137cdc30
commit 539a5e4dbb
6 changed files with 17 additions and 10 deletions

View File

@ -58,7 +58,7 @@ func TestClientMetrics(t *testing.T) {
metrics.Register(cp.NewLatencyMetric(), cp.NewResultMetric())
// Reset the metrics configuration to avoid leaked state from other tests.
setCurMetricsConfig(nil)
setCurMetricsConfig(testMc)
views := cp.DefaultViews()
if got, want := len(views), 2; got != want {

View File

@ -153,7 +153,10 @@ func NewStackdriverClientConfigFromMap(config map[string]string) *StackdriverCli
// record applies the `ros` Options to each measurement in `mss` and then records the resulting
// measurements in the metricsConfig's designated backend.
func (mc *metricsConfig) record(ctx context.Context, mss []stats.Measurement, ros ...stats.Options) error {
if mc == nil || mc.recorder == nil {
if mc == nil {
return nil
}
if mc.recorder == nil {
return stats.RecordWithOptions(ctx, append(ros, stats.WithMeasurements(mss...))...)
}
return mc.recorder(ctx, mss, ros...)

View File

@ -49,6 +49,13 @@ const (
)
var (
// A metric config used for unit tests only.
testMc = &metricsConfig{
backendDestination: Prometheus,
component: "test",
domain: "test",
}
errorTests = []struct {
name string
ops ExporterOptions

View File

@ -36,7 +36,7 @@ func TestMemStatsMetrics(t *testing.T) {
msp.Start(ctx, period)
// Reset the metrics configuration to avoid leaked state from other tests.
setCurMetricsConfig(nil)
setCurMetricsConfig(testMc)
views := msp.DefaultViews()
if got, want := len(views), 27; got != want {

View File

@ -58,9 +58,6 @@ func TestRecordServing(t *testing.T) {
stackdriverMetricTypePrefix: "knative.dev/unsupported",
},
measurement: measure.M(3),
}, {
name: "empty metricsConfig",
measurement: measure.M(4),
}}
testRecord(t, measure, shouldReportCases)
}
@ -87,9 +84,6 @@ func TestRecordEventing(t *testing.T) {
stackdriverMetricTypePrefix: "knative.dev/unsupported",
},
measurement: measure.M(3),
}, {
name: "empty metricsConfig",
measurement: measure.M(4),
}}
testRecord(t, measure, shouldReportCases)
}
@ -149,6 +143,9 @@ func testRecord(t *testing.T, measure *stats.Int64Measure, shouldReportCases []c
},
},
measurement: measure.M(5),
}, {
name: "empty metricsConfig",
measurement: measure.M(4),
}}
for _, test := range shouldNotReportCases {

View File

@ -48,7 +48,7 @@ func TestWorkqueueMetrics(t *testing.T) {
workqueue.SetProvider(wp)
// Reset the metrics configuration to avoid leaked state from other tests.
setCurMetricsConfig(nil)
setCurMetricsConfig(testMc)
views := wp.DefaultViews()
if got, want := len(views), 7; got != want {