From 9484af650ba115284d4846af82bf0a4c6d44339b Mon Sep 17 00:00:00 2001 From: Timothy Mothra Date: Thu, 19 Nov 2020 10:52:32 -0800 Subject: [PATCH] Azure Monitor Exporter: refactor test (#17082) * refactor test * Update TelemetryItemTests.cs --- .../TelemetryItemTests.cs | 49 +++++++++++-------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/sdk/monitor/Microsoft.OpenTelemetry.Exporter.AzureMonitor/tests/Microsoft.OpenTelemetry.Exporter.AzureMonitor.Integration.Tests/TelemetryItemTests.cs b/sdk/monitor/Microsoft.OpenTelemetry.Exporter.AzureMonitor/tests/Microsoft.OpenTelemetry.Exporter.AzureMonitor.Integration.Tests/TelemetryItemTests.cs index bdfd180d4fe6..09b40fda7e09 100644 --- a/sdk/monitor/Microsoft.OpenTelemetry.Exporter.AzureMonitor/tests/Microsoft.OpenTelemetry.Exporter.AzureMonitor.Integration.Tests/TelemetryItemTests.cs +++ b/sdk/monitor/Microsoft.OpenTelemetry.Exporter.AzureMonitor/tests/Microsoft.OpenTelemetry.Exporter.AzureMonitor.Integration.Tests/TelemetryItemTests.cs @@ -1,12 +1,14 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Microsoft.OpenTelemetry.Exporter.AzureMonitor.Integration.Tests.TestFramework; +using Microsoft.OpenTelemetry.Exporter.AzureMonitor.Models; using OpenTelemetry; using OpenTelemetry.Trace; @@ -20,8 +22,6 @@ namespace Microsoft.OpenTelemetry.Exporter.AzureMonitor.Integration.Tests /// public class TelemetryItemTests { - private const string ActivitySourceName = "MyCompany.MyProduct.MyLibrary"; - private static readonly ActivitySource MyActivitySource = new ActivitySource(ActivitySourceName); private const string EmptyConnectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000"; /// @@ -38,21 +38,17 @@ public class TelemetryItemTests [InlineData(ActivityKind.Server)] public void VerifyActivity(ActivityKind activityKind) { - this.Setup(processor: out BatchExportProcessor processor, transmitter: out MockTransmitter transmitter); - var activityName = "TestActivity"; - using (var activity = MyActivitySource.StartActivity(name: activityName, kind: activityKind)) - { - activity.SetTag("integer", 1); - activity.SetTag("message", "Hello World!"); - activity.SetTag("intArray", new int[] { 1, 2, 3 }); - } - - processor.ForceFlush(); - Task.Delay(100).Wait(); //TODO: HOW TO REMOVE THIS WAIT? - Assert.True(transmitter.TelemetryItems.Any(), "test project did not capture telemetry"); - var telemetryItem = transmitter.TelemetryItems.First(); + var telemetryItem = this.RunActivityTest((activitySource) => + { + using (var activity = activitySource.StartActivity(name: activityName, kind: activityKind)) + { + activity.SetTag("integer", 1); + activity.SetTag("message", "Hello World!"); + activity.SetTag("intArray", new int[] { 1, 2, 3 }); + } + }); VerifyTelemetryItem.Verify( telemetryItem: telemetryItem, @@ -67,13 +63,14 @@ public void VerifyActivity(ActivityKind activityKind) }); } - // TODO: ADD THIS TEST AFTER IMPLEMENTING ILOGGER EXPORTER - //public void VerifyILoggerAsTelemetryItem - - private void Setup(out BatchExportProcessor processor, out MockTransmitter transmitter ) + private TelemetryItem RunActivityTest(Action testScenario) { - transmitter = new MockTransmitter(); - processor = new BatchExportProcessor(new AzureMonitorTraceExporter( + // SETUP + var ActivitySourceName = "MyCompany.MyProduct.MyLibrary"; + using var activitySource = new ActivitySource(ActivitySourceName); + + var transmitter = new MockTransmitter(); + var processor = new BatchExportProcessor(new AzureMonitorTraceExporter( options: new AzureMonitorExporterOptions { ConnectionString = EmptyConnectionString, @@ -85,6 +82,16 @@ private void Setup(out BatchExportProcessor processor, out MockTransmi .AddSource(ActivitySourceName) .AddProcessor(processor) .Build(); + + // ACT + testScenario(activitySource); + + // CLEANUP + processor.ForceFlush(); + Task.Delay(100).Wait(); //TODO: HOW TO REMOVE THIS WAIT? + + Assert.True(transmitter.TelemetryItems.Any(), "test project did not capture telemetry"); + return transmitter.TelemetryItems.Single(); } } }