Exemple #1
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);
        }
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 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);
        }
        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);
        }
        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 #6
0
        public void MetricEqualsItself()
        {
            using (var manager = new MetricManagerV1())
            {
                MetricV1 metric = manager.CreateMetric("My metric");

                Assert.IsTrue(metric.Equals(metric));
            }
        }
        public void CanDisposeMetricManagerMultipleTimes()
        {
            MetricManagerV1 manager = null;

            using (manager = new MetricManagerV1()) { }

            //Assert.DoesNotThrow
            manager.Dispose();
        }
Exemple #8
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 #9
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 #10
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 #11
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 #12
0
        public void MetricNeverEqualsNull()
        {
            using (var manager = new MetricManagerV1())
            {
                MetricV1 metric = manager.CreateMetric("My metric");
                object   other  = null;

                Assert.IsFalse(metric.Equals(other));
            }
        }
Exemple #13
0
        /// <summary>
        /// Disposes this telemetry extractor.
        /// </summary>
        public void Dispose()
        {
            IDisposable metricMgr = this.metricManager;

            if (metricMgr != null)
            {
                // benign race
                metricMgr.Dispose();
                this.metricManager = null;
            }
        }
Exemple #14
0
        /// <summary>
        /// This class implements the <see cref="ITelemetryModule"/> interface by defining this method.
        /// It will be called by the infrastructure when the telemetry pipeline is being built.
        /// This will ensure that the extractor is initialized using the same <see cref="TelemetryConfiguration"/> as the rest of the pipeline.
        /// Specifically, this will also ensure that the <see cref="Microsoft.ApplicationInsights.Extensibility.MetricManagerV1"/> and its
        /// respective <see cref="TelemetryClient"/> used internally for sending extracted metrics use the same configuration.
        /// </summary>
        /// <param name="configuration">The telemetric configuration to be used by this extractor.</param>
        public void Initialize(TelemetryConfiguration configuration)
        {
            TelemetryClient telemetryClient = (configuration == null)
                                                    ? new TelemetryClient()
                                                    : new TelemetryClient(configuration);

            if (!string.IsNullOrWhiteSpace(MetricTerms.Autocollection.Moniker.Key))
            {
                telemetryClient.Context.Properties[MetricTerms.Autocollection.Moniker.Key] = MetricTerms.Autocollection.Moniker.Value;
            }

            this.metricManager = new MetricManagerV1(telemetryClient);
            this.InitializeExtractors();
        }
Exemple #15
0
        /// <summary>
        /// Calls all participating extractors to initialize themselves.
        /// </summary>
        private void InitializeExtractors()
        {
            MetricManagerV1 thisMetricManager = this.metricManager;

            foreach (ExtractorWithInfo participant in this.extractors)
            {
                try
                {
                    participant.Extractor.InitializeExtractor(thisMetricManager);
                }
                catch (Exception ex)
                {
                    CoreEventSource.Log.LogError("Error in " + participant.Info + ": " + ex.ToString());
                }
            }
        }
Exemple #16
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);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MetricV1"/> class.
        /// </summary>
        /// <param name="manager">Aggregator manager handling this instance.</param>
        /// <param name="name">Metric name.</param>
        /// <param name="dimensions">Metric dimensions.</param>
        internal MetricV1(
            MetricManagerV1 manager,
            string name,
            IDictionary <string, string> dimensions = null)
        {
            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }

            this.manager    = manager;
            this.Name       = name;
            this.Dimensions = dimensions;

            this.aggregatorId = MetricV1.GetAggregatorId(name, dimensions);
            this.hashCode     = this.aggregatorId.GetHashCode();
        }
        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 #19
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 #20
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);
            }
        }