Exemple #1
0
        public HttpResponseHeaders GetRestSharpTaskResultClientWithHeaders(string method, bool generic, bool cancelable)
        {
            var address = $"http://{DestinationServerName}:{Port}/RestSharp/TaskResultClient?method={method}&generic={generic}&cancelable={cancelable}";

            using (var httpClient = new HttpClient())
            {
                var requestData = new CrossApplicationRequestData("guid", false, "tripId", "pathHash");

                var headers = new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("X-NewRelic-ID", GetXNewRelicId()),
                    new KeyValuePair <string, string>("X-NewRelic-Transaction", GetXNewRelicRequestData(requestData))
                };

                var httpRequestMessage = new HttpRequestMessage {
                    RequestUri = new Uri(address), Method = HttpMethod.Get
                };
                foreach (var header in headers)
                {
                    httpRequestMessage.Headers.Add(header.Key, header.Value);
                }

                return(Task.Run(() => httpClient.SendAsync(httpRequestMessage)).Result.Headers);
            }
        }
Exemple #2
0
        private void UpdateTransactionMetadata(IInternalTransaction transaction, CrossApplicationRequestData crossApplicationRequestData, long?contentLength)
        {
            if (transaction.TransactionMetadata.CrossApplicationReferrerTransactionGuid != null)
            {
                _catMetricCounters.Record(CATSupportabilityCondition.Request_Accept_Multiple);
                //We don't return here to support legacy behavior.
            }

            if (crossApplicationRequestData.TripId != null)
            {
                transaction.TransactionMetadata.SetCrossApplicationReferrerTripId(crossApplicationRequestData.TripId);
            }

            if (contentLength != null && contentLength.Value > 0)
            {
                transaction.TransactionMetadata.SetCrossApplicationReferrerContentLength(contentLength.Value);
            }

            if (crossApplicationRequestData.PathHash != null)
            {
                transaction.TransactionMetadata.SetCrossApplicationReferrerPathHash(crossApplicationRequestData.PathHash);
            }

            if (crossApplicationRequestData.TransactionGuid != null)
            {
                transaction.TransactionMetadata.SetCrossApplicationReferrerTransactionGuid(crossApplicationRequestData.TransactionGuid);
            }
        }
Exemple #3
0
        public void SerializeAndEncode_CreatesCorrectEncodedString_IfNonNullEncodingKey()
        {
            var data = new CrossApplicationRequestData("guid", false, "tripId", "pathHash");

            var encoded = HeaderEncoder.EncodeSerializedData(JsonConvert.SerializeObject(data), "encodingKey");

            Assert.AreEqual("PkwEGg0NTEstBBUWC09NEBsHFwIBW0lMEw4QASYGOA1bOA==", encoded);
        }
Exemple #4
0
        public void SerializeAndEncode_CreatesCorrectEncodedString_IfNullEncodingKey()
        {
            var data = new CrossApplicationRequestData("guid", false, "tripId", "pathHash");

            var encoded = HeaderEncoder.EncodeSerializedData(JsonConvert.SerializeObject(data), null);

            Assert.AreEqual("WyJndWlkIixmYWxzZSwidHJpcElkIiwicGF0aEhhc2giXQ==", encoded);
        }
Exemple #5
0
        /// <summary>
        /// Makes a request, optionally including CAT headers, to the "Chained" endpoint (which will itself make a request).
        /// </summary>

        public HttpResponseHeaders GetWithCatHeaderChainedHttpClient(CrossApplicationRequestData requestData)
        {
            var headers = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("X-NewRelic-ID", GetXNewRelicId()),
                new KeyValuePair <string, string>("X-NewRelic-Transaction", GetXNewRelicRequestData(requestData))
            };

            const string action      = "Index";
            var          queryString = $"?chainedServerName={DestinationServerName}&chainedPortNumber={Port}&chainedAction={action}";

            return(GetWithHeaders(headers, "ChainedHttpClient", queryString));
        }
        public void SetUp()
        {
            _compositeTestAgent = new CompositeTestAgent();
            _compositeTestAgent.LocalConfiguration.distributedTracing.enabled     = false;
            _compositeTestAgent.LocalConfiguration.crossApplicationTracingEnabled = true;
            _compositeTestAgent.ServerConfiguration.AccountId            = _accountId.ToString();
            _compositeTestAgent.ServerConfiguration.TrustedAccountKey    = _trustKey;
            _compositeTestAgent.ServerConfiguration.PrimaryApplicationId = _appId.ToString();
            _catMetricCounters = _compositeTestAgent.Container.Resolve <ICATSupportabilityMetricCounters>();

            var crossProcessID = $"{_accountId}#{_appId}";

            _crossProcessIDEncoded = Strings.Base64Encode(crossProcessID, _agent.Configuration.EncodingKey);

            var reqData = new CrossApplicationRequestData("referrerTransactionGuid", false, "referrerTripId", "referrerPathHash");

            _reqDataEncoded = HeaderEncoder.SerializeAndEncode(reqData, _agent.Configuration.EncodingKey);

            _compositeTestAgent.ServerConfiguration.TrustedIds = new long[] { _accountId };

            _compositeTestAgent.PushConfiguration();
        }
Exemple #7
0
 private string GetXNewRelicRequestData(CrossApplicationRequestData requestData)
 {
     return(HeaderEncoder.SerializeAndEncode(requestData, HeaderEncoder.IntegrationTestEncodingKey));
 }
Exemple #8
0
        public HttpResponseHeaders GetWithCatHeader(bool includeCrossProcessIdHeader = true, CrossApplicationRequestData requestData = null)
        {
            var headers = new List <KeyValuePair <string, string> >();

            if (includeCrossProcessIdHeader)
            {
                headers.Add(new KeyValuePair <string, string>("X-NewRelic-ID", GetXNewRelicId()));
            }
            if (requestData != null)
            {
                headers.Add(new KeyValuePair <string, string>("X-NewRelic-Transaction", GetXNewRelicRequestData(requestData)));
            }

            return(GetWithHeaders(headers, "Index"));
        }
        public void Test(TestCase testCase)
        {
            Mock.Arrange(() => _configuration.ApplicationNames).Returns(new[] { testCase.AppName });

            // ConvertToImmutableTransaction a transaction for the test data
            _transaction = GetTransactionBuilderFor(_configuration, testCase);

            // Simulate external requests as dictated by the test data
            var namePriority = 10;
            var expectedAndActualOutboundRequestPayloads = new Dictionary <CrossApplicationRequestData, CrossApplicationRequestData>();

            testCase.OutboundRequests?.ForEach(request =>
            {
                var transactionName = GetTransactionNameFromString(request.OutboundTxnName);
                _transaction.CandidateTransactionName.TrySet(transactionName, (TransactionNamePriority)namePriority++);
                var outboundHeaders       = _agent.CurrentTransaction.GetRequestMetadata().ToDictionary();
                var actualOutboundPayload = _catHeaderHandler.TryDecodeInboundRequestHeaders(outboundHeaders, GetHeaderValue);
                var requestData           = new CrossApplicationRequestData(
                    (string)request.ExpectedOutboundPayload[0],
                    (bool)request.ExpectedOutboundPayload[1],
                    (string)request.ExpectedOutboundPayload[2],
                    (string)request.ExpectedOutboundPayload[3]
                    );
                expectedAndActualOutboundRequestPayloads.Add(requestData, actualOutboundPayload);
                _transaction.TransactionMetadata.MarkHasCatResponseHeaders();

                List <string> GetHeaderValue(Dictionary <string, string> headers, string key)
                {
                    var headerValues = new List <string>();
                    foreach (var item in headers)
                    {
                        if (item.Key.Equals(key, StringComparison.OrdinalIgnoreCase))
                        {
                            headerValues.Add(item.Value);
                        }
                    }
                    return(headerValues);
                }
            });

            // Simulate the transaction ending (this logic is normally performed by Agent.EndTransaction)
            _transaction.CandidateTransactionName.TrySet(GetTransactionNameFromString(testCase.TransactionName), (TransactionNamePriority)9999);
            var currentTransactionName       = _transaction.CandidateTransactionName.CurrentTransactionName;
            var currentTransactionMetricName = _transactionMetricNameMaker.GetTransactionMetricName(currentTransactionName);
            var pathHash = _pathHashMaker.CalculatePathHash(currentTransactionMetricName.PrefixedName, _transaction.TransactionMetadata.CrossApplicationReferrerPathHash);

            _transaction.TransactionMetadata.SetCrossApplicationPathHash(pathHash);
            var transaction = _transaction.ConvertToImmutableTransaction();
            var totalTime   = transaction.Duration;

            // Get the attributes that would be created for this transaction
            var transactionMetricName = _transactionMetricNameMaker.GetTransactionMetricName(transaction.TransactionName);
            var txStats    = new TransactionMetricStatsCollection(transactionMetricName);
            var attributes = _transactionAttributeMaker.GetAttributes(transaction, transactionMetricName, null, totalTime, txStats);
            var intrinsics = attributes.GetAttributeValuesDic(AttributeClassification.Intrinsics);

            // Run assertions
            testCase.ExpectedIntrinsicFields.ForEach(kvp =>
            {
                Assert.True(intrinsics.ContainsKey(kvp.Key), $"Expected intrinsic attribute '{kvp.Key}' was not found");
                Assert.AreEqual(kvp.Value, intrinsics[kvp.Key], $"Attribute '{kvp.Key}': expected value '{kvp.Value}' but found '{intrinsics[kvp.Key]}'");
            });

            testCase.NonExpectedIntrinsicFields.ForEach(field =>
            {
                Assert.False(intrinsics.ContainsKey(field), $"Found unexpected intrinsic attribute '{field}'");
            });

            if (testCase.OutboundRequests != null)
            {
                expectedAndActualOutboundRequestPayloads.ForEach(kvp =>
                {
                    var expected = kvp.Key;
                    var actual   = kvp.Value;
                    Assert.NotNull(actual, "Outbound request did not have any CAT headers");
                    Assert.AreEqual(expected.TransactionGuid, actual.TransactionGuid, $"Expected outbound.TransactionGuid to be '{expected.TransactionGuid}' but found '{actual.TransactionGuid}'");
                    Assert.AreEqual(expected.PathHash, actual.PathHash, $"Expected outbound.PathHash to be '{expected.TransactionGuid}' but found '{actual.TransactionGuid}'");
                    Assert.AreEqual(expected.TripId, actual.TripId, $"Expected outbound.TripId to be '{expected.TransactionGuid}' but found '{actual.TransactionGuid}'");
                    Assert.AreEqual(expected.Unused, actual.Unused, $"Expected outbound.Unused to be '{expected.Unused}' but found '{actual.Unused}'");
                });
            }
        }