public void TestExceptionStrategy5() // Test custom exception strategy
        {
            List <Type> l = new List <Type>();

            l.Add(typeof(ArgumentNullException));
            var recorder = new AWSXRayRecorderBuilder().WithExceptionSerializationStrategy(new DefaultExceptionSerializationStrategy(10, l)).Build(); // set custom stackframe size

            AWSXRayRecorder.InitializeInstance(recorder: recorder);
            AWSXRayRecorder.Instance.BeginSegment("parent", TraceId);

            var segment = AWSXRayRecorder.Instance.TraceContext.GetEntity();

            try
            {
                recorder.BeginSubsegment("child1");
                try
                {
                    try
                    {
                        recorder.BeginSubsegment("child2");
                        throw new AmazonServiceException();
                    }
                    catch (AmazonServiceException e)
                    {
                        recorder.AddException(e);
                        recorder.EndSubsegment();
                        throw new ArgumentNullException("value");
                    }
                }
                catch (ArgumentNullException e)
                {
                    recorder.AddException(e);
                    recorder.EndSubsegment();
                    throw new EntityNotAvailableException("Dummy message", e);
                }
            }
            catch (EntityNotAvailableException e)
            {
                recorder.AddException(e);
                recorder.EndSegment();
            }

            Assert.AreEqual("Dummy message", segment.Cause.ExceptionDescriptors[0].Message);
            Assert.AreEqual("EntityNotAvailableException", segment.Cause.ExceptionDescriptors[0].Type);
            Assert.IsFalse(segment.Cause.ExceptionDescriptors[0].Remote); // default false
            Assert.AreEqual(segment.Cause.ExceptionDescriptors[0].Cause, segment.Subsegments[0].Cause.ExceptionDescriptors[0].Id);
            Assert.AreEqual(1, segment.Cause.ExceptionDescriptors.Count);

            Assert.AreEqual("ArgumentNullException", segment.Subsegments[0].Cause.ExceptionDescriptors[0].Type);
            Assert.IsTrue(segment.Subsegments[0].Cause.ExceptionDescriptors[0].Remote); // set to true

            Assert.AreEqual("AmazonServiceException", segment.Subsegments[0].Subsegments[0].Cause.ExceptionDescriptors[0].Type);
            Assert.IsTrue(segment.Subsegments[0].Subsegments[0].Cause.ExceptionDescriptors[0].Remote); // set to true
        }
        public void TestExceptionStrategy6() // Setting stack frame size to 0, so no stack trace is recorded
        {
            int stackFrameSize = 0;
            var recorder       = new AWSXRayRecorderBuilder().WithExceptionSerializationStrategy(new DefaultExceptionSerializationStrategy(stackFrameSize)).Build(); // set custom stackframe size

            AWSXRayRecorder.InitializeInstance(recorder: recorder);
            AWSXRayRecorder.Instance.BeginSegment("parent", TraceId);

            var segment = AWSXRayRecorder.Instance.TraceContext.GetEntity();

            try
            {
                recorder.BeginSubsegment("child1");
                try
                {
                    try
                    {
                        recorder.BeginSubsegment("child2");
                        throw new AmazonServiceException();
                    }
                    catch (AmazonServiceException e)
                    {
                        recorder.AddException(e);
                        recorder.EndSubsegment();
                        throw new ArgumentNullException("value");
                    }
                }
                catch (ArgumentNullException e)
                {
                    recorder.AddException(e);
                    recorder.EndSubsegment();
                    throw new EntityNotAvailableException("Dummy message", e);
                }
            }
            catch (EntityNotAvailableException e)
            {
                recorder.AddException(e);
                recorder.EndSegment();
            }

            Assert.AreEqual("Dummy message", segment.Cause.ExceptionDescriptors[0].Message);
            Assert.AreEqual("EntityNotAvailableException", segment.Cause.ExceptionDescriptors[0].Type);
            Assert.IsFalse(segment.Cause.ExceptionDescriptors[0].Remote);                        // default false
            Assert.AreEqual(segment.Cause.ExceptionDescriptors[0].Cause, segment.Subsegments[0].Cause.ExceptionDescriptors[0].Id);
            Assert.AreEqual(segment.Cause.ExceptionDescriptors[0].Stack.Length, stackFrameSize); // no stack frames should be recorded
            Assert.IsTrue(segment.Cause.ExceptionDescriptors[0].Truncated > 0);
            Assert.AreEqual(1, segment.Cause.ExceptionDescriptors.Count);

            Assert.AreEqual("ArgumentNullException", segment.Subsegments[0].Cause.ExceptionDescriptors[0].Type);
            Assert.AreEqual("AmazonServiceException", segment.Subsegments[0].Subsegments[0].Cause.ExceptionDescriptors[0].Type);
        }
示例#3
0
        public void TestEndSubsegmentWithCustomTime()
        {
            AWSXRayRecorder recorder = new AWSXRayRecorderBuilder().Build();

            recorder.BeginSubsegment("Subsegment1");

            Subsegment subsegment = (Subsegment)recorder.TraceContext.GetEntity();

            Assert.IsTrue(DateTime.UtcNow.ToUnixTimeSeconds() >= subsegment.StartTime);

            var custom_time = new DateTime(2019, 07, 14);

            recorder.EndSubsegment(custom_time);
            Assert.AreEqual(1563062400, subsegment.EndTime);
        }