Skip to content

Commit

Permalink
OTEL for all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Struan Judd committed Jan 5, 2024
1 parent 10a9470 commit 53380fe
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ _site/
_exported_templates/

test/**/*.received.*
publish/
activities.yml
per-class
per-class/
runs-*.yml
19 changes: 17 additions & 2 deletions GqlPlus.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
.gitignore = .gitignore
.prettierrc.yml = .prettierrc.yml
coverage.ps1 = coverage.ps1
coverage.runsettings = coverage.runsettings
.github\dependabot.yml = .github\dependabot.yml
Directory.Build.props = Directory.Build.props
.github\workflows\dotnet-test.yml = .github\workflows\dotnet-test.yml
Expand All @@ -31,6 +29,21 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GqlPlus.Verifier.ComponentT
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GqlPlus.Verifier.TestBase", "test\GqlPlus.Verifier.TestBase\GqlPlus.Verifier.TestBase.csproj", "{B73CDEC7-3F67-444E-8A06-16C20DF56DFB}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Coverage", "Coverage", "{B152D8E1-AD02-4204-8E0D-A475F2A14183}"
ProjectSection(SolutionItems) = preProject
class-combine.ps1 = class-combine.ps1
class-coverage.ps1 = class-coverage.ps1
class.runsettings = class.runsettings
coverage.ps1 = coverage.ps1
coverage.runsettings = coverage.runsettings
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Otel", "Otel", "{90C6E29D-04F6-427C-8E57-27C57664AC04}"
ProjectSection(SolutionItems) = preProject
otel-tests.ps1 = otel-tests.ps1
otel.runsettings = otel.runsettings
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -62,6 +75,8 @@ Global
{B6FDD7DD-6C32-4A87-8304-8DA0A2718E50} = {3B90AC37-1A6B-41AF-8490-E9D48E429ACD}
{D97ABCF2-E6C4-4384-AFB7-C55943EBE1E2} = {3B90AC37-1A6B-41AF-8490-E9D48E429ACD}
{B73CDEC7-3F67-444E-8A06-16C20DF56DFB} = {3B90AC37-1A6B-41AF-8490-E9D48E429ACD}
{B152D8E1-AD02-4204-8E0D-A475F2A14183} = {4E1645F9-6194-4B1F-B93D-5B2CDDE5E6DE}
{90C6E29D-04F6-427C-8E57-27C57664AC04} = {4E1645F9-6194-4B1F-B93D-5B2CDDE5E6DE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {148B651C-DFAA-434F-B1ED-51E535FC2DE8}
Expand Down
18 changes: 18 additions & 0 deletions otel-tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[CmdletBinding()]
param (
[switch]$Keep = $false
)

dotnet publish -r win-x64 -o .\publish

if (!$Keep) {
Remove-Item .\auto-logs\ -Force -Recurse -ErrorAction Ignore
}

Push-Location .\publish
try {
dotnet vstest .\GqlPlus.Verifier.*Tests.dll /settings:../otel.runsettings
}
finally {
Pop-Location
}
34 changes: 34 additions & 0 deletions otel.runsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- File name extension must be .runsettings -->
<RunSettings>
<RunConfiguration>
<TargetPlatform>x64</TargetPlatform>
<EnvironmentVariables>
<!-- Settings for .NET -->
<CORECLR_ENABLE_PROFILING>1</CORECLR_ENABLE_PROFILING>
<CORECLR_PROFILER>{918728DD-259F-4A6A-AC2B-B85E1B658318}</CORECLR_PROFILER>
<CORECLR_PROFILER_PATH>C:\Dev\graphql-plus\publish\OpenTelemetry.AutoInstrumentation.Native.dll</CORECLR_PROFILER_PATH>

<DOTNET_STARTUP_HOOKS>C:\Dev\graphql-plus\publish\OpenTelemetry.AutoInstrumentation.StartupHook.dll</DOTNET_STARTUP_HOOKS>
<ASPNETCORE_HOSTINGSTARTUPASSEMBLIES>OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper</ASPNETCORE_HOSTINGSTARTUPASSEMBLIES>

<!-- Settings for OpenTelemetry -->
<OTEL_DOTNET_AUTO_HOME>C:\Dev\graphql-plus\publish\</OTEL_DOTNET_AUTO_HOME>
<OTEL_DOTNET_AUTO_RULE_ENGINE_ENABLED>false</OTEL_DOTNET_AUTO_RULE_ENGINE_ENABLED>
<OTEL_DOTNET_AUTO_TRACES_ADDITIONAL_SOURCES>Xunit.*</OTEL_DOTNET_AUTO_TRACES_ADDITIONAL_SOURCES>
<OTEL_DOTNET_AUTO_TRACES_CONSOLE_EXPORTER_ENABLED>true</OTEL_DOTNET_AUTO_TRACES_CONSOLE_EXPORTER_ENABLED>
<OTEL_TRACES_SAMPLER>always_on</OTEL_TRACES_SAMPLER>
<OTEL_BSP_SCHEDULE_DELAY>1500</OTEL_BSP_SCHEDULE_DELAY>
<OTEL_METRIC_EXPORT_INTERVAL>2500</OTEL_METRIC_EXPORT_INTERVAL>

<OTEL_DOTNET_AUTO_LOG_DIRECTORY>..\auto-logs</OTEL_DOTNET_AUTO_LOG_DIRECTORY>
<!--
<OTEL_LOG_LEVEL>debug</OTEL_LOG_LEVEL>
-->
</EnvironmentVariables>
<xUnit>
<DiagnosticMessages>true</DiagnosticMessages>
<InternalDiagnosticMessages>true</InternalDiagnosticMessages>
</xUnit>
</RunConfiguration>
</RunSettings>
7 changes: 0 additions & 7 deletions test/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
<Project>

<ItemGroup>
<PackageReference Include="AutoFixture.Xunit2" Version="4.18.1" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.4" />
</ItemGroup>

<ItemGroup Condition="$(IsTestProject) == true">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
1 change: 1 addition & 0 deletions test/GqlPlus.Verifier.ClassTests/Ast/AstBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public abstract class AstBaseTests
: AstBaseTests<string>
{ }

[TracePerTest]
public abstract class AstBaseTests<I>
{
[Fact]
Expand Down
9 changes: 0 additions & 9 deletions test/GqlPlus.Verifier.ClassTests/BaseTestWithOpenTelemetry.cs

This file was deleted.

2 changes: 2 additions & 0 deletions test/GqlPlus.Verifier.ClassTests/Merging/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace GqlPlus.Verifier.Merging;

[TracePerTest]

public abstract class TestBase<TItem>
where TItem : AstBase
{
Expand Down
2 changes: 2 additions & 0 deletions test/GqlPlus.Verifier.ClassTests/Parse/ClassTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace GqlPlus.Verifier.Parse;

[TracePerTest]

public class ClassTestBase
{
protected static Tokenizer Tokens(string input)
Expand Down
1 change: 1 addition & 0 deletions test/GqlPlus.Verifier.ClassTests/Result/BaseResultTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace GqlPlus.Verifier.Result;

[TracePerTest]
public class BaseResultTests
{
protected const string Sample = "Sample";
Expand Down
2 changes: 1 addition & 1 deletion test/GqlPlus.Verifier.ClassTests/TokenizerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace GqlPlus.Verifier;

[TracePerTest]
public class TokenizerTests : BaseTestWithOpenTelemetry
public class TokenizerTests
{
private static Tokenizer PrepareTokens(string input)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Verify.Xunit" Version="22.11.1" />
<PackageReference Include="Verify.Xunit" Version="22.11.3" />
<PackageReference Include="Xunit.DependencyInjection" Version="8.9.1" />
<PackageReference Include="Xunit.DependencyInjection.Logging" Version="8.1.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoFixture.Xunit2" Version="4.18.1" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="OpenTelemetry.AutoInstrumentation" Version="1.2.0" />
<PackageReference Include="OpenTelemetry.Exporter.InMemory" Version="1.7.0" />
<PackageReference Include="xunit" Version="2.6.5" />
<PackageReference Include="YamlDotNet" Version="13.7.1" />
</ItemGroup>

Expand Down
28 changes: 23 additions & 5 deletions test/GqlPlus.Verifier.TestBase/TracePerTestAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,30 @@ public override void Before(MethodInfo methodUnderTest)
{
ActivityLink[]? links = OpenTelemetryFixture.TestRun is not null
? [new(OpenTelemetryFixture.TestRun.Context, new() {
["test.run_id"] = OpenTelemetryFixture.TestRunId
})]
["test.run_id"] = OpenTelemetryFixture.TestRunId
})]
: null;
Map<object?>? tags = new() { ["namespace"] = methodUnderTest.DeclaringType?.Namespace };
var name = methodUnderTest.DeclaringType?.ExpandTypeName().Suffixed(".") + methodUnderTest.Name;
_activity = OpenTelemetryFixture.ActivitySource.StartActivity(ActivityKind.Internal, name: name, links: links, tags: tags, parentContext: new ActivityContext());

var testClass = methodUnderTest.ReflectedType?.ExpandTypeName();
var testNamespace = methodUnderTest.ReflectedType?.Namespace;
var testMethod = methodUnderTest.Name;

Map<object?>? tags = new() {
["namespace"] = testNamespace,
["class"] = testClass,
["method"] = testMethod,
};

if (methodUnderTest.DeclaringType != methodUnderTest.ReflectedType) {
tags["declared-in"] = methodUnderTest.DeclaringType?.FullTypeName(testNamespace);
}

_activity = OpenTelemetryFixture.ActivitySource.StartActivity(
ActivityKind.Internal,
name: testClass.Suffixed(".") + testMethod,
links: links,
tags: tags,
parentContext: new ActivityContext());

base.Before(methodUnderTest);
}
Expand Down

0 comments on commit 53380fe

Please sign in to comment.