Search in sources :

Example 26 with Segment

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

the class TracedResponseHandlerTest method testHandleResponse200SetsNoFlags.

@Test
public void testHandleResponse200SetsNoFlags() {
    Segment segment = segmentInResponseToCode(200);
    Subsegment subsegment = segment.getSubsegments().get(0);
    Assert.assertFalse(subsegment.isFault());
    Assert.assertFalse(subsegment.isError());
    Assert.assertFalse(subsegment.isThrottle());
}
Also used : Subsegment(com.amazonaws.xray.entities.Subsegment) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.Test)

Example 27 with Segment

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

the class TracingHandlerTest method testS3PutObjectSubsegmentContainsBucketName.

@Test
public void testS3PutObjectSubsegmentContainsBucketName() {
    // Setup test
    AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRequestHandlers(new TracingHandler()).withRegion(Regions.US_EAST_1).withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("fake", "fake"))).build();
    mockHttpClient(s3, null);
    final String BUCKET = "test-bucket", KEY = "test-key";
    // Test logic
    Segment segment = AWSXRay.beginSegment("test");
    s3.putObject(BUCKET, KEY, "This is a test from java");
    Assert.assertEquals(1, segment.getSubsegments().size());
    Assert.assertEquals("PutObject", segment.getSubsegments().get(0).getAws().get("operation"));
    System.out.println(segment.getSubsegments().get(0).getAws());
    Assert.assertEquals(BUCKET, segment.getSubsegments().get(0).getAws().get("bucket_name"));
    Assert.assertEquals(KEY, segment.getSubsegments().get(0).getAws().get("key"));
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.Test)

Example 28 with Segment

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

the class TracingHandlerTest method testLambdaInvokeSubsegmentContainsFunctionName.

@Test
public void testLambdaInvokeSubsegmentContainsFunctionName() {
    // Setup test
    AWSLambda lambda = AWSLambdaClientBuilder.standard().withRequestHandlers(new TracingHandler()).withRegion(Regions.US_EAST_1).withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("fake", "fake"))).build();
    // Lambda returns "null" on successful fn. with no return value
    mockHttpClient(lambda, "null");
    // Test logic
    Segment segment = AWSXRay.beginSegment("test");
    InvokeRequest request = new InvokeRequest();
    request.setFunctionName("testFunctionName");
    InvokeResult r = lambda.invoke(request);
    System.out.println(r.getStatusCode());
    System.out.println(r);
    Assert.assertEquals(1, segment.getSubsegments().size());
    Assert.assertEquals("Invoke", segment.getSubsegments().get(0).getAws().get("operation"));
    System.out.println(segment.getSubsegments().get(0).getAws());
    Assert.assertEquals("testFunctionName", segment.getSubsegments().get(0).getAws().get("function_name"));
}
Also used : AWSLambda(com.amazonaws.services.lambda.AWSLambda) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) InvokeRequest(com.amazonaws.services.lambda.model.InvokeRequest) InvokeResult(com.amazonaws.services.lambda.model.InvokeResult) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.Test)

Example 29 with Segment

use of com.amazonaws.xray.entities.Segment 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)29 Test (org.junit.Test)16 Subsegment (com.amazonaws.xray.entities.Subsegment)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 TraceID (com.amazonaws.xray.entities.TraceID)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 SegmentImpl (com.amazonaws.xray.entities.SegmentImpl)4 SubsegmentImpl (com.amazonaws.xray.entities.SubsegmentImpl)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)3 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)3 HashMap (java.util.HashMap)3 FilterChain (javax.servlet.FilterChain)3 AmazonS3 (com.amazonaws.services.s3.AmazonS3)2 TraceHeader (com.amazonaws.xray.entities.TraceHeader)2 AWSLambda (com.amazonaws.services.lambda.AWSLambda)1 InvokeRequest (com.amazonaws.services.lambda.model.InvokeRequest)1 InvokeResult (com.amazonaws.services.lambda.model.InvokeResult)1 Emitter (com.amazonaws.xray.emitters.Emitter)1 UDPEmitter (com.amazonaws.xray.emitters.UDPEmitter)1