Skip to content

Commit

Permalink
Support MongoDB integration by nuget
Browse files Browse the repository at this point in the history
  • Loading branch information
Kielek committed Mar 28, 2023
1 parent dc6c551 commit 4cf165e
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 19 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ jobs:
run: ./build.cmd BuildNuGetPackages

- name: Test NuGet Packages
run: ./build.cmd TestNuGetPackages
run: ./build.cmd TestNuGetPackages --containers windows

- name: Upload Nuget Artifacts
uses: actions/[email protected]
Expand All @@ -179,7 +179,9 @@ jobs:
matrix:
include:
- machine: ubuntu-20.04
containers: linux
- machine: macos-11
containers: none
runs-on: ${{ matrix.machine }}
steps:
- uses: actions/[email protected]
Expand All @@ -196,4 +198,4 @@ jobs:
path: bin/nuget-artifacts/

- name: Test NuGet Packages
run: ./build.cmd TestNuGetPackages
run: ./build.cmd TestNuGetPackages --containers ${{ matrix.containers }}
21 changes: 21 additions & 0 deletions OpenTelemetry.AutoInstrumentation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,19 @@ EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NuGetPackagesTests", "test\NuGetPackagesTests\NuGetPackagesTests.csproj", "{AFD7582B-E9CD-4DF4-93B4-D5BBD7F539D0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "nuget-packages", "nuget-packages", "{2EF2F7CE-E56F-4B81-A5A5-277693529D43}"
ProjectSection(SolutionItems) = preProject
test\test-applications\nuget-packages\Directory.Build.props = test\test-applications\nuget-packages\Directory.Build.props
test\test-applications\nuget-packages\Directory.Build.targets = test\test-applications\nuget-packages\Directory.Build.targets
test\test-applications\nuget-packages\Directory.Packages.props = test\test-applications\nuget-packages\Directory.Packages.props
test\test-applications\nuget-packages\nuget.config = test\test-applications\nuget-packages\nuget.config
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestApplication.SelfContained", "test\test-applications\nuget-packages\TestApplication.SelfContained\TestApplication.SelfContained.csproj", "{25ED93D0-A70C-4A07-84D9-EF94115259C9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestApplication.EntityFrameworkCore.Pomelo.MySql", "test\test-applications\integrations\TestApplication.EntityFrameworkCore.Pomelo.MySql\TestApplication.EntityFrameworkCore.Pomelo.MySql.csproj", "{1D7E11AA-27B6-4863-B5EC-1F0ECC6979B2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestApplication.MongoDB.Nuget", "test\test-applications\nuget-packages\TestApplication.MongoDB.Nuget\TestApplication.MongoDB.Nuget.csproj", "{D1FD2D61-45AA-4A4E-82E3-4BEA55F16E2E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -849,6 +857,18 @@ Global
{1D7E11AA-27B6-4863-B5EC-1F0ECC6979B2}.Release|x64.Build.0 = Release|x64
{1D7E11AA-27B6-4863-B5EC-1F0ECC6979B2}.Release|x86.ActiveCfg = Release|x86
{1D7E11AA-27B6-4863-B5EC-1F0ECC6979B2}.Release|x86.Build.0 = Release|x86
{D1FD2D61-45AA-4A4E-82E3-4BEA55F16E2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D1FD2D61-45AA-4A4E-82E3-4BEA55F16E2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1FD2D61-45AA-4A4E-82E3-4BEA55F16E2E}.Debug|x64.ActiveCfg = Debug|Any CPU
{D1FD2D61-45AA-4A4E-82E3-4BEA55F16E2E}.Debug|x64.Build.0 = Debug|Any CPU
{D1FD2D61-45AA-4A4E-82E3-4BEA55F16E2E}.Debug|x86.ActiveCfg = Debug|Any CPU
{D1FD2D61-45AA-4A4E-82E3-4BEA55F16E2E}.Debug|x86.Build.0 = Debug|Any CPU
{D1FD2D61-45AA-4A4E-82E3-4BEA55F16E2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1FD2D61-45AA-4A4E-82E3-4BEA55F16E2E}.Release|Any CPU.Build.0 = Release|Any CPU
{D1FD2D61-45AA-4A4E-82E3-4BEA55F16E2E}.Release|x64.ActiveCfg = Release|Any CPU
{D1FD2D61-45AA-4A4E-82E3-4BEA55F16E2E}.Release|x64.Build.0 = Release|Any CPU
{D1FD2D61-45AA-4A4E-82E3-4BEA55F16E2E}.Release|x86.ActiveCfg = Release|Any CPU
{D1FD2D61-45AA-4A4E-82E3-4BEA55F16E2E}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -914,6 +934,7 @@ Global
{2EF2F7CE-E56F-4B81-A5A5-277693529D43} = {91A299AD-6C09-4B7F-BD8B-A705D9BFC672}
{25ED93D0-A70C-4A07-84D9-EF94115259C9} = {2EF2F7CE-E56F-4B81-A5A5-277693529D43}
{1D7E11AA-27B6-4863-B5EC-1F0ECC6979B2} = {E409ADD3-9574-465C-AB09-4324D205CC7C}
{D1FD2D61-45AA-4A4E-82E3-4BEA55F16E2E} = {2EF2F7CE-E56F-4B81-A5A5-277693529D43}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {160A1D00-1F5B-40F8-A155-621B4459D78F}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<dependencies>
<!-- TODO: Need a test/validation to make sure that this are in sync with actual projects -->
<group targetFramework="net6.0">
<dependency id="MongoDB.Driver.Core.Extensions.DiagnosticSources" version="1.3.0" />
<dependency id="OpenTelemetry" version="1.4.0" />
<dependency id="OpenTelemetry.Api" version="1.4.0" />
<dependency id="OpenTelemetry.Exporter.Console" version="1.4.0" />
Expand Down
11 changes: 10 additions & 1 deletion test/IntegrationTests/Helpers/EnvironmentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,21 @@ public string GetTestApplicationApplicationOutputDirectory(string packageVersion
"app.publish");
}

return Path.Combine(
var nonSelfContainedOutputDir = Path.Combine(
baseBinDirectory,
packageVersion,
EnvironmentTools.GetPlatform().ToLowerInvariant(),
EnvironmentTools.GetBuildConfiguration(),
targetFramework);

#if NUGET_PACKAGE_TESTS
// The self-contained app is going to have an extra folder before it: the one
// with a RID like "win-x64", "linux-x64", etc.

return Directory.GetDirectories(nonSelfContainedOutputDir)[0];
#else
return nonSelfContainedOutputDir;
#endif
}

public string GetTargetFramework()
Expand Down
6 changes: 3 additions & 3 deletions test/IntegrationTests/Helpers/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ public void EnableDefaultExporters()
{
testSettings ??= new();
using var process = StartTestApplication(testSettings);
Output.WriteLine($"ProcessName: " + process?.ProcessName);
Output.WriteLine($"ProcessName: {process?.ProcessName}");
using var helper = new ProcessHelper(process);

process.Should().NotBeNull();

bool processTimeout = !process!.WaitForExit((int)TestTimeout.ProcessExit.TotalMilliseconds);
var processTimeout = !process!.WaitForExit((int)TestTimeout.ProcessExit.TotalMilliseconds);
if (processTimeout)
{
process.Kill();
Expand All @@ -133,7 +133,7 @@ public void EnableDefaultExporters()
/// and returns the Process instance for further interaction.
/// </summary>
/// <returns>Test application process</returns>
public Process? StartTestApplication(TestSettings? testSettings = null)
public virtual Process? StartTestApplication(TestSettings? testSettings = null)
{
testSettings ??= new();

Expand Down
18 changes: 17 additions & 1 deletion test/IntegrationTests/MongoDBTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,41 @@
// </copyright>

using IntegrationTests.Helpers;
#if NUGET_PACKAGE_TESTS
using NuGetPackagesTests;
#endif
using Xunit.Abstractions;

namespace IntegrationTests;

[Collection(MongoDBCollection.Name)]
public class MongoDBTests : TestHelper
public class MongoDBTests
#if NUGET_PACKAGE_TESTS
: NugetTestHelper
#else
: TestHelper
#endif
{
private readonly MongoDBFixture _mongoDB;

public MongoDBTests(ITestOutputHelper output, MongoDBFixture mongoDB)
#if NUGET_PACKAGE_TESTS
: base("MongoDB.Nuget", output)
#else
: base("MongoDB", output)
#endif
{
_mongoDB = mongoDB;
}

[Theory]
[Trait("Category", "EndToEnd")]
[Trait("Containers", "Linux")]
#if NUGET_PACKAGE_TESTS
[InlineData("")]
#else
[MemberData(nameof(LibraryVersion.MongoDB), MemberType = typeof(LibraryVersion))]
#endif
public void SubmitsTraces(string packageVersion)
{
using var collector = new MockSpansCollector(Output);
Expand Down
8 changes: 8 additions & 0 deletions test/NuGetPackagesTests/NuGetPackagesTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
<DefineConstants>$(DefineConstants);_WINDOWS</DefineConstants>
</PropertyGroup>


<PropertyGroup>
<DefineConstants>$(DefineConstants);NUGET_PACKAGE_TESTS</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\IntegrationTests\Helpers\**\*.cs" />
<Compile Include="..\IntegrationTests\MongoDBTests.cs" />
<Compile Include="..\IntegrationTests\MongoDBCollection.cs" />
</ItemGroup>

<ItemGroup>
<Content Include="..\IntegrationTests\docker\mongodb.Dockerfile" CopyToOutputDirectory="PreserveNewest" Link="docker\mongodb.Dockerfile"/>
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

Expand Down
56 changes: 56 additions & 0 deletions test/NuGetPackagesTests/NugetTestHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// <copyright file="NugetTestHelper.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System.Diagnostics;
using FluentAssertions;
using IntegrationTests.Helpers;
using Xunit.Abstractions;

namespace NuGetPackagesTests;

public abstract class NugetTestHelper : TestHelper
{
protected NugetTestHelper(string testApplicationName, ITestOutputHelper output, string testApplicationType = "nuget-packages")
: base(testApplicationName, output, testApplicationType)
{
}

public override Process? StartTestApplication(TestSettings? testSettings = null)
{
testSettings ??= new();

var instrumentationScriptExtension = EnvironmentTools.IsWindows() ? ".cmd" : ".sh";
var instrumentationScriptPath = Path.Combine(EnvironmentHelper.GetTestApplicationApplicationOutputDirectory(testSettings.PackageVersion, testSettings.Framework), "instrument") + instrumentationScriptExtension;

if (!File.Exists(instrumentationScriptPath))
{
throw new Exception($"instrumentation script not found: {instrumentationScriptPath}");
}

// get path to test application that the profiler will attach to
var testApplicationPath = EnvironmentHelper.GetTestApplicationPath(testSettings.PackageVersion, testSettings.Framework);
if (!File.Exists(testApplicationPath))
{
throw new Exception($"application not found: {testApplicationPath}");
}

Output.WriteLine($"Starting Application: {instrumentationScriptPath} {testApplicationPath}");

var args = $"{(EnvironmentHelper.IsCoreClr() ? EnvironmentHelper.GetTestApplicationExecutionSource() : string.Empty)} {testApplicationPath} {testSettings.Arguments ?? string.Empty}";

return InstrumentedProcessHelper.Start(instrumentationScriptPath, args, EnvironmentHelper);
}
}
16 changes: 4 additions & 12 deletions test/NuGetPackagesTests/SelfContainedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,17 @@
using IntegrationTests.Helpers;
using Xunit.Abstractions;

namespace IntegrationTests;
namespace NuGetPackagesTests;

[Trait("Category", "EndToEnd")]
public sealed class SelfContainedTests : TestHelper
public sealed class SelfContainedTests : NugetTestHelper
{
private readonly string _selfContainedAppDir;

public SelfContainedTests(ITestOutputHelper output)
: base("SelfContained", output, "nuget-packages")
: base("SelfContained", output)
{
var nonSelfContainedOutputDir = EnvironmentHelper
.GetTestApplicationApplicationOutputDirectory();

// The self-contained app is going to have an extra folder before it: the one
// with a RID like "win-x64", "linux-x64", etc.
var childrenDirs = Directory.GetDirectories(nonSelfContainedOutputDir);
childrenDirs.Should().ContainSingle();

_selfContainedAppDir = childrenDirs[0];
_selfContainedAppDir = EnvironmentHelper.GetTestApplicationApplicationOutputDirectory();
}

[Fact]
Expand Down
4 changes: 4 additions & 0 deletions test/test-applications/nuget-packages/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@
<BaseOutputPath>..\bin\$(MSBuildProjectName)\</BaseOutputPath>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)..\integrations\dependency-libs\TestApplication.Shared\ConsoleHelper.cs" Link="Shared\ConsoleHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)..\integrations\dependency-libs\TestApplication.Shared\ProfilerHelper.cs" Link="Shared\ProfilerHelper.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<SelfContained>true</SelfContained>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\..\integrations\TestApplication.MongoDB\*.cs" />
</ItemGroup>

<PropertyGroup>
<DefineConstants>$(DefineConstants);MONGODB_2_15_OR_GREATER</DefineConstants>

<!-- Required to build multiple projects with the same Configuration|Platform -->
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry.AutoInstrumentation" Version="$(NuGetPackageVersion)" />
<PackageReference Include="MongoDB.Driver" Version="2.19.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
// limitations under the License.
// </copyright>

using TestApplication.Shared;

ConsoleHelper.WriteSplashScreen(args);
var httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(5);
var response = await httpClient.GetAsync("http://example.com");
Expand Down

0 comments on commit 4cf165e

Please sign in to comment.