/// <summary>Gets the specified metric, or creates one if no such metric exists.</summary>
        /// <param name="metricIdentifier">The identity of the metric.</param>
        /// <param name="metricConfiguration">@The configutration of the metric.</param>
        /// <returns>A metric witht he specified identify and configuration.</returns>
        /// <exception cref="ArgumentException">If a metric with the specified identify exists,
        /// but its configuration does not match the specified configuration.
        /// You may not change a metric configurations once a metric was created for the first time.
        /// Either specify the same configuration every time, or specify <c>null</c> during every
        /// invocation except the first one. <c>null</c> will match against any previously specified
        /// configuration when retrieving existing metrics, or fall back to the default when
        /// creating new metrics.</exception>
        public Metric GetOrCreate(
            MetricIdentifier metricIdentifier,
            MetricConfiguration metricConfiguration)
        {
            Util.ValidateNotNull(metricIdentifier, nameof(metricIdentifier));

            Metric metric = this.metrics.GetOrAdd(
                metricIdentifier,
                (key) => new Metric(
                    this.metricManager,
                    metricIdentifier,
                    metricConfiguration ?? MetricConfigurations.Common.Default()));

            if (metricConfiguration != null && false == metric.configuration.Equals(metricConfiguration))
            {
                throw new ArgumentException("A Metric with the specified Namespace, Id and dimension names already exists, but it has a configuration"
                                            + " that is different from the specified configuration. You may not change configurations once a"
                                            + " metric was created for the first time. Either specify the same configuration every time, or"
                                            + " specify 'null' during every invocation except the first one. 'Null' will match against any"
                                            + " previously specified configuration when retrieving existing metrics, or fall back to"
                                            + Invariant($" the default when creating new metrics. ({nameof(metricIdentifier)} = \"{metricIdentifier.ToString()}\".)"));
            }

            return(metric);
        }
        public void GetConfiguration()
        {
            TelemetryConfiguration pipeline = Util.CreateAITelemetryConfig();
            TelemetryClient        client   = new TelemetryClient(pipeline);

            {
                Metric metric = client.GetMetric("CowsSold");
                Assert.AreEqual(MetricConfigurations.Common.Measurement(), metric.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Measurement(), metric.GetConfiguration());
            }
            {
                Metric metric = client.GetMetric("PigsSold", MetricConfigurations.Common.Accumulator());
                Assert.AreEqual(MetricConfigurations.Common.Accumulator(), metric.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Accumulator(), metric.GetConfiguration());
            }
            {
                Metric metric = client.GetMetric("HorsesSold", MetricConfigurations.Common.Measurement());
                Assert.AreEqual(MetricConfigurations.Common.Measurement(), metric.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Measurement(), metric.GetConfiguration());
            }
            {
                MetricConfiguration config = new MetricConfiguration(10, 5, new MetricSeriesConfigurationForMeasurement(false));
                Metric metric = client.GetMetric("ChickensSold", config);
                Assert.AreEqual(config, metric.GetConfiguration());
                Assert.AreSame(config, metric.GetConfiguration());
            }

            Util.CompleteDefaultAggregationCycle(pipeline.GetMetricManager());
            pipeline.Dispose();
        }
        public void TestGetHashCode()
        {
            var config1 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimit:    100,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.AreNotEqual(0, config1.GetHashCode());

            var config2 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimit:    100,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.AreEqual(config1.GetHashCode(), config2.GetHashCode());

            config2 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimit:    100,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: true));

            Assert.AreNotEqual(config1.GetHashCode(), config2.GetHashCode());
            Assert.AreNotEqual(0, config2.GetHashCode());

            config2 = new MetricConfiguration(
                seriesCountLimit:           1001,
                valuesPerDimensionLimit:    100,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.AreNotEqual(config1.GetHashCode(), config2.GetHashCode());
            Assert.AreNotEqual(0, config2.GetHashCode());
        }
        public void TestEquals()
        {
            var config1 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimit:    100,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.IsTrue(config1.Equals(config1));

            var config2 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimit:    100,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.IsTrue(config1.Equals(config2));
            Assert.IsTrue(config2.Equals(config1));

            config2 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimit:    100,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: true));

            Assert.IsFalse(config1.Equals(config2));
            Assert.IsFalse(config2.Equals(config1));

            config2 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimit:    100,
                seriesConfig:               new MetricSeriesConfigurationForAccumulator(restrictToUInt32Values: false));

            Assert.IsFalse(config1.Equals(config2));
            Assert.IsFalse(config2.Equals(config1));

            config2 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimit:    101,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.IsFalse(config1.Equals(config2));
            Assert.IsFalse(config2.Equals(config1));

            config2 = new MetricConfiguration(
                seriesCountLimit:           1001,
                valuesPerDimensionLimit:    100,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.IsFalse(config1.Equals(config2));
            Assert.IsFalse(config2.Equals(config1));
        }
 public void Ctor()
 {
     {
         var config = new MetricConfiguration(
             seriesCountLimit:           1000,
             valuesPerDimensionLimit:    100,
             seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));
         Assert.IsNotNull(config);
         Assert.AreEqual(1000, config.SeriesCountLimit);
         Assert.AreEqual(100, config.ValuesPerDimensionLimit);
         Assert.IsNotNull(config.SeriesConfig);
         Assert.IsInstanceOfType(config.SeriesConfig, typeof(MetricSeriesConfigurationForMeasurement));
         Assert.AreEqual(false, config.SeriesConfig.RequiresPersistentAggregation);
         Assert.AreEqual(false, ((MetricSeriesConfigurationForMeasurement)config.SeriesConfig).RestrictToUInt32Values);
     }
     {
         var config = new MetricConfiguration(
             seriesCountLimit:           1,
             valuesPerDimensionLimit:    0,
             seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: true));
         Assert.IsNotNull(config);
         Assert.AreEqual(1, config.SeriesCountLimit);
         Assert.AreEqual(0, config.ValuesPerDimensionLimit);
         Assert.IsNotNull(config.SeriesConfig);
         Assert.IsInstanceOfType(config.SeriesConfig, typeof(MetricSeriesConfigurationForMeasurement));
         Assert.AreEqual(false, config.SeriesConfig.RequiresPersistentAggregation);
         Assert.AreEqual(true, ((MetricSeriesConfigurationForMeasurement)config.SeriesConfig).RestrictToUInt32Values);
     }
     {
         var config = new MetricConfiguration(
             seriesCountLimit:           Int32.MaxValue,
             valuesPerDimensionLimit:    Int32.MaxValue,
             seriesConfig:               new MetricSeriesConfigurationForAccumulator(restrictToUInt32Values: false));
         Assert.IsNotNull(config);
         Assert.AreEqual(Int32.MaxValue, config.SeriesCountLimit);
         Assert.AreEqual(Int32.MaxValue, config.ValuesPerDimensionLimit);
         Assert.IsNotNull(config.SeriesConfig);
         Assert.IsInstanceOfType(config.SeriesConfig, typeof(MetricSeriesConfigurationForAccumulator));
         Assert.AreEqual(true, config.SeriesConfig.RequiresPersistentAggregation);
         Assert.AreEqual(false, ((MetricSeriesConfigurationForAccumulator)config.SeriesConfig).RestrictToUInt32Values);
     }
     {
         Assert.ThrowsException <ArgumentOutOfRangeException>(() => new MetricConfiguration(
                                                                  seriesCountLimit:           0,
                                                                  valuesPerDimensionLimit:    100,
                                                                  seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false)));
     }
     {
         Assert.ThrowsException <ArgumentOutOfRangeException>(() => new MetricConfiguration(
                                                                  seriesCountLimit:           -1,
                                                                  valuesPerDimensionLimit:    100,
                                                                  seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false)));
     }
     {
         Assert.ThrowsException <ArgumentOutOfRangeException>(() => new MetricConfiguration(
                                                                  seriesCountLimit:           1000,
                                                                  valuesPerDimensionLimit:    -1,
                                                                  seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false)));
     }
     {
         Assert.ThrowsException <ArgumentNullException>(() => new MetricConfiguration(
                                                            seriesCountLimit:           1000,
                                                            valuesPerDimensionLimit:    100,
                                                            seriesConfig:               null));
     }
 }
        public void SeriesCountLimit()
        {
            {
                var config = new MetricConfiguration(
                    seriesCountLimit:           1000,
                    valuesPerDimensionLimit:    100,
                    seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

                Assert.ThrowsException <ArgumentOutOfRangeException>(() => config.GetValuesPerDimensionLimit(-1));
                Assert.ThrowsException <ArgumentOutOfRangeException>(() => config.GetValuesPerDimensionLimit(0));

                Assert.AreEqual(100, config.GetValuesPerDimensionLimit(1));
                Assert.AreEqual(100, config.GetValuesPerDimensionLimit(2));
                Assert.AreEqual(100, config.GetValuesPerDimensionLimit(10));

                Assert.ThrowsException <ArgumentOutOfRangeException>(() => config.GetValuesPerDimensionLimit(11));
            }
            {
                var config = new MetricConfiguration(
                    seriesCountLimit:           1000,
                    valuesPerDimensionLimits:   new[] { 42 },
                    seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

                Assert.ThrowsException <ArgumentOutOfRangeException>(() => config.GetValuesPerDimensionLimit(-1));
                Assert.ThrowsException <ArgumentOutOfRangeException>(() => config.GetValuesPerDimensionLimit(0));

                Assert.AreEqual(42, config.GetValuesPerDimensionLimit(1));
                Assert.AreEqual(42, config.GetValuesPerDimensionLimit(2));
                Assert.AreEqual(42, config.GetValuesPerDimensionLimit(10));

                Assert.ThrowsException <ArgumentOutOfRangeException>(() => config.GetValuesPerDimensionLimit(11));
            }
            {
                var config = new MetricConfiguration(
                    seriesCountLimit:           1000,
                    valuesPerDimensionLimits:   new[] { 11, 12, 13, 14 },
                    seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

                Assert.ThrowsException <ArgumentOutOfRangeException>(() => config.GetValuesPerDimensionLimit(-1));
                Assert.ThrowsException <ArgumentOutOfRangeException>(() => config.GetValuesPerDimensionLimit(0));

                Assert.AreEqual(11, config.GetValuesPerDimensionLimit(1));
                Assert.AreEqual(12, config.GetValuesPerDimensionLimit(2));
                Assert.AreEqual(13, config.GetValuesPerDimensionLimit(3));
                Assert.AreEqual(14, config.GetValuesPerDimensionLimit(4));
                Assert.AreEqual(14, config.GetValuesPerDimensionLimit(6));
                Assert.AreEqual(14, config.GetValuesPerDimensionLimit(10));

                Assert.ThrowsException <ArgumentOutOfRangeException>(() => config.GetValuesPerDimensionLimit(11));
            }
            {
                var config = new MetricConfiguration(
                    seriesCountLimit:           1000,
                    valuesPerDimensionLimits:   new[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 },
                    seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

                Assert.ThrowsException <ArgumentOutOfRangeException>(() => config.GetValuesPerDimensionLimit(-1));
                Assert.ThrowsException <ArgumentOutOfRangeException>(() => config.GetValuesPerDimensionLimit(0));

                Assert.AreEqual(11, config.GetValuesPerDimensionLimit(1));
                Assert.AreEqual(12, config.GetValuesPerDimensionLimit(2));
                Assert.AreEqual(13, config.GetValuesPerDimensionLimit(3));
                Assert.AreEqual(14, config.GetValuesPerDimensionLimit(4));
                Assert.AreEqual(15, config.GetValuesPerDimensionLimit(5));
                Assert.AreEqual(16, config.GetValuesPerDimensionLimit(6));
                Assert.AreEqual(17, config.GetValuesPerDimensionLimit(7));
                Assert.AreEqual(18, config.GetValuesPerDimensionLimit(8));
                Assert.AreEqual(19, config.GetValuesPerDimensionLimit(9));
                Assert.AreEqual(20, config.GetValuesPerDimensionLimit(10));

                Assert.ThrowsException <ArgumentOutOfRangeException>(() => config.GetValuesPerDimensionLimit(11));
            }
        }
        public void TestGetHashCode()
        {
            var config1 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimit:    100,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.AreNotEqual(0, config1.GetHashCode());

            var config2 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimit:    100,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.AreEqual(config1.GetHashCode(), config2.GetHashCode());

            config2 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimit:    100,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: true));

            Assert.AreNotEqual(config1.GetHashCode(), config2.GetHashCode());
            Assert.AreNotEqual(0, config2.GetHashCode());

            config2 = new MetricConfiguration(
                seriesCountLimit:           1001,
                valuesPerDimensionLimit:    100,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.AreNotEqual(config1.GetHashCode(), config2.GetHashCode());
            Assert.AreNotEqual(0, config2.GetHashCode());

            config1 = new MetricConfiguration(
                seriesCountLimit: 1000,
                valuesPerDimensionLimits: new[] { 42 },
                seriesConfig: new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.AreNotEqual(0, config1.GetHashCode());

            config2 = new MetricConfiguration(
                seriesCountLimit: 1000,
                valuesPerDimensionLimit: 42,
                seriesConfig: new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.AreEqual(config1.GetHashCode(), config2.GetHashCode());

            config2 = new MetricConfiguration(
                seriesCountLimit: 1000,
                valuesPerDimensionLimits: new[] { 42, 42, 42 },
                seriesConfig: new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.AreEqual(config1.GetHashCode(), config2.GetHashCode());

            config2 = new MetricConfiguration(
                seriesCountLimit: 1000,
                valuesPerDimensionLimits: new[] { 42, 18, 42 },
                seriesConfig: new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.AreNotEqual(0, config2.GetHashCode());
            Assert.AreNotEqual(config1.GetHashCode(), config2.GetHashCode());

            config2 = new MetricConfiguration(
                seriesCountLimit: 1000,
                valuesPerDimensionLimits: new[] { 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 },
                seriesConfig: new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.AreEqual(config1.GetHashCode(), config2.GetHashCode());

            config1 = new MetricConfiguration(
                seriesCountLimit: 1000,
                valuesPerDimensionLimits: new[] { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 30 },
                seriesConfig: new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            config2 = new MetricConfiguration(
                seriesCountLimit: 1000,
                valuesPerDimensionLimits: new[] { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 40 },
                seriesConfig: new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.AreNotEqual(0, config1.GetHashCode());
            Assert.AreNotEqual(0, config2.GetHashCode());
            Assert.AreEqual(config1.GetHashCode(), config2.GetHashCode());

            config1 = new MetricConfiguration(
                seriesCountLimit: 1000,
                valuesPerDimensionLimits: new[] { 10, 11, 12, 13, 14, 15, 16, 17, 18, 25, 30 },
                seriesConfig: new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            config2 = new MetricConfiguration(
                seriesCountLimit: 1000,
                valuesPerDimensionLimits: new[] { 10, 11, 12, 13, 14, 15, 16, 17, 18, 26, 40 },
                seriesConfig: new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.AreNotEqual(0, config1.GetHashCode());
            Assert.AreNotEqual(0, config2.GetHashCode());
            Assert.AreNotEqual(config1.GetHashCode(), config2.GetHashCode());
        }
        public void TestEquals()
        {
            var config1 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimit:    100,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.IsTrue(config1.Equals(config1));

            var config2 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimit:    100,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.IsTrue(config1.Equals(config2));
            Assert.IsTrue(config2.Equals(config1));

            config2 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimit:    100,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: true));

            Assert.IsFalse(config1.Equals(config2));
            Assert.IsFalse(config2.Equals(config1));

            config2 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimit:    101,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.IsFalse(config1.Equals(config2));
            Assert.IsFalse(config2.Equals(config1));

            config2 = new MetricConfiguration(
                seriesCountLimit:           1001,
                valuesPerDimensionLimit:    100,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.IsFalse(config1.Equals(config2));
            Assert.IsFalse(config2.Equals(config1));

            config1 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimits:   new[] { 42 },
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.IsTrue(config1.Equals(config1));

            config2 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimit:    42,
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.IsTrue(config1.Equals(config2));
            Assert.IsTrue(config2.Equals(config1));

            config2 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimits:   new[] { 42, 42, 42 },
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.IsTrue(config1.Equals(config2));
            Assert.IsTrue(config2.Equals(config1));

            config2 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimits:   new[] { 42, 18, 42 },
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.IsFalse(config1.Equals(config2));
            Assert.IsFalse(config2.Equals(config1));

            config2 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimits:   new[] { 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 },
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.IsTrue(config1.Equals(config2));
            Assert.IsTrue(config2.Equals(config1));

            config1 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimits:   new[] { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 30 },
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            config2 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimits:   new[] { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 40 },
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.IsTrue(config1.Equals(config2));
            Assert.IsTrue(config2.Equals(config1));

            config1 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimits:   new[] { 10, 11, 12, 13, 14, 15, 16, 17, 18, 25, 30 },
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            config2 = new MetricConfiguration(
                seriesCountLimit:           1000,
                valuesPerDimensionLimits:   new[] { 10, 11, 12, 13, 14, 15, 16, 17, 18, 26, 40 },
                seriesConfig:               new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));

            Assert.IsFalse(config1.Equals(config2));
            Assert.IsFalse(config2.Equals(config1));
        }
        public void GetMetric_DetectsMetricConfigurationConflicts()
        {
            IList <ITelemetry>     sentTelemetry;
            TelemetryConfiguration telemetryPipeline = Util.CreateAITelemetryConfig(out sentTelemetry);
            TelemetryClient        client            = new TelemetryClient(telemetryPipeline);

            {
                Metric m1 = client.GetMetric("M01");
                Assert.IsNotNull(m1);

                Metric m2 = client.GetMetric("M01");
                Assert.AreSame(m1, m2);

                m2 = client.GetMetric("M01 ");
                Assert.AreSame(m1, m2);

                m2 = client.GetMetric("M01", MetricConfigurations.Common.Measurement());
                Assert.AreSame(m1, m2);

                m2 = client.GetMetric("M01", metricConfiguration: null);
                Assert.AreSame(m1, m2);

                Assert.ThrowsException <ArgumentException>(() => client.GetMetric("M01", MetricConfigurations.Common.Accumulator()));

                MetricConfiguration config1 = new MetricConfiguration(10, 10, new MetricSeriesConfigurationForMeasurement(false));
                MetricConfiguration config2 = new MetricConfiguration(10, 10, new MetricSeriesConfigurationForMeasurement(false));
                Assert.AreEqual(config1, config2);
                Assert.AreNotSame(config1, config2);

                m1 = client.GetMetric("M02", config1);
                Assert.IsNotNull(m1);

                m2 = client.GetMetric("M02", config2);
                Assert.AreSame(m1, m2);

                m2 = client.GetMetric("M02", metricConfiguration: null);
                Assert.AreSame(m1, m2);

                config2 = new MetricConfiguration(10, 101, new MetricSeriesConfigurationForMeasurement(false));
                Assert.AreNotEqual(config1, config2);
                Assert.AreNotSame(config1, config2);

                Assert.ThrowsException <ArgumentException>(() => client.GetMetric("M02", config2));
                Assert.ThrowsException <ArgumentException>(() => client.GetMetric("M02 ", config2));
            }
            {
                Metric m1 = client.GetMetric("M11", "Dim1");
                Assert.IsNotNull(m1);

                Metric m2 = client.GetMetric("M11", "Dim1");
                Assert.AreSame(m1, m2);

                m2 = client.GetMetric(" M11", "Dim1");
                Assert.AreSame(m1, m2);

                m2 = client.GetMetric("M11", " Dim1");
                Assert.AreSame(m1, m2);

                m2 = client.GetMetric(" M11", " Dim1", MetricConfigurations.Common.Measurement());
                Assert.AreSame(m1, m2);

                m2 = client.GetMetric("M11", "Dim1", metricConfiguration: null);
                Assert.AreSame(m1, m2);

                Assert.ThrowsException <ArgumentException>(() => client.GetMetric("M11", "Dim1 ", MetricConfigurations.Common.Accumulator()));

                MetricConfiguration config1 = new MetricConfiguration(10, 10, new MetricSeriesConfigurationForMeasurement(false));
                MetricConfiguration config2 = new MetricConfiguration(10, 10, new MetricSeriesConfigurationForMeasurement(false));
                Assert.AreEqual(config1, config2);
                Assert.AreNotSame(config1, config2);

                m1 = client.GetMetric("M12 ", "Dim1", config1);
                Assert.IsNotNull(m1);

                m2 = client.GetMetric("M12", "Dim1 ", config2);
                Assert.AreSame(m1, m2);

                m2 = client.GetMetric("M12", "Dim1", metricConfiguration: null);
                Assert.AreSame(m1, m2);

                config2 = new MetricConfiguration(10, 101, new MetricSeriesConfigurationForMeasurement(false));
                Assert.AreNotEqual(config1, config2);
                Assert.AreNotSame(config1, config2);

                Assert.ThrowsException <ArgumentException>(() => client.GetMetric("M12", "Dim1", config2));
                Assert.ThrowsException <ArgumentException>(() => client.GetMetric("M12 ", "Dim1", config2));
            }
            {
                Metric m1 = client.GetMetric("M21", "Dim1", "Dim2");
                Assert.IsNotNull(m1);

                Metric m2 = client.GetMetric("M21", "Dim1", "Dim2");
                Assert.AreSame(m1, m2);

                m2 = client.GetMetric(" M21", "Dim1", "Dim2");
                Assert.AreSame(m1, m2);

                m2 = client.GetMetric("M21", " Dim1", "Dim2");
                Assert.AreSame(m1, m2);

                m2 = client.GetMetric("M21", "Dim1", " Dim2");
                Assert.AreSame(m1, m2);

                m2 = client.GetMetric(" M21", " Dim1", "Dim2", MetricConfigurations.Common.Measurement());
                Assert.AreSame(m1, m2);

                m2 = client.GetMetric("M21", "Dim1", "Dim2 ", metricConfiguration: null);
                Assert.AreSame(m1, m2);

                Assert.ThrowsException <ArgumentException>(() => client.GetMetric("M21", "Dim1 ", "Dim2", MetricConfigurations.Common.Accumulator()));

                MetricConfiguration config1 = new MetricConfiguration(10, 10, new MetricSeriesConfigurationForMeasurement(false));
                MetricConfiguration config2 = new MetricConfiguration(10, 10, new MetricSeriesConfigurationForMeasurement(false));
                Assert.AreEqual(config1, config2);
                Assert.AreNotSame(config1, config2);

                m1 = client.GetMetric("M22 ", "Dim1", "Dim2 ", config1);
                Assert.IsNotNull(m1);

                m2 = client.GetMetric("M22", "Dim1 ", "Dim2", config2);
                Assert.AreSame(m1, m2);

                m2 = client.GetMetric("M22", "Dim1", "Dim2", metricConfiguration: null);
                Assert.AreSame(m1, m2);

                config2 = new MetricConfiguration(10, 101, new MetricSeriesConfigurationForMeasurement(false));
                Assert.AreNotEqual(config1, config2);
                Assert.AreNotSame(config1, config2);

                Assert.ThrowsException <ArgumentException>(() => client.GetMetric("M22", "Dim1", "Dim2", config2));
                Assert.ThrowsException <ArgumentException>(() => client.GetMetric("M22 ", "Dim1", "Dim2", config2));
            }
            {
                Metric m0 = client.GetMetric("Xxx");
                Metric m1 = client.GetMetric("Xxx", "Dim1");
                Metric m2 = client.GetMetric("Xxx", "Dim1", "Dim2");

                Assert.IsNotNull(m0);
                Assert.IsNotNull(m1);
                Assert.IsNotNull(m2);

                Assert.AreNotSame(m0, m1);
                Assert.AreNotSame(m0, m2);
                Assert.AreNotSame(m1, m2);

                Assert.AreSame(m0.GetConfiguration(), m1.GetConfiguration());
                Assert.AreSame(m0.GetConfiguration(), m2.GetConfiguration());
                Assert.AreSame(m1.GetConfiguration(), m2.GetConfiguration());

                Assert.AreSame(MetricConfigurations.Common.Measurement(), m0.GetConfiguration());
            }

            Util.CompleteDefaultAggregationCycle(telemetryPipeline.GetMetricManager());
            telemetryPipeline.Dispose();
        }
        public void GetMetric_RespectsMetricConfiguration()
        {
            IList <ITelemetry>     sentTelemetry;
            TelemetryConfiguration telemetryPipeline = Util.CreateAITelemetryConfig(out sentTelemetry);
            TelemetryClient        client            = new TelemetryClient(telemetryPipeline);

            {
                Metric metric = client.GetMetric("M1");
                Assert.IsNotNull(metric);
                Assert.AreEqual(0, metric.Identifier.DimensionsCount);
                Assert.AreEqual("M1", metric.Identifier.MetricId);
                Assert.AreEqual(MetricConfigurations.Common.Measurement(), metric.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Measurement(), metric.GetConfiguration());

                MetricSeries series;
                Assert.IsTrue(metric.TryGetDataSeries(out series));
                Assert.AreEqual(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
            }
            {
                Metric metric = client.GetMetric("M2", MetricConfigurations.Common.Measurement());
                Assert.IsNotNull(metric);
                Assert.AreEqual(0, metric.Identifier.DimensionsCount);
                Assert.AreEqual("M2", metric.Identifier.MetricId);
                Assert.AreEqual(MetricConfigurations.Common.Measurement(), metric.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Measurement(), metric.GetConfiguration());

                MetricSeries series;
                Assert.IsTrue(metric.TryGetDataSeries(out series));
                Assert.AreEqual(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
            }
            {
                Metric metric = client.GetMetric("M3", MetricConfigurations.Common.Accumulator());
                Assert.IsNotNull(metric);
                Assert.AreEqual(0, metric.Identifier.DimensionsCount);
                Assert.AreEqual("M3", metric.Identifier.MetricId);
                Assert.AreEqual(MetricConfigurations.Common.Accumulator(), metric.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Accumulator(), metric.GetConfiguration());

                MetricSeries series;
                Assert.IsTrue(metric.TryGetDataSeries(out series));
                Assert.AreEqual(MetricConfigurations.Common.Accumulator().SeriesConfig, series.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Accumulator().SeriesConfig, series.GetConfiguration());
            }
            {
                MetricConfiguration config = new MetricConfiguration(10, 10, new MetricSeriesConfigurationForMeasurement(true));
                Metric metric = client.GetMetric("M4", config);
                Assert.IsNotNull(metric);
                Assert.AreEqual(0, metric.Identifier.DimensionsCount);
                Assert.AreEqual("M4", metric.Identifier.MetricId);
                Assert.AreEqual(config, metric.GetConfiguration());
                Assert.AreSame(config, metric.GetConfiguration());

                MetricSeries series;
                Assert.IsTrue(metric.TryGetDataSeries(out series));
                Assert.AreEqual(new MetricSeriesConfigurationForMeasurement(true), series.GetConfiguration());
                Assert.AreNotSame(new MetricSeriesConfigurationForMeasurement(true), series.GetConfiguration());
            }
            {
                Metric metric = client.GetMetric("M5", "Dim1");
                Assert.IsNotNull(metric);
                Assert.AreEqual(1, metric.Identifier.DimensionsCount);
                Assert.AreEqual("Dim1", metric.Identifier.GetDimensionName(1));
                Assert.AreEqual("M5", metric.Identifier.MetricId);
                Assert.AreEqual(MetricConfigurations.Common.Measurement(), metric.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Measurement(), metric.GetConfiguration());

                MetricSeries series;
                Assert.IsTrue(metric.TryGetDataSeries(out series));
                Assert.AreEqual(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
                Assert.IsTrue(metric.TryGetDataSeries(out series, "Dim1Val"));
                Assert.AreEqual(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
            }
            {
                Metric metric = client.GetMetric("M6", "Dim1", MetricConfigurations.Common.Measurement());
                Assert.IsNotNull(metric);
                Assert.AreEqual(1, metric.Identifier.DimensionsCount);
                Assert.AreEqual("Dim1", metric.Identifier.GetDimensionName(1));
                Assert.AreEqual("M6", metric.Identifier.MetricId);
                Assert.AreEqual(MetricConfigurations.Common.Measurement(), metric.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Measurement(), metric.GetConfiguration());

                MetricSeries series;
                Assert.IsTrue(metric.TryGetDataSeries(out series));
                Assert.AreEqual(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
                Assert.IsTrue(metric.TryGetDataSeries(out series, "Dim1Val"));
                Assert.AreEqual(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
            }
            {
                Metric metric = client.GetMetric("M7", "Dim1", MetricConfigurations.Common.Accumulator());
                Assert.IsNotNull(metric);
                Assert.AreEqual(1, metric.Identifier.DimensionsCount);
                Assert.AreEqual("Dim1", metric.Identifier.GetDimensionName(1));
                Assert.AreEqual("M7", metric.Identifier.MetricId);
                Assert.AreEqual(MetricConfigurations.Common.Accumulator(), metric.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Accumulator(), metric.GetConfiguration());

                MetricSeries series;
                Assert.IsTrue(metric.TryGetDataSeries(out series));
                Assert.AreEqual(MetricConfigurations.Common.Accumulator().SeriesConfig, series.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Accumulator().SeriesConfig, series.GetConfiguration());
                Assert.IsTrue(metric.TryGetDataSeries(out series, "Dim1Val"));
                Assert.AreEqual(MetricConfigurations.Common.Accumulator().SeriesConfig, series.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Accumulator().SeriesConfig, series.GetConfiguration());
            }
            {
                MetricConfiguration config = new MetricConfiguration(10, 10, new MetricSeriesConfigurationForMeasurement(true));
                Metric metric = client.GetMetric("M8", "Dim1", config);
                Assert.IsNotNull(metric);
                Assert.AreEqual(1, metric.Identifier.DimensionsCount);
                Assert.AreEqual("Dim1", metric.Identifier.GetDimensionName(1));
                Assert.AreEqual("M8", metric.Identifier.MetricId);
                Assert.AreEqual(config, metric.GetConfiguration());
                Assert.AreSame(config, metric.GetConfiguration());

                MetricSeries series;
                Assert.IsTrue(metric.TryGetDataSeries(out series));
                Assert.AreEqual(config.SeriesConfig, series.GetConfiguration());
                Assert.AreSame(config.SeriesConfig, series.GetConfiguration());
                Assert.IsTrue(metric.TryGetDataSeries(out series, "Dim1Val"));
                Assert.AreEqual(new MetricSeriesConfigurationForMeasurement(true), series.GetConfiguration());
                Assert.AreNotSame(new MetricSeriesConfigurationForMeasurement(true), series.GetConfiguration());
            }
            {
                Metric metric = client.GetMetric("M9", "Dim1", "Dim2");
                Assert.IsNotNull(metric);
                Assert.AreEqual(2, metric.Identifier.DimensionsCount);
                Assert.AreEqual("Dim1", metric.Identifier.GetDimensionName(1));
                Assert.AreEqual("Dim2", metric.Identifier.GetDimensionName(2));
                Assert.AreEqual("M9", metric.Identifier.MetricId);
                Assert.AreEqual(MetricConfigurations.Common.Measurement(), metric.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Measurement(), metric.GetConfiguration());

                MetricSeries series;
                Assert.IsTrue(metric.TryGetDataSeries(out series));
                Assert.AreEqual(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
                Assert.IsTrue(metric.TryGetDataSeries(out series, "Dim1Val", "Dim2val"));
                Assert.AreEqual(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
            }
            {
                Metric metric = client.GetMetric("M10", "Dim1", "Dim2", MetricConfigurations.Common.Measurement());
                Assert.IsNotNull(metric);
                Assert.AreEqual(2, metric.Identifier.DimensionsCount);
                Assert.AreEqual("Dim1", metric.Identifier.GetDimensionName(1));
                Assert.AreEqual("Dim2", metric.Identifier.GetDimensionName(2));
                Assert.AreEqual("M10", metric.Identifier.MetricId);
                Assert.AreEqual(MetricConfigurations.Common.Measurement(), metric.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Measurement(), metric.GetConfiguration());

                MetricSeries series;
                Assert.IsTrue(metric.TryGetDataSeries(out series));
                Assert.AreEqual(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
                Assert.IsTrue(metric.TryGetDataSeries(out series, "Dim1Val", "Dim2val"));
                Assert.AreEqual(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Measurement().SeriesConfig, series.GetConfiguration());
            }
            {
                Metric metric = client.GetMetric("M11", "Dim1", "Dim2", MetricConfigurations.Common.Accumulator());
                Assert.IsNotNull(metric);
                Assert.AreEqual(2, metric.Identifier.DimensionsCount);
                Assert.AreEqual("Dim1", metric.Identifier.GetDimensionName(1));
                Assert.AreEqual("Dim2", metric.Identifier.GetDimensionName(2));
                Assert.AreEqual("M11", metric.Identifier.MetricId);
                Assert.AreEqual(MetricConfigurations.Common.Accumulator(), metric.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Accumulator(), metric.GetConfiguration());

                MetricSeries series;
                Assert.IsTrue(metric.TryGetDataSeries(out series));
                Assert.AreEqual(MetricConfigurations.Common.Accumulator().SeriesConfig, series.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Accumulator().SeriesConfig, series.GetConfiguration());
                Assert.IsTrue(metric.TryGetDataSeries(out series, "Dim1Val", "Dim2val"));
                Assert.AreEqual(MetricConfigurations.Common.Accumulator().SeriesConfig, series.GetConfiguration());
                Assert.AreSame(MetricConfigurations.Common.Accumulator().SeriesConfig, series.GetConfiguration());
            }
            {
                MetricConfiguration config = new MetricConfiguration(10, 10, new MetricSeriesConfigurationForMeasurement(true));
                Metric metric = client.GetMetric("M12", "Dim1", "Dim2", config);
                Assert.IsNotNull(metric);
                Assert.AreEqual(2, metric.Identifier.DimensionsCount);
                Assert.AreEqual("Dim1", metric.Identifier.GetDimensionName(1));
                Assert.AreEqual("Dim2", metric.Identifier.GetDimensionName(2));
                Assert.AreEqual("M12", metric.Identifier.MetricId);
                Assert.AreEqual(config, metric.GetConfiguration());
                Assert.AreSame(config, metric.GetConfiguration());

                MetricSeries series;
                Assert.IsTrue(metric.TryGetDataSeries(out series));
                Assert.AreEqual(config.SeriesConfig, series.GetConfiguration());
                Assert.AreSame(config.SeriesConfig, series.GetConfiguration());
                Assert.IsTrue(metric.TryGetDataSeries(out series, "Dim1Val", "Dim2val"));
                Assert.AreEqual(new MetricSeriesConfigurationForMeasurement(true), series.GetConfiguration());
                Assert.AreNotSame(new MetricSeriesConfigurationForMeasurement(true), series.GetConfiguration());
                Assert.AreSame(config.SeriesConfig, series.GetConfiguration());
            }

            Util.CompleteDefaultAggregationCycle(telemetryPipeline.GetMetricManager());
        }