-
Notifications
You must be signed in to change notification settings - Fork 99
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
Replace calls to X-Ray daemon for rules / targets with a simple JDK-b… #145
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ad22c0d
Replace calls to X-Ray daemon for rules / targets with a simple JDK-b…
7d3a134
Formatting
71bf797
Test and better handle failure cases.
f14a788
test scope
a5e2d82
Remove not abrupt main thread tests which don't seem to test anything.
ffc8df9
Clarify comment and add XrayClientException
0dd5f31
Update test
19909dc
Ditto
7a08cbc
Merge branch 'master' into url-connection-xray-client
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
150 changes: 150 additions & 0 deletions
150
aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/internal/UnsignedXrayClient.java
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,150 @@ | ||
package com.amazonaws.xray.internal; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.io.UncheckedIOException; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.ProtocolException; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import com.amazonaws.AmazonWebServiceRequest; | ||
import com.amazonaws.AmazonWebServiceResult; | ||
import com.amazonaws.services.xray.model.GetSamplingRulesRequest; | ||
import com.amazonaws.services.xray.model.GetSamplingRulesResult; | ||
import com.amazonaws.services.xray.model.GetSamplingTargetsRequest; | ||
import com.amazonaws.services.xray.model.GetSamplingTargetsResult; | ||
import com.amazonaws.xray.config.DaemonConfiguration; | ||
import com.fasterxml.jackson.annotation.JsonInclude.Include; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategy; | ||
import com.fasterxml.jackson.databind.introspect.AnnotatedMember; | ||
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector; | ||
|
||
/** | ||
* A simple client for sending API requests via the X-Ray daemon. Requests do not have to be | ||
* signed, so we can avoid having a strict dependency on the full AWS SDK in instrumentation. This | ||
* is an internal utility and not meant to represent the entire X-Ray API nor be particularly | ||
* efficient as we only use it in long poll loops. | ||
*/ | ||
public class UnsignedXrayClient { | ||
|
||
// Visible for testing | ||
static final ObjectMapper OBJECT_MAPPER = new ObjectMapper() | ||
.setSerializationInclusion(Include.NON_EMPTY) | ||
.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE) | ||
.setAnnotationIntrospector(new JacksonAnnotationIntrospector() { | ||
@Override | ||
public boolean hasIgnoreMarker(AnnotatedMember m) { | ||
// This is a somewhat hacky way of having ObjectMapper only serialize the fields in our | ||
// model classes instead of the base class that comes from the SDK. In the future, we will | ||
// remove the SDK dependency itself and the base classes and this hack will go away. | ||
if (m.getDeclaringClass() == AmazonWebServiceRequest.class || | ||
m.getDeclaringClass() == AmazonWebServiceResult.class) { | ||
return true; | ||
} | ||
return super.hasIgnoreMarker(m); | ||
} | ||
}); | ||
private static final int TIME_OUT_MILLIS = 2000; | ||
|
||
private final URL getSamplingRulesEndpoint; | ||
private final URL getSamplingTargetsEndpoint; | ||
|
||
public UnsignedXrayClient() { | ||
this(new DaemonConfiguration().getEndpointForTCPConnection()); | ||
} | ||
|
||
// Visible for testing | ||
UnsignedXrayClient(String endpoint) { | ||
try { | ||
getSamplingRulesEndpoint = new URL(endpoint + "/GetSamplingRules"); | ||
getSamplingTargetsEndpoint = new URL(endpoint + "/GetSamplingTargets"); | ||
} catch (MalformedURLException e) { | ||
throw new IllegalArgumentException("Invalid URL: " + endpoint, e); | ||
} | ||
} | ||
|
||
public GetSamplingRulesResult getSamplingRules(GetSamplingRulesRequest request) { | ||
return sendRequest(getSamplingRulesEndpoint, request, GetSamplingRulesResult.class); | ||
} | ||
|
||
public GetSamplingTargetsResult getSamplingTargets(GetSamplingTargetsRequest request) { | ||
return sendRequest(getSamplingTargetsEndpoint, request, GetSamplingTargetsResult.class); | ||
} | ||
|
||
private <T> T sendRequest(URL endpoint, Object request, Class<T> responseClass) { | ||
final HttpURLConnection connection; | ||
try { | ||
connection = (HttpURLConnection) endpoint.openConnection(); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("Could not connect to endpoint " + endpoint, e); | ||
anuraaga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
connection.setConnectTimeout(TIME_OUT_MILLIS); | ||
connection.setReadTimeout(TIME_OUT_MILLIS); | ||
|
||
try { | ||
connection.setRequestMethod("POST"); | ||
} catch (ProtocolException e) { | ||
throw new IllegalStateException("Invalid protocol, can't happen."); | ||
} | ||
|
||
connection.addRequestProperty("Content-Type", "application/json"); | ||
connection.setDoOutput(true); | ||
|
||
try (OutputStream outputStream = connection.getOutputStream()) { | ||
OBJECT_MAPPER.writeValue(outputStream, request); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("Could not serialize and send request.", e); | ||
} | ||
|
||
final int responseCode; | ||
try { | ||
responseCode = connection.getResponseCode(); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("Could not read response code.", e); | ||
} | ||
|
||
if (responseCode != 200) { | ||
throw new IllegalStateException("Error response from X-Ray: " + | ||
readResponseString(connection)); | ||
} | ||
|
||
try { | ||
return OBJECT_MAPPER.readValue(connection.getInputStream(), responseClass); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("Error reading response.", e); | ||
} | ||
} | ||
|
||
private static String readResponseString(HttpURLConnection connection) { | ||
ByteArrayOutputStream os = new ByteArrayOutputStream(); | ||
try (InputStream is = connection.getInputStream()) { | ||
readTo(is, os); | ||
} catch (IOException e) { | ||
// Only best effort read if we can. | ||
} | ||
try (InputStream is = connection.getErrorStream()) { | ||
readTo(is, os); | ||
} catch (IOException e) { | ||
// Only best effort read if we can. | ||
} | ||
try { | ||
return os.toString(StandardCharsets.UTF_8.name()); | ||
} catch (UnsupportedEncodingException e) { | ||
throw new IllegalStateException("UTF-8 not supported can't happen."); | ||
} | ||
} | ||
|
||
private static void readTo(InputStream is, ByteArrayOutputStream os) throws IOException { | ||
int b; | ||
while ((b = is.read()) != -1) { | ||
os.write(b); | ||
} | ||
} | ||
} |
44 changes: 22 additions & 22 deletions
44
...-core/src/main/java/com/amazonaws/xray/strategy/sampling/CentralizedSamplingStrategy.java
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
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
Oops, something went wrong.
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.
Outside the scope of this PR, but we should add the DaemonConfiguration to the global recorder rather than constructing one here so that customers can programmatically set custom daemon addresses, e.g. with
AWSXRay.setDaemonAddress()