-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add manual+automatic instrumentation tests + fix
SetUser
bug (#4938)
* Add integration tests + sample for manual instrumentation * Add fix for calling SetUser outside of an ASP.NET Core context * Add workaround for the flake * Fix manual snapshots
- Loading branch information
1 parent
924dc94
commit a1a1a81
Showing
9 changed files
with
1,689 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// <copyright file="AspNetCoreAvailabilityChecker.cs" company="Datadog"> | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
// </copyright> | ||
|
||
#if !NETFRAMEWORK | ||
|
||
using System; | ||
|
||
namespace Datadog.Trace; | ||
|
||
internal static class AspNetCoreAvailabilityChecker | ||
{ | ||
private static readonly Lazy<bool> IsAvailable = new(CheckForRequiredTypes); | ||
|
||
private static bool CheckForRequiredTypes() | ||
{ | ||
try | ||
{ | ||
// Try to load the HttpContext type | ||
// Assumes that this is only called when we would expect the type to | ||
// already be loaded, for example inside an ASP.NET Core request. | ||
// As this is only checked _once_, it assumes the customer doesn't call | ||
// this API both _before_ ASP.NET Core types are loaded _and_ afterwards. | ||
var httpContextType = Type.GetType("Microsoft.AspNetCore.Http.HttpContext, Microsoft.AspNetCore.Http.Abstractions", throwOnError: false); | ||
return httpContextType is not null; | ||
} | ||
catch | ||
{ | ||
// Problem loading the type, so assume we're not in aspnetcore | ||
return false; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Should be called when ASP.NET Core is expected to be loaded, | ||
/// and checks if ASP.NET Core types are available | ||
/// </summary> | ||
public static bool IsAspNetCoreAvailable() => IsAvailable.Value; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
tracer/test/Datadog.Trace.ClrProfiler.IntegrationTests/ManualInstrumentationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// <copyright file="ManualInstrumentationTests.cs" company="Datadog"> | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
// </copyright> | ||
|
||
using System.Threading.Tasks; | ||
using Datadog.Trace.TestHelpers; | ||
using FluentAssertions; | ||
using FluentAssertions.Execution; | ||
using VerifyXunit; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Datadog.Trace.ClrProfiler.IntegrationTests; | ||
|
||
[UsesVerify] | ||
public class ManualInstrumentationTests : TestHelper | ||
{ | ||
public ManualInstrumentationTests(ITestOutputHelper output) | ||
: base("ManualInstrumentation", output) | ||
{ | ||
} | ||
|
||
[SkippableFact] | ||
[Trait("RunOnWindows", "True")] | ||
public async Task ManualAndAutomatic() | ||
{ | ||
const int expectedSpans = 36; | ||
using var telemetry = this.ConfigureTelemetry(); | ||
using var agent = EnvironmentHelper.GetMockAgent(); | ||
using var assert = new AssertionScope(); | ||
using var process = RunSampleAndWaitForExit(agent); | ||
|
||
var spans = agent.WaitForSpans(expectedSpans); | ||
spans.Should().HaveCount(expectedSpans); | ||
|
||
var settings = VerifyHelper.GetSpanVerifierSettings(); | ||
|
||
await VerifyHelper.VerifySpans(spans, settings); | ||
} | ||
|
||
[SkippableFact] | ||
[Trait("RunOnWindows", "True")] | ||
public async Task ManualOnly() | ||
{ | ||
EnvironmentHelper.SetAutomaticInstrumentation(false); | ||
const int expectedSpans = 29; | ||
using var telemetry = this.ConfigureTelemetry(); | ||
using var agent = EnvironmentHelper.GetMockAgent(); | ||
using var assert = new AssertionScope(); | ||
using var process = RunSampleAndWaitForExit(agent); | ||
|
||
var spans = agent.WaitForSpans(expectedSpans); | ||
spans.Should().HaveCount(expectedSpans); | ||
|
||
var settings = VerifyHelper.GetSpanVerifierSettings(); | ||
|
||
await VerifyHelper.VerifySpans(spans, settings); | ||
} | ||
} |
Oops, something went wrong.