public void CanCreateMetricWithASetOfDimensions()
        {
            // Arrange
            var sentTelemetry = new List <ITelemetry>();

            var client = this.InitializeTelemetryClient(sentTelemetry);

            var dimensions = new Dictionary <string, string> {
                { "Dim1", "Value1" },
                { "Dim2", "Value2" }
            };

            using (MetricManagerV1 manager = new MetricManagerV1(client))
            {
                // Act
                MetricV1 metric = manager.CreateMetric("Test Metric", dimensions);
                metric.Track(42);
            }

            // Assert
            var aggregatedMetric = (MetricTelemetry)sentTelemetry.Single();

            Assert.AreEqual("Test Metric", aggregatedMetric.Name);

            Assert.AreEqual(1, aggregatedMetric.Count);
            Assert.AreEqual(42, aggregatedMetric.Sum);

            // note: interval duration property is auto-generated
            Assert.AreEqual(3, aggregatedMetric.Properties.Count);

            Assert.AreEqual("Value1", aggregatedMetric.Properties["Dim1"]);
            Assert.AreEqual("Value2", aggregatedMetric.Properties["Dim2"]);
        }
Exemple #2
0
        public void MetricInvokesMetricProcessorsForEachValueTracked()
        {
            // Arrange
            var sentTelemetry = new List <ITelemetry>();
            var sentSamples   = new List <MetricSample>();

            var client = this.InitializeTelemetryClient(sentTelemetry, sentSamples);

            var dimensions = new Dictionary <string, string> {
                { "Dim1", "Value1" },
                { "Dim2", "Value2" }
            };

            using (MetricManagerV1 manager = new MetricManagerV1(client))
            {
                MetricV1 metric = manager.CreateMetric("Test Metric", dimensions);

                // Act
                metric.Track(42);
            }

            // Assert
            var sample = (MetricSample)sentSamples.Single();

            Assert.AreEqual("Test Metric", sample.Name);

            Assert.AreEqual(42, sample.Value);

            Assert.AreEqual("Value1", sample.Dimensions["Dim1"]);
            Assert.AreEqual("Value2", sample.Dimensions["Dim2"]);
        }
        public void AggregatedMetricTelemetryIntervalDurationPropertyIsPositiveInteger()
        {
            // Arrange
            var sentTelemetry = new List <ITelemetry>();

            var client = this.InitializeTelemetryClient(sentTelemetry);

            using (MetricManagerV1 manager = new MetricManagerV1(client))
            {
                MetricV1 metric = manager.CreateMetric("Test Metric");

                // Act
                metric.Track(42);
            }

            // Assert
            var aggregatedMetric = (MetricTelemetry)sentTelemetry.Single();

            Assert.AreEqual("Test Metric", aggregatedMetric.Name);

            Assert.AreEqual(1, aggregatedMetric.Count);
            Assert.AreEqual(1, aggregatedMetric.Properties.Count);

            Assert.IsTrue(aggregatedMetric.Properties.ContainsKey("_MS.AggregationIntervalMs"));
            Assert.IsTrue(long.Parse(aggregatedMetric.Properties["_MS.AggregationIntervalMs"]) > 0);
        }
Exemple #4
0
        public void MetricAggregatorCalculatesSumCorrectly()
        {
            // Arrange
            double[] testValues = { 4.45, 8, 29.21, 78.43, 0 };

            var sentTelemetry = new List <ITelemetry>();
            var sentSamples   = new List <MetricSample>();

            var client = this.InitializeTelemetryClient(sentTelemetry, sentSamples);

            using (MetricManagerV1 manager = new MetricManagerV1(client))
            {
                MetricV1 metric = manager.CreateMetric("Test Metric");

                // Act
                for (int i = 0; i < testValues.Length; i++)
                {
                    metric.Track(testValues[i]);
                }
            }

            // Assert
            double sentSampleSum = sentTelemetry.Sum(
                (telemetry) => {
                var metric = telemetry as MetricTelemetry;
                return(metric == null ? 0 : metric.Sum);
            });

            Assert.AreEqual(testValues.Sum(), sentSampleSum);
        }
        public void CanCreateMetricExplicitlySettingDimensionsToNull()
        {
            // Arrange
            var sentTelemetry = new List <ITelemetry>();

            var client = this.InitializeTelemetryClient(sentTelemetry);

            using (MetricManagerV1 manager = new MetricManagerV1(client))
            {
                // Act
                MetricV1 metric = manager.CreateMetric("Test Metric", null);
                metric.Track(42);
            }

            // Assert
            var aggregatedMetric = (MetricTelemetry)sentTelemetry.Single();

            Assert.AreEqual("Test Metric", aggregatedMetric.Name);

            Assert.AreEqual(1, aggregatedMetric.Count);
            Assert.AreEqual(42, aggregatedMetric.Sum);

            // note: interval duration property is auto-generated
            Assert.AreEqual(1, aggregatedMetric.Properties.Count);
        }
Exemple #6
0
        public void MetricEqualsItself()
        {
            using (var manager = new MetricManagerV1())
            {
                MetricV1 metric = manager.CreateMetric("My metric");

                Assert.IsTrue(metric.Equals(metric));
            }
        }
Exemple #7
0
        public void MetricsAreEqualIfDimensionsSetToNothingImplicitlyAndExplicitlyAsEmptySet()
        {
            using (var manager = new MetricManagerV1())
            {
                MetricV1 metric = manager.CreateMetric("My metric", new Dictionary <string, string>());
                MetricV1 other  = manager.CreateMetric("My metric");

                Assert.IsTrue(metric.Equals(other));
            }
        }
Exemple #8
0
        public void MetricNameIsAccentSensitive()
        {
            using (var manager = new MetricManagerV1())
            {
                MetricV1 metric = manager.CreateMetric("My metric");
                MetricV1 other  = manager.CreateMetric("My métric");

                Assert.IsFalse(metric.Equals(other));
            }
        }
Exemple #9
0
        public void MetricsAreEqualForTheSameMetricNameWithoutDimensions()
        {
            using (var manager = new MetricManagerV1())
            {
                MetricV1 metric = manager.CreateMetric("My metric");
                MetricV1 other  = manager.CreateMetric("My metric");

                Assert.IsTrue(metric.Equals(other));
            }
        }
Exemple #10
0
        public void MetricNotEqualsOtherObject()
        {
            using (var manager = new MetricManagerV1())
            {
                MetricV1 metric = manager.CreateMetric("My metric");
                var      other  = new object();

                Assert.IsFalse(metric.Equals(other));
            }
        }
Exemple #11
0
        public void MetricNeverEqualsNull()
        {
            using (var manager = new MetricManagerV1())
            {
                MetricV1 metric = manager.CreateMetric("My metric");
                object   other  = null;

                Assert.IsFalse(metric.Equals(other));
            }
        }
 public void Track(MetricV1 metric, double value)
 {
     this.sampleList.Add(
         new MetricSample()
     {
         Name       = metric.Name,
         Dimensions = metric.Dimensions,
         Value      = value
     });
 }
Exemple #13
0
        public void MetricAggregatorCalculatesStandardDeviationCorrectly()
        {
            // Arrange
            double[] testValues = { 1, 2, 3, 4, 5 };

            var sentTelemetry = new List <ITelemetry>();
            var sentSamples   = new List <MetricSample>();

            var client = this.InitializeTelemetryClient(sentTelemetry, sentSamples);

            using (MetricManagerV1 manager = new MetricManagerV1(client))
            {
                MetricV1 metric = manager.CreateMetric("Test Metric");

                // Act
                for (int i = 0; i < testValues.Length; i++)
                {
                    metric.Track(testValues[i]);
                }
            }

            // Assert
            double sumOfSquares = sentTelemetry.Sum(
                (telemetry) => {
                var metric = telemetry as MetricTelemetry;
                return
                (metric == null
                        ? 0
                        : Math.Pow(metric.StandardDeviation.Value, 2) * metric.Count.Value + Math.Pow(metric.Sum, 2) / metric.Count.Value);
            });

            int count = sentTelemetry.Sum(
                (telemetry) => {
                var metric = telemetry as MetricTelemetry;
                return(metric == null ? 0 : metric.Count.Value);
            });

            double sum = sentTelemetry.Sum(
                (telemetry) => {
                var metric = telemetry as MetricTelemetry;
                return(metric == null ? 0 : metric.Sum);
            });

            double stddev = Math.Sqrt(sumOfSquares / count - Math.Pow(sum / count, 2));

            Assert.AreEqual(testValues.StdDev(), stddev);
        }
        public void EqualMetricsAreCombinedIntoSignleAggregatedStatsStructure()
        {
            // Arrange
            var sentTelemetry = new List <ITelemetry>();

            var client = this.InitializeTelemetryClient(sentTelemetry);

            MetricV1 metric1 = null;
            MetricV1 metric2 = null;

            using (MetricManagerV1 manager = new MetricManagerV1(client))
            {
                // note: on first go aggregators may be different because manager may
                // snapshot after first got created but before the second
                for (int i = 0; i < 2; i++)
                {
                    metric1 = manager.CreateMetric("Test Metric");
                    metric2 = manager.CreateMetric("Test Metric");

                    // Act
                    metric1.Track(10);
                    metric2.Track(5);

                    manager.Flush();

                    if (sentTelemetry.Count == 1)
                    {
                        break;
                    }
                    else
                    {
                        sentTelemetry.Clear();
                    }
                }
            }

            // Assert
            Assert.AreEqual(1, sentTelemetry.Count);

            var aggregatedMetric = (MetricTelemetry)sentTelemetry.Single();

            Assert.AreEqual(2, aggregatedMetric.Count);
            Assert.AreEqual(15, aggregatedMetric.Sum);
        }
Exemple #15
0
        public void DimensionValuesAreAccentSensitive()
        {
            using (var manager = new MetricManagerV1())
            {
                var dimensionSet1 = new Dictionary <string, string>()
                {
                    { "Dim1", "Value1" }
                };
                var dimensionSet2 = new Dictionary <string, string>()
                {
                    { "Dim1", "Válue1" }
                };

                MetricV1 metric = manager.CreateMetric("My metric", dimensionSet1);
                MetricV1 other  = manager.CreateMetric("My metric", dimensionSet2);

                Assert.IsFalse(metric.Equals(other));
            }
        }
Exemple #16
0
        public void DimensionsAreOrderInsensitive()
        {
            using (var manager = new MetricManagerV1())
            {
                var dimensionSet1 = new Dictionary <string, string>()
                {
                    { "Dim1", "Value1" },
                    { "Dim2", "Value2" },
                };

                var dimensionSet2 = new Dictionary <string, string>()
                {
                    { "Dim2", "Value2" },
                    { "Dim1", "Value1" },
                };

                MetricV1 metric = manager.CreateMetric("My metric", dimensionSet1);
                MetricV1 other  = manager.CreateMetric("My metric", dimensionSet2);

                Assert.IsTrue(metric.Equals(other));
            }
        }
        public void DisposingManagerCreatesAggregatedMetricTelemetry()
        {
            // Arrange
            var sentTelemetry = new List <ITelemetry>();

            var client = this.InitializeTelemetryClient(sentTelemetry);

            using (MetricManagerV1 manager = new MetricManagerV1(client))
            {
                MetricV1 metric = manager.CreateMetric("Test Metric");

                metric.Track(42);

                // Act
                manager.Dispose();

                // Assert
                Assert.AreEqual(1, sentTelemetry.Count);

                var aggregatedMetric = (MetricTelemetry)sentTelemetry.Single();
                Assert.IsNotNull(aggregatedMetric);
            }
        }
Exemple #18
0
        /// <summary>
        /// Generates telemetry object based on the metric aggregator.
        /// </summary>
        /// <param name="metric">Metric definition.</param>
        /// <param name="statistics">Metric aggregator statistics calculated for a period of time.</param>
        /// <returns>Metric telemetry object resulting from aggregation.</returns>
        private static MetricTelemetry CreateAggregatedMetricTelemetry(MetricV1 metric, SimpleMetricStatisticsAggregator statistics)
        {
            var telemetry = new MetricTelemetry(
                metric.Name,
                statistics.Count,
                statistics.Sum,
                statistics.Min,
                statistics.Max,
                statistics.StandardDeviation);

            if (metric.Dimensions != null)
            {
                foreach (KeyValuePair <string, string> property in metric.Dimensions)
                {
                    telemetry.Properties.Add(property);
                }
            }

            // add a header allowing to distinguish metrics
            // built using metric manager from other metrics
            telemetry.Context.GetInternalContext().SdkVersion = sdkVersionPropertyValue;

            return(telemetry);
        }
Exemple #19
0
 internal SimpleMetricStatisticsAggregator GetStatisticsAggregator(MetricV1 metric)
 {
     return(this.metricDictionary.GetOrAdd(metric, (m) => { return new SimpleMetricStatisticsAggregator(); }));
 }