public void SubmitsTraces()
        {
            int agentPort      = TcpPortProvider.GetOpenPort();
            int aspNetCorePort = TcpPortProvider.GetOpenPort();

            using (var agent = new MockTracerAgent(agentPort))
                using (Process process = StartSample(agent.Port, arguments: null, packageVersion: string.Empty, aspNetCorePort: aspNetCorePort))
                {
                    var wh = new EventWaitHandle(false, EventResetMode.AutoReset);

                    process.OutputDataReceived += (sender, args) =>
                    {
                        if (args.Data != null)
                        {
                            if (args.Data.Contains("Now listening on:") || args.Data.Contains("Unable to start Kestrel"))
                            {
                                wh.Set();
                            }

                            Output.WriteLine($"[webserver][stdout] {args.Data}");
                        }
                    };
                    process.BeginOutputReadLine();

                    process.ErrorDataReceived += (sender, args) =>
                    {
                        if (args.Data != null)
                        {
                            Output.WriteLine($"[webserver][stderr] {args.Data}");
                        }
                    };
                    process.BeginErrorReadLine();

                    // wait for server to start
                    wh.WaitOne(5000);

                    SubmitRequests(aspNetCorePort);
                    var graphQLValidateSpans = agent.WaitForSpans(_expectedGraphQLValidateSpanCount, operationName: _graphQLValidateOperationName, returnAllOperations: false)
                                               .GroupBy(s => s.SpanId)
                                               .Select(grp => grp.First())
                                               .OrderBy(s => s.Start);
                    var graphQLExecuteSpans = agent.WaitForSpans(_expectedGraphQLExecuteSpanCount, operationName: _graphQLExecuteOperationName, returnAllOperations: false)
                                              .GroupBy(s => s.SpanId)
                                              .Select(grp => grp.First())
                                              .OrderBy(s => s.Start);

                    if (!process.HasExited)
                    {
                        process.Kill();
                    }

                    var spans = graphQLValidateSpans.Concat(graphQLExecuteSpans).ToList();
                    SpanTestHelpers.AssertExpectationsMet(_expectations, spans);
                }
        }
Exemplo n.º 2
0
        public void SubmitsTraces(bool enableCallTarget)
        {
            SetCallTargetSettings(enableCallTarget);

            int agentPort      = TcpPortProvider.GetOpenPort();
            int aspNetCorePort = TcpPortProvider.GetOpenPort();

            using (var agent = new MockTracerAgent(agentPort))
                using (Process process = StartSample(agent.Port, arguments: null, packageVersion: string.Empty, aspNetCorePort: aspNetCorePort))
                {
                    var wh = new EventWaitHandle(false, EventResetMode.AutoReset);

                    process.OutputDataReceived += (sender, args) =>
                    {
                        if (args.Data != null)
                        {
                            if (args.Data.Contains("Now listening on:") || args.Data.Contains("Unable to start Kestrel"))
                            {
                                wh.Set();
                            }

                            Output.WriteLine($"[webserver][stdout] {args.Data}");
                        }
                    };
                    process.BeginOutputReadLine();

                    process.ErrorDataReceived += (sender, args) =>
                    {
                        if (args.Data != null)
                        {
                            Output.WriteLine($"[webserver][stderr] {args.Data}");
                        }
                    };
                    process.BeginErrorReadLine();

                    wh.WaitOne(5000);

                    var maxMillisecondsToWait = 15_000;
                    var intervalMilliseconds  = 500;
                    var intervals             = maxMillisecondsToWait / intervalMilliseconds;
                    var serverReady           = false;

                    // wait for server to be ready to receive requests
                    while (intervals-- > 0)
                    {
                        var aliveCheckRequest = new RequestInfo()
                        {
                            HttpMethod = "GET", Url = "/alive-check"
                        };
                        try
                        {
                            serverReady = SubmitRequest(aspNetCorePort, aliveCheckRequest, false) == HttpStatusCode.OK;
                        }
                        catch
                        {
                            // ignore
                        }

                        if (serverReady)
                        {
                            Output.WriteLine("The server is ready.");
                            break;
                        }

                        Thread.Sleep(intervalMilliseconds);
                    }

                    if (!serverReady)
                    {
                        throw new Exception("Couldn't verify the application is ready to receive requests.");
                    }

                    var testStart = DateTime.Now;

                    SubmitRequests(aspNetCorePort);
                    var graphQLValidateSpans = agent.WaitForSpans(_expectedGraphQLValidateSpanCount, operationName: _graphQLValidateOperationName, returnAllOperations: false)
                                               .GroupBy(s => s.SpanId)
                                               .Select(grp => grp.First())
                                               .OrderBy(s => s.Start);
                    var graphQLExecuteSpans = agent.WaitForSpans(_expectedGraphQLExecuteSpanCount, operationName: _graphQLExecuteOperationName, returnAllOperations: false)
                                              .GroupBy(s => s.SpanId)
                                              .Select(grp => grp.First())
                                              .OrderBy(s => s.Start);

                    if (!process.HasExited)
                    {
                        process.Kill();
                    }

                    var spans = graphQLValidateSpans.Concat(graphQLExecuteSpans).ToList();
                    SpanTestHelpers.AssertExpectationsMet(_expectations, spans);
                }
        }
Exemplo n.º 3
0
        protected void RunSubmitsTraces(string packageVersion = "")
        {
            using var telemetry = this.ConfigureTelemetry();
            int?aspNetCorePort = null;

            using (var agent = EnvironmentHelper.GetMockAgent())
                using (Process process = StartSample(agent, arguments: null, packageVersion: packageVersion, aspNetCorePort: 0))
                {
                    var wh = new EventWaitHandle(false, EventResetMode.AutoReset);

                    process.OutputDataReceived += (sender, args) =>
                    {
                        if (args.Data != null)
                        {
                            if (args.Data.Contains("Now listening on:"))
                            {
                                var splitIndex = args.Data.LastIndexOf(':');
                                aspNetCorePort = int.Parse(args.Data.Substring(splitIndex + 1));

                                wh.Set();
                            }
                            else if (args.Data.Contains("Unable to start Kestrel"))
                            {
                                wh.Set();
                            }

                            Output.WriteLine($"[webserver][stdout] {args.Data}");
                        }
                    };
                    process.BeginOutputReadLine();

                    process.ErrorDataReceived += (sender, args) =>
                    {
                        if (args.Data != null)
                        {
                            Output.WriteLine($"[webserver][stderr] {args.Data}");
                        }
                    };
                    process.BeginErrorReadLine();

                    wh.WaitOne(15_000);
                    if (!aspNetCorePort.HasValue)
                    {
                        throw new Exception("Unable to determine port application is listening on");
                    }

                    Output.WriteLine($"The ASP.NET Core server is ready on port {aspNetCorePort}");

                    SubmitRequests(aspNetCorePort.Value);
                    var graphQLValidateSpans = agent.WaitForSpans(_expectedGraphQLValidateSpanCount, operationName: _graphQLValidateOperationName, returnAllOperations: false)
                                               .GroupBy(s => s.SpanId)
                                               .Select(grp => grp.First())
                                               .OrderBy(s => s.Start);
                    var graphQLExecuteSpans = agent.WaitForSpans(_expectedGraphQLExecuteSpanCount, operationName: _graphQLExecuteOperationName, returnAllOperations: false)
                                              .GroupBy(s => s.SpanId)
                                              .Select(grp => grp.First())
                                              .OrderBy(s => s.Start);

                    if (!process.HasExited)
                    {
                        // Try shutting down gracefully
                        var shutdownRequest = new RequestInfo()
                        {
                            HttpMethod = "GET", Url = "/shutdown"
                        };
                        SubmitRequest(aspNetCorePort.Value, shutdownRequest);

                        if (!process.WaitForExit(5000))
                        {
                            Output.WriteLine("The process didn't exit in time. Killing it.");
                            process.Kill();
                        }
                    }

                    var spans = graphQLValidateSpans.Concat(graphQLExecuteSpans).ToList();
                    SpanTestHelpers.AssertExpectationsMet(_expectations, spans);
                }

            telemetry.AssertIntegrationEnabled(IntegrationId.GraphQL);
        }