Java Code Examples for com.amazonaws.xray.entities.Subsegment#addException()

The following examples show how to use com.amazonaws.xray.entities.Subsegment#addException() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultHttpClient.java    From aws-xray-sdk-java with Apache License 2.0 6 votes vote down vote up
@Override
public CloseableHttpResponse execute(
    HttpHost target, HttpRequest request, HttpContext context) throws IOException, ClientProtocolException {
    Subsegment subsegment = getRecorder().beginSubsegment(target.getHostName());
    try {
        if (null != subsegment) {
            TracedHttpClient.addRequestInformation(subsegment, request, TracedHttpClient.getUrl(target, request));
        }
        CloseableHttpResponse response = super.execute(target, request, context);
        if (null != subsegment) {
            TracedResponseHandler.addResponseInformation(subsegment, response);
        }
        return response;
    } catch (Exception e) {
        if (null != subsegment) {
            subsegment.addException(e);
        }
        throw e;
    } finally {
        if (null != subsegment) {
            getRecorder().endSubsegment();
        }
    }
}
 
Example 2
Source File: DefaultHttpClient.java    From aws-xray-sdk-java with Apache License 2.0 6 votes vote down vote up
@Override
public CloseableHttpResponse execute(
    HttpUriRequest request, HttpContext context) throws IOException, ClientProtocolException {
    Subsegment subsegment = getRecorder().beginSubsegment(TracedHttpClient.determineTarget(request).getHostName());
    try {
        if (null != subsegment) {
            TracedHttpClient.addRequestInformation(subsegment, request, TracedHttpClient.getUrl(request));
        }
        CloseableHttpResponse response = super.execute(request, context);
        if (null != subsegment) {
            TracedResponseHandler.addResponseInformation(subsegment, response);
        }
        return response;
    } catch (Exception e) {
        if (null != subsegment) {
            subsegment.addException(e);
        }
        throw e;
    } finally {
        if (null != subsegment) {
            getRecorder().endSubsegment();
        }
    }
}
 
Example 3
Source File: DefaultHttpClient.java    From aws-xray-sdk-java with Apache License 2.0 6 votes vote down vote up
@Override
public CloseableHttpResponse execute(HttpHost target, HttpRequest request) throws IOException, ClientProtocolException {
    Subsegment subsegment = getRecorder().beginSubsegment(target.getHostName());
    try {
        if (null != subsegment) {
            TracedHttpClient.addRequestInformation(subsegment, request, TracedHttpClient.getUrl(target, request));
        }
        CloseableHttpResponse response = super.execute(target, request);
        if (null != subsegment) {
            TracedResponseHandler.addResponseInformation(subsegment, response);
        }
        return response;
    } catch (Exception e) {
        if (null != subsegment) {
            subsegment.addException(e);
        }
        throw e;
    } finally {
        if (null != subsegment) {
            getRecorder().endSubsegment();
        }
    }
}
 
Example 4
Source File: AWSXRayServlet.java    From deployment-examples with MIT License 5 votes vote down vote up
private void traceS3() {
    // Add subsegment to current request to track call to S3
    Subsegment subsegment = AWSXRay.beginSubsegment("## Getting object metadata");
    try {
        // Gets metadata about this sample app object in S3
        s3Client.getObjectMetadata(BUCKET_NAME, OBJECT_KEY);
    } catch (Exception ex) {
        subsegment.addException(ex);
    } finally {
        AWSXRay.endSubsegment();
    }
}
 
Example 5
Source File: TracedHttpClient.java    From aws-xray-sdk-java with Apache License 2.0 5 votes vote down vote up
private <R> R wrapHttpSupplier(Subsegment subsegment, HttpSupplier<R> supplier) throws IOException, ClientProtocolException {
    try {
        return supplier.get();
    } catch (Exception e) {
        if (null != subsegment) {
            subsegment.addException(e);
        }
        throw e;
    } finally {
        if (null != subsegment) {
            recorder.endSubsegment();
        }
    }
}
 
Example 6
Source File: TracingInterceptor.java    From aws-xray-sdk-java with Apache License 2.0 5 votes vote down vote up
private void populateSubsegmentException(Subsegment subsegment, Context.FailedExecution context) {
    Throwable exception = context.exception();
    subsegment.addException(exception);

    int statusCode = -1;
    if (exception instanceof SdkServiceException) {
        statusCode = ((SdkServiceException) exception).statusCode();
        subsegment.getCause().setMessage(exception.getMessage());
        if (((SdkServiceException) exception).isThrottlingException()) {
            subsegment.setThrottle(true);
            // throttling errors are considered client-side errors
            subsegment.setError(true);
        }
        setRemoteForException(subsegment, exception);
    } else if (context.httpResponse().isPresent()) {
        statusCode = context.httpResponse().get().statusCode();
    }

    if (statusCode == -1) {
        return;
    }

    if (statusCode >= 400 && statusCode < 500) {
        subsegment.setFault(false);
        subsegment.setError(true);
        if (statusCode == 429) {
            subsegment.setThrottle(true);
        }
    } else if (statusCode >= 500) {
        subsegment.setFault(true);
    }
}
 
Example 7
Source File: TracingStatement.java    From aws-xray-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    Subsegment subsegment = null;

    if (isExecution(method)) {
        // only trace on execution methods
        subsegment = createSubsegment();
    }

    logger.debug(
        String.format("Invoking statement execution with X-Ray tracing. Tracing active: %s", subsegment != null));
    try {
        // execute the query "wrapped" in a XRay Subsegment
        return method.invoke(delegate, args);
    } catch (Throwable t) {
        Throwable rootThrowable = t;
        if (t instanceof InvocationTargetException) {
            // the reflection may wrap the actual error with an InvocationTargetException.
            // we want to use the root cause to make the instrumentation seamless
            InvocationTargetException ite = (InvocationTargetException) t;
            if (ite.getTargetException() != null) {
                rootThrowable = ite.getTargetException();
            } else if (ite.getCause() != null) {
                rootThrowable = ite.getCause();
            }
        }

        if (subsegment != null) {
            subsegment.addException(rootThrowable);
        }
        throw rootThrowable;
    } finally {
        if (subsegment != null && isExecution(method)) {
            AWSXRay.endSubsegment();
        }
    }
}
 
Example 8
Source File: AWSXRayRecorder.java    From aws-xray-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Begins a subsegment, passes it to the supplied function, and ends the subsegment before returning the supplied function's
 * result. Intercepts exceptions, adds them to the subsegment, and re-throws them.
 *
 * @param <R>
 *            the type of the value returned by {@code function}
 * @param name
 *            the name to use for the created subsegment
 * @param function
 *            the function to invoke
 * @return the value returned by the supplied function
 */
@Nullable
public <R> R createSubsegment(String name, Function<@Nullable Subsegment, @Nullable R> function) {
    Subsegment subsegment = beginSubsegment(name);
    try {
        return function.apply(subsegment);
    } catch (Exception e) {
        if (subsegment != null) {
            subsegment.addException(e);
        }
        throw e;
    } finally {
        endSubsegment();
    }
}
 
Example 9
Source File: AWSXRayRecorder.java    From aws-xray-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Begins a subsegment and passes it to the supplied consumer, and ends the subsegment before returning the consumer's result.
 * Intercepts exceptions, adds them to the subsegment, and re-throws them.
 *
 * @param name
 *            the name to use for the created subsegment
 * @param consumer
 *            the function to invoke
 */
public void createSubsegment(String name, Consumer<@Nullable Subsegment> consumer) {
    Subsegment subsegment = beginSubsegment(name);
    try {
        consumer.accept(subsegment);
    } catch (Exception e) {
        if (subsegment != null) {
            subsegment.addException(e);
        }
        throw e;
    } finally {
        endSubsegment();
    }
}
 
Example 10
Source File: AWSXRayRecorder.java    From aws-xray-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Begins a subsegment, passes it to the provided supplier, and ends the subsegment before returning the supplier's result.
 * Intercepts exceptions, adds them to the subsegment, and re-throws them.
 *
 * @param <R>
 *            the type of the value returned by {@code function}
 * @param name
 *            the name to use for the created subsegment
 * @param supplier
 *            the supplier to invoke
 * @return the value returned by the provided supplier
 */
@Nullable
public <R> R createSubsegment(String name, Supplier<R> supplier) {
    Subsegment subsegment = beginSubsegment(name);
    try {
        return supplier.get();
    } catch (Exception e) {
        if (subsegment != null) {
            subsegment.addException(e);
        }
        throw e;
    } finally {
        endSubsegment();
    }
}
 
Example 11
Source File: AWSXRayRecorder.java    From aws-xray-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Begins a subsegment, runs the provided runnable, and ends the subsegment once complete. Intercepts exceptions, adds them to
 * the subsegment, and re-throws them.
 *
 * @param name
 *            the name to use for the created subsegment
 * @param runnable
 *            the runnable to run
 */
public void createSubsegment(String name, Runnable runnable) {
    Subsegment subsegment = beginSubsegment(name);
    try {
        runnable.run();
    } catch (Exception e) {
        if (subsegment != null) {
            subsegment.addException(e);
        }
        throw e;
    } finally {
        endSubsegment();
    }
}
 
Example 12
Source File: TracingInterceptor.java    From aws-xray-sdk-java with Apache License 2.0 4 votes vote down vote up
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    //get the name of the method for comparison
    final String name = method.getName();
    //was close invoked?
    boolean close = compare(JdbcInterceptor.CLOSE_VAL, name);
    //allow close to be called multiple times
    if (close && closed) {
        return null;
    }
    //are we calling isClosed?
    if (compare(JdbcInterceptor.ISCLOSED_VAL, name)) {
        return Boolean.valueOf(closed);
    }
    //if we are calling anything else, bail out
    if (closed) {
        throw new SQLException("Statement closed.");
    }
    //check to see if we are about to execute a query
    final boolean process = isExecute(method);
    Object result = null;
    Subsegment subsegment = null;
    if (process) {
        subsegment = AWSXRay.beginSubsegment(hostname);
    }
    try {
        if (process && null != subsegment) {
            subsegment.putAllSql(additionalParams);
            subsegment.setNamespace(Namespace.REMOTE.toString());
        }
        result = method.invoke(delegate, args); //execute the query
    } catch (Throwable t) {
        if (null != subsegment) {
            subsegment.addException(t);
        }
        if (t instanceof InvocationTargetException && t.getCause() != null) {
            throw t.getCause();
        } else {
            throw t;
        }
    } finally {
        if (process && null != subsegment) {
            AWSXRay.endSubsegment();
        }
    }

    //perform close cleanup
    if (close) {
        closed = true;
        delegate = null;
    }

    return result;
}
 
Example 13
Source File: TracingInterceptor.java    From aws-xray-sdk-java with Apache License 2.0 4 votes vote down vote up
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    //get the name of the method for comparison
    final String name = method.getName();
    //was close invoked?
    boolean close = compare(JdbcInterceptor.CLOSE_VAL, name);
    //allow close to be called multiple times
    if (close && closed) {
        return null;
    }
    //are we calling isClosed?
    if (compare(JdbcInterceptor.ISCLOSED_VAL, name)) {
        return Boolean.valueOf(closed);
    }
    //if we are calling anything else, bail out
    if (closed) {
        throw new SQLException("Statement closed.");
    }
    //check to see if we are about to execute a query
    final boolean process = isExecute(method);
    Object result = null;
    Subsegment subsegment = null;
    if (process) {
        subsegment = AWSXRay.beginSubsegment(hostname);
    }
    try {
        if (process && null != subsegment) {
            subsegment.putAllSql(additionalParams);
            subsegment.setNamespace(Namespace.REMOTE.toString());
        }
        result = method.invoke(delegate, args); //execute the query
    } catch (Throwable t) {
        if (null != subsegment) {
            subsegment.addException(t);
        }
        if (t instanceof InvocationTargetException && t.getCause() != null) {
            throw t.getCause();
        } else {
            throw t;
        }
    } finally {
        if (process && null != subsegment) {
            AWSXRay.endSubsegment();
        }
    }

    //perform close cleanup
    if (close) {
        closed = true;
        delegate = null;
    }

    return result;
}
 
Example 14
Source File: TracingHandler.java    From aws-xray-sdk-java with Apache License 2.0 4 votes vote down vote up
@Override
public void afterError(Request<?> request, Response<?> response, Exception e) {
    if (isSubsegmentDuplicate(recorder.getCurrentSubsegmentOptional(), request)) {
        Optional<Subsegment> currentSubsegmentOptional = recorder.getCurrentSubsegmentOptional();
        if (!currentSubsegmentOptional.isPresent()) {
            return;
        }
        Subsegment currentSubsegment = currentSubsegmentOptional.get();

        int statusCode = -1;

        if (null != response) {
            statusCode = response.getHttpResponse().getStatusCode();
        } else {
            if (e instanceof AmazonServiceException) {
                AmazonServiceException ase = (AmazonServiceException) e;
                statusCode = ase.getStatusCode();
                // The S3 client will throw and re-swallow AmazonServiceExceptions if they have these status codes. Customers
                // will never see the exceptions in their application code but they still travel through our
                // TracingHandler#afterError method. We special case these status codes in order to prevent addition of the
                // full exception object to the current subsegment. Instead, we'll just add any exception error message to the
                // current subsegment's cause's message.
                if ((304 == statusCode || 412 == statusCode) && S3_SERVICE_NAME.equals(ase.getServiceName())) {
                    populateAndEndSubsegment(currentSubsegment, request, response, ase);
                    return;
                }
                if (RetryUtils.isThrottlingException(ase)) {
                    currentSubsegment.setError(true);
                    currentSubsegment.setThrottle(true);
                }
            }
        }

        if (-1 != statusCode) {
            int statusCodePrefix = statusCode / 100;
            if (4 == statusCodePrefix) {
                currentSubsegment.setError(true);
                if (429 == statusCode) {
                    currentSubsegment.setThrottle(true);
                }
            }
        }

        currentSubsegment.addException(e);

        if (e instanceof AmazonServiceException) {
            populateAndEndSubsegment(currentSubsegment, request, response, (AmazonServiceException) e);
        } else {
            populateAndEndSubsegment(currentSubsegment, request, response);
        }
    }
}