Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add opencensus tracing/stats support #130

Merged
merged 2 commits into from
Jun 8, 2020

Conversation

suraj-qlogic
Copy link
Contributor

Fixes #109

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label May 19, 2020
@codecov
Copy link

codecov bot commented May 19, 2020

Codecov Report

Merging #130 into master will decrease coverage by 1.12%.
The diff coverage is 0.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #130      +/-   ##
============================================
- Coverage     58.30%   57.17%   -1.13%     
  Complexity      384      384              
============================================
  Files            47       48       +1     
  Lines          2331     2377      +46     
  Branches        261      264       +3     
============================================
  Hits           1359     1359              
- Misses          865      911      +46     
  Partials        107      107              
Impacted Files Coverage Δ Complexity Δ
...java/com/google/cloud/datastore/DatastoreImpl.java 0.00% <0.00%> (ø) 0.00 <0.00> (ø)
...ain/java/com/google/cloud/datastore/TraceUtil.java 0.00% <0.00%> (ø) 0.00 <0.00> (?)
...oogle/cloud/datastore/spi/v1/HttpDatastoreRpc.java 0.00% <0.00%> (ø) 0.00 <0.00> (ø)

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 d572efb...9d85b03. Read the comment docs.

@skuruppu
Copy link

@mayurkale22, would you be able to take a quick look at this PR to see if OpenCensus is being used correctly? Thanks heaps.

pom.xml Outdated
@@ -162,6 +162,7 @@
<guava.version>29.0-android</guava.version>
<threeten.version>1.4.4</threeten.version>
<javax.annotations.version>1.3.2</javax.annotations.version>
<opencensus.version>0.24.0</opencensus.version>
Copy link
Member

Choose a reason for hiding this comment

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

I would use latest version: 0.26.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Recently google-cloud-shared-dependencies was added in this client which is pull latest version automatically.

@@ -546,6 +593,8 @@ ByteString requestTransactionId(

com.google.datastore.v1.BeginTransactionResponse beginTransaction(
final com.google.datastore.v1.BeginTransactionRequest requestPb) {
Span span = traceUtil.startSpan(TraceUtil.SPAN_NAME_BEGINTRANSACTION);
Scope scope = traceUtil.getTracer().withSpan(span);
Copy link
Member

Choose a reason for hiding this comment

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

Personally, I prefer to use try-with-resources for scope. It implements AutoClosable, so it'll be closed when the try block ends. Ex:

try (Scope s = tracer.withSpan(span)) {
 ... rest of the logic
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


DatastoreImpl(DatastoreOptions options) {
super(options);
this.datastoreRpc = options.getDatastoreRpcV1();
retrySettings =
MoreObjects.firstNonNull(options.getRetrySettings(), ServiceOptions.getNoRetrySettings());
traceUtil = TraceUtil.getInstance();
Copy link
Contributor

Choose a reason for hiding this comment

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

just set this in the declaration on line 57

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -131,29 +136,41 @@ public T call() throws DatastoreException {
@Override
public <T> T runInTransaction(final TransactionCallable<T> callable) {
final DatastoreImpl self = this;
Copy link
Contributor

Choose a reason for hiding this comment

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

this self variable seems pointless

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

throw DatastoreException.translateAndThrow(e);
} finally {
scope.close();
span.end(TraceUtil.END_SPAN_OPTIONS);
}
}

@Override
public <T> T runInTransaction(
final TransactionCallable<T> callable, TransactionOptions transactionOptions) {
final DatastoreImpl self = this;
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

}
}

@Override
public <T> T runInTransaction(
final TransactionCallable<T> callable, TransactionOptions transactionOptions) {
final DatastoreImpl self = this;
Span span = traceUtil.startSpan(TraceUtil.SPAN_NAME_TRANSACTION);
Scope scope = traceUtil.getTracer().withSpan(span);
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use try with resources for the scope

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -173,6 +190,8 @@ public T call() throws DatastoreException {

com.google.datastore.v1.RunQueryResponse runQuery(
final com.google.datastore.v1.RunQueryRequest requestPb) {
Span span = traceUtil.startSpan(TraceUtil.SPAN_NAME_RUNQUERY);
Scope scope = traceUtil.getTracer().withSpan(span);
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use try with resources for the scope (unless the order of closing the span would be wrong?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

* Helper class for tracing utility. It is used for instrumenting {@link HttpDatastoreRpc} with Open
* Census APIs.
*
* <p>TraceUtil Instance are created by the {@link TraceUtil#getInstance()} method.
Copy link
Contributor

Choose a reason for hiding this comment

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

instances

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

*/
public class TraceUtil {
private final Tracer tracer = Tracing.getTracer();
private static TraceUtil traceUtil;
Copy link
Contributor

Choose a reason for hiding this comment

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

final

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

*
* @return An instance of {@link TraceUtil}
*/
public static TraceUtil getInstance() {
Copy link
Contributor

Choose a reason for hiding this comment

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

no need, just create a TraceUtil on the field

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

// Open Census initialization
CensusHttpModule censusHttpModule =
new CensusHttpModule(TraceUtil.getInstance().getTracer(), true);
delegate = censusHttpModule.getHttpRequestInitializer(delegate);
Copy link
Contributor

Choose a reason for hiding this comment

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

try to avoid reassigning local variables

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@suraj-qlogic suraj-qlogic requested a review from elharo May 21, 2020 05:37
@suraj-qlogic
Copy link
Contributor Author

@mayurkale22 PTAL

Copy link
Member

@mayurkale22 mayurkale22 left a comment

Choose a reason for hiding this comment

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

Added one comment, overall LGTM.

@@ -397,7 +416,10 @@ protected Entity computeNext() {
EXCEPTION_HANDLER,
getOptions().getClock());
} catch (RetryHelperException e) {
span.setStatus(Status.UNKNOWN.withDescription(e.getMessage()));
Copy link
Member

Choose a reason for hiding this comment

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

Any specific reason for Status.UNKNOWN instead of status.INTERNAL? Generally, an example of (UNKNOWN) where this error may be returned is if a Status value received from another address space belongs to an error-space that is not known in this address space. On the other side, Internal errors mean some invariants expected by underlying system has been broken.

Also, DEADLINE_EXCEEDED can be useful in case of retries exceeded.

Having said that, I am not familiar with datastore codebase. Feel free to ignore if you disagree.

@suraj-qlogic
Copy link
Contributor Author

@elharo PTAL

@suraj-qlogic
Copy link
Contributor Author

@elharo gentle ping

@suraj-qlogic
Copy link
Contributor Author

@BenWhitehead @elharo PTAL

@BenWhitehead BenWhitehead merged commit 5aee52f into googleapis:master Jun 8, 2020
gcf-merge-on-green bot pushed a commit that referenced this pull request Jun 17, 2020
🤖 I have created a release \*beep\* \*boop\* 
---
## [1.104.0](https://www.github.com/googleapis/java-datastore/compare/v1.103.0...v1.104.0) (2020-06-17)


### Features

* add opencensus tracing/stats support for Datastore RPC operations ([#130](https://www.github.com/googleapis/java-datastore/issues/130)) ([5aee52f](https://www.github.com/googleapis/java-datastore/commit/5aee52f5013d6780e523e0c6d7d00a1826b83b9b))
* add support to customize gcloud command of LocalDatastoreHelper ([#137](https://www.github.com/googleapis/java-datastore/issues/137)) ([976d979](https://www.github.com/googleapis/java-datastore/commit/976d9791572117dc703d8d7d6963bdd6603ecd63))


### Bug Fixes

* fix version number in changelog to correctly reflect what was released to maven central ([#145](https://www.github.com/googleapis/java-datastore/issues/145)) ([c509a2a](https://www.github.com/googleapis/java-datastore/commit/c509a2a4229f864edef8681677d73f3c7be1101f))


### Dependencies

* update dependency com.google.cloud:google-cloud-shared-dependencies to v0.8.0 ([#161](https://www.github.com/googleapis/java-datastore/issues/161)) ([39c8d72](https://www.github.com/googleapis/java-datastore/commit/39c8d723b318d08ca494b71167eaa80b1df6423d))
* update dependency com.google.cloud:google-cloud-shared-dependencies to v0.8.1 ([#163](https://www.github.com/googleapis/java-datastore/issues/163)) ([7bfa07e](https://www.github.com/googleapis/java-datastore/commit/7bfa07eb3a7cf84fcf3e19f6a33914162fa28499))
---


This PR was generated with [Release Please](https://github.com/googleapis/release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add OpenCensus tracing/stats support
6 participants