Stats/Stackdriver: export data using metrics API#1501
Stats/Stackdriver: export data using metrics API#1501mayurkale22 merged 3 commits intocensus-instrumentation:masterfrom mayurkale22:sd_metrics
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1501 +/- ##
============================================
- Coverage 82.6% 82.29% -0.31%
+ Complexity 1648 1628 -20
============================================
Files 251 247 -4
Lines 7782 7576 -206
Branches 743 728 -15
============================================
- Hits 6428 6235 -193
+ Misses 1129 1117 -12
+ Partials 225 224 -1
Continue to review full report at Codecov.
|
| @@ -148,6 +148,7 @@ General guidelines on imports: | |||
| <subpackage name="stats"> | |||
| <allow pkg="io.opencensus.stats"/> | |||
There was a problem hiding this comment.
Are stats and tags still needed?
There was a problem hiding this comment.
Both are still needed for other two exporters (prometheus and signalFx), I will remove that once we update these exporters to use Metrics API.
| // StackDriver. | ||
| static List<TimeSeries> createTimeSeriesList( | ||
| @javax.annotation.Nullable ViewData viewData, | ||
| @javax.annotation.Nullable io.opencensus.metrics.export.Metric metric, |
There was a problem hiding this comment.
The @Nullable annotation is no longer needed.
| return value.match( | ||
| typedValueDoubleFunction, | ||
| typedValueLongFunction, | ||
| new Function<io.opencensus.metrics.export.Distribution, TypedValue>() { |
There was a problem hiding this comment.
You can make this function static too.
...ackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverExportUtils.java
Show resolved
Hide resolved
...ackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverExportUtils.java
Show resolved
Hide resolved
...driver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverExporterWorker.java
Outdated
Show resolved
Hide resolved
| if (existing != null) { | ||
| if (existing.equals(view)) { | ||
| // Ignore views that are already registered. | ||
| boolean registerMetric(Metric metric) { |
There was a problem hiding this comment.
Same, this should be called registerMetricDescriptor.
| if (registerView(view)) { | ||
| // Only upload stats for valid views. | ||
| viewDataList.add(viewManager.getView(view.getName())); | ||
| List</*@Nullable*/ Metric> metricsList = Lists.newArrayList(); |
There was a problem hiding this comment.
@Nullable annotation is no longer needed here, since I don't think metricProducer.getMetrics() would return null.
|
|
||
| List<TimeSeries> timeSeriesList = Lists.newArrayList(); | ||
| for (/*@Nullable*/ ViewData viewData : viewDataList) { | ||
| for (/*@Nullable*/ Metric metric : metricsList) { |
...river/src/test/java/io/opencensus/exporter/stats/stackdriver/StackdriverExportUtilsTest.java
Outdated
Show resolved
Hide resolved
| .setBucketOptions(createBucketOptions(distribution.getBucketOptions())) | ||
| .addAllBucketCounts(createBucketCounts(distribution.getBuckets())) | ||
| .setCount(distribution.getCount()) | ||
| .setMean(distribution.getSum() / distribution.getCount()) |
There was a problem hiding this comment.
We should check for divided-by-0 here.
| for (int i = 0; i < tagValues.size(); i++) { | ||
| TagKey key = columns.get(i); | ||
| TagValue value = tagValues.get(i); | ||
| labelKeys.size() == labelValues.size(), "LabelKeys and LabelValues don't have same size."); |
There was a problem hiding this comment.
I see ambiguity here, if someone using the stats API will get confused by seeing this message. Can we say "TagKeys/LabelKeys and TagValues/LabelValues don't have same size." @bogdandrutu @songy23
There was a problem hiding this comment.
Maybe it's less confusing to just call them "key" and "value"? i.e "Keys and Values don't have same size."
...driver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverExporterWorker.java
Show resolved
Hide resolved
| View.create(VIEW_NAME, VIEW_DESCRIPTION, MEASURE, SUM, Arrays.asList(KEY), CUMULATIVE); | ||
| doReturn(ImmutableSet.of(view)).when(mockViewManager).getAllExportedViews(); | ||
| public void doNotExportIfFailedToRegisterMetric() { | ||
| doReturn(ImmutableSet.of(metricProducer)).when(metricProducerManager).getAllMetricProducer(); |
There was a problem hiding this comment.
nit: This line can be extracted to
@Before
public void setUp() {
...
}
...driver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverExporterWorker.java
Show resolved
Hide resolved
...driver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverExporterWorker.java
Show resolved
Hide resolved
| String type = generateType(metricDescriptor.getName(), domain); | ||
| // Name format refers to | ||
| // cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors/create | ||
| builder.setName(String.format("projects/%s/metricDescriptors/%s", projectId, type)); |
There was a problem hiding this comment.
I know that you did not write this but is probably faster to do string concatenation.
...ackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverExportUtils.java
Show resolved
Hide resolved
...ackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverExportUtils.java
Show resolved
Hide resolved
| TagValue value = tagValues.get(i); | ||
| List<LabelKey> labelKeys = metricDescriptor.getLabelKeys(); | ||
|
|
||
| checkArgument(labelKeys.size() == labelValues.size(), "Keys and Values don't have same size."); |
There was a problem hiding this comment.
We already check for this when we create the Metric so I think this is useless.

Dependency on #1499
This PR is part of #1483