public void TestElasticBeanstalkPlugin() { var mockEBPlugin = new Mock <IPlugin>(); IDictionary <string, object> fakeEBContext = new Dictionary <string, object>(); fakeEBContext.Add("deployment_id", "1"); fakeEBContext.Add("environment_id", "1"); fakeEBContext.Add("environment_name", "test"); fakeEBContext.Add("version_label", "v0"); mockEBPlugin.Setup(x => x.Origin).Returns("AWS::ElasticBeanstalk::Environment"); mockEBPlugin.Setup(x => x.ServiceName).Returns("elastic_beanstalk"); mockEBPlugin.Setup(x => x.TryGetRuntimeContext(out fakeEBContext)).Returns(true); var recorder = new AWSXRayRecorderBuilder().WithPlugin(mockEBPlugin.Object).Build(); var traceId = TraceId.NewId(); recorder.BeginSegment(GetType().Name, traceId); Thread.Sleep(100); recorder.EndSegment(); #if NET45 var response = BatchGetTraces(traceId); #else var response = BatchGetTracesAsync(traceId).Result; #endif Assert.IsTrue(response.Traces.Count > 0); var segmentJsonData = JsonMapper.ToObject(response.Traces[0].Segments[0].Document); Assert.AreEqual("AWS::ElasticBeanstalk::Environment", (string)segmentJsonData["origin"]); var ebJsonData = segmentJsonData["aws"]["elastic_beanstalk"]; Assert.AreEqual("1", (string)ebJsonData["deployment_id"]); Assert.AreEqual("1", (string)ebJsonData["environment_id"]); Assert.AreEqual("test", (string)ebJsonData["environment_name"]); Assert.AreEqual("v0", (string)ebJsonData["version_label"]); }
public void TestECSPlugin() { var mockECSPlugin = new Mock <IPlugin>(); IDictionary <string, object> fakeECSContext = new Dictionary <string, object>(); fakeECSContext.Add("container", "localhost"); mockECSPlugin.Setup(x => x.Origin).Returns("AWS::ECS::Container"); mockECSPlugin.Setup(x => x.ServiceName).Returns("ecs"); mockECSPlugin.Setup(x => x.TryGetRuntimeContext(out fakeECSContext)).Returns(true); var recorder = new AWSXRayRecorderBuilder().WithPlugin(mockECSPlugin.Object).Build(); var traceId = TraceId.NewId(); recorder.BeginSegment(GetType().Name, traceId); Thread.Sleep(100); recorder.EndSegment(); #if NET45 var response = BatchGetTraces(traceId); #else var response = BatchGetTracesAsync(traceId).Result; #endif Assert.IsTrue(response.Traces.Count > 0); var segmentJsonData = JsonMapper.ToObject(response.Traces[0].Segments[0].Document); Assert.AreEqual("AWS::ECS::Container", (string)segmentJsonData["origin"]); var ecsJsonData = segmentJsonData["aws"]["ecs"]; Assert.AreEqual("localhost", (string)ecsJsonData["container"]); }
public void TestEC2Plugin() { var mockEC2Plugin = new Mock <IPlugin>(); IDictionary <string, object> fakeEC2Context = new Dictionary <string, object>(); fakeEC2Context.Add("instance_id", "i-0ae00afcb550c1164"); fakeEC2Context.Add("availability_zone", "us-east-1d"); fakeEC2Context.Add("instance_size", "c4.large"); fakeEC2Context.Add("ami_id", "ami-a32a7eb4"); mockEC2Plugin.Setup(x => x.Origin).Returns("AWS::EC2::Instance"); mockEC2Plugin.Setup(x => x.ServiceName).Returns("ec2"); mockEC2Plugin.Setup(x => x.TryGetRuntimeContext(out fakeEC2Context)).Returns(true); var recorder = new AWSXRayRecorderBuilder().WithPlugin(mockEC2Plugin.Object).Build(); var traceId = TraceId.NewId(); recorder.BeginSegment(GetType().Name, traceId); Thread.Sleep(100); recorder.EndSegment(); #if NET45 var response = BatchGetTraces(traceId); #else var response = BatchGetTracesAsync(traceId).Result; #endif Assert.IsTrue(response.Traces.Count > 0); var segmentJsonData = JsonMapper.ToObject(response.Traces[0].Segments[0].Document); Assert.AreEqual("AWS::EC2::Instance", (string)segmentJsonData["origin"]); var ec2JsonData = segmentJsonData["aws"]["ec2"]; Assert.AreEqual("i-0ae00afcb550c1164", (string)ec2JsonData["instance_id"]); Assert.AreEqual("us-east-1d", (string)ec2JsonData["availability_zone"]); Assert.AreEqual("c4.large", (string)ec2JsonData["instance_size"]); Assert.AreEqual("ami-a32a7eb4", (string)ec2JsonData["ami_id"]); }
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 TestBeginSegmentWithCustomTime() { var custom_time = new DateTime(2020, 1, 13, 21, 18, 47, 228, DateTimeKind.Utc); AWSXRayRecorder recorder = new AWSXRayRecorderBuilder().Build(); recorder.BeginSegment("Segment1", timestamp: custom_time); Segment segment = (Segment)recorder.TraceContext.GetEntity(); Assert.AreEqual(1578950327.228m, segment.StartTime); recorder.EndSegment(); }
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); }
public void TestBuildWithDummyPlugin() { var dummyPlugin = new DummyPlugin(); AWSXRayRecorder recorder = new AWSXRayRecorderBuilder().WithPlugin(dummyPlugin).Build(); recorder.BeginSegment("test", TraceId); var segment = (Segment)TraceContext.GetEntity(); recorder.EndSegment(); Assert.AreEqual("Origin", segment.Origin); var dict = segment.Aws[dummyPlugin.ServiceName] as Dictionary <string, object>; Assert.AreEqual("value1", dict["key1"]); }
public void TestEndSubsegmentWithCustomTime() { AWSXRayRecorder recorder = new AWSXRayRecorderBuilder().Build(); recorder.BeginSegment("Segment1"); 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); recorder.EndSegment(); }