コード例 #1
0
        public async Task Auto_Instrument_With_StartupHook_Should_Capture_Error(string targetFramework)
        {
            var apmLogger = new InMemoryBlockingLogger(LogLevel.Error);
            var apmServer = new MockApmServer(apmLogger, nameof(Auto_Instrument_With_StartupHook_Should_Capture_Error));
            var port      = apmServer.FindAvailablePortToListen();

            apmServer.RunInBackground(port);
            var transactionWaitHandle = new ManualResetEvent(false);
            var errorWaitHandle       = new ManualResetEvent(false);

            apmServer.OnReceive += o =>
            {
                if (o is TransactionDto)
                {
                    transactionWaitHandle.Set();
                }
                if (o is ErrorDto)
                {
                    errorWaitHandle.Set();
                }
            };

            using (var sampleApp = new SampleApplication())
            {
                var environmentVariables = new Dictionary <string, string>
                {
                    [EnvVarNames.ServerUrl]     = $"http://localhost:{port}",
                    [EnvVarNames.CloudProvider] = "none"
                };

                var uri     = sampleApp.Start(targetFramework, environmentVariables);
                var builder = new UriBuilder(uri)
                {
                    Path = "Home/Exception"
                };
                var client   = new HttpClient();
                var response = await client.GetAsync(builder.Uri);

                response.IsSuccessStatusCode.Should().BeFalse();

                transactionWaitHandle.WaitOne(TimeSpan.FromMinutes(2));
                apmServer.ReceivedData.Transactions.Should().HaveCount(1);

                var transaction = apmServer.ReceivedData.Transactions.First();
                transaction.Name.Should().Be("GET Home/Exception");

                errorWaitHandle.WaitOne(TimeSpan.FromMinutes(2));
                apmServer.ReceivedData.Errors.Should().HaveCount(1);

                var error = apmServer.ReceivedData.Errors.First();
                error.Culprit.Should().Be("Elastic.Apm.StartupHook.Sample.Controllers.HomeController");
            }

            await apmServer.StopAsync();
        }
コード例 #2
0
        public async Task Auto_Instrument_With_StartupHook_Should_Capture_Metadata(
            string targetFramework,
            string expectedRuntimeName,
            string expectedFrameworkVersion)
        {
            var apmLogger = new InMemoryBlockingLogger(LogLevel.Error);
            var apmServer = new MockApmServer(apmLogger, nameof(Auto_Instrument_With_StartupHook_Should_Capture_Metadata));
            var port      = apmServer.FindAvailablePortToListen();

            apmServer.RunInBackground(port);

            var waitHandle = new ManualResetEvent(false);

            apmServer.OnReceive += o =>
            {
                if (o is MetadataDto)
                {
                    waitHandle.Set();
                }
            };

            using (var sampleApp = new SampleApplication())
            {
                var environmentVariables = new Dictionary <string, string>
                {
                    [EnvVarNames.ServerUrl]     = $"http://localhost:{port}",
                    [EnvVarNames.CloudProvider] = "none"
                };

                var uri      = sampleApp.Start(targetFramework, environmentVariables);
                var client   = new HttpClient();
                var response = await client.GetAsync(uri);

                response.IsSuccessStatusCode.Should().BeTrue();

                waitHandle.WaitOne(TimeSpan.FromMinutes(2));
                apmServer.ReceivedData.Metadata.Should().HaveCountGreaterOrEqualTo(1);

                var metadata = apmServer.ReceivedData.Metadata.First();
                metadata.Service.Runtime.Name.Should().Be(expectedRuntimeName);
                metadata.Service.Framework.Name.Should().Be("ASP.NET Core");
                metadata.Service.Framework.Version.Should().Be(expectedFrameworkVersion);
            }

            await apmServer.StopAsync();
        }