Search in sources :

Example 1 with TraceID

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

the class EntityTest method testSegmentWithSubsegment.

@Test
public void testSegmentWithSubsegment() throws JSONException {
    TraceID traceId = new TraceID();
    Segment segment = new SegmentImpl(AWSXRay.getGlobalRecorder(), "test", traceId);
    Subsegment subsegment = new SubsegmentImpl(AWSXRay.getGlobalRecorder(), "test", segment);
    segment.addSubsegment(subsegment);
    segment.setStartTime(1.0);
    subsegment.setStartTime(1.0);
    subsegment.end();
    segment.end();
    String expected = expectedCompletedSegmentWithSubsegment(traceId, segment.getId(), subsegment.getId(), 1.0, subsegment.getEndTime(), segment.getEndTime()).toString();
    JSONAssert.assertEquals(expected, segment.serialize(), JSONCompareMode.NON_EXTENSIBLE);
}
Also used : TraceID(com.amazonaws.xray.entities.TraceID) SubsegmentImpl(com.amazonaws.xray.entities.SubsegmentImpl) SegmentImpl(com.amazonaws.xray.entities.SegmentImpl) Subsegment(com.amazonaws.xray.entities.Subsegment) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.Test)

Example 2 with TraceID

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

the class AWSXRayServletAsyncListener method preFilter.

public Segment preFilter(ServletRequest request, ServletResponse response) {
    Segment created = null;
    HttpServletRequest httpServletRequest = castServletRequest(request);
    if (null == httpServletRequest) {
        logger.warn("Null value for incoming HttpServletRequest. Beginning DummySegment.");
        return recorder.beginDummySegment(new TraceID());
    }
    Optional<TraceHeader> incomingHeader = getTraceHeader(httpServletRequest);
    SamplingStrategy samplingStrategy = recorder.getSamplingStrategy();
    if (logger.isDebugEnabled() && incomingHeader.isPresent()) {
        logger.debug("Incoming trace header received: " + incomingHeader.get().toString());
    }
    SampleDecision sampleDecision = incomingHeader.isPresent() ? incomingHeader.get().getSampled() : fromSamplingStrategy(httpServletRequest);
    if (SampleDecision.REQUESTED.equals(sampleDecision) || SampleDecision.UNKNOWN.equals(sampleDecision)) {
        sampleDecision = fromSamplingStrategy(httpServletRequest);
    }
    TraceID traceId = incomingHeader.isPresent() ? incomingHeader.get().getRootTraceId() : null;
    if (null == traceId) {
        traceId = new TraceID();
    }
    String parentId = incomingHeader.isPresent() ? incomingHeader.get().getParentId() : null;
    if (SampleDecision.SAMPLED.equals(sampleDecision)) {
        created = recorder.beginSegment(getSegmentName(httpServletRequest), traceId, parentId);
    } else {
        // NOT_SAMPLED
        if (samplingStrategy.isForcedSamplingSupported()) {
            created = recorder.beginSegment(getSegmentName(httpServletRequest), traceId, parentId);
            created.setSampled(false);
        } else {
            created = recorder.beginDummySegment(traceId);
        }
    }
    Map<String, Object> requestAttributes = new HashMap<String, Object>();
    requestAttributes.put("url", httpServletRequest.getRequestURL().toString());
    requestAttributes.put("method", httpServletRequest.getMethod());
    Optional<String> userAgent = getUserAgent(httpServletRequest);
    if (userAgent.isPresent()) {
        requestAttributes.put("user_agent", userAgent.get());
    }
    Optional<String> xForwardedFor = getXForwardedFor(httpServletRequest);
    if (xForwardedFor.isPresent()) {
        requestAttributes.put("client_ip", xForwardedFor.get());
        requestAttributes.put("x_forwarded_for", true);
    } else {
        Optional<String> clientIp = getClientIp(httpServletRequest);
        if (clientIp.isPresent()) {
            requestAttributes.put("client_ip", clientIp.get());
        }
    }
    created.putHttp("request", requestAttributes);
    HttpServletResponse httpServletResponse = castServletResponse(response);
    if (null == response) {
        return created;
    }
    TraceHeader responseHeader = null;
    if (incomingHeader.isPresent()) {
        // create a new header, and use the incoming header so we know what to do in regards to sending back the sampling decision.
        responseHeader = new TraceHeader(created.getTraceId());
        if (SampleDecision.REQUESTED == incomingHeader.get().getSampled()) {
            responseHeader.setSampled(created.isSampled() ? SampleDecision.SAMPLED : SampleDecision.NOT_SAMPLED);
        }
    } else {
        // Create a new header, we're the tracing root. We wont return the sampling decision.
        responseHeader = new TraceHeader(created.getTraceId());
    }
    httpServletResponse.addHeader(TraceHeader.HEADER_KEY, responseHeader.toString());
    return created;
}
Also used : TraceHeader(com.amazonaws.xray.entities.TraceHeader) HashMap(java.util.HashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) SamplingStrategy(com.amazonaws.xray.strategy.sampling.SamplingStrategy) SampleDecision(com.amazonaws.xray.entities.TraceHeader.SampleDecision) Segment(com.amazonaws.xray.entities.Segment) HttpServletRequest(javax.servlet.http.HttpServletRequest) TraceID(com.amazonaws.xray.entities.TraceID)

Example 3 with TraceID

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

the class EntityTest method testInProgressSegment.

@Test
public void testInProgressSegment() throws JSONException {
    String segmentId = Entity.generateId();
    TraceID traceId = new TraceID();
    Segment segment = new SegmentImpl(AWSXRay.getGlobalRecorder(), "test", traceId);
    segment.setId(segmentId);
    segment.setStartTime(1.0);
    String expected = expectedInProgressSegment(traceId, segmentId, segment.getStartTime()).toString();
    JSONAssert.assertEquals(expected, segment.serialize(), JSONCompareMode.NON_EXTENSIBLE);
}
Also used : TraceID(com.amazonaws.xray.entities.TraceID) SegmentImpl(com.amazonaws.xray.entities.SegmentImpl) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.Test)

Example 4 with TraceID

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

the class EntityTest method testModifyingSegmentAfterEndingThrowsAlreadyEmittedException.

@SuppressWarnings("resource")
@Test(expected = AlreadyEmittedException.class)
public void testModifyingSegmentAfterEndingThrowsAlreadyEmittedException() {
    TraceID traceId = new TraceID();
    Segment segment = new SegmentImpl(AWSXRay.getGlobalRecorder(), "test", traceId);
    segment.end();
    segment.setStartTime(1.0);
}
Also used : TraceID(com.amazonaws.xray.entities.TraceID) SegmentImpl(com.amazonaws.xray.entities.SegmentImpl) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.Test)

Example 5 with TraceID

use of com.amazonaws.xray.entities.TraceID 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

Segment (com.amazonaws.xray.entities.Segment)5 TraceID (com.amazonaws.xray.entities.TraceID)5 SegmentImpl (com.amazonaws.xray.entities.SegmentImpl)3 Test (org.junit.Test)3 Subsegment (com.amazonaws.xray.entities.Subsegment)2 SubsegmentImpl (com.amazonaws.xray.entities.SubsegmentImpl)2 FacadeSegment (com.amazonaws.xray.entities.FacadeSegment)1 TraceHeader (com.amazonaws.xray.entities.TraceHeader)1 SampleDecision (com.amazonaws.xray.entities.TraceHeader.SampleDecision)1 SamplingStrategy (com.amazonaws.xray.strategy.sampling.SamplingStrategy)1 HashMap (java.util.HashMap)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1