Search in sources :

Example 26 with Subsegment

use of com.amazonaws.xray.entities.Subsegment in project aws-xray-sdk-java by aws.

the class TracingHandler method beforeRequest.

@Override
public void beforeRequest(Request<?> request) {
    if (S3_SERVICE_NAME.equals(extractServiceName(request)) && S3_PRESIGN_REQUEST.equals(extractOperationName(request))) {
        return;
    }
    if (isSubsegmentDuplicate(recorder.getCurrentSubsegmentOptional(), request)) {
        return;
    }
    Entity entityContext = request.getHandlerContext(entityKey);
    if (null != entityContext) {
        recorder.setTraceEntity(entityContext);
    }
    Subsegment currentSubsegment = recorder.beginSubsegment(extractServiceName(request));
    if (null == currentSubsegment) {
        return;
    }
    currentSubsegment.putAllAws(extractRequestParameters(request));
    currentSubsegment.putAws(OPERATION_SUBSEGMENT_KEY, extractOperationName(request));
    if (null != accountId) {
        currentSubsegment.putAws(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());
    }
}
Also used : Entity(com.amazonaws.xray.entities.Entity) TraceHeader(com.amazonaws.xray.entities.TraceHeader) Subsegment(com.amazonaws.xray.entities.Subsegment)

Example 27 with Subsegment

use of com.amazonaws.xray.entities.Subsegment in project aws-xray-sdk-java by aws.

the class LambdaSegmentContext method beginSubsegment.

@Override
public Subsegment beginSubsegment(AWSXRayRecorder recorder, String name) {
    if (logger.isDebugEnabled()) {
        logger.debug("Beginning subsegment named: " + name);
    }
    if (null == getTraceEntity()) {
        // First subsgment of a subsegment branch.
        Segment parentSegment = null;
        if (LambdaSegmentContext.isInitializing(LambdaSegmentContext.getTraceHeaderFromEnvironment())) {
            logger.warn(LAMBDA_TRACE_HEADER_KEY + " is missing a trace ID, parent ID, or sampling decision. Subsegment " + name + " discarded.");
            parentSegment = new FacadeSegment(recorder, new TraceID(), "", SampleDecision.NOT_SAMPLED);
        } else {
            parentSegment = LambdaSegmentContext.newFacadeSegment(recorder);
        }
        Subsegment subsegment = new SubsegmentImpl(recorder, name, parentSegment);
        subsegment.setParent(parentSegment);
        setTraceEntity(subsegment);
        return subsegment;
    } else {
        // Continuation of a subsegment branch.
        Subsegment parentSubsegment = (Subsegment) getTraceEntity();
        // Ensure customers have not leaked subsegments across invocations
        TraceID environmentRootTraceId = LambdaSegmentContext.getTraceHeaderFromEnvironment().getRootTraceId();
        if (null != environmentRootTraceId && !environmentRootTraceId.equals(parentSubsegment.getParentSegment().getTraceId())) {
            clearTraceEntity();
            return beginSubsegment(recorder, name);
        }
        Subsegment subsegment = new SubsegmentImpl(recorder, name, parentSubsegment.getParentSegment());
        subsegment.setParent(parentSubsegment);
        parentSubsegment.addSubsegment(subsegment);
        setTraceEntity(subsegment);
        return subsegment;
    }
}
Also used : TraceID(com.amazonaws.xray.entities.TraceID) SubsegmentImpl(com.amazonaws.xray.entities.SubsegmentImpl) Subsegment(com.amazonaws.xray.entities.Subsegment) FacadeSegment(com.amazonaws.xray.entities.FacadeSegment) Segment(com.amazonaws.xray.entities.Segment) FacadeSegment(com.amazonaws.xray.entities.FacadeSegment)

Aggregations

Subsegment (com.amazonaws.xray.entities.Subsegment)27 Segment (com.amazonaws.xray.entities.Segment)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 Test (org.junit.Test)7 TraceHeader (com.amazonaws.xray.entities.TraceHeader)4 Emitter (com.amazonaws.xray.emitters.Emitter)3 UDPEmitter (com.amazonaws.xray.emitters.UDPEmitter)3 Entity (com.amazonaws.xray.entities.Entity)3 FacadeSegment (com.amazonaws.xray.entities.FacadeSegment)3 SubsegmentImpl (com.amazonaws.xray.entities.SubsegmentImpl)3 TraceID (com.amazonaws.xray.entities.TraceID)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 SegmentImpl (com.amazonaws.xray.entities.SegmentImpl)1 SegmentNotFoundException (com.amazonaws.xray.exceptions.SegmentNotFoundException)1 SubsegmentNotFoundException (com.amazonaws.xray.exceptions.SubsegmentNotFoundException)1 Around (org.aspectj.lang.annotation.Around)1