private static void Metrics_SpecifiedPipeline(TelemetryConfiguration telemetryPipeline)
        {
            telemetryPipeline.InstrumentationKey = Guid.NewGuid().ToString("D");

            MetricManager manager1 = telemetryPipeline.GetMetricManager();

            Assert.IsNotNull(manager1);

            MetricManager manager2 = telemetryPipeline.GetMetricManager();

            Assert.IsNotNull(manager2);

            Assert.AreEqual(manager1, manager2);
            Assert.AreSame(manager1, manager2);
            Assert.IsTrue(Object.ReferenceEquals(manager1, manager2));

            StubTelemetryChannel telemetryCollector = new StubTelemetryChannel();

            telemetryPipeline.TelemetryChannel = telemetryCollector;
            Assert.AreSame(telemetryCollector, telemetryPipeline.TelemetryChannel);

            //CollectingTelemetryInitializer telemetryCollector = new CollectingTelemetryInitializer();
            //defaultTelemetryPipeline.TelemetryInitializers.Add(coll);

            IMetricSeriesConfiguration seriesConfig = new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false);

            manager1.CreateNewSeries("ns", "Metric A", seriesConfig).TrackValue(42);
            manager1.CreateNewSeries("ns", "Metric A", seriesConfig).TrackValue("18");
            manager2.CreateNewSeries("ns", "Metric A", seriesConfig).TrackValue(10000);
            manager2.CreateNewSeries("ns", "Metric B", seriesConfig).TrackValue(-0.001);
            manager1.Flush();

            Assert.AreEqual(4, telemetryCollector.TelemetryItems.Count);

            Assert.IsInstanceOfType(telemetryCollector.TelemetryItems[0], typeof(MetricTelemetry));
            Assert.AreEqual("Metric B", ((MetricTelemetry)telemetryCollector.TelemetryItems[0]).Name);
            Assert.AreEqual(1, ((MetricTelemetry)telemetryCollector.TelemetryItems[0]).Count);
            Assert.AreEqual(-0.001, ((MetricTelemetry)telemetryCollector.TelemetryItems[0]).Sum);

            Assert.IsInstanceOfType(telemetryCollector.TelemetryItems[1], typeof(MetricTelemetry));
            Assert.AreEqual("Metric A", ((MetricTelemetry)telemetryCollector.TelemetryItems[1]).Name);
            Assert.AreEqual(1, ((MetricTelemetry)telemetryCollector.TelemetryItems[1]).Count);
            Assert.AreEqual(10000, ((MetricTelemetry)telemetryCollector.TelemetryItems[1]).Sum);

            Assert.IsInstanceOfType(telemetryCollector.TelemetryItems[2], typeof(MetricTelemetry));
            Assert.AreEqual("Metric A", ((MetricTelemetry)telemetryCollector.TelemetryItems[2]).Name);
            Assert.AreEqual(1, ((MetricTelemetry)telemetryCollector.TelemetryItems[2]).Count);
            Assert.AreEqual(18, ((MetricTelemetry)telemetryCollector.TelemetryItems[2]).Sum);

            Assert.IsInstanceOfType(telemetryCollector.TelemetryItems[3], typeof(MetricTelemetry));
            Assert.AreEqual("Metric A", ((MetricTelemetry)telemetryCollector.TelemetryItems[3]).Name);
            Assert.AreEqual(1, ((MetricTelemetry)telemetryCollector.TelemetryItems[3]).Count);
            Assert.AreEqual(42, ((MetricTelemetry)telemetryCollector.TelemetryItems[3]).Sum);
        }
Пример #2
0
        private MetricSeries CreateNewMetricSeries(string[] dimensionValues)
        {
            KeyValuePair <string, string>[] dimValsNames = null;

            if (dimensionValues != null)
            {
                dimValsNames = new KeyValuePair <string, string> [dimensionValues.Length];

                for (int d = 0; d < dimensionValues.Length; d++)
                {
                    string dimensionName  = _dimensionNames[d];
                    string dimensionValue = dimensionValues[d];

                    if (dimensionValue == null)
                    {
                        throw new ArgumentNullException($"The value for dimension number {d} is null.");
                    }

                    if (String.IsNullOrWhiteSpace(dimensionValue))
                    {
                        throw new ArgumentNullException($"The value for dimension number {d} is empty or white-space.");
                    }


                    dimValsNames[d] = new KeyValuePair <string, string>(dimensionName, dimensionValue);
                }
            }

            MetricSeries series = _metricManager.CreateNewSeries(MetricId, dimValsNames, _configuration.SeriesConfig);

            return(series);
        }