Stackdriver Trace Exporter: add resource labels to Spans.#1243
Stackdriver Trace Exporter: add resource labels to Spans.#1243songy23 merged 7 commits intocensus-instrumentation:masterfrom
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1243 +/- ##
============================================
- Coverage 82.06% 81.72% -0.34%
- Complexity 1241 1249 +8
============================================
Files 193 193
Lines 6044 6097 +53
Branches 560 562 +2
============================================
+ Hits 4960 4983 +23
- Misses 935 962 +27
- Partials 149 152 +3
Continue to review full report at Codecov.
|
| compileOnly libraries.auto_value | ||
|
|
||
| compile project(':opencensus-api'), | ||
| project(':opencensus-contrib-monitored-resource-util'), |
There was a problem hiding this comment.
Is there any issue with adding a dependency from an exporter to a package under contrib/ (for example, if the contrib packages are less stable)?
/cc @bogdandrutu
There was a problem hiding this comment.
I cannot think of any particular issue. Stackdriver Stats exporter also depends on this artifact, and I think we need to maintain the same backwards compatibility for contrib/ artifacts to apis.
| return attributesBuilder; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") |
There was a problem hiding this comment.
Why do we need to suppress warnings in this method? Is it possible to suppress them in a smaller scope?
There was a problem hiding this comment.
Because of the type casting. I moved it to the casting statement instead.
There was a problem hiding this comment.
I'm not sure I understand why the @SuppressWarnings is needed on those lines. They are casting to non-generic types, so the casts are checked.
There was a problem hiding this comment.
You're right, removed.
| (AwsEc2InstanceMonitoredResource) resource; | ||
| attributesBuilder.putAttributeMap( | ||
| createResourceLabelKey(resourceType, "aws_account"), | ||
| toStringAttributeValueProto(awsEc2InstanceMonitoredResource.getAccount())); |
There was a problem hiding this comment.
attributesBuilder.putAttributeMap( createResourceLabelKey(resourceType, "aws_account"), toStringAttributeValueProto(awsEc2InstanceMonitoredResource.getAccount()));
Suggestion: The code that adds an attribute to the builder could probably be factored out into a helper method.
| return; | ||
| } | ||
| ResourceType resourceType = StackdriverV2ExporterHandler.RESOURCE.getResourceType(); | ||
| switch (resourceType) { |
There was a problem hiding this comment.
It looks like it is possible for this method to not test the logic of putResourceLabels if the ResourceType isn't detected. Is there a way to refactor the code so that the ResourceType is passed in to generateSpan, or putResourceLabels is tested directly? Then we could easily test the code for all three cases, independent of the environment.
There was a problem hiding this comment.
Done. The constructor of MonitoredResource is not public so I used some mocks here.
| String attributeName, | ||
| String attributeValue) { | ||
| attributesBuilder.putAttributeMap( | ||
| createResourceLabelKey(resourceType, attributeName), |
There was a problem hiding this comment.
We should cache all of these work.
private static final java.util.Map<java.lang.String, com.google.devtools.cloudtrace.v2.AttributeValue> GCE_INSTANCE_LABELS;
attributesBuilder.putAllAttributeMap(GCE_INSTANCE_LABELS);
There was a problem hiding this comment.
Make sense to me, though making static map makes it difficult to test.
If we really want to test the resource labels under all 3 cases, we may need to use something like PowerMock to mock MonitoredResourceUtils.getDefaultResource(). @sebright any idea on this?
There was a problem hiding this comment.
If it is better to cache the whole map, couldn't we just pass RESOURCE_LABELS into generateSpan, instead of passing in RESOURCE?
There was a problem hiding this comment.
Ahh you're right, updated. I also left a TODO to test the parsing.
73c9f5e to
cd76f7e
Compare
| return attributesBuilder; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") |
There was a problem hiding this comment.
I'm not sure I understand why the @SuppressWarnings is needed on those lines. They are casting to non-generic types, so the casts are checked.
| Map<String, AttributeValue> attributeMap = span.getAttributes().getAttributeMapMap(); | ||
| assertThat(attributeMap).containsKey(createResourceLabelKey(AWS_EC2_INSTANCE, "aws_account")); | ||
| assertThat(attributeMap).containsKey(createResourceLabelKey(AWS_EC2_INSTANCE, "instance_id")); | ||
| assertThat(attributeMap).containsKey(createResourceLabelKey(AWS_EC2_INSTANCE, "region")); |
There was a problem hiding this comment.
Should the three new tests also check the values associated with the resource label keys?
| String attributeName, | ||
| String attributeValue) { | ||
| attributesBuilder.putAttributeMap( | ||
| createResourceLabelKey(resourceType, attributeName), |
There was a problem hiding this comment.
If it is better to cache the whole map, couldn't we just pass RESOURCE_LABELS into generateSpan, instead of passing in RESOURCE?
cd76f7e to
64abe59
Compare
| } | ||
|
|
||
| @Test | ||
| public void generateSpan_WithResourceLabels() { |
There was a problem hiding this comment.
This change looks good, though I think we should also test the other two ResourceTypes, as in 57765e8.
There was a problem hiding this comment.
Done. All these tests tested the same code path though, I'll update this to test parsing MonitoredResource instead, after #1250 is merged.
Resource labels are consistent with Specs. The format of label key is
g.co/r/<resource_type>/<label>, for exampleg.co/r/aws_ec2_instance/aws_account.