-
Notifications
You must be signed in to change notification settings - Fork 329
Added test exporter for use in unit tests. #1185
Conversation
With this exporter one can write unit tests to verify that the instrumentation is working. See the included code example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor comments.
metrics.ReadAndExport() | ||
metricValue := getCounter(metrics, myMetric.Name(), newMetricKey("label1")) | ||
fmt.Printf("increased by %d\n", metricValue-metricBase) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not simply display the value of Counter?
Counter value 1
Counter value 3
Counter value 6
why do you need metricBase?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
metricBase
is needed to isolate test runs. The metric state is global, so if there are two tests that refer to the same metric, a test that looks for absolute values will fail. I confirmed that by removing metricBase
, then creating a copy of the example. The output was:
% go test -v go.opencensus.io/metric/test
=== RUN ExampleExporter
--- PASS: ExampleExporter (0.00s)
=== RUN ExampleExporter_2
--- FAIL: ExampleExporter_2 (0.00s)
got:
increased by 7
increased by 9
increased by 12
want:
increased by 1
increased by 3
increased by 6
FAIL
FAIL go.opencensus.io/metric/test 0.005s
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just added a new example to cover the metric
package. @rghetia what do you think? It seems less fragile than the existing tests that use something like:
ms := r.Read()
if got, want := ms[0].TimeSeries[0].Points[0].Value.(int64), int64(3); got != want { ... }
With this exporter one can write unit tests to verify that the instrumentation is working. See the included code example.