-
Notifications
You must be signed in to change notification settings - Fork 896
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
fix NPE for commons-httpclient v3.1 (#5944) #5949
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...ient/apache-httpclient-2.0/javaagent/src/test/groovy/AbstractCommonsHttpClientTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import io.opentelemetry.api.common.AttributeKey | ||
import io.opentelemetry.instrumentation.test.AgentTestTrait | ||
import io.opentelemetry.instrumentation.test.base.HttpClientTest | ||
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes | ||
import org.apache.commons.httpclient.HttpClient | ||
import org.apache.commons.httpclient.HttpConnectionManager | ||
import org.apache.commons.httpclient.HttpMethod | ||
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager | ||
import org.apache.commons.httpclient.methods.DeleteMethod | ||
import org.apache.commons.httpclient.methods.GetMethod | ||
import org.apache.commons.httpclient.methods.HeadMethod | ||
import org.apache.commons.httpclient.methods.OptionsMethod | ||
import org.apache.commons.httpclient.methods.PostMethod | ||
import org.apache.commons.httpclient.methods.PutMethod | ||
import org.apache.commons.httpclient.methods.TraceMethod | ||
import spock.lang.Shared | ||
|
||
abstract class AbstractCommonsHttpClientTest extends HttpClientTest<HttpMethod> implements AgentTestTrait { | ||
@Shared | ||
HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager() | ||
@Shared | ||
HttpClient client = buildClient(false) | ||
@Shared | ||
HttpClient clientWithReadTimeout = buildClient(true) | ||
|
||
def buildClient(boolean readTimeout) { | ||
HttpClient client = new HttpClient(connectionManager) | ||
client.setConnectionTimeout(CONNECT_TIMEOUT_MS) | ||
if (readTimeout) { | ||
client.setTimeout(READ_TIMEOUT_MS) | ||
} | ||
return client | ||
} | ||
|
||
HttpClient getClient(URI uri) { | ||
if (uri.toString().contains("/read-timeout")) { | ||
return clientWithReadTimeout | ||
} | ||
return client | ||
} | ||
|
||
@Override | ||
HttpMethod buildRequest(String method, URI uri, Map<String, String> headers) { | ||
def request | ||
switch (method) { | ||
case "GET": | ||
request = new GetMethod(uri.toString()) | ||
break | ||
case "PUT": | ||
request = new PutMethod(uri.toString()) | ||
break | ||
case "POST": | ||
request = new PostMethod(uri.toString()) | ||
break | ||
case "HEAD": | ||
request = new HeadMethod(uri.toString()) | ||
break | ||
case "DELETE": | ||
request = new DeleteMethod(uri.toString()) | ||
break | ||
case "OPTIONS": | ||
request = new OptionsMethod(uri.toString()) | ||
break | ||
case "TRACE": | ||
request = new TraceMethod(uri.toString()) | ||
break | ||
default: | ||
throw new IllegalStateException("Unsupported method: " + method) | ||
} | ||
headers.each { request.setRequestHeader(it.key, it.value) } | ||
return request | ||
} | ||
|
||
@Override | ||
boolean testCircularRedirects() { | ||
false | ||
} | ||
|
||
@Override | ||
boolean testReusedRequest() { | ||
// apache commons throws an exception if the request is reused without being recycled first | ||
// at which point this test is not useful (and requires re-populating uri) | ||
false | ||
} | ||
|
||
@Override | ||
boolean testCallback() { | ||
false | ||
} | ||
|
||
@Override | ||
boolean testReadTimeout() { | ||
true | ||
} | ||
|
||
@Override | ||
Set<AttributeKey<?>> httpAttributes(URI uri) { | ||
Set<AttributeKey<?>> extra = [ | ||
SemanticAttributes.HTTP_SCHEME, | ||
SemanticAttributes.HTTP_TARGET | ||
] | ||
super.httpAttributes(uri) + extra | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...nt/apache-httpclient-2.0/javaagent/src/test/groovy/CommonsHttpClientLatestDepsTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import org.apache.commons.httpclient.HttpMethod | ||
import spock.lang.IgnoreIf | ||
|
||
//this test will be ignored if not executed with -PtestLatestDeps=true | ||
//because the latest dependency commons-httpclient v3.1 allows a call to the executeMethod | ||
//with some null parameters like HttpClient.executeMethod(null, request, null) | ||
//but this construct is not allowed in commons-httpclient v2 that is used for regular otel testing | ||
@IgnoreIf({ !Boolean.getBoolean("testLatestDeps") }) | ||
class CommonsHttpClientLatestDepsTest extends AbstractCommonsHttpClientTest { | ||
|
||
@Override | ||
int sendRequest(HttpMethod request, String method, URI uri, Map<String, String> headers) { | ||
try { | ||
getClient(uri).executeMethod(null, request, null) | ||
return request.getStatusCode() | ||
} finally { | ||
request.releaseConnection() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WDYT about extracting a common base class for this class and
CommonsHttpClientTest
. I believe this is the only method that is different in them.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
of course this makes sense.
I hope the naming will be fine, otherwise just let me know.
Thank you for reviewing this.