public void AggregateMetricTelemetrySerializesToJsonCorrectly() { var expected = new MetricTelemetry(); expected.Name = "My Page"; expected.Value = 42; expected.Count = 5; expected.Min = 1.2; expected.Max = 6.4; expected.StandardDeviation = 0.5; expected.Properties.Add("Property1", "Value1"); var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<MetricTelemetry, DataPlatformModel.MetricData>(expected); Assert.Equal(typeof(DataPlatformModel.MetricData).Name, item.Data.BaseType); Assert.Equal(2, item.Data.BaseData.Ver); Assert.Equal(1, item.Data.BaseData.Metrics.Count); Assert.Equal(expected.Name, item.Data.BaseData.Metrics[0].Name); Assert.Equal(DataPlatformModel.DataPointType.Aggregation, item.Data.BaseData.Metrics[0].Kind); Assert.Equal(expected.Value, item.Data.BaseData.Metrics[0].Value); Assert.Equal(expected.Count.Value, item.Data.BaseData.Metrics[0].Count.Value); Assert.Equal(expected.Min.Value, item.Data.BaseData.Metrics[0].Min.Value); Assert.Equal(expected.Max.Value, item.Data.BaseData.Metrics[0].Max.Value); Assert.Equal(expected.StandardDeviation.Value, item.Data.BaseData.Metrics[0].StdDev.Value); Assert.Equal(expected.Properties.ToArray(), item.Data.BaseData.Properties.ToArray()); }
public void SessionIdIsNotOverriden() { var metricTelemetry = new MetricTelemetry("name", 0); metricTelemetry.Context.Session.Id = "SessionId"; var source = new TestableSyntheticTelemetryInitializer(new Dictionary<string, string> { { "SyntheticTest-RunId", "ID" } }); source.Initialize(metricTelemetry); Assert.AreEqual("SessionId", metricTelemetry.Context.Session.Id); }
public void InitializeSetsIdForTelemetryUsingIdFromRequestTelemetry() { var metricTelemetry = new MetricTelemetry("name", 0); var source = new TestableUserTelemetryInitializer(); var requestTelemetry = source.FakeContext.CreateRequestTelemetryPrivate(); requestTelemetry.Context.User.Id = "1"; source.Initialize(metricTelemetry); Assert.AreEqual("1", metricTelemetry.Context.User.Id); }
internal static MetricTelemetry CreateDerivedMetric(this MetricTelemetry metricTelemetry, string nameSuffix, double value) { var derivedTelemetry = new MetricTelemetry(string.Format(CultureInfo.InvariantCulture, "{0}_{1}", metricTelemetry.Name, nameSuffix), value) { Timestamp = metricTelemetry.Timestamp }; derivedTelemetry.Context.GetInternalContext().SdkVersion = metricTelemetry.Context.GetInternalContext().SdkVersion; return derivedTelemetry; }
public void InitializeDoesNotSetIdIfTelemetryHasIt() { var metricTelemetry = new MetricTelemetry("name", 0); var source = new TestableUserTelemetryInitializer(); var requestTelemetry = source.FakeContext.CreateRequestTelemetryPrivate(); requestTelemetry.Context.User.Id = "1"; metricTelemetry.Context.User.Id = "2"; source.Initialize(metricTelemetry); Assert.AreEqual("2", metricTelemetry.Context.User.Id); }
public void SyntheticSourceIsSetToWellKnownValue() { var metricTelemetry = new MetricTelemetry("name", 0); var source = new TestableSyntheticTelemetryInitializer(new Dictionary<string, string> { { "SyntheticTest-RunId", "ID" } }); source.Initialize(metricTelemetry); Assert.AreEqual("Application Insights Availability Monitoring", metricTelemetry.Context.Operation.SyntheticSource); }
public void MetricTelemetrySuppliesPropertiesForCustomerToSendAggregatedMetric() { var instance = new MetricTelemetry("Test Metric", 4.2); instance.Count = 5; instance.Min = 1.2; instance.Max = 6.4; instance.StandardDeviation = 0.5; Assert.Equal(5, instance.Count); Assert.Equal(1.2, instance.Min); Assert.Equal(6.4, instance.Max); Assert.Equal(0.5, instance.StandardDeviation); }
public void SyntheticSourceIsNotSetIfUserProvidedValue() { var metricTelemetry = new MetricTelemetry("name", 0); metricTelemetry.Context.Operation.SyntheticSource = "SOURCE"; var source = new TestableWebTestTelemetryInitializer(new Dictionary<string, string> { { "synthetictest-runid", "ID" } }); source.Initialize(metricTelemetry); Assert.AreEqual("SOURCE", metricTelemetry.Context.Operation.SyntheticSource); }
public void UserIdIsSetToLocation() { var metricTelemetry = new MetricTelemetry("name", 0); var source = new TestableSyntheticTelemetryInitializer(new Dictionary<string, string> { { "SyntheticTest-Location", "LOCATION" }, { "SyntheticTest-RunId", "ID" } }); source.Initialize(metricTelemetry); Assert.AreEqual("LOCATION", metricTelemetry.Context.User.Id); }
public void InitializeSetsIdForTelemetryUsingIdFromRequestTelemetry() { // Arrange var metricTelemetry = new MetricTelemetry("name", 0); var source = new TestableAccountIdTelemetryInitializer(); RequestTelemetry requestTelemetry = source.FakeContext.CreateRequestTelemetryPrivate(); requestTelemetry.Context.User.AccountId = "1"; // Act source.Initialize(metricTelemetry); // Assert Assert.AreEqual("1", metricTelemetry.Context.User.AccountId); }
public void InitializeDoesNotSetIdIfTelemetryHasIt() { // Arrange var metricTelemetry = new MetricTelemetry("name", 0); var source = new TestableAccountIdTelemetryInitializer(); RequestTelemetry requestTelemetry = source.FakeContext.CreateRequestTelemetryPrivate(); requestTelemetry.Context.User.AccountId = "1"; metricTelemetry.Context.User.AccountId = "2"; // Act source.Initialize(metricTelemetry); // Assert Assert.AreEqual("2", metricTelemetry.Context.User.AccountId); }
public void SessionIdIsSetToRunId() { var metricTelemetry = new MetricTelemetry("name", 0); var source = new TestableWebTestTelemetryInitializer(new Dictionary<string, string> { { "SyntheticTest-RunId", "ID" } }); source.Filters.Add(new WebTestHeaderFilter { FilterHeader = "SyntheticTest-RunId", SourceName = "Application Insights Availability Monitoring", UserIdHeader = "SyntheticTest-Location" }); source.Initialize(metricTelemetry); Assert.AreEqual("ID", metricTelemetry.Context.Session.Id); }
public void SyntheticSourceIsNotSetIfUserProvidedValue() { var metricTelemetry = new MetricTelemetry("name", 0); metricTelemetry.Context.Operation.SyntheticSource = "SOURCE"; var source = new TestableSyntheticUserAgentTelemetryInitializer(new Dictionary<string, string> { { "User-Agent", "YandexBot" } }); source.Filters.Add(new SyntheticUserAgentFilter { Pattern = "(YottaaMonitor|BrowserMob|HttpMonitor|YandexBot|BingPreview|PagePeeker|ThumbShotsBot|WebThumb|URL2PNG|ZooShot|GomezA|Catchpoint bot|Willow Internet Crawler|Google SketchUp|Read%20Later)", }); source.Initialize(metricTelemetry); Assert.AreEqual("SOURCE", metricTelemetry.Context.Operation.SyntheticSource); }
public static void TrackMetric(string metricName, double value, string logFileName) { if (!_initialized) { return; } var telemetryClient = new TelemetryClient(); var telemetry = new MetricTelemetry(metricName, value); if (!string.IsNullOrWhiteSpace(logFileName)) { telemetry.Properties.Add("LogFile", logFileName); } telemetryClient.TrackMetric(telemetry); telemetryClient.Flush(); }
public void MetricTelemetrySuppliesConstructorThatAllowsToFullyPopulateAggregationData() { var instance = new MetricTelemetry( metricNamespace: "Test MetricNamespace", name: "Test Metric", count: 4, sum: 40, min: 5, max: 15, standardDeviation: 4.2); Assert.AreEqual("Test MetricNamespace", instance.MetricNamespace); Assert.AreEqual("Test Metric", instance.Name); Assert.AreEqual(4, instance.Count); Assert.AreEqual(40, instance.Sum); Assert.AreEqual(5, instance.Min); Assert.AreEqual(15, instance.Max); Assert.AreEqual(4.2, instance.StandardDeviation); }
public void MetricTelemetrySuppliesPropertiesForCustomerToSendAggregionData() { var instance = new MetricTelemetry(); instance.MetricNamespace = "Test MetricNamespace"; instance.Name = "Test Metric"; instance.Count = 4; instance.Sum = 40; instance.Min = 5.0; instance.Max = 15.0; instance.StandardDeviation = 4.2; Assert.AreEqual("Test MetricNamespace", instance.MetricNamespace); Assert.AreEqual("Test Metric", instance.Name); Assert.AreEqual(4, instance.Count); Assert.AreEqual(40, instance.Sum); Assert.AreEqual(5, instance.Min); Assert.AreEqual(15, instance.Max); Assert.AreEqual(4.2, instance.StandardDeviation); }
public void SanitizeWillTrimAppropriateFields() { MetricTelemetry telemetry = new MetricTelemetry(); telemetry.Name = new string('Z', Property.MaxNameLength + 1); telemetry.Properties.Add(new string('X', Property.MaxDictionaryNameLength) + 'X', new string('X', Property.MaxValueLength + 1)); telemetry.Properties.Add(new string('X', Property.MaxDictionaryNameLength) + 'Y', new string('X', Property.MaxValueLength + 1)); ((ITelemetry)telemetry).Sanitize(); Assert.AreEqual(new string('Z', Property.MaxNameLength), telemetry.Name); Assert.AreEqual(2, telemetry.Properties.Count); var t = new SortedList <string, string>(telemetry.Properties); Assert.AreEqual(new string('X', Property.MaxDictionaryNameLength), t.Keys.ToArray()[1]); Assert.AreEqual(new string('X', Property.MaxValueLength), t.Values.ToArray()[1]); Assert.AreEqual(new string('X', Property.MaxDictionaryNameLength - 3) + "1", t.Keys.ToArray()[0]); Assert.AreEqual(new string('X', Property.MaxValueLength), t.Values.ToArray()[0]); }
public void MetricTelemetryDeepCloneCopiesAllProperties() { var metric = new MetricTelemetry(); metric.Name = "My Page"; #pragma warning disable CS0618 metric.Value = 42; #pragma warning restore CS0618 metric.Count = 5; metric.Min = 1.2; metric.Max = 6.4; metric.StandardDeviation = 0.5; metric.Properties.Add("Property1", "Value1"); MetricTelemetry other = (MetricTelemetry)metric.DeepClone(); CompareLogic deepComparator = new CompareLogic(); var comparisonResult = deepComparator.Compare(metric, other); Assert.IsTrue(comparisonResult.AreEqual, comparisonResult.DifferencesString); }
private static void TrackTelemetryOnIndexReopened(object sender, SegmentInfoEventArgs e) { var telemetryClient = new TelemetryClient(); // track the index reopened event var eventTelemetry = new EventTelemetry("Index Reopened"); eventTelemetry.Metrics.Add("Segment Count", e.Segments.Count()); eventTelemetry.Metrics.Add("Segment Size (Sum)", e.Segments.Sum(s => s.NumDocs)); telemetryClient.TrackEvent(eventTelemetry); foreach (var segment in e.Segments) { var metricTelemetry = new MetricTelemetry("Segment Size", segment.NumDocs); metricTelemetry.Properties.Add("Segment Name", segment.Name); telemetryClient.TrackMetric(metricTelemetry); } telemetryClient.Flush(); }
public void MetricTelemetryPropertiesFromContextAndItemSerializesToPropertiesInJson() { var expected = new MetricTelemetry(); expected.Name = "TestMetric"; expected.Context.GlobalProperties.Add("TestPropertyGlobal", "contextpropvalue"); expected.Properties.Add("TestProperty", "TestPropertyValue"); ((ITelemetry)expected).Sanitize(); Assert.AreEqual(1, expected.Properties.Count); Assert.AreEqual(1, expected.Context.GlobalProperties.Count); Assert.IsTrue(expected.Properties.ContainsKey("TestProperty")); Assert.IsTrue(expected.Context.GlobalProperties.ContainsKey("TestPropertyGlobal")); var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem <AI.MetricData>(expected); // Items added to both Metric.Properties, and Metric.Context.GlobalProperties are serialized to properties. Assert.AreEqual(2, item.data.baseData.properties.Count); Assert.IsTrue(item.data.baseData.properties.ContainsKey("TestPropertyGlobal")); Assert.IsTrue(item.data.baseData.properties.ContainsKey("TestProperty")); }
public void MeasurementMetricTelemetrySerializesToJsonCorrectly() { var expected = new MetricTelemetry(); expected.Name = "My Page"; expected.Value = 42; expected.Properties.Add("Property1", "Value1"); var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<MetricTelemetry, DataPlatformModel.MetricData>(expected); // NOTE: It's correct that we use the v1 name here, and therefore we test against it. Assert.Equal(item.Name, Microsoft.Developer.Analytics.DataCollection.Model.v1.ItemType.Metric); Assert.Equal(typeof(DataPlatformModel.MetricData).Name, item.Data.BaseType); Assert.Equal(2, item.Data.BaseData.Ver); Assert.Equal(1, item.Data.BaseData.Metrics.Count); Assert.Equal(expected.Name, item.Data.BaseData.Metrics[0].Name); Assert.Equal(DataPlatformModel.DataPointType.Measurement, item.Data.BaseData.Metrics[0].Kind); Assert.Equal(expected.Value, item.Data.BaseData.Metrics[0].Value); Assert.False(item.Data.BaseData.Metrics[0].Count.HasValue); Assert.False(item.Data.BaseData.Metrics[0].Min.HasValue); Assert.False(item.Data.BaseData.Metrics[0].Max.HasValue); Assert.False(item.Data.BaseData.Metrics[0].StdDev.HasValue); Assert.Equal(expected.Properties.ToArray(), item.Data.BaseData.Properties.ToArray()); }
public void MetricTelemetryReturnsNonNullContext() { MetricTelemetry item = new MetricTelemetry(); Assert.IsNotNull(item.Context); }
public void EventTelemetryReturnsNonNullContext() { MetricTelemetry item = new MetricTelemetry(); Assert.NotNull(item.Context); }
public void MetricTelemetrySuppliesConstructorThatTakesNameAndValueToSimplifyAdvancedScenarios() { var instance = new MetricTelemetry("Test Metric", 4.2); Assert.Equal("Test Metric", instance.Name); Assert.Equal(4.2, instance.Value); }
/// <summary> /// Send a <see cref="MetricTelemetry"/> for aggregation in Metric Explorer. /// </summary> /// <param name="name">Metric name.</param> /// <param name="value">Metric value.</param> /// <param name="properties">Named string values you can use to classify and filter metrics.</param> public void TrackMetric(string name, double value, IDictionary<string, string> properties = null) { var telemetry = new MetricTelemetry(name, value); if (properties != null && properties.Count > 0) { Utils.CopyDictionary(properties, telemetry.Properties); } this.TrackMetric(telemetry); }
public void SerializeWritesNullValuesAsExpectedByEndpoint() { MetricTelemetry original = new MetricTelemetry(); original.Name = null; original.Max = null; original.Min = null; original.Count = null; original.StandardDeviation = null; ((ITelemetry)original).Sanitize(); var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<MetricTelemetry, DataPlatformModel.MetricData>(original); Assert.Equal(2, item.Data.BaseData.Ver); }
public void MetricTelemetrySerializesStructuredIKeyCorrectlyPreservingCaseOfPrefix() { var metricTelemetry = new MetricTelemetry(); metricTelemetry.Context.InstrumentationKey = "AIC-" + Guid.NewGuid().ToString(); ((ITelemetry)metricTelemetry).Sanitize(); var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<MetricTelemetry, DataPlatformModel.MetricData>(metricTelemetry); Assert.Equal(metricTelemetry.Context.InstrumentationKey, item.IKey); }
public void TrackMetrics(Object state) { double audioPacketRatio = (_audioPacketsSent != 0) ? (double) _audioPacketsLost / _audioPacketsSent : 0; double videoPacketRatio = (_videoPacketsSent != 0) ? (double) _videoPacketsLost / _videoPacketsSent : 0; if (_audioPacketsSent > 0) { MetricTelemetry metric = new MetricTelemetry("Audio Packet Lost Ratio", audioPacketRatio); metric.Timestamp = System.DateTimeOffset.UtcNow; Task.Run(() => _telemetry.TrackMetric(metric)); } if (_videoPacketsSent > 0) { MetricTelemetry metric = new MetricTelemetry("Video Packet Lost Ratio", videoPacketRatio); metric.Timestamp = System.DateTimeOffset.UtcNow; Task.Run(() => _telemetry.TrackMetric(metric)); } // reset flags for the new time period ResetPackets(); }
/// <summary> /// Initializes a new instance of the <see cref="PerformanceCounterTelemetry"/> class. /// </summary> public PerformanceCounterTelemetry() { this.Data = new MetricTelemetry(); }
/// <summary> /// Initializes a new instance of the <see cref="PerformanceCounterTelemetry"/> class by cloning an existing instance. /// </summary> /// <param name="source">Source instance of <see cref="PerformanceCounterTelemetry"/> to clone from.</param> private PerformanceCounterTelemetry(PerformanceCounterTelemetry source) { this.Data = (MetricTelemetry)source.Data.DeepClone(); this.categoryName = source.categoryName; this.counterName = source.counterName; }
private static void SerializeMetricTelemetry(MetricTelemetry metricTelemetry, JsonWriter writer) { writer.WriteStartObject(); metricTelemetry.WriteTelemetryName(writer, MetricTelemetry.TelemetryName); metricTelemetry.WriteEnvelopeProperties(writer); writer.WritePropertyName("data"); { writer.WriteStartObject(); // TODO: MetricTelemetry should write type as this.data.baseType once Common Schema 2.0 compliant. writer.WriteProperty("baseType", metricTelemetry.BaseType); writer.WritePropertyName("baseData"); { writer.WriteStartObject(); writer.WriteProperty("ver", metricTelemetry.Data.ver); writer.WritePropertyName("metrics"); { writer.WriteStartArray(); writer.WriteStartObject(); writer.WriteProperty("name", metricTelemetry.Metric.name); writer.WriteProperty("kind", metricTelemetry.Metric.kind.ToString()); writer.WriteProperty("value", metricTelemetry.Metric.value); writer.WriteProperty("count", metricTelemetry.Metric.count); writer.WriteProperty("min", metricTelemetry.Metric.min); writer.WriteProperty("max", metricTelemetry.Metric.max); writer.WriteProperty("stdDev", metricTelemetry.Metric.stdDev); writer.WriteEndObject(); writer.WriteEndArray(); } writer.WriteProperty("properties", metricTelemetry.Data.properties); writer.WriteEndObject(); } writer.WriteEndObject(); } writer.WriteEndObject(); }
public void CountPropertyGetterReturnsOneIfNoValueIsSet() { MetricTelemetry telemetry = new MetricTelemetry(); Assert.AreEqual(1, telemetry.Count); }
/// <summary> /// Send a <see cref="MetricTelemetry"/> for aggregation in Metric Explorer. /// </summary> public void TrackMetric(MetricTelemetry telemetry) { if (telemetry == null) { telemetry = new MetricTelemetry(); } this.Track(telemetry); }
private static ITelemetry CreateTelemetry(PerformanceCounter pc, string reportAs, bool isCustomCounter, float value) { if (isCustomCounter) { // string.Format(CultureInfo.InvariantCulture, @"\{0}\{1}", pc.CategoryName, pc.CounterName), var metricName = !string.IsNullOrWhiteSpace(reportAs) ? reportAs : string.Format( CultureInfo.InvariantCulture, "{0} - {1}", pc.CategoryName, pc.CounterName); var metricTelemetry = new MetricTelemetry(metricName, value); metricTelemetry.Properties.Add("CounterInstanceName", pc.InstanceName); metricTelemetry.Properties.Add("CustomPerfCounter", "true"); return metricTelemetry; } else { return new PerformanceCounterTelemetry(pc.CategoryName, pc.CounterName, pc.InstanceName, value); } }
public void SanitizeWillTrimAppropriateFields() { MetricTelemetry telemetry = new MetricTelemetry(); telemetry.Name = new string('Z', Property.MaxNameLength + 1); telemetry.Properties.Add(new string('X', Property.MaxDictionaryNameLength) + 'X', new string('X', Property.MaxValueLength + 1)); telemetry.Properties.Add(new string('X', Property.MaxDictionaryNameLength) + 'Y', new string('X', Property.MaxValueLength + 1)); ((ITelemetry)telemetry).Sanitize(); Assert.Equal(new string('Z', Property.MaxNameLength), telemetry.Name); Assert.Equal(2, telemetry.Properties.Count); Assert.Equal(new string('X', Property.MaxDictionaryNameLength), telemetry.Properties.Keys.ToArray()[0]); Assert.Equal(new string('X', Property.MaxValueLength), telemetry.Properties.Values.ToArray()[0]); Assert.Equal(new string('X', Property.MaxDictionaryNameLength - 3) + "001", telemetry.Properties.Keys.ToArray()[1]); Assert.Equal(new string('X', Property.MaxValueLength), telemetry.Properties.Values.ToArray()[1]); }
public void TrackMetric(String name, double value) { MetricTelemetry metric = new MetricTelemetry(name, value); metric.Timestamp = System.DateTimeOffset.UtcNow; Task.Run(() => _telemetry.TrackMetric(metric)); }
public void SanitizePopulatesNameWithErrorBecauseItIsRequiredByEndpoint() { var telemetry = new MetricTelemetry { Name = null }; ((ITelemetry)telemetry).Sanitize(); Assert.Contains("name", telemetry.Name, StringComparison.OrdinalIgnoreCase); Assert.Contains("required", telemetry.Name, StringComparison.OrdinalIgnoreCase); }
public void TrackCurrentDelayMetrics() { if (_audioDelayCount > 0) { MetricTelemetry metric = new MetricTelemetry("Audio Current Delay Ratio", _audioCurrentDelayMs / _audioDelayCount); metric.Timestamp = System.DateTimeOffset.UtcNow; Task.Run(() => _telemetry.TrackMetric(metric)); } if (_videoDelayCount > 0) { MetricTelemetry metric = new MetricTelemetry("Video Current Delay Ratio", _videoCurrentDelayMs / _videoDelayCount); metric.Timestamp = System.DateTimeOffset.UtcNow; Task.Run(() => _telemetry.TrackMetric(metric)); } ResetDelays(); }
public void TrackMetric(string metricName, double metricValue) { var tel = new MetricTelemetry(metricName, metricValue); tel.Properties.Add(new KeyValuePair<string, string>("Machine_name", _machine)); tel.Properties.Add(new KeyValuePair<string, string>("User_name", _user)); tel.Properties.Add(new KeyValuePair<string, string>("Domain_name", _domain)); _telemetryClient.TrackMetric(tel); }