Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AzureMonitorExporter] minor test improvements. add additional logging to TelemetryItems tests #31557

Merged
merged 7 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using Azure.Monitor.OpenTelemetry.Exporter.Models;
using Xunit.Abstractions;

namespace Azure.Monitor.OpenTelemetry.Exporter.Tests.CommonTestFramework
{
internal class TelemetryItemOutputHelper
{
private readonly ITestOutputHelper output;

public TelemetryItemOutputHelper(ITestOutputHelper output)
{
this.output = output;
}

public void Write(IEnumerable<TelemetryItem> telemetryItems)
{
if (telemetryItems == null)
{
return;
}

foreach (var telemetryItem in telemetryItems)
{
this.Write(telemetryItem);
}
}

public void Write(TelemetryItem telemetryItem)
{
output.WriteLine(new string('-', 32));

output.WriteLine($"Name: {telemetryItem.Name}");
output.WriteLine($"Tags: {telemetryItem.Tags.Count}");
foreach (var tag in telemetryItem.Tags)
{
output.WriteLine($"\t{tag.Key}: {tag.Value}");
}

WriteBaseData(telemetryItem);
}

private void WriteBaseData(TelemetryItem telemetryItem)
{
var baseType = telemetryItem.Data.BaseType;
var baseData = telemetryItem.Data.BaseData;
output.WriteLine($"\nBaseData: {baseType}");

switch (baseType)
{
case nameof(RequestData):
WriteRequestData((RequestData)baseData);
break;
case nameof(RemoteDependencyData):
WriteRemoteDependencyData((RemoteDependencyData)baseData);
break;
case nameof(MessageData):
WriteMessageData((MessageData)baseData);
break;
case "MetricData":
WriteMetricsData((MetricsData)baseData);
break;
case "ExceptionData":
WriteExceptionData((TelemetryExceptionData)baseData);
break;
default:
output.WriteLine($"***WriteBaseData not implemented for '{baseType}'***");
break;
}
}

private void WriteExceptionData(TelemetryExceptionData exceptionData)
{
output.WriteLine($"SeverityLevel: {exceptionData.SeverityLevel}");

output.WriteLine($"Exceptions: {exceptionData.Exceptions.Count}");
foreach (var exceptionDetails in exceptionData.Exceptions)
{
output.WriteLine($"\tTypeName: {exceptionDetails.TypeName}");
output.WriteLine($"\tMessage: {exceptionDetails.Message}");
}
}

private void WriteMetricsData(MetricsData metricsData)
{
foreach (var metric in metricsData.Metrics)
{
output.WriteLine($"Name: {metric.Name}");
output.WriteLine($"Namespace: {metric.Namespace}");
output.WriteLine($"\tCount: {metric.Count}");
output.WriteLine($"\tValue: {metric.Value}");
output.WriteLine($"\tMin: {metric.Min}");
output.WriteLine($"\tMax: {metric.Max}");
}
}

private void WriteMessageData(MessageData messageData)
{
output.WriteLine($"Name: {messageData.Message}");
output.WriteLine($"SeverityLevel: {messageData.SeverityLevel}");

output.WriteLine($"Properties: {messageData.Properties.Count}");
foreach (var prop in messageData.Properties)
{
output.WriteLine($"\t{prop.Key}: {prop.Value}");
}
}

private void WriteRemoteDependencyData(RemoteDependencyData remoteDependencyData)
{
output.WriteLine($"Name: {remoteDependencyData.Name}");

output.WriteLine($"Properties: {remoteDependencyData.Properties.Count}");
foreach (var prop in remoteDependencyData.Properties)
{
output.WriteLine($"\t{prop.Key}: {prop.Value}");
}
}

private void WriteRequestData(RequestData requestData)
{
output.WriteLine($"Name: {requestData.Name}");
output.WriteLine($"Url: {requestData.Url}");
output.WriteLine($"ResponseCode: {requestData.ResponseCode}");

output.WriteLine($"Properties: {requestData.Properties.Count}");
foreach (var prop in requestData.Properties)
{
output.WriteLine($"\t{prop.Key}: {prop.Value}");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Extensions.Logging;
using OpenTelemetry.Logs;
using Xunit;
using Xunit.Abstractions;

namespace Azure.Monitor.OpenTelemetry.Exporter.Tests.E2ETelemetryItemValidation
{
Expand All @@ -19,6 +20,13 @@ namespace Azure.Monitor.OpenTelemetry.Exporter.Tests.E2ETelemetryItemValidation
/// </summary>
public class LogsTests
{
internal readonly TelemetryItemOutputHelper telemetryOutput;

public LogsTests(ITestOutputHelper output)
{
this.telemetryOutput = new TelemetryItemOutputHelper(output);
}

[Theory]
[InlineData(LogLevel.Information, "Information")]
[InlineData(LogLevel.Warning, "Warning")]
Expand Down Expand Up @@ -59,6 +67,7 @@ public void VerifyLog(LogLevel logLevel, string expectedSeverityLevel)

// ASSERT
Assert.True(telemetryItems.Any(), "Unit test failed to collect telemetry.");
this.telemetryOutput.Write(telemetryItems);
var telemetryItem = telemetryItems.Single();

TelemetryItemValidationHelper.AssertLog_As_MessageTelemetry(
Expand Down Expand Up @@ -118,6 +127,7 @@ public void VerifyException(LogLevel logLevel, string expectedSeverityLevel)

// ASSERT
Assert.True(telemetryItems.Any(), "Unit test failed to collect telemetry.");
this.telemetryOutput.Write(telemetryItems);
var telemetryItem = telemetryItems.Single();

TelemetryItemValidationHelper.AssertLog_As_ExceptionTelemetry(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
using Xunit;
using Xunit.Abstractions;

namespace Azure.Monitor.OpenTelemetry.Exporter.Tests.E2ETelemetryItemValidation
{
Expand All @@ -21,6 +22,13 @@ namespace Azure.Monitor.OpenTelemetry.Exporter.Tests.E2ETelemetryItemValidation
/// </summary>
public class MetricsTests
{
internal readonly TelemetryItemOutputHelper telemetryOutput;

public MetricsTests(ITestOutputHelper output)
{
this.telemetryOutput = new TelemetryItemOutputHelper(output);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down Expand Up @@ -57,6 +65,7 @@ public void VerifyCounter(bool asView)

// ASSERT
Assert.True(telemetryItems.Any(), "Unit test failed to collect telemetry.");
this.telemetryOutput.Write(telemetryItems);
var telemetryItem = telemetryItems.Single();

TelemetryItemValidationHelper.AssertCounter_As_MetricTelemetry(
Expand Down Expand Up @@ -107,6 +116,7 @@ public void VerifyHistogram(bool asView)

// ASSERT
Assert.True(telemetryItems.Any(), "Unit test failed to collect telemetry.");
this.telemetryOutput.Write(telemetryItems);
var telemetryItem = telemetryItems.Single();

TelemetryItemValidationHelper.AssertHistogram_As_MetricTelemetry(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using OpenTelemetry.Logs;
using OpenTelemetry.Trace;
using Xunit;
using Xunit.Abstractions;

namespace Azure.Monitor.OpenTelemetry.Exporter.Tests.E2ETelemetryItemValidation
{
Expand All @@ -22,6 +23,13 @@ namespace Azure.Monitor.OpenTelemetry.Exporter.Tests.E2ETelemetryItemValidation
/// </summary>
public class TracesTests
{
internal readonly TelemetryItemOutputHelper telemetryOutput;

public TracesTests(ITestOutputHelper output)
{
this.telemetryOutput = new TelemetryItemOutputHelper(output);
}

[Theory]
[InlineData(ActivityKind.Client)]
[InlineData(ActivityKind.Producer)]
Expand Down Expand Up @@ -56,6 +64,7 @@ public void VerifyTrace_CreatesDependency(ActivityKind activityKind)

// ASSERT
Assert.True(telemetryItems.Any(), "Unit test failed to collect telemetry.");
this.telemetryOutput.Write(telemetryItems);
var telemetryItem = telemetryItems.Single();

TelemetryItemValidationHelper.AssertActivity_As_DependencyTelemetry(
Expand Down Expand Up @@ -98,6 +107,7 @@ public void VerifyTrace_CreatesRequest(ActivityKind activityKind)

// ASSERT
Assert.True(telemetryItems.Any(), "Unit test failed to collect telemetry.");
this.telemetryOutput.Write(telemetryItems);
var telemetryItem = telemetryItems.Single();

TelemetryItemValidationHelper.AssertActivity_As_RequestTelemetry(
Expand Down Expand Up @@ -144,8 +154,9 @@ public void VerifyLogWithinActivity(LogLevel logLevel, string expectedSeverityLe

// ACT
string spanId, traceId;
string activityName = $"TestActivity {nameof(VerifyLogWithinActivity)} {logLevel}";

using (var activity = activitySource.StartActivity(name: "SayHello"))
using (var activity = activitySource.StartActivity(name: activityName))
{
spanId = activity.SpanId.ToHexString();
traceId = activity.TraceId.ToHexString();
Expand All @@ -165,16 +176,18 @@ public void VerifyLogWithinActivity(LogLevel logLevel, string expectedSeverityLe
loggerFactory.Dispose();

// ASSERT
Assert.True(activityTelemetryItems.Any(), "test project did not capture telemetry");
Assert.True(activityTelemetryItems.Any(), "Unit test failed to collect telemetry.");
this.telemetryOutput.Write(activityTelemetryItems);
var activityTelemetryItem = activityTelemetryItems.Single();

TelemetryItemValidationHelper.AssertActivity_As_DependencyTelemetry(
telemetryItem: activityTelemetryItem,
expectedName: "SayHello",
expectedName: activityName,
expectedTraceId: traceId,
expectedProperties: null);

Assert.True(logTelemetryItems.Any(), "Unit test failed to collect telemetry.");
this.telemetryOutput.Write(logTelemetryItems);
var logTelemetryItem = logTelemetryItems.Single();

TelemetryItemValidationHelper.AssertLog_As_MessageTelemetry(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Threading;

using Azure.Monitor.OpenTelemetry.Exporter.Models;

using Azure.Monitor.OpenTelemetry.Exporter.Tests.CommonTestFramework;
using Microsoft.AspNetCore.Mvc.Testing;

using Xunit;
Expand Down