Ejemplo n.º 1
0
        public void Init()
        {
            var monitors = MonitorBuilder.Build("TestMonitors.xml");

            instance       = monitors["HBus"].MonitoredObjs["HaiYi queue_msgcenter_cmds"];
            remoteInstance = monitors["HBus"].MonitoredObjs["gupdate"];
        }
        public async Task ShouldCaptureFailedApplicationEvent()
        {
            var          sink      = new TestSink();
            const string operation = "slow test";

            string expectedMessage = null;

            try
            {
                using (var monitor = new MonitorBuilder(sink).Begin(User, operation))
                {
                    monitor.Action(BreakStuff);
                    await monitor.Complete();
                }
            }
            catch (ApplicationException e)
            {
                expectedMessage = e.Message;
            }

            sink.SentEvents.Count.Should().Be(1);
            var actual = sink.SentEvents.First();

            actual.User.Should().Be(User);
            actual.Operation.Should().Be(operation);
            actual.Outcome.Should().Be(Outcome.Failed);
            actual.FailureCause.Should().Be(expectedMessage);
        }
        public void ShouldCaptureApplicationEventForActionWithOperationStep()
        {
            var          sink      = new TestSink();
            const string operation = "simple test";

            using (var monitor = new MonitorBuilder(sink).Begin(User, operation))
            {
                monitor.Action(DoStuff, "this is a step");
            }

            sink.SentEvents.First().Operation.Should().Be(operation);
            sink.SentEvents.First().OperationStep.Should().Be("this is a step");
        }
        public async Task ShouldCaptureSlowApplicationEvent()
        {
            var          sink      = new TestSink();
            const string operation = "slow test";

            using (var monitor = new MonitorBuilder(sink).Begin(User, operation, acceptableDurationMilliseconds: 1000))
            {
                var result = monitor.Function(() => ReturnStuff(2000));
                result.Should().BeTrue();

                await monitor.Complete();
            }

            sink.SentEvents.Count.Should().Be(1);
            var actual = sink.SentEvents.First();

            actual.User.Should().Be(User);
            actual.Operation.Should().Be(operation);
            actual.Outcome.Should().Be(Outcome.Slow);
        }
        public void ShouldCaptureApplicationEventAndMeasureDuration()
        {
            var          sink      = new TestSink();
            const string operation = "duration test";

            using (var monitor = new MonitorBuilder(sink).Begin(User, operation))
            {
                var result = monitor.Function(() => ReturnStuff(1000));
                result.Should().BeTrue();
            }

            sink.SentEvents.Count.Should().Be(1);
            var actual = sink.SentEvents.First();

            actual.User.Should().Be(User);
            actual.Operation.Should().Be(operation);
            actual.OperationStep.Should().BeNull();
            actual.Outcome.Should().Be(Outcome.Successful);
            sink.SentEvents.First().Duration.Should().BeGreaterOrEqualTo(1000.0);
        }
        public void ShouldCaptureApplicationEventForFunc()
        {
            Clock.Utc.Freeze();

            var          sink      = new TestSink();
            const string operation = "func test";

            using (var monitor = new MonitorBuilder(sink).Begin(User, operation))
            {
                var result = monitor.Function(ReturnStuff);
                result.Should().BeTrue();
            }

            sink.SentEvents.Count.Should().Be(1);
            sink.SentEvents.First().User.Should().Be(User);
            sink.SentEvents.First().Operation.Should().Be(operation);
            sink.SentEvents.First().OperationStep.Should().BeNull();
            sink.SentEvents.First().Outcome.Should().Be(Outcome.Successful);
            sink.SentEvents.First().Timestamp.Should().Be(Clock.Utc.Now);
            sink.SentEvents.First().Duration.Should().Be(0.0);
        }
        public void ShouldCaptureApplicationEventForAction()
        {
            Clock.Utc.Freeze();

            var          sink      = new TestSink();
            const string operation = "simple test";

            using (var monitor = new MonitorBuilder(sink).Begin(User, operation))
            {
                monitor.Action(DoStuff);
            }

            sink.SentEvents.Count.Should().Be(1);
            sink.SentEvents.First().Identifier.Should().NotBeNull();
            sink.SentEvents.First().User.Should().Be(User);
            sink.SentEvents.First().Operation.Should().Be(operation);
            sink.SentEvents.First().OperationStep.Should().BeNull();
            sink.SentEvents.First().Outcome.Should().Be(Outcome.Successful);
            sink.SentEvents.First().Timestamp.Should().Be(Clock.Utc.Now);
            sink.SentEvents.First().Duration.Should().Be(0.0);
        }
        public static IServiceCollection AddDapper <TDbProvider>(this IServiceCollection services, Action <MonitorBuilder> monitorBuilder = null) where TDbProvider : IDapper
        {
            if (monitorBuilder != null)
            {
                var builder = new MonitorBuilder(services);
                monitorBuilder.Invoke(builder);
                services.AddSingleton(new MonitorConfiguration
                {
                    SlowCriticalValue       = builder.Threshold,
                    EnableLog               = builder.EnableLog,
                    HasCustomMonitorHandler = builder.HasCustomMonitorHandler
                });
                services.AddScoped(typeof(TDbProvider))
                .AddScoped <IDapper>(sc => new DapperProxy(sc.GetRequiredService <TDbProvider>(), sc.GetRequiredService <IServiceProvider>()));
            }
            else
            {
                services.AddScoped(typeof(IDapper), typeof(TDbProvider));
            }

            return(services.AddSingleton <IConnectionStringProvider, DefaultConnectionStringProvider>());
        }
        public async Task ShouldCaptureMultipleApplicationEventsWithStepNames()
        {
            var          sink      = new TestSink();
            const string operation = "multi test";

            using (var monitor = new MonitorBuilder(sink).Begin(User, operation))
            {
                monitor.Action(DoStuff, "do stuff");

                monitor.Function(ReturnStuff, "return stuff").Should().BeTrue();

                (await monitor.Function(ReturnStuffAsync, "return stuff async")).Should().BeTrue();

                await monitor.Complete();
            }

            sink.SentEvents.Count.Should().Be(3);
            sink.SentEvents[0].Operation.Should().Be(operation);
            sink.SentEvents[0].OperationStep.Should().Be("do stuff");
            sink.SentEvents[1].Operation.Should().Be(operation);
            sink.SentEvents[1].OperationStep.Should().Be("return stuff");
            sink.SentEvents[2].Operation.Should().Be(operation);
            sink.SentEvents[2].OperationStep.Should().Be("return stuff async");
        }
Ejemplo n.º 10
0
        public void TestBuild()
        {
            var monitors = MonitorBuilder.Build();

            Assert.IsTrue(monitors.Count > 0);
        }
Ejemplo n.º 11
0
        public void TestLoadConfig()
        {
            var monitors = MonitorBuilder.LoadConfig("Monitors.xml");

            Assert.IsTrue(monitors.Count > 0);
        }
Ejemplo n.º 12
0
        public void Init()
        {
            var monitors = MonitorBuilder.Build("TestMonitors.xml");

            instance = monitors["HBus"].MonitoredObjs["TestWeb"];
        }
Ejemplo n.º 13
0
        public static ContainerBuilder AddDapper <TDbProvider>(this ContainerBuilder container, string connectionName = "DefaultConnection", string serviceKey = null, bool enableMasterSlave = false, Action <MonitorBuilder> monitorBuilder = null) where TDbProvider : IDapper
        {
            container.RegisterType <ResolveContext>().As <IResolveContext>().IfNotRegistered(typeof(IResolveContext)).InstancePerLifetimeScope();
            container.RegisterType <ResolveKeyed>().As <IResolveKeyed>().IfNotRegistered(typeof(IResolveKeyed)).InstancePerLifetimeScope();
            container.RegisterType <DefaultConnectionStringProvider>().As <IConnectionStringProvider>().SingleInstance();
            container.RegisterType <WeightedPolling>().As <ILoadBalancing>().SingleInstance();

            if (monitorBuilder != null)
            {
                var builder = new MonitorBuilder(container);
                monitorBuilder.Invoke(builder);
                container.RegisterInstance(new MonitorConfiguration
                {
                    SlowCriticalValue       = builder.Threshold,
                    EnableLog               = builder.EnableLog,
                    HasCustomMonitorHandler = builder.HasCustomMonitorHandler
                }).SingleInstance();
            }

            if (string.IsNullOrWhiteSpace(serviceKey))
            {
                if (enableMasterSlave)
                {
                    if (monitorBuilder != null)
                    {
                        container.RegisterType <TDbProvider>().WithParameters(new[]
                        {
                            new NamedParameter("connectionName", connectionName),
                            new NamedParameter("enableMasterSlave", true)
                        }).InstancePerLifetimeScope();
                        container.Register <IDapper>((ctx, @params) => new DapperProxy(ctx.Resolve <TDbProvider>(@params), ctx.Resolve <IServiceProvider>())).InstancePerLifetimeScope();



                        container.RegisterType <TDbProvider>().Keyed <TDbProvider>("_slave").WithParameters(new[]
                        {
                            new NamedParameter("connectionName", connectionName),
                            new NamedParameter("enableMasterSlave", true)
                        }).InstancePerLifetimeScope();
                        container.Register <IDapper>((ctx, @params) => new DapperProxy(ctx.ResolveKeyed <TDbProvider>("_slave", @params), ctx.Resolve <IServiceProvider>()))
                        .InstancePerLifetimeScope();
                    }
                    else
                    {
                        container.RegisterType <TDbProvider>().As <IDapper>().WithParameters(new[]
                        {
                            new NamedParameter("connectionName", connectionName),
                            new NamedParameter("enableMasterSlave", true)
                        }).InstancePerLifetimeScope();
                        container.RegisterType <TDbProvider>().Keyed <IDapper>("_slave").WithParameters(new[]
                        {
                            new NamedParameter("connectionName", connectionName),
                            new NamedParameter("enableMasterSlave", true)
                        }).InstancePerLifetimeScope();
                    }
                }
                else
                {
                    if (monitorBuilder != null)
                    {
                        container.RegisterType <TDbProvider>().WithParameter("connectionName", connectionName).InstancePerLifetimeScope();
                        container.Register <IDapper>((ctx, @params) => new DapperProxy(ctx.Resolve <TDbProvider>(@params), ctx.Resolve <IServiceProvider>())).InstancePerLifetimeScope();
                    }
                    else
                    {
                        container.RegisterType <TDbProvider>().As <IDapper>().WithParameter("connectionName", connectionName)
                        .InstancePerLifetimeScope();
                    }
                }
            }
            else
            {
                if (enableMasterSlave)
                {
                    if (monitorBuilder != null)
                    {
                        container.RegisterType <TDbProvider>().Keyed <TDbProvider>(serviceKey).WithParameters(new[]
                        {
                            new NamedParameter("connectionName", connectionName),
                            new NamedParameter("enableMasterSlave", true)
                        }).InstancePerLifetimeScope();
                        container.Register <IDapper>((ctx, @params) => new DapperProxy(ctx.ResolveKeyed <TDbProvider>(serviceKey, @params), ctx.Resolve <IServiceProvider>())).Keyed <IDapper>(serviceKey).InstancePerLifetimeScope();

                        container.RegisterType <TDbProvider>().Keyed <TDbProvider>($"{serviceKey}_slave").WithParameters(new[]
                        {
                            new NamedParameter("connectionName", connectionName),
                            new NamedParameter("enableMasterSlave", true)
                        }).InstancePerLifetimeScope();
                        container.Register <IDapper>((ctx, @params) => new DapperProxy(ctx.ResolveKeyed <TDbProvider>($"{serviceKey}_slave", @params), ctx.Resolve <IServiceProvider>())).Keyed <IDapper>($"{serviceKey}_slave").InstancePerLifetimeScope();
                    }
                    else
                    {
                        container.RegisterType <TDbProvider>().Keyed <IDapper>(serviceKey).WithParameters(new[]
                        {
                            new NamedParameter("connectionName", connectionName),
                            new NamedParameter("enableMasterSlave", true)
                        }).InstancePerLifetimeScope();
                        container.RegisterType <TDbProvider>().Keyed <IDapper>($"{serviceKey}_slave").WithParameters(new[]
                        {
                            new NamedParameter("connectionName", connectionName),
                            new NamedParameter("enableMasterSlave", true)
                        }).InstancePerLifetimeScope();
                    }
                }
                else
                {
                    if (monitorBuilder != null)
                    {
                        container.RegisterType <TDbProvider>().Keyed <TDbProvider>(serviceKey)
                        .WithParameter("connectionName", connectionName).InstancePerLifetimeScope();
                        container.Register <IDapper>((ctx, @params) =>
                                                     new DapperProxy(ctx.ResolveKeyed <TDbProvider>(serviceKey, @params), ctx.Resolve <IServiceProvider>())).InstancePerLifetimeScope();
                    }
                    else
                    {
                        container.RegisterType <TDbProvider>().Keyed <IDapper>(serviceKey)
                        .WithParameter("connectionName", connectionName).InstancePerLifetimeScope();
                    }
                }
            }


            return(container);
        }
        public void Init()
        {
            var monitors = MonitorBuilder.Build("TestMonitors.xml");

            instance = monitors["XXX"].MonitoredObjs["TestSchTask"];
        }