Exemplo n.º 1
0
        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"]);
        }
Exemplo n.º 2
0
        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"]);
        }
Exemplo n.º 3
0
        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"]);
        }
Exemplo n.º 4
0
        public static AWSXRayRecorder BuildAWSXRayRecorder(ISamplingStrategy samplingStrategy = null, ISegmentEmitter segmentEmitter = null, string daemonAddress = null, ITraceContext traceContext = null)
        {
            AWSXRayRecorderBuilder builder = new AWSXRayRecorderBuilder();

            if (samplingStrategy != null)
            {
                builder.WithSamplingStrategy(samplingStrategy);
            }
            if (segmentEmitter != null)
            {
                builder.WithSegmentEmitter(segmentEmitter);
            }
            if (!string.IsNullOrEmpty(daemonAddress))
            {
                builder.WithDaemonAddress(daemonAddress);
            }
            if (traceContext != null)
            {
                builder.WithTraceContext(traceContext);
            }

            var result = builder.Build();

            return(result);
        }
        public void TestExceptionStrategy1() // valid input
        {
            var exceptionStrategy = new DefaultExceptionSerializationStrategy(10);
            var recorder          = new AWSXRayRecorderBuilder().WithExceptionSerializationStrategy(exceptionStrategy).Build(); // set custom stackframe size

            AWSXRayRecorder.InitializeInstance(recorder: recorder);
            Assert.AreSame(exceptionStrategy, AWSXRayRecorder.Instance.ExceptionSerializationStrategy);
        }
        public void TestExceptionStrategy4() // Test custom exception strategy
        {
            var exceptionStrategy = new DummyExceptionSerializationStrategy();
            var recorder          = new AWSXRayRecorderBuilder().WithExceptionSerializationStrategy(exceptionStrategy).Build(); // set custom stackframe size

            DummyExceptionSerializationStrategy actual = recorder.ExceptionSerializationStrategy as DummyExceptionSerializationStrategy;

            Assert.AreSame(exceptionStrategy, actual);
        }
Exemplo n.º 7
0
        public void TestPluginSettingMissing()
        {
#if NET45
            var builder = new AWSXRayRecorderBuilder().WithPluginsFromAppSettings();
#else
            var builder = new AWSXRayRecorderBuilder().WithPluginsFromConfig(_xRayOptions);
#endif
            Assert.AreEqual(0, builder.Plugins.Count);
        }
Exemplo n.º 8
0
        private static AWSXRayRecorder BuildAWSXRayRecorder()
        {
            var builder = new AWSXRayRecorderBuilder()
                          .WithSamplingStrategy(new TestSamplingStrategy());

            var result = builder.Build();

            return(result);
        }
Exemplo n.º 9
0
        public void TestDefaultStreamingStrategyWithCustomValue()
        {
            IStreamingStrategy defaultStreamingStrategy = new DefaultStreamingStrategy(50);
            AWSXRayRecorder    recorder = new AWSXRayRecorderBuilder().WithStreamingStrategy(defaultStreamingStrategy).Build();

            Assert.AreEqual(typeof(DefaultStreamingStrategy), recorder.StreamingStrategy.GetType());
            DefaultStreamingStrategy dss = (DefaultStreamingStrategy)recorder.StreamingStrategy;

            Assert.AreEqual(50, dss.MaxSubsegmentSize);
        }
        public void TestExceptionStrategy2() // invalid input
        {
            var exceptionStrategy = new DefaultExceptionSerializationStrategy(-10);
            var recorder          = new AWSXRayRecorderBuilder().WithExceptionSerializationStrategy(exceptionStrategy).Build(); // set custom stackframe size

            AWSXRayRecorder.InitializeInstance(recorder: recorder);
            DefaultExceptionSerializationStrategy actual = AWSXRayRecorder.Instance.ExceptionSerializationStrategy as DefaultExceptionSerializationStrategy;

            Assert.AreEqual(DefaultExceptionSerializationStrategy.DefaultStackFrameSize, actual.MaxStackFrameSize);
        }
        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 TestSetContextMissingUsingConfiguration2() // Contextmissing startegy not set
        {
#if NET45
            AppSettings.Reset();
            AWSXRayRecorderBuilder builder = new AWSXRayRecorderBuilder().WithContextMissingStrategyFromAppSettings();
#else
            AWSXRayRecorderBuilder builder = new AWSXRayRecorderBuilder().WithContextMissingStrategyFromConfig(_xRayOptions);
#endif
            AWSXRayRecorder recorder = builder.Build();
            Assert.AreEqual(ContextMissingStrategy.RUNTIME_ERROR, recorder.ContextMissingStrategy); // Default context missing strategy is set
        }
Exemplo n.º 13
0
        public void TestInvalidPluginSetting()
        {
#if NET45
            ConfigurationManager.AppSettings[PluginKey] = "UnknownPlugin, IPlugin";
            AppSettings.Reset();
            AWSXRayRecorderBuilder builder = new AWSXRayRecorderBuilder().WithPluginsFromAppSettings();
#else
            _xRayOptions.PluginSetting = "UnknownPlugin, IPlugin";
            AWSXRayRecorderBuilder builder = new AWSXRayRecorderBuilder().WithPluginsFromConfig(_xRayOptions);
#endif
            Assert.AreEqual(0, builder.Plugins.Count);
        }
Exemplo n.º 14
0
        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 TestSetContextMissingUsingConfiguration1() // Contextmissing startegy set to log error from configuration
        {
#if NET45
            ConfigurationManager.AppSettings[UseRuntimeErrors] = "false";
            AppSettings.Reset();
            AWSXRayRecorderBuilder builder = new AWSXRayRecorderBuilder().WithContextMissingStrategyFromAppSettings();
#else
            _xRayOptions.UseRuntimeErrors = false;
            AWSXRayRecorderBuilder builder = new AWSXRayRecorderBuilder().WithContextMissingStrategyFromConfig(_xRayOptions);
#endif
            AWSXRayRecorder recorder = builder.Build();
            Assert.AreEqual(ContextMissingStrategy.LOG_ERROR, recorder.ContextMissingStrategy);
        }
        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);
        }
Exemplo n.º 17
0
        public static IServiceCollection AddAppAWS(this IServiceCollection services,
                                                   IConfiguration configuration,
                                                   IWebHostEnvironment environment)
        {
            if (!environment.IsEnvironment("Testing"))
            {
                AWSXRayRecorder recorder = new AWSXRayRecorderBuilder().Build();
                AWSXRayRecorder.InitializeInstance(configuration, recorder);
                AWSSDKHandler.RegisterXRayForAllServices();
            }

            services.AddAWSService <IAmazonSimpleNotificationService>();

            return(services);
        }
        public void TestSetContextMissingUsingConfiguration3() // Contextmissing startegy is set through environment and configurations
        {
            Environment.SetEnvironmentVariable(AWSXRayRecorder.EnvironmentVariableContextMissingStrategy, "LOG_ERROR");
#if NET45
            ConfigurationManager.AppSettings[UseRuntimeErrors] = "true";
            AppSettings.Reset();
            AWSXRayRecorderBuilder builder = new AWSXRayRecorderBuilder().WithContextMissingStrategyFromAppSettings();
#else
            _xRayOptions.UseRuntimeErrors = true;
            AWSXRayRecorderBuilder builder = new AWSXRayRecorderBuilder().WithContextMissingStrategyFromConfig(_xRayOptions);
#endif
            AWSXRayRecorder recorder = builder.Build();
            Assert.AreEqual(ContextMissingStrategy.LOG_ERROR, recorder.ContextMissingStrategy); // Preference given to environment variable
            Environment.SetEnvironmentVariable(AWSXRayRecorder.EnvironmentVariableContextMissingStrategy, null);
        }
Exemplo n.º 19
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);
        }
Exemplo n.º 20
0
        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"]);
        }
Exemplo n.º 21
0
        public void TestWithDefaultPlugins()
        {
#if NET45
            ConfigurationManager.AppSettings[PluginKey] = "EC2Plugin";
            AppSettings.Reset();
            AWSXRayRecorderBuilder builder = new AWSXRayRecorderBuilder().WithPluginsFromAppSettings();
#else
            _xRayOptions.PluginSetting = "EC2Plugin";
            AWSXRayRecorderBuilder builder = new AWSXRayRecorderBuilder().WithPluginsFromConfig(_xRayOptions);
#endif

            Assert.AreEqual(1, builder.Plugins.Count);
            var expectedType = typeof(EC2Plugin);
            var actualType   = builder.Plugins[0].GetType();
            Assert.AreEqual(expectedType, actualType);
        }
        public static IServiceCollection AddXRayTracing(this IServiceCollection services, IConfiguration config)
        {
            var xRaySamplingRulePath = XRaySamplingSettings.SamplingRuleFileMap[XRaySamplingRule.All];

            IAWSXRayRecorder xRayRecorder = new AWSXRayRecorderBuilder()
                                            .WithSamplingStrategy(new LocalizedSamplingStrategy(xRaySamplingRulePath))
                                            .Build();

            AWSXRayRecorder.InitializeInstance(config);
            AWSSDKHandler.RegisterXRayForAllServices();

            services
            .AddSingleton(xRayRecorder)
            .AddSingleton <XRaySegmentFactory>()
            .AddSingleton <XRaySubsegmentFactory>();

            return(services);
        }
Exemplo n.º 23
0
        private static AWSXRayRecorder BuildAWSXRayRecorder(ISamplingStrategy samplingStrategy, ISegmentEmitter segmentEmitter = null)
        {
            AWSXRayRecorderBuilder builder;

            if (segmentEmitter != null)
            {
                builder = new AWSXRayRecorderBuilder()
                          .WithSamplingStrategy(samplingStrategy).WithSegmentEmitter(segmentEmitter);
            }
            else
            {
                builder = new AWSXRayRecorderBuilder()
                          .WithSamplingStrategy(samplingStrategy);
            }

            var result = builder.Build();

            return(result);
        }
        public void TestSetNullExceptionSerializationStrategy()
        {
            var recorder = new AWSXRayRecorderBuilder().WithExceptionSerializationStrategy(null).Build();

            Assert.Fail();
        }
Exemplo n.º 25
0
        public void TestSetSamplingStrategy()
        {
            var recorder = new AWSXRayRecorderBuilder().WithSamplingStrategy(new DummySamplingStrategy()).Build();

            Assert.AreEqual(typeof(DummySamplingStrategy).FullName, recorder.SamplingStrategy.GetType().FullName);
        }
Exemplo n.º 26
0
        public void TestDefaultValueOfContextMissingStrategy()
        {
            var recorder = new AWSXRayRecorderBuilder().Build();

            Assert.AreEqual(ContextMissingStrategy.RUNTIME_ERROR, recorder.ContextMissingStrategy);
        }
Exemplo n.º 27
0
        public void TestSetContextMissingStrategy()
        {
            var recorder = new AWSXRayRecorderBuilder().WithContextMissingStrategy(ContextMissingStrategy.LOG_ERROR).Build();

            Assert.AreEqual(ContextMissingStrategy.LOG_ERROR, recorder.ContextMissingStrategy);
        }
Exemplo n.º 28
0
        public void TestSetEmitter()
        {
            var recorder = new AWSXRayRecorderBuilder().WithContextMissingStrategy(ContextMissingStrategy.LOG_ERROR).WithSegmentEmitter(new DummyEmitter()).Build();

            Assert.AreEqual(typeof(DummyEmitter).FullName, recorder.Emitter.GetType().FullName);
        }
Exemplo n.º 29
0
        public void TestSetDefaultEmitter()
        {
            var recorder = new AWSXRayRecorderBuilder().WithContextMissingStrategy(ContextMissingStrategy.LOG_ERROR).Build(); // set defualt UDP emitter

            Assert.AreEqual(typeof(UdpSegmentEmitter).FullName, recorder.Emitter.GetType().FullName);
        }
Exemplo n.º 30
0
        public void TestSetNullEmitter()
        {
            var recorder = new AWSXRayRecorderBuilder().WithContextMissingStrategy(ContextMissingStrategy.LOG_ERROR).WithSegmentEmitter(null).Build();

            Assert.Fail();
        }