Developer Guide

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.

Instrumenting Outgoing HTTP Calls

The user factory class shows how the application uses the X-Ray SDK for Java's version of HTTPClientBuilder to instrument outgoing HTTP calls.

Example src/main/java/scorekeep/ – HTTPClient Instrumentation

import com.amazonaws.xray.proxies.apache.http.HttpClientBuilder; public String randomName() throws IOException { CloseableHttpClient httpclient = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet(""); CloseableHttpResponse response = httpclient.execute(httpGet); try { HttpEntity entity = response.getEntity(); InputStream inputStream = entity.getContent(); ObjectMapper mapper = new ObjectMapper(); Map<String, String> jsonMap = mapper.readValue(inputStream, Map.class); String name = jsonMap.get("name"); EntityUtils.consume(entity); return name; } finally { response.close(); } }

If you currently use org.apache.http.impl.client.HttpClientBuilder, you can simply swap out the import statement for that class with one for com.amazonaws.xray.proxies.apache.http.HttpClientBuilder.