public void GetMetric_RespectsClientContext() { IList <ITelemetry> sentTelemetry; TelemetryConfiguration telemetryPipeline = Util.CreateAITelemetryConfig(out sentTelemetry); telemetryPipeline.InstrumentationKey = "754DD89F-61D6-4539-90C7-D886449E12BC"; TelemetryClient client = new TelemetryClient(telemetryPipeline); Metric animalsSold = client.GetMetric("AnimalsSold", "Species", MetricConfigurations.Common.Measurement(), MetricAggregationScope.TelemetryClient); animalsSold.TryTrackValue(10, "Cow"); animalsSold.TryTrackValue(20, "Cow"); client.GetMetricManager(MetricAggregationScope.TelemetryClient).Flush(); animalsSold.TryTrackValue(100, "Rabbit"); animalsSold.TryTrackValue(200, "Rabbit"); client.Context.InstrumentationKey = "3A3C34B6-CA2D-4372-B772-3B015E1E83DC"; client.Context.Device.Model = "Super-Fancy"; client.Context.Properties["MyTag"] = "MyValue"; animalsSold.TryTrackValue(30, "Cow"); animalsSold.TryTrackValue(40, "Cow"); animalsSold.TryTrackValue(300, "Rabbit"); animalsSold.TryTrackValue(400, "Rabbit"); client.GetMetricManager(MetricAggregationScope.TelemetryClient).Flush(); Assert.AreEqual(3, sentTelemetry.Count); MetricTelemetry[] orderedTelemetry = sentTelemetry .OrderByDescending((t) => ((MetricTelemetry)t).Count * 10000 + ((MetricTelemetry)t).Sum) .Select((t) => (MetricTelemetry)t) .ToArray(); Util.ValidateNumericAggregateValues(orderedTelemetry[0], "AnimalsSold", 4, 1000, 400, 100, 111.803398874989); Assert.AreEqual(3, orderedTelemetry[0].Context.Properties.Count); Assert.IsTrue(orderedTelemetry[0].Context.Properties.ContainsKey(Util.AggregationIntervalMonikerPropertyKey)); Assert.AreEqual("Rabbit", orderedTelemetry[0].Context.Properties["Species"]); Assert.AreEqual("MyValue", orderedTelemetry[0].Context.Properties["MyTag"]); Assert.AreEqual("Super-Fancy", orderedTelemetry[0].Context.Device.Model); Assert.AreEqual("3A3C34B6-CA2D-4372-B772-3B015E1E83DC", orderedTelemetry[0].Context.InstrumentationKey); Util.ValidateNumericAggregateValues(orderedTelemetry[1], "AnimalsSold", 2, 70, 40, 30, 5); Assert.AreEqual(3, orderedTelemetry[1].Context.Properties.Count); Assert.IsTrue(orderedTelemetry[1].Context.Properties.ContainsKey(Util.AggregationIntervalMonikerPropertyKey)); Assert.AreEqual("Cow", orderedTelemetry[1].Context.Properties["Species"]); Assert.AreEqual("MyValue", orderedTelemetry[1].Context.Properties["MyTag"]); Assert.AreEqual("Super-Fancy", orderedTelemetry[1].Context.Device.Model); Assert.AreEqual("3A3C34B6-CA2D-4372-B772-3B015E1E83DC", orderedTelemetry[1].Context.InstrumentationKey); Util.ValidateNumericAggregateValues(orderedTelemetry[2], "AnimalsSold", 2, 30, 20, 10, 5); Assert.AreEqual(2, orderedTelemetry[2].Context.Properties.Count); Assert.IsTrue(orderedTelemetry[2].Context.Properties.ContainsKey(Util.AggregationIntervalMonikerPropertyKey)); Assert.AreEqual("Cow", orderedTelemetry[2].Context.Properties["Species"]); Assert.IsNull(orderedTelemetry[2].Context.Device.Model); Assert.AreEqual("754DD89F-61D6-4539-90C7-D886449E12BC", orderedTelemetry[2].Context.InstrumentationKey); Util.CompleteDefaultAggregationCycle(client.GetMetricManager(MetricAggregationScope.TelemetryClient)); telemetryPipeline.Dispose(); }
public void GetMetric_RespectsAggregationScope() { IList <ITelemetry> sentTelemetry1, sentTelemetry2; TelemetryConfiguration telemetryPipeline1 = Util.CreateAITelemetryConfig(out sentTelemetry1); TelemetryConfiguration telemetryPipeline2 = Util.CreateAITelemetryConfig(out sentTelemetry2); TelemetryClient client11 = new TelemetryClient(telemetryPipeline1); TelemetryClient client12 = new TelemetryClient(telemetryPipeline1); TelemetryClient client21 = new TelemetryClient(telemetryPipeline2); Metric metricA111 = client11.GetMetric("Metric A", "Dim1", MetricConfigurations.Common.Measurement(), MetricAggregationScope.TelemetryConfiguration); metricA111.TrackValue(101); metricA111.TrackValue(102); metricA111.TryTrackValue(111, "Val"); metricA111.TryTrackValue(112, "Val"); Metric metricA112 = client11.GetMetric("Metric A", "Dim1", MetricConfigurations.Common.Measurement()); metricA112.TrackValue(103); metricA112.TrackValue(104); metricA112.TryTrackValue(113, "Val"); metricA112.TryTrackValue(114, "Val"); Metric metricA113 = client11.GetMetric("Metric A", "Dim1"); metricA113.TrackValue(105); metricA113.TrackValue(106); metricA113.TryTrackValue(115, "Val"); metricA113.TryTrackValue(116, "Val"); Assert.AreSame(metricA111, metricA112); Assert.AreSame(metricA111, metricA113); Assert.AreSame(metricA112, metricA113); MetricSeries series1, series2; Assert.IsTrue(metricA111.TryGetDataSeries(out series1)); Assert.IsTrue(metricA112.TryGetDataSeries(out series2)); Assert.AreSame(series1, series2); Assert.IsTrue(metricA113.TryGetDataSeries(out series2)); Assert.AreSame(series1, series2); Assert.IsTrue(metricA112.TryGetDataSeries(out series1)); Assert.AreSame(series1, series2); Assert.IsTrue(metricA111.TryGetDataSeries(out series1, "Val")); Assert.IsTrue(metricA112.TryGetDataSeries(out series2, "Val")); Assert.AreSame(series1, series2); Assert.IsTrue(metricA113.TryGetDataSeries(out series2, "Val")); Assert.AreSame(series1, series2); Assert.IsTrue(metricA112.TryGetDataSeries(out series1, "Val")); Assert.AreSame(series1, series2); Metric metricA121 = client12.GetMetric("Metric A", "Dim1", MetricConfigurations.Common.Measurement(), MetricAggregationScope.TelemetryConfiguration); metricA121.TrackValue(107); metricA121.TrackValue(108); metricA121.TryTrackValue(117, "Val"); metricA121.TryTrackValue(118, "Val"); Assert.AreSame(metricA111, metricA121); Metric metricA211 = client21.GetMetric("Metric A", "Dim1", MetricConfigurations.Common.Measurement(), MetricAggregationScope.TelemetryConfiguration); metricA211.TrackValue(201); metricA211.TrackValue(202); metricA211.TryTrackValue(211, "Val"); metricA211.TryTrackValue(212, "Val"); Assert.AreNotSame(metricA111, metricA211); Metric metricA11c1 = client11.GetMetric("Metric A", "Dim1", MetricConfigurations.Common.Measurement(), MetricAggregationScope.TelemetryClient); metricA11c1.TrackValue(301); metricA11c1.TrackValue(302); metricA11c1.TryTrackValue(311, "Val"); metricA11c1.TryTrackValue(312, "Val"); Metric metricA11c2 = client11.GetMetric("Metric A", "Dim1", MetricConfigurations.Common.Measurement(), MetricAggregationScope.TelemetryClient); metricA11c2.TrackValue(303); metricA11c2.TrackValue(304); metricA11c2.TryTrackValue(313, "Val"); metricA11c2.TryTrackValue(314, "Val"); Assert.AreNotSame(metricA111, metricA11c1); Assert.AreSame(metricA11c1, metricA11c2); Assert.IsTrue(metricA11c1.TryGetDataSeries(out series1)); Assert.IsTrue(metricA11c1.TryGetDataSeries(out series2)); Assert.AreSame(series1, series2); Assert.IsTrue(metricA11c1.TryGetDataSeries(out series1, "Val")); Assert.IsTrue(metricA11c1.TryGetDataSeries(out series2, "Val")); Assert.AreSame(series1, series2); Metric metricA12c1 = client12.GetMetric("Metric A", "Dim1", MetricConfigurations.Common.Measurement(), MetricAggregationScope.TelemetryClient); metricA12c1.TrackValue(305); metricA12c1.TrackValue(306); metricA12c1.TryTrackValue(315, "Val"); metricA12c1.TryTrackValue(316, "Val"); Assert.AreNotSame(metricA11c1, metricA12c1); client11.GetMetricManager(MetricAggregationScope.TelemetryClient).Flush(); client12.GetMetricManager(MetricAggregationScope.TelemetryClient).Flush(); client21.GetMetricManager(MetricAggregationScope.TelemetryClient).Flush(); telemetryPipeline1.GetMetricManager().Flush(); telemetryPipeline2.GetMetricManager().Flush(); Assert.AreEqual(6, sentTelemetry1.Count); Assert.AreEqual(2, sentTelemetry2.Count); MetricTelemetry[] orderedTelemetry = sentTelemetry1 .OrderByDescending((t) => ((MetricTelemetry)t).Count * 10000 + ((MetricTelemetry)t).Sum) .Select((t) => (MetricTelemetry)t) .ToArray(); Util.ValidateNumericAggregateValues(orderedTelemetry[0], "Metric A", 8, 916, 118, 111, 2.29128784747792); Assert.AreEqual(2, orderedTelemetry[0].Context.Properties.Count); Assert.IsTrue(orderedTelemetry[0].Context.Properties.ContainsKey(Util.AggregationIntervalMonikerPropertyKey)); Assert.AreEqual("Val", orderedTelemetry[0].Context.Properties["Dim1"]); Util.ValidateNumericAggregateValues(orderedTelemetry[1], "Metric A", 8, 836, 108, 101, 2.29128784747792); Assert.AreEqual(1, orderedTelemetry[1].Context.Properties.Count); Assert.IsTrue(orderedTelemetry[1].Context.Properties.ContainsKey(Util.AggregationIntervalMonikerPropertyKey)); Util.ValidateNumericAggregateValues(orderedTelemetry[2], "Metric A", 4, 1250, 314, 311, 1.11803398874989); Assert.AreEqual(2, orderedTelemetry[2].Context.Properties.Count); Assert.IsTrue(orderedTelemetry[2].Context.Properties.ContainsKey(Util.AggregationIntervalMonikerPropertyKey)); Assert.AreEqual("Val", orderedTelemetry[2].Context.Properties["Dim1"]); Util.ValidateNumericAggregateValues(orderedTelemetry[3], "Metric A", 4, 1210, 304, 301, 1.11803398874989); Assert.AreEqual(1, orderedTelemetry[3].Context.Properties.Count); Assert.IsTrue(orderedTelemetry[3].Context.Properties.ContainsKey(Util.AggregationIntervalMonikerPropertyKey)); Util.ValidateNumericAggregateValues(orderedTelemetry[4], "Metric A", 2, 631, 316, 315, 0.5); Assert.AreEqual(2, orderedTelemetry[4].Context.Properties.Count); Assert.IsTrue(orderedTelemetry[4].Context.Properties.ContainsKey(Util.AggregationIntervalMonikerPropertyKey)); Assert.AreEqual("Val", orderedTelemetry[4].Context.Properties["Dim1"]); Util.ValidateNumericAggregateValues(orderedTelemetry[5], "Metric A", 2, 611, 306, 305, 0.5); Assert.AreEqual(1, orderedTelemetry[5].Context.Properties.Count); Assert.IsTrue(orderedTelemetry[5].Context.Properties.ContainsKey(Util.AggregationIntervalMonikerPropertyKey)); orderedTelemetry = sentTelemetry2 .OrderByDescending((t) => ((MetricTelemetry)t).Count * 10000 + ((MetricTelemetry)t).Sum) .Select((t) => (MetricTelemetry)t) .ToArray(); Util.ValidateNumericAggregateValues(orderedTelemetry[0], "Metric A", 2, 423, 212, 211, 0.5); Assert.AreEqual(2, orderedTelemetry[0].Context.Properties.Count); Assert.IsTrue(orderedTelemetry[0].Context.Properties.ContainsKey(Util.AggregationIntervalMonikerPropertyKey)); Assert.AreEqual("Val", orderedTelemetry[0].Context.Properties["Dim1"]); Util.ValidateNumericAggregateValues(orderedTelemetry[1], "Metric A", 2, 403, 202, 201, 0.5); Assert.AreEqual(1, orderedTelemetry[1].Context.Properties.Count); Assert.IsTrue(orderedTelemetry[1].Context.Properties.ContainsKey(Util.AggregationIntervalMonikerPropertyKey)); Metric metricB21c1 = client21.GetMetric("Metric B", MetricConfigurations.Common.Measurement(), MetricAggregationScope.TelemetryClient); TelemetryClient client22 = new TelemetryClient(telemetryPipeline2); TelemetryClient client23 = new TelemetryClient(telemetryPipeline2); Assert.AreNotSame(metricB21c1, client22.GetMetric("Metric B", MetricConfigurations.Common.Measurement(), MetricAggregationScope.TelemetryClient)); Assert.AreSame(metricB21c1, client21.GetMetric("Metric B", MetricConfigurations.Common.Measurement(), MetricAggregationScope.TelemetryClient)); Assert.ThrowsException <ArgumentException>(() => client21.GetMetric("Metric B", MetricConfigurations.Common.Accumulator(), MetricAggregationScope.TelemetryClient)); Assert.IsNotNull(client23.GetMetric("Metric B", MetricConfigurations.Common.Accumulator(), MetricAggregationScope.TelemetryClient)); Metric metricB211 = client21.GetMetric("Metric B", MetricConfigurations.Common.Measurement(), MetricAggregationScope.TelemetryConfiguration); TelemetryClient client24 = new TelemetryClient(telemetryPipeline2); TelemetryClient client25 = new TelemetryClient(telemetryPipeline2); Assert.AreSame(metricB211, client24.GetMetric("Metric B", MetricConfigurations.Common.Measurement(), MetricAggregationScope.TelemetryConfiguration)); Assert.AreSame(metricB211, client21.GetMetric("Metric B", MetricConfigurations.Common.Measurement(), MetricAggregationScope.TelemetryConfiguration)); Assert.ThrowsException <ArgumentException>(() => client21.GetMetric("Metric B", MetricConfigurations.Common.Accumulator(), MetricAggregationScope.TelemetryConfiguration)); Assert.ThrowsException <ArgumentException>(() => client25.GetMetric("Metric B", MetricConfigurations.Common.Accumulator(), MetricAggregationScope.TelemetryConfiguration)); Assert.ThrowsException <ArgumentException>(() => client11.GetMetric("Metric C", MetricConfigurations.Common.Accumulator(), (MetricAggregationScope)42)); Util.CompleteDefaultAggregationCycle( client11.GetMetricManager(MetricAggregationScope.TelemetryClient), client12.GetMetricManager(MetricAggregationScope.TelemetryClient), client21.GetMetricManager(MetricAggregationScope.TelemetryClient), client22.GetMetricManager(MetricAggregationScope.TelemetryClient), client23.GetMetricManager(MetricAggregationScope.TelemetryClient), client24.GetMetricManager(MetricAggregationScope.TelemetryClient), client25.GetMetricManager(MetricAggregationScope.TelemetryClient), telemetryPipeline2.GetMetricManager(), telemetryPipeline1.GetMetricManager()); telemetryPipeline1.Dispose(); telemetryPipeline2.Dispose(); }
public void GetMetricManager() { TelemetryConfiguration telemetryPipeline1 = TestUtil.CreateAITelemetryConfig(); TelemetryConfiguration telemetryPipeline2 = TestUtil.CreateAITelemetryConfig(); TelemetryClient client1 = new TelemetryClient(telemetryPipeline1); TelemetryClient client2 = new TelemetryClient(telemetryPipeline1); MetricManager managerP11 = telemetryPipeline1.GetMetricManager(); MetricManager managerP12 = telemetryPipeline1.GetMetricManager(); MetricManager managerP21 = telemetryPipeline2.GetMetricManager(); MetricManager managerP22 = telemetryPipeline2.GetMetricManager(); MetricManager managerCp11 = client1.GetMetricManager(MetricAggregationScope.TelemetryConfiguration); MetricManager managerCp12 = client1.GetMetricManager(MetricAggregationScope.TelemetryConfiguration); MetricManager managerCp21 = client2.GetMetricManager(MetricAggregationScope.TelemetryConfiguration); MetricManager managerCp22 = client2.GetMetricManager(MetricAggregationScope.TelemetryConfiguration); MetricManager managerCc11 = client1.GetMetricManager(MetricAggregationScope.TelemetryClient); MetricManager managerCc12 = client1.GetMetricManager(MetricAggregationScope.TelemetryClient); MetricManager managerCc21 = client2.GetMetricManager(MetricAggregationScope.TelemetryClient); MetricManager managerCc22 = client2.GetMetricManager(MetricAggregationScope.TelemetryClient); Assert.IsNotNull(managerP11); Assert.IsNotNull(managerP12); Assert.IsNotNull(managerP21); Assert.IsNotNull(managerP22); Assert.IsNotNull(managerCp11); Assert.IsNotNull(managerCp12); Assert.IsNotNull(managerCp21); Assert.IsNotNull(managerCp22); Assert.IsNotNull(managerCc11); Assert.IsNotNull(managerCc12); Assert.IsNotNull(managerCc21); Assert.IsNotNull(managerCc22); Assert.AreSame(managerP11, managerP12); Assert.AreSame(managerP21, managerP22); Assert.AreNotSame(managerP11, managerP21); Assert.AreSame(managerP11, managerCp11); Assert.AreSame(managerP11, managerCp12); Assert.AreSame(managerP11, managerCp21); Assert.AreSame(managerP11, managerCp22); Assert.AreSame(managerCc11, managerCc12); Assert.AreSame(managerCc21, managerCc22); Assert.AreNotSame(managerCc11, managerCc21); Assert.AreNotSame(managerP11, managerCc11); Assert.AreNotSame(managerP11, managerCc21); Assert.AreNotSame(managerP21, managerCc11); Assert.AreNotSame(managerP21, managerCc21); TestUtil.CompleteDefaultAggregationCycle( managerP11, managerP21, managerCc11, managerCc21); telemetryPipeline1.Dispose(); telemetryPipeline2.Dispose(); }