Skip to content

Commit

Permalink
test: simplify integration tests (#414)
Browse files Browse the repository at this point in the history
* test: simplify integration tests

* feat: add operation ID to correlation options

* test: simplify integration tests

* pr-follow: up merge w/ 'master'

* pr-fix: update with correct serilog config building

* pr-fix: remove unn serilog config field

* pr-fix: use test output in docker test

* pr-fix: update with correct parent methods

* pr-fix: use http trigger logging back
  • Loading branch information
stijnmoreels authored Jun 28, 2022
1 parent 1374fec commit fef9766
Show file tree
Hide file tree
Showing 23 changed files with 816 additions and 1,400 deletions.
3 changes: 0 additions & 3 deletions src/Arcus.Observability.Tests.Integration/IntegrationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ namespace Arcus.Observability.Tests.Integration
public class IntegrationTest
{
protected IConfiguration Configuration { get; }
protected XunitTestLogger Logger { get; }

public IntegrationTest(ITestOutputHelper testOutput)
{
Logger = new XunitTestLogger(testOutput);

// The appsettings.local.json allows users to override (gitignored) settings locally for testing purposes
Configuration = new ConfigurationBuilder()
.AddJsonFile(path: "appsettings.json")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GuardNet;
using Microsoft.Azure.ApplicationInsights.Query;
using Microsoft.Azure.ApplicationInsights.Query.Models;
using Xunit;
using Xunit.Sdk;

namespace Arcus.Observability.Tests.Integration.Serilog.Sinks.ApplicationInsights
{
/// <summary>
/// Represents a simple client to interact with the available features of Application Insights.
/// </summary>
public class ApplicationInsightsClient
{
private const string PastHalfHourTimeSpan = "PT30M";

private readonly IApplicationInsightsDataClient _dataClient;
private readonly string _applicationId;

/// <summary>
/// Initializes a new instance of the <see cref="ApplicationInsightsClient" /> class.
/// </summary>
/// <param name="dataClient">The core client to retrieve telemetry from Application Insights.</param>
/// <param name="applicationId">The application ID to identify the Application Insights resource to request telemetry from.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="dataClient"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="applicationId"/> is blank.</exception>
public ApplicationInsightsClient(IApplicationInsightsDataClient dataClient, string applicationId)
{
Guard.NotNull(dataClient, nameof(dataClient), "Requires a data client implementation to retrieve telemetry form Application Insights");
Guard.NotNullOrWhitespace(applicationId, nameof(applicationId), "Requires an application ID to identify the Application Insights resource to request telemetry from");

_dataClient = dataClient;
_applicationId = applicationId;
}

/// <summary>
/// Gets the currently available dependencies tracked on Application Insights.
/// </summary>
/// <exception cref="NotEmptyException">Thrown when the retrieved telemetry doesn't contain any items.</exception>
public async Task<EventsDependencyResult[]> GetDependenciesAsync()
{
EventsResults<EventsDependencyResult> result =
await _dataClient.Events.GetDependencyEventsAsync(_applicationId, timespan: PastHalfHourTimeSpan);

Assert.NotEmpty(result.Value);
return result.Value.ToArray();
}

/// <summary>
/// Gets the currently available requests tracked on Application Insights.
/// </summary>
/// <exception cref="NotEmptyException">Thrown when the retrieved telemetry doesn't contain any items.</exception>
public async Task<EventsRequestResult[]> GetRequestsAsync()
{
EventsResults<EventsRequestResult> result =
await _dataClient.Events.GetRequestEventsAsync(_applicationId, timespan: PastHalfHourTimeSpan);

Assert.NotEmpty(result.Value);
return result.Value.ToArray();
}

/// <summary>
/// Gets the currently available metrics tracked on Application Insights.
/// </summary>
/// <param name="body">The schema body content of the request to filter out metrics.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="body"/> is <c>null</c>.</exception>
/// <exception cref="NotEmptyException">Thrown when the retrieved telemetry doesn't contain any items.</exception>
public async Task<MetricsResultsItem[]> GetMetricsAsync(MetricsPostBodySchema body)
{
Guard.NotNull(body, nameof(body), "Requires a metrics body schema to request specific metrics from Application Insights");

IList<MetricsResultsItem> items =
await _dataClient.Metrics.GetMultipleAsync(_applicationId, new List<MetricsPostBodySchema> { body });

return items.ToArray();
}

/// <summary>
/// Gets the currently available exceptions tracked on Application Insights.
/// </summary>
/// <exception cref="NotEmptyException">Thrown when the retrieved telemetry doesn't contain any items.</exception>
public async Task<EventsExceptionResult[]> GetExceptionsAsync()
{
EventsResults<EventsExceptionResult> result =
await _dataClient.Events.GetExceptionEventsAsync(_applicationId, timespan: PastHalfHourTimeSpan);

Assert.NotEmpty(result.Value);
return result.Value.ToArray();
}

/// <summary>
/// Gets the currently available traces tracked on Application Insights.
/// </summary>
/// <exception cref="NotEmptyException">Thrown when the retrieved telemetry doesn't contain any items.</exception>
public async Task<EventsTraceResult[]> GetTracesAsync()
{
EventsResults<EventsTraceResult> result =
await _dataClient.Events.GetTraceEventsAsync(_applicationId, timespan: PastHalfHourTimeSpan);

Assert.NotEmpty(result.Value);
return result.Value.ToArray();
}

/// <summary>
/// Gets the currently available events tracked on Application Insights.
/// </summary>
/// <exception cref="NotEmptyException">Thrown when the retrieved telemetry doesn't contain any items.</exception>
public async Task<EventsCustomEventResult[]> GetEventsAsync()
{
EventsResults<EventsCustomEventResult> result =
await _dataClient.Events.GetCustomEventsAsync(_applicationId, timespan: PastHalfHourTimeSpan);

Assert.NotEmpty(result.Value);
return result.Value.ToArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Threading.Tasks;
using Microsoft.Azure.ApplicationInsights.Query;
using Microsoft.Azure.ApplicationInsights.Query.Models;
using Microsoft.Extensions.Logging;
using Serilog;
Expand Down Expand Up @@ -29,29 +28,20 @@ public async Task Sink_WithConnectionString_WritesTelemetry()
.WriteTo.AzureApplicationInsightsWithConnectionString(connectionString);

var message = "Something to log with connection string";
ILogger logger = CreateLogger(configuration);

// Act
using (ILoggerFactory factory = CreateLoggerFactory(configuration))
{
ILogger logger = factory.CreateLogger<ApplicationInsightsSinkExtensionTests>();
logger.LogInformation(message);
}
logger.LogInformation(message);

// Assert
using (ApplicationInsightsDataClient client = CreateApplicationInsightsClient())
await RetryAssertUntilTelemetryShouldBeAvailableAsync(async client =>
{
await RetryAssertUntilTelemetryShouldBeAvailableAsync(async () =>
EventsTraceResult[] traces = await client.GetTracesAsync();
AssertX.Any(traces, trace =>
{
EventsResults<EventsTraceResult> traces =
await client.Events.GetTraceEventsAsync(ApplicationId, PastHalfHourTimeSpan);

Assert.NotEmpty(traces.Value);
AssertX.Any(traces.Value, trace =>
{
Assert.Equal(message, trace.Trace.Message);
});
Assert.Equal(message, trace.Trace.Message);
});
}
});
}

[Fact]
Expand All @@ -62,29 +52,20 @@ public async Task Sink_WithInstrumentationKey_WritesTelemetry()
.WriteTo.AzureApplicationInsightsWithInstrumentationKey(InstrumentationKey);

var message = "Something to log with connection string";
ILogger logger = CreateLogger(configuration);

// Act
using (ILoggerFactory factory = CreateLoggerFactory(configuration))
{
ILogger logger = factory.CreateLogger<ApplicationInsightsSinkExtensionTests>();
logger.LogInformation(message);
}
logger.LogInformation(message);

// Assert
using (ApplicationInsightsDataClient client = CreateApplicationInsightsClient())
await RetryAssertUntilTelemetryShouldBeAvailableAsync(async client =>
{
await RetryAssertUntilTelemetryShouldBeAvailableAsync(async () =>
EventsTraceResult[] traces = await client.GetTracesAsync();
AssertX.Any(traces, trace =>
{
EventsResults<EventsTraceResult> traces =
await client.Events.GetTraceEventsAsync(ApplicationId, PastHalfHourTimeSpan);

Assert.NotEmpty(traces.Value);
AssertX.Any(traces.Value, trace =>
{
Assert.Equal(message, trace.Trace.Message);
});
Assert.Equal(message, trace.Trace.Message);
});
}
});
}
}
}
Loading

0 comments on commit fef9766

Please sign in to comment.