Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Stackdriver Trace Exporter: add resource labels to Spans.#1243

Merged
songy23 merged 7 commits intocensus-instrumentation:masterfrom
songy23:sd-trace-resources
Jun 12, 2018
Merged

Stackdriver Trace Exporter: add resource labels to Spans.#1243
songy23 merged 7 commits intocensus-instrumentation:masterfrom
songy23:sd-trace-resources

Conversation

@songy23
Copy link
Copy Markdown
Contributor

@songy23 songy23 commented Jun 7, 2018

Resource labels are consistent with Specs. The format of label key is g.co/r/<resource_type>/<label>, for example g.co/r/aws_ec2_instance/aws_account.

@codecov-io
Copy link
Copy Markdown

codecov-io commented Jun 7, 2018

Codecov Report

Merging #1243 into master will decrease coverage by 0.33%.
The diff coverage is 44.64%.

Impacted file tree graph

@@             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
Impacted Files Coverage Δ Complexity Δ
...race/stackdriver/StackdriverV2ExporterHandler.java 79.32% <44.64%> (-12.29%) 40 <7> (+8)
...rter/stats/stackdriver/StackdriverExportUtils.java 81.53% <0%> (ø) 29% <0%> (ø) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a6e0485...f1e2bdb. Read the comment docs.

compileOnly libraries.auto_value

compile project(':opencensus-api'),
project(':opencensus-contrib-monitored-resource-util'),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to suppress warnings in this method? Is it possible to suppress them in a smaller scope?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the type casting. I moved it to the casting statement instead.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, removed.

(AwsEc2InstanceMonitoredResource) resource;
attributesBuilder.putAttributeMap(
createResourceLabelKey(resourceType, "aws_account"),
toStringAttributeValueProto(awsEc2InstanceMonitoredResource.getAccount()));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, done.

return;
}
ResourceType resourceType = StackdriverV2ExporterHandler.RESOURCE.getResourceType();
switch (resourceType) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The constructor of MonitoredResource is not public so I used some mocks here.

@sebright2 sebright2 assigned songy23 and unassigned sebright2 Jun 8, 2018
@songy23 songy23 assigned sebright2 and unassigned songy23 Jun 8, 2018
String attributeName,
String attributeValue) {
attributesBuilder.putAttributeMap(
createResourceLabelKey(resourceType, attributeName),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is better to cache the whole map, couldn't we just pass RESOURCE_LABELS into generateSpan, instead of passing in RESOURCE?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh you're right, updated. I also left a TODO to test the parsing.

@songy23 songy23 force-pushed the sd-trace-resources branch from 73c9f5e to cd76f7e Compare June 8, 2018 23:55
@songy23 songy23 requested review from bogdandrutu and removed request for bogdandrutu June 11, 2018 16:42
return attributesBuilder;
}

@SuppressWarnings("unchecked")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the three new tests also check the values associated with the resource label keys?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, done.

String attributeName,
String attributeValue) {
attributesBuilder.putAttributeMap(
createResourceLabelKey(resourceType, attributeName),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is better to cache the whole map, couldn't we just pass RESOURCE_LABELS into generateSpan, instead of passing in RESOURCE?

@sebright2 sebright2 assigned songy23 and unassigned sebright2 Jun 12, 2018
@songy23 songy23 force-pushed the sd-trace-resources branch from cd76f7e to 64abe59 Compare June 12, 2018 01:31
@songy23 songy23 assigned rghetia and sebright2 and unassigned rghetia and songy23 Jun 12, 2018
}

@Test
public void generateSpan_WithResourceLabels() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks good, though I think we should also test the other two ResourceTypes, as in 57765e8.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. All these tests tested the same code path though, I'll update this to test parsing MonitoredResource instead, after #1250 is merged.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants