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 library instrumentation for Apache HTTPClient 4.3 #3623

Merged
merged 10 commits into from
Jul 28, 2021

Conversation

anuraaga
Copy link
Contributor

I referred to both opentracing and zipkin to help with corner cases but mostly wrote from scratch. Both of them have and lack features vs the other one with regards to apache.

For now I have copied much from 4.0 javaagent instrumentation, in a future PR will try to share in a common library.

@anuraaga anuraaga marked this pull request as draft July 19, 2021 08:42
@anuraaga anuraaga marked this pull request as ready for review July 20, 2021 04:31
…y/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v4_3/ApacheHttpClientRequest.java

Co-authored-by: Lauri Tulmin <[email protected]>
error = e;
throw e;
} finally {
if (!pendingRedirect(context, httpContext, request, instrumenterRequest, response)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It is unfortunate that we can't wrap around redirect and automatic retry. Perhaps we should just let this instrumentation create multiple spans when there is a redirect? If I understand correctly if retry is enable then we only record the first failing request even when retry succeeds?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nah, the normal redirect tests in HttpClientTest are passing so we're recording it as a success. And the timing is around all the redirects since we end the span after the last one.

The code is sort of complex but I think the modeling is consistent with our other instrumentation so guess it's not too bad.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah redirects look ok. There is also an option to retry request when it fails, imo it would need similar handling as redirects to make sure that we don't end span on the first attempt. We don't have a test for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@laurit We discussed a bit and figure it's good to try this code first and iterate later, since we don't have any tests for retries yet. We'd probably need to check everything in the future more broadly

Copy link
Contributor

Choose a reason for hiding this comment

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

I just wanted to point out that with library instrumentation we have limited options to force the framework to do things as we like so we could instead create spans in a way that fits best with the integration points the framework provides us. This way there should be less corner cases we have to worry about. Sure it will be bad that different library instrumentations don't behave in a consistent way and even worse would be testing them, but instrumentation would be simpler and the chance of something breaking because http client is configured slightly differently or customized a bit should be smaller.

* items. The {@link AttributesExtractor} will be executed after all default extractors.
*/
public ApacheHttpClientTracingBuilder addAttributeExtractor(
AttributesExtractor<? super ApacheHttpClientRequest, ? super HttpResponse>
Copy link
Member

Choose a reason for hiding this comment

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

Should ApacheHttpClientRequest be public? We're exposing a possibility to add a custom attributes extractor, but you can't really use request in it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@trask This is an interesting case for wanting to go back to the HttpUriRequest interface perhaps :-)

For now I just made public with a single method returning the delegate.

Copy link
Member

Choose a reason for hiding this comment

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

i'm not sure wrangling into HttpUriRequest is ideal for custom attribute extractors either, I think ideally they would want access to HttpHost + HttpRequest in the case where an HttpHost + HttpRequest overload is used

Comment on lines 7 to 10
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

HttpRequestWrapper request,
ApacheHttpClientRequest instrumenterRequest,
@Nullable CloseableHttpResponse response)
throws ProtocolException {
Copy link
Member

Choose a reason for hiding this comment

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

This method can throw and is called in a finally block - would it make sense to catch whatever it throws and addSuppressed() to error? Or is it a case when you have to declare throws but nothing should ever fail here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for noticing! Tightened up the below catch to prevent it from leaking, which would be bad for many reasons not just being in a finally (well technically being in the finally would save the app from crashing maybe :)

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.

4 participants