コード例 #1
0
        protected void VerifyDataReceivedFromAgent(Action <ReceivedData> verifyAction)
        {
            var attemptNumber = 0;

            while (true)
            {
                ++attemptNumber;

                if (!_mockApmServer.ReceivedData.InvalidPayloadErrors.IsEmpty)
                {
                    var messageBuilder = new StringBuilder();
                    messageBuilder.AppendLine("There is at least one invalid payload error - the test is considered as failed.");
                    messageBuilder.AppendLine(TextUtils.AddIndentation("Invalid payload error(s):", 1));
                    foreach (var invalidPayloadError in _mockApmServer.ReceivedData.InvalidPayloadErrors)
                    {
                        messageBuilder.AppendLine(TextUtils.AddIndentation(invalidPayloadError, 2));
                    }
                    throw new XunitException(messageBuilder.ToString());
                }

                try
                {
                    verifyAction(_mockApmServer.ReceivedData);
                    _logger.Debug()
                    ?.Log("Data received from agent passed verification. Attempt #{AttemptNumber} out of {MaxNumberOfAttempts}",
                          attemptNumber, DataSentByAgentVerificationConsts.MaxNumberOfAttemptsToVerify);
                    return;
                }
                catch (XunitException ex)
                {
                    _logger.Debug()
                    ?.LogException(ex,
                                   "Data received from agent did NOT pass verification. Attempt #{AttemptNumber} out of {MaxNumberOfAttempts}",
                                   attemptNumber, DataSentByAgentVerificationConsts.MaxNumberOfAttemptsToVerify);

                    if (attemptNumber == DataSentByAgentVerificationConsts.MaxNumberOfAttemptsToVerify)
                    {
                        _logger.Error()?.LogException(ex, "Reached max number of attempts to verify payload - Rethrowing the last exception...");
                        AnalyzePotentialIssues();
                        throw;
                    }

                    _logger.Debug()
                    ?.Log("Waiting {WaitTimeMs}ms before the next attempt...", DataSentByAgentVerificationConsts.WaitBetweenVerifyAttemptsMs);
                    Thread.Sleep(DataSentByAgentVerificationConsts.WaitBetweenVerifyAttemptsMs);
                }
                catch (Exception ex)
                {
                    _logger.Error()?.LogException(ex, "Exception escaped from verifier");
                    throw;
                }
            }
        }
コード例 #2
0
        private void ParsePayloadLineAndAddToReceivedData(string line)
        {
            var foundDto = false;

            var lineDto = JsonConvert.DeserializeObject <PayloadLineDto>(
                line,
                new JsonSerializerSettings
            {
                MissingMemberHandling = MissingMemberHandling.Error,
                Error = (_, errorEventArgs) =>
                {
                    _logger.Error()
                    ?.Log("Failed to parse payload line as JSON. Error: {PayloadParsingErrorMessage}, line: `{PayloadLine}'",
                          errorEventArgs.ErrorContext.Error.Message, line);
                    throw new ArgumentException(errorEventArgs.ErrorContext.Error.Message);
                }
            });

            _mockApmServer.ReceivedData.Errors       = HandleParsed("Error", lineDto.Error, _mockApmServer.ReceivedData.Errors);
            _mockApmServer.ReceivedData.Metadata     = HandleParsed("Metadata", lineDto.Metadata, _mockApmServer.ReceivedData.Metadata);
            _mockApmServer.ReceivedData.Metrics      = HandleParsed("MetricSet", lineDto.MetricSet, _mockApmServer.ReceivedData.Metrics);
            _mockApmServer.ReceivedData.Spans        = HandleParsed("Span", lineDto.Span, _mockApmServer.ReceivedData.Spans);
            _mockApmServer.ReceivedData.Transactions = HandleParsed("Transaction", lineDto.Transaction, _mockApmServer.ReceivedData.Transactions);

            foundDto.Should().BeTrue($"There should be exactly one object per line: `{line}'");

            ImmutableList <TDto> HandleParsed <TDto>(string dtoType, TDto dto, ImmutableList <TDto> accumulatingList) where TDto : IDto
            {
                if (dto == null)
                {
                    return(accumulatingList);
                }

                foundDto.Should().BeFalse($"There should be exactly one object per line: `{line}'");
                foundDto = true;

                try
                {
                    dto.AssertValid();
                }
                catch (XunitException ex)
                {
                    _logger.Error()
                    ?.LogException(ex, "{DtoType} #{DtoSeqNum} was parsed successfully but it didn't pass semantic verification. " +
                                   "\n" + TextUtils.Indentation + "Input line (pretty formatted):\n{FormattedPayloadLine}" +
                                   "\n" + TextUtils.Indentation + "Parsed object:\n{Dto}",
                                   dtoType, accumulatingList.Count,
                                   TextUtils.AddIndentation(JsonUtils.PrettyFormat(line), 2),
                                   TextUtils.AddIndentation(dto.ToString(), 2));
                    _mockApmServer.ReceivedData.InvalidPayloadErrors = _mockApmServer.ReceivedData.InvalidPayloadErrors.Add(ex.ToString());
                    return(accumulatingList);
                }

                _logger.Debug()
                ?.Log("Successfully parsed and verified {DtoType} #{DtoSeqNum}." +
                      "\n" + TextUtils.Indentation + "Input line (pretty formatted):\n{FormattedPayloadLine}" +
                      "\n" + TextUtils.Indentation + "Parsed object:\n{Dto}",
                      dtoType, accumulatingList.Count + 1,
                      TextUtils.AddIndentation(JsonUtils.PrettyFormat(line), 2),
                      TextUtils.AddIndentation(dto.ToString(), 2));

                return(accumulatingList.Add(dto));
            }
        }