Esempio n. 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 (MetricManager manager = new MetricManager(client))
            {
                Metric 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.Equal(testValues.Sum(), sentSampleSum);
        }
        public void AggregatedMetricTelemetryIntervalDurationPropertyIsPositiveInteger()
        {
            // Arrange
            var sentTelemetry = new List <ITelemetry>();

            var client = this.InitializeTelemetryClient(sentTelemetry);

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

                // Act
                metric.Track(42);
            }

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

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

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

            Assert.True(aggregatedMetric.Properties.ContainsKey("IntervalDurationMs"));
            Assert.True(long.Parse(aggregatedMetric.Properties["IntervalDurationMs"]) > 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 (MetricManager manager = new MetricManager(client))
            {
                // Act
                Metric metric = manager.CreateMetric("Test Metric", dimensions);
                metric.Track(42);
            }

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

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

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

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

            Assert.Equal("Value1", aggregatedMetric.Properties["Dim1"]);
            Assert.Equal("Value2", aggregatedMetric.Properties["Dim2"]);
        }
Esempio n. 4
0
        public void CanCreateMetricHavingNoDimensions()
        {
            // Arrange
            var sentTelemetry = new List <ITelemetry>();

            var client = this.InitializeTelemetryClient(sentTelemetry);

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

            // Assert (single metric aggregation exists in the output)
            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);
        }
Esempio n. 5
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 (MetricManager manager = new MetricManager(client))
            {
                Metric metric = manager.CreateMetric("Test Metric", dimensions);

                // Act
                metric.Track(42);
            }

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

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

            Assert.Equal(42, sample.Value);

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

            var client = this.InitializeTelemetryClient(sentTelemetry);

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

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

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

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

            // note: interval duration property is auto-generated
            Assert.Equal(1, aggregatedMetric.Properties.Count);
        }
        public void CanDisposeMetricManagerMultipleTimes()
        {
            MetricManager manager = null;

            using (manager = new MetricManager()) { }

            Assert.DoesNotThrow(() => { manager.Dispose(); });
        }
Esempio n. 8
0
        public void MetricEqualsItself()
        {
            using (var manager = new MetricManager())
            {
                Metric metric = manager.CreateMetric("My metric");

                Assert.True(metric.Equals(metric));
            }
        }
Esempio n. 9
0
        public void MetricsAreEqualIfDimensionsSetToNothingImplicitlyAndExplicitlyAsEmptySet()
        {
            using (var manager = new MetricManager())
            {
                Metric metric = manager.CreateMetric("My metric", new Dictionary <string, string>());
                Metric other  = manager.CreateMetric("My metric");

                Assert.True(metric.Equals(other));
            }
        }
Esempio n. 10
0
        public void MetricsAreEqualForTheSameMetricNameWithoutDimensions()
        {
            using (var manager = new MetricManager())
            {
                Metric metric = manager.CreateMetric("My metric");
                Metric other  = manager.CreateMetric("My metric");

                Assert.True(metric.Equals(other));
            }
        }
Esempio n. 11
0
        public void MetricNotEqualsOtherObject()
        {
            using (var manager = new MetricManager())
            {
                Metric metric = manager.CreateMetric("My metric");
                var    other  = new object();

                Assert.False(metric.Equals(other));
            }
        }
Esempio n. 12
0
        public void MetricNeverEqualsNull()
        {
            using (var manager = new MetricManager())
            {
                Metric metric = manager.CreateMetric("My metric");
                object other  = null;

                Assert.False(metric.Equals(other));
            }
        }
        public void MetricsAreEqualIfDimensionsSetToNothingImplicitlyAndExplicitly()
        {
            using (var manager = new MetricManager())
            {
                Metric metric = manager.CreateMetric("My metric", null);
                Metric other  = manager.CreateMetric("My metric");

                Assert.IsTrue(metric.Equals(other));
            }
        }
Esempio n. 14
0
        public void MetricNameIsAccentSensitive()
        {
            using (var manager = new MetricManager())
            {
                Metric metric = manager.CreateMetric("My metric");
                Metric other  = manager.CreateMetric("My métric");

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

            if (metricMgr != null)
            {
                // benign race
                metricMgr.Dispose();
                this.metricManager = null;
            }
        }
        /// <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.MetricManager"/> 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 MetricManager(telemetryClient);
            this.InitializeExtractors();
        }
        /// <summary>
        /// Calls all participating extractors to initialize themselves.
        /// </summary>
        private void InitializeExtractors()
        {
            MetricManager 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());
                }
            }
        }
Esempio n. 18
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 (MetricManager manager = new MetricManager(client))
            {
                Metric 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.Equal(testValues.StdDev(), stddev);
        }
Esempio n. 19
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Metric"/> class.
        /// </summary>
        /// <param name="manager">Aggregator manager handling this instance.</param>
        /// <param name="name">Metric name.</param>
        /// <param name="dimensions">Metric dimensions.</param>
        internal Metric(
            MetricManager 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 = Metric.GetAggregatorId(name, dimensions);
            this.hashCode     = this.aggregatorId.GetHashCode();
        }
        public void EqualMetricsAreCombinedIntoSignleAggregatedStatsStructure()
        {
            // Arrange
            var sentTelemetry = new List <ITelemetry>();

            var client = this.InitializeTelemetryClient(sentTelemetry);

            Metric metric1 = null;
            Metric metric2 = null;

            using (MetricManager manager = new MetricManager(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.Equal(1, sentTelemetry.Count);

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

            Assert.Equal(2, aggregatedMetric.Count);
            Assert.Equal(15, aggregatedMetric.Sum);
        }
Esempio n. 21
0
        public void DimensionValuesAreAccentSensitive()
        {
            using (var manager = new MetricManager())
            {
                var dimensionSet1 = new Dictionary <string, string>()
                {
                    { "Dim1", "Value1" }
                };
                var dimensionSet2 = new Dictionary <string, string>()
                {
                    { "Dim1", "Válue1" }
                };

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

                Assert.False(metric.Equals(other));
            }
        }
Esempio n. 22
0
        public void DimensionsAreOrderInsensitive()
        {
            using (var manager = new MetricManager())
            {
                var dimensionSet1 = new Dictionary <string, string>()
                {
                    { "Dim1", "Value1" },
                    { "Dim2", "Value2" },
                };

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

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

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

            var client = this.InitializeTelemetryClient(sentTelemetry);

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

                metric.Track(42);

                // Act
                manager.Dispose();

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

                var aggregatedMetric = (MetricTelemetry)sentTelemetry.Single();
                Assert.NotNull(aggregatedMetric);
            }
        }
Esempio n. 24
0
        internal MetricManager GetMetricManager(bool createIfNotExists)
        {
            MetricManager manager = this.metricManager;

            if (manager == null && createIfNotExists)
            {
                var           pipelineAdapter = new ApplicationInsightsTelemetryPipeline(this);
                MetricManager newManager      = new MetricManager(pipelineAdapter);
                MetricManager prevManager     = Interlocked.CompareExchange(ref this.metricManager, newManager, null);

                if (prevManager == null)
                {
                    manager = newManager;
                }
                else
                {
                    // We just created a new manager that we are not using. Stop is before discarding.
                    Task fireAndForget = newManager.StopDefaultAggregationCycleAsync();
                    manager = prevManager;
                }
            }

            return(manager);
        }