Beispiel #1
0
        public RollbarClient(RollbarConfig config, HttpClient httpClient)
        {
            Assumption.AssertNotNull(config, nameof(config));
            Assumption.AssertNotNull(httpClient, nameof(httpClient));

            this._config = config;

            this._payloadPostUri = new Uri($"{this._config.EndPoint}item/");


            this._httpClient = httpClient;

            var header = new MediaTypeWithQualityHeaderValue("application/json");

            if (!this._httpClient.DefaultRequestHeaders.Accept.Contains(header))
            {
                this._httpClient.DefaultRequestHeaders.Accept.Add(header);
            }

            var sp = ServicePointManager.FindServicePoint(new Uri(this._config.EndPoint));

            try
            {
                sp.ConnectionLeaseTimeout = 60 * 1000; // 1 minute
            }
            catch (NotImplementedException ex)
            {
                // just a crash prevention.
                // this is a work around the unimplemented property within Mono runtime...
            }

            this._payloadTruncationStrategy = new IterativeTruncationStrategy();
        }
        public RollbarClient(RollbarConfig config, HttpClient httpClient)
        {
            Assumption.AssertNotNull(config, nameof(config));
            Assumption.AssertNotNull(httpClient, nameof(httpClient));

            this._config = config;

            this._payloadPostUri = new Uri($"{this._config.EndPoint}item/");


            this._httpClient = httpClient;

            var header = new MediaTypeWithQualityHeaderValue("application/json");

            if (!this._httpClient.DefaultRequestHeaders.Accept.Contains(header))
            {
                this._httpClient.DefaultRequestHeaders.Accept.Add(header);
            }

            var sp = ServicePointManager.FindServicePoint(new Uri(this._config.EndPoint));

            sp.ConnectionLeaseTimeout = 60 * 1000; // 1 minute


            this._payloadTruncationStrategy = new IterativeTruncationStrategy();
        }
Beispiel #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RollbarClient" /> class.
        /// </summary>
        /// <param name="rollbarLogger">The rollbar logger.</param>
        public RollbarClient(RollbarLogger rollbarLogger)
            : this(rollbarLogger.Config)
        {
            Assumption.AssertNotNull(rollbarLogger, nameof(rollbarLogger));

            this._rollbarLogger = rollbarLogger;

            this._payloadTruncationStrategy = new IterativeTruncationStrategy();
            this._payloadScrubber           = new RollbarPayloadScrubber(this._rollbarLogger.Config.GetFieldsToScrub());
        }
Beispiel #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RollbarClient"/> class.
        /// </summary>
        /// <param name="rollbarLogger">The rollbar logger.</param>
        public RollbarClient(RollbarLogger rollbarLogger)
        {
            Assumption.AssertNotNull(rollbarLogger, nameof(rollbarLogger));
            Assumption.AssertNotNull(rollbarLogger.Config, nameof(rollbarLogger.Config));

            this._rollbarLogger = rollbarLogger;

            this._payloadPostUri =
                new Uri($"{this._rollbarLogger.Config.EndPoint}item/");
            this._httpClient =
                RollbarQueueController.Instance.ProvideHttpClient(
                    this._rollbarLogger.Config.ProxyAddress,
                    this._rollbarLogger.Config.ProxyUsername,
                    this._rollbarLogger.Config.ProxyPassword
                    );

            var header = new MediaTypeWithQualityHeaderValue("application/json");

            if (!this._httpClient.DefaultRequestHeaders.Accept.Contains(header))
            {
                this._httpClient.DefaultRequestHeaders.Accept.Add(header);
            }

            var sp = ServicePointManager.FindServicePoint(new Uri(this._rollbarLogger.Config.EndPoint));

            try
            {
                sp.ConnectionLeaseTimeout = 60 * 1000; // 1 minute
            }
#pragma warning disable CS0168                         // Variable is declared but never used
            catch (NotImplementedException ex)
#pragma warning restore CS0168                         // Variable is declared but never used
            {
                // just a crash prevention.
                // this is a work around the unimplemented property within Mono runtime...
            }

            this._payloadTruncationStrategy = new IterativeTruncationStrategy();
        }
Beispiel #5
0
        public void TestTruncation()
        {
            Payload[] testPayloads = new Payload[]
            {
                new Payload(this._config.RollbarLoggerConfig.RollbarDestinationOptions.AccessToken, new Data(
                                this._config.RollbarLoggerConfig,
                                new Body(new Message("A message I wish to send to the rollbar overlords", new Dictionary <string, object>()
                {
                    { "longMessageString", "very-long-string-very-long-string-very-long-" }, { "theMessageNumber", 11 },
                })),
                                new Dictionary <string, object>()
                {
                    { "longDataString", "long-string-very-long-string-very-long-" }, { "theDataNumber", 15 },
                })
                            ),
                new Payload(this._config.RollbarLoggerConfig.RollbarDestinationOptions.AccessToken, new Data(
                                this._config.RollbarLoggerConfig,
                                new Body("A terrible crash!"),
                                new Dictionary <string, object>()
                {
                    { "longDataString", "long-string-very-long-string-very-long-" }, { "theDataNumber", 15 },
                })
                            ),
                new Payload(this._config.RollbarLoggerConfig.RollbarDestinationOptions.AccessToken, new Data(
                                this._config.RollbarLoggerConfig,
                                new Body(GetException()),
                                new Dictionary <string, object>()
                {
                    { "longDataString", "long-string-very-long-string-very-long-" }, { "theDataNumber", 15 },
                })
                            ),
                new Payload(this._config.RollbarLoggerConfig.RollbarDestinationOptions.AccessToken, new Data(
                                this._config.RollbarLoggerConfig,
                                new Body(GetAggregateException()),
                                new Dictionary <string, object>()
                {
                    { "longDataString", "long-string-very-long-string-very-long-" }, { "theDataNumber", 15 },
                })
                            ),
            };

            TimeSpan blockingTimeout = TimeSpan.FromSeconds(10);

            using (var logger = RollbarFactory.CreateNew(this._config.RollbarLoggerConfig))
            {
                Assert.IsTrue(logger.Config.RollbarDeveloperOptions.Transmit);

                foreach (var payload in testPayloads)
                {
                    logger.AsBlockingLogger(blockingTimeout).Log(payload.Data);
                }
            }

            IterativeTruncationStrategy truncationStrategy = null;

            const int payloadByteSizeLimit = 10; //we are intentionally exaggerating here to force all the iterations to happen...
            List <IPayloadTruncationStrategy> iterations = new List <IPayloadTruncationStrategy>();

            iterations.Add(new RawTruncationStrategy());
            truncationStrategy = new IterativeTruncationStrategy(400, iterations);
            AssertPayloadSizeReduction(false, testPayloads, truncationStrategy);
            AssertPayloadSizeReduction(false, testPayloads, truncationStrategy);
            //using (var logger = RollbarFactory.CreateNew(this._config))
            //{
            //    foreach (var payload in testPayloads)
            //    {
            //        logger.AsBlockingLogger(blockingTimeout).Log(payload.Data);
            //    }
            //}

            iterations.Add(new FramesTruncationStrategy(totalHeadFramesToPreserve: 1, totalTailFramesToPreserve: 1));
            truncationStrategy = new IterativeTruncationStrategy(payloadByteSizeLimit, iterations);
            AssertPayloadSizeReduction(false, testPayloads.Take(2).ToArray(), truncationStrategy);
            AssertPayloadSizeReduction(false, testPayloads.Take(2).ToArray(), truncationStrategy);
            Payload[] payloadsWithFrames = testPayloads.Reverse().Take(2).ToArray();
            foreach (var payload in payloadsWithFrames)
            {
                bool hasFramesToTrim = false;
                hasFramesToTrim |= (payload?.Data?.Body?.Trace?.Frames?.Length > 1);
                hasFramesToTrim |= ((payload?.Data?.Body?.TraceChain != null) && payload.Data.Body.TraceChain.Any(trace => trace?.Frames?.Length > 1));
                AssertPayloadSizeReduction(hasFramesToTrim, payload, truncationStrategy);
                AssertPayloadSizeReduction(false, payload, truncationStrategy);
            }
            //using (var logger = RollbarFactory.CreateNew(this._config))
            //{
            //    foreach (var payload in testPayloads)
            //    {
            //        logger.AsBlockingLogger(blockingTimeout).Log(payload.Data);
            //    }
            //}

            iterations.Add(new StringsTruncationStrategy(stringBytesLimit: 10));
            truncationStrategy = new IterativeTruncationStrategy(payloadByteSizeLimit, iterations);
            AssertPayloadSizeReduction(true, testPayloads, truncationStrategy);
            AssertPayloadSizeReduction(false, testPayloads, truncationStrategy);
            //using (var logger = RollbarFactory.CreateNew(this._config))
            //{
            //    foreach (var payload in testPayloads)
            //    {
            //        logger.AsBlockingLogger(blockingTimeout).Log(payload.Data);
            //    }
            //}

            iterations.Add(new StringsTruncationStrategy(stringBytesLimit:  7));
            truncationStrategy = new IterativeTruncationStrategy(payloadByteSizeLimit, iterations);
            AssertPayloadSizeReduction(true, testPayloads, truncationStrategy);
            AssertPayloadSizeReduction(false, testPayloads, truncationStrategy);
            //using (var logger = RollbarFactory.CreateNew(this._config))
            //{
            //    foreach (var payload in testPayloads)
            //    {
            //        logger.AsBlockingLogger(blockingTimeout).Log(payload.Data);
            //    }
            //}

            iterations.Add(new StringsTruncationStrategy(stringBytesLimit:  3));
            truncationStrategy = new IterativeTruncationStrategy(payloadByteSizeLimit, iterations);
            AssertPayloadSizeReduction(true, testPayloads, truncationStrategy);
            AssertPayloadSizeReduction(false, testPayloads, truncationStrategy);
            //using (var logger = RollbarFactory.CreateNew(this._config))
            //{
            //    foreach (var payload in testPayloads)
            //    {
            //        logger.AsBlockingLogger(blockingTimeout).Log(payload.Data);
            //    }
            //}

            iterations.Add(new MinBodyTruncationStrategy());
            truncationStrategy = new IterativeTruncationStrategy(payloadByteSizeLimit, iterations);
            AssertPayloadSizeReduction(false, testPayloads.Take(2).ToArray(), truncationStrategy);
            AssertPayloadSizeReduction(false, testPayloads.Take(2).ToArray(), truncationStrategy);

            payloadsWithFrames = testPayloads.Reverse().Take(2).ToArray();
            foreach (var payload in payloadsWithFrames)
            {
                bool hasFramesToTrim = false;
                hasFramesToTrim |= (payload?.Data?.Body?.Trace?.Frames?.Length > 1);
                hasFramesToTrim |= ((payload?.Data?.Body?.TraceChain != null) && payload.Data.Body.TraceChain.Any(trace => trace?.Frames?.Length > 1));
                AssertPayloadSizeReduction(hasFramesToTrim, payload, truncationStrategy);
                AssertPayloadSizeReduction(false, payload, truncationStrategy);
            }

            //using (var logger = RollbarFactory.CreateNew(this._config))
            //{
            //    foreach (var payload in testPayloads)
            //    {
            //        logger.AsBlockingLogger(blockingTimeout).Log(payload.Data);
            //    }
            //}
        }
Beispiel #6
0
        private static void AssertPayloadSizeReduction(bool expectReduction, Payload[] testPayloads, IterativeTruncationStrategy trancationStrategy)
        {
            foreach (var testPayload in testPayloads)
            {
                string original = JsonConvert.SerializeObject(testPayload);
                System.Diagnostics.Trace.WriteLine($"Original payload ({original.Length}): " + original);

                string truncated = null;
                System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
                trancationStrategy.Truncate(testPayload);
                truncated = JsonConvert.SerializeObject(testPayload);
                System.Diagnostics.Trace.WriteLine($"Truncated payload ({truncated.Length}): " + truncated);

                string metadata = $"TruncationStrategy: {trancationStrategy} [ {trancationStrategy.OrderedTruncationStrategies.Select(strategy => strategy.ToString()).Aggregate((strategy, next) => next + ", " + strategy)} ], "
                                  + Environment.NewLine + $"expectedReduction: {expectReduction}, "
                                  + Environment.NewLine + $"original: {original} "
                                  + Environment.NewLine + $"AND truncated: {truncated}";
                if (expectReduction)
                {
                    Assert.IsTrue(truncated.Length < original.Length, metadata);
                }
                else
                {
                    Assert.IsFalse(truncated.Length < original.Length, metadata);
                }
            }
        }
Beispiel #7
0
 private void AssertPayloadSizeReduction(bool expectReduction, Payload testPayload, IterativeTruncationStrategy trancationStrategy)
 {
     AssertPayloadSizeReduction(expectReduction, new Payload[] { testPayload }, trancationStrategy);
 }
        public void TestTruncation()
        {
            Payload[] testPayloads = new Payload[]
            {
                new Payload(this._config.AccessToken, new Data(
                                this._config,
                                new Body(new Message("A message I wish to send to the rollbar overlords", new Dictionary <string, object>()
                {
                    { "longMessageString", "very-long-string-very-long-string-very-long-" }, { "theMessageNumber", 11 },
                })),
                                new Dictionary <string, object>()
                {
                    { "longDataString", "long-string-very-long-string-very-long-" }, { "theDataNumber", 15 },
                })
                            ),
                new Payload(this._config.AccessToken, new Data(
                                this._config,
                                new Body("A terrible crash!"),
                                new Dictionary <string, object>()
                {
                    { "longDataString", "long-string-very-long-string-very-long-" }, { "theDataNumber", 15 },
                })
                            ),
                new Payload(this._config.AccessToken, new Data(
                                this._config,
                                new Body(GetException()),
                                new Dictionary <string, object>()
                {
                    { "longDataString", "long-string-very-long-string-very-long-" }, { "theDataNumber", 15 },
                })
                            ),
                new Payload(this._config.AccessToken, new Data(
                                this._config,
                                new Body(GetAggregateException()),
                                new Dictionary <string, object>()
                {
                    { "longDataString", "long-string-very-long-string-very-long-" }, { "theDataNumber", 15 },
                })
                            ),
            };

            TimeSpan blockingTimeout = TimeSpan.FromSeconds(5);

            using (var logger = RollbarFactory.CreateNew().Configure(this._config))
            {
                foreach (var payload in testPayloads)
                {
                    logger.AsBlockingLogger(blockingTimeout).Log(payload.Data);
                }
            }

            IterativeTruncationStrategy trancationStrategy = null;

            const int payloadByteSizeLimit = 10; //we are intentionally exaggerating here to force all the iterations to happen...
            List <IPayloadTruncationStrategy> iterations = new List <IPayloadTruncationStrategy>();

            iterations.Add(new RawTruncationStrategy());
            trancationStrategy = new IterativeTruncationStrategy(400, iterations);
            AssertPayloadSizeReduction(false, testPayloads, trancationStrategy);
            AssertPayloadSizeReduction(false, testPayloads, trancationStrategy);
            using (var logger = RollbarFactory.CreateNew().Configure(this._config))
            {
                foreach (var payload in testPayloads)
                {
                    logger.AsBlockingLogger(blockingTimeout).Log(payload.Data);
                }
            }

            iterations.Add(new FramesTruncationStrategy(totalHeadFramesToPreserve: 1, totalTailFramesToPreserve: 1));
            trancationStrategy = new IterativeTruncationStrategy(payloadByteSizeLimit, iterations);
            AssertPayloadSizeReduction(false, testPayloads.Take(3).ToArray(), trancationStrategy);
            AssertPayloadSizeReduction(false, testPayloads.Take(3).ToArray(), trancationStrategy);
            AssertPayloadSizeReduction(true, testPayloads.Reverse().Take(1).ToArray(), trancationStrategy);
            AssertPayloadSizeReduction(false, testPayloads.Reverse().Take(1).ToArray(), trancationStrategy);
            using (var logger = RollbarFactory.CreateNew().Configure(this._config))
            {
                foreach (var payload in testPayloads)
                {
                    logger.AsBlockingLogger(blockingTimeout).Log(payload.Data);
                }
            }

            iterations.Add(new StringsTruncationStrategy(stringBytesLimit: 10));
            trancationStrategy = new IterativeTruncationStrategy(payloadByteSizeLimit, iterations);
            AssertPayloadSizeReduction(true, testPayloads, trancationStrategy);
            AssertPayloadSizeReduction(false, testPayloads, trancationStrategy);
            using (var logger = RollbarFactory.CreateNew().Configure(this._config))
            {
                foreach (var payload in testPayloads)
                {
                    logger.AsBlockingLogger(blockingTimeout).Log(payload.Data);
                }
            }

            iterations.Add(new StringsTruncationStrategy(stringBytesLimit:  7));
            trancationStrategy = new IterativeTruncationStrategy(payloadByteSizeLimit, iterations);
            AssertPayloadSizeReduction(true, testPayloads, trancationStrategy);
            AssertPayloadSizeReduction(false, testPayloads, trancationStrategy);
            using (var logger = RollbarFactory.CreateNew().Configure(this._config))
            {
                foreach (var payload in testPayloads)
                {
                    logger.AsBlockingLogger(blockingTimeout).Log(payload.Data);
                }
            }

            iterations.Add(new StringsTruncationStrategy(stringBytesLimit:  3));
            trancationStrategy = new IterativeTruncationStrategy(payloadByteSizeLimit, iterations);
            AssertPayloadSizeReduction(true, testPayloads, trancationStrategy);
            AssertPayloadSizeReduction(false, testPayloads, trancationStrategy);
            using (var logger = RollbarFactory.CreateNew().Configure(this._config))
            {
                foreach (var payload in testPayloads)
                {
                    logger.AsBlockingLogger(blockingTimeout).Log(payload.Data);
                }
            }

            iterations.Add(new MinBodyTruncationStrategy());
            trancationStrategy = new IterativeTruncationStrategy(payloadByteSizeLimit, iterations);
            AssertPayloadSizeReduction(false, testPayloads.Take(2).ToArray(), trancationStrategy);
            AssertPayloadSizeReduction(false, testPayloads.Take(2).ToArray(), trancationStrategy);
            AssertPayloadSizeReduction(true, testPayloads.Reverse().Take(2).ToArray(), trancationStrategy);
            AssertPayloadSizeReduction(false, testPayloads.Reverse().Take(2).ToArray(), trancationStrategy);
            using (var logger = RollbarFactory.CreateNew().Configure(this._config))
            {
                foreach (var payload in testPayloads)
                {
                    logger.AsBlockingLogger(blockingTimeout).Log(payload.Data);
                }
            }
        }
        private void AssertPayloadSizeReduction(bool expectReduction, Payload[] testPayloads, IterativeTruncationStrategy trancationStrategy)
        {
            foreach (var testPayload in testPayloads)
            {
                string original = JsonConvert.SerializeObject(testPayload);
                System.Diagnostics.Trace.WriteLine($"Original payload ({original.Length}): " + original);

                string truncated = null;
                System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
                trancationStrategy.Truncate(testPayload);
                truncated = JsonConvert.SerializeObject(testPayload);
                System.Diagnostics.Trace.WriteLine($"Truncated payload ({truncated.Length}): " + truncated);

                if (expectReduction)
                {
                    Assert.IsTrue(truncated.Length < original.Length);
                }
                else
                {
                    Assert.IsTrue(truncated.Length == original.Length);
                }
            }
        }