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

The following examples show how to use com.amazonaws.xray.entities.Subsegment#putAllAws() . 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: TracingInterceptor.java    From aws-xray-sdk-java with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeExecution(Context.BeforeExecution context, ExecutionAttributes executionAttributes) {
    AWSXRayRecorder recorder = getRecorder();
    Entity origin = recorder.getTraceEntity();

    Subsegment subsegment = recorder.beginSubsegment(executionAttributes.getAttribute(SdkExecutionAttribute.SERVICE_NAME));
    if (subsegment == null) {
        return;
    }
    subsegment.setNamespace(Namespace.AWS.toString());
    subsegment.putAws(EntityDataKeys.AWS.OPERATION_KEY,
                      executionAttributes.getAttribute(SdkExecutionAttribute.OPERATION_NAME));
    Region region = executionAttributes.getAttribute(AwsExecutionAttribute.AWS_REGION);
    if (region != null) {
        subsegment.putAws(EntityDataKeys.AWS.REGION_KEY, region.id());
    }
    subsegment.putAllAws(extractRequestParameters(context, executionAttributes));
    if (accountId != null) {
        subsegment.putAws(EntityDataKeys.AWS.ACCOUNT_ID_SUBSEGMENT_KEY, accountId);
    }

    recorder.setTraceEntity(origin);
    // store the subsegment in the AWS SDK's executionAttributes so it can be accessed across threads
    executionAttributes.putAttribute(entityKey, subsegment);
}
 
Example 2
Source File: TracingInterceptor.java    From aws-xray-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public void afterExecution(Context.AfterExecution context, ExecutionAttributes executionAttributes) {
    Subsegment subsegment = executionAttributes.getAttribute(entityKey);
    if (subsegment == null) {
        return;
    }

    populateRequestId(subsegment, context);
    populateSubsegmentWithResponse(subsegment, context.httpResponse());
    subsegment.putAllAws(extractResponseParameters(context, executionAttributes));

    getRecorder().endSubsegment(subsegment);
}
 
Example 3
Source File: TracingHandler.java    From aws-xray-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeRequest(Request<?> request) {
    String serviceName = extractServiceName(request);
    String operationName = extractOperationName(request);

    if (S3_SERVICE_NAME.equals(serviceName) && S3_PRESIGN_REQUEST.equals(operationName)) {
        return;
    }

    if (XRAY_SERVICE_NAME.equals(serviceName) && (XRAY_SAMPLING_RULE_REQUEST.equals(operationName)
            || XRAY_SAMPLING_TARGET_REQUEST.equals(operationName))) {
        return;
    }

    if (isSubsegmentDuplicate(recorder.getCurrentSubsegmentOptional(), request)) {
        return;
    }
    Entity entityContext = request.getHandlerContext(ENTITY_KEY);
    if (null != entityContext) {
        recorder.setTraceEntity(entityContext);
    }
    Subsegment currentSubsegment = recorder.beginSubsegment(serviceName);
    if (null == currentSubsegment) {
        return;
    }
    currentSubsegment.putAllAws(extractRequestParameters(request));
    currentSubsegment.putAws(EntityDataKeys.AWS.OPERATION_KEY, operationName);
    if (null != accountId) {
        currentSubsegment.putAws(EntityDataKeys.AWS.ACCOUNT_ID_SUBSEGMENT_KEY, accountId);
    }
    currentSubsegment.setNamespace(Namespace.AWS.toString());

    if (null != recorder.getCurrentSegment()) {
        TraceHeader header =
            new TraceHeader(recorder.getCurrentSegment().getTraceId(),
                            recorder.getCurrentSegment().isSampled() ? currentSubsegment.getId() : null,
                            recorder.getCurrentSegment().isSampled() ? SampleDecision.SAMPLED : SampleDecision.NOT_SAMPLED);
        request.addHeader(TraceHeader.HEADER_KEY, header.toString());
    }
}
 
Example 4
Source File: TracingHandler.java    From aws-xray-sdk-java with Apache License 2.0 5 votes vote down vote up
private void populateAndEndSubsegment(Subsegment currentSubsegment, Request<?> request, Response<?> response) {
    if (null != response) {
        String requestId = null;
        if (response.getAwsResponse() instanceof AmazonWebServiceResult<?>) {
            // Not all services return responses extending AmazonWebServiceResult (e.g. S3)
            ResponseMetadata metadata = ((AmazonWebServiceResult<?>) response.getAwsResponse()).getSdkResponseMetadata();
            if (null != metadata) {
                requestId = metadata.getRequestId();
                if (null != requestId) {
                    currentSubsegment.putAws(REQUEST_ID_SUBSEGMENT_KEY, requestId);
                }
            }
        } else if (null != response.getHttpResponse()) { // S3 does not follow request id header convention
            if (null != response.getHttpResponse().getHeader(S3_REQUEST_ID_HEADER_KEY)) {
                currentSubsegment.putAws(REQUEST_ID_SUBSEGMENT_KEY,
                                         response.getHttpResponse().getHeader(S3_REQUEST_ID_HEADER_KEY));
            }
            if (null != response.getHttpResponse().getHeader(EntityHeaderKeys.AWS.EXTENDED_REQUEST_ID_HEADER)) {
                currentSubsegment.putAws(EntityDataKeys.AWS.EXTENDED_REQUEST_ID_KEY,
                                         response.getHttpResponse().getHeader(
                                             EntityHeaderKeys.AWS.EXTENDED_REQUEST_ID_HEADER));
            }
        }
        currentSubsegment.putAllAws(extractResponseParameters(request, response.getAwsResponse()));
        currentSubsegment.putAllHttp(extractHttpResponseInformation(response.getHttpResponse()));
    }

    finalizeSubsegment(request);
}