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

Add utility executors that can be used with asynchronous frameworks t… #150

Merged
merged 6 commits into from
May 22, 2020

Conversation

anuraaga
Copy link
Contributor

…o propagate segment context.

I noticed that we don't have any convenience executors for context propagation across threads. While they are not the only way, context propagating executors tend to be a good low level primitive for context propagation (works decently with CompletableFuture, RxJava, etc).

I may move some methods around later but want to get this in as a start.

I also added a loose validation utility to allow us to validate arguments with optional errors.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copy link
Contributor

@willarmiros willarmiros left a comment

Choose a reason for hiding this comment

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

Really great feature idea! We'll have to update these docs for the next release to include this as an option.


/**
* {@link Executor}s that will mount a segment before running a command. When switching threads, for example when instrumenting an
* asynchronous appilcation, it is recommended to use one of these {@link Executor}s to make sure callbacks have the trace segment
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: typo in application

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haven't used Mac in a while and now I'm back to hating this keyboard...

}

private static ValidationMode validationMode() {
String config = System.getProperty("com.amazonaws.xray.validationMode", "").toLowerCase();
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious, why not use ValidationMode.valueOf(config) instead of the switch statement?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No real reason, switched to using it. After switching, realized the straightforwardness was lost and wrote unit tests instead so not sure which is better - oh well :)

private static final ValidationMode VALIDATION_MODE = validationMode();

/**
* Returhs whether {@code obj} is {@code null}.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: typo

recorder.setTraceEntity(segment);
try {
command.run();
} finally {
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be cool if the executor caught any throwables and added them to the segment with segment.addException(throwable) before rethrowing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm wary of this since it could result in double logging I think. For example, if it was a spring app I'd expect a spring exception handler to be listening to unhandled exceptions and adding to the segment appropriately. That being said, maybe we don't have any instrumentation that does this. Let me know if you still think this is a good place for such handling.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's a fair point. I was thinking about how we add the exception in function capturing (sub)segments here, but we also create the (sub)segments in those cases rather than using the current one in the context. Since using the segment in the context could risk double adding exceptions, e.g. for Spring segments like you mentioned, I think it's ok as is.

@anuraaga anuraaga merged commit 896967a into aws:master May 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants