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

Add OTLP exporter integration test #1671

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,14 @@ jobs:
- uses: actions/checkout@v2
- name: Run W3C Trace Context docker-compose.integration
run: docker-compose --file=test/OpenTelemetry.Instrumentation.W3cTraceContext.Tests/docker-compose.yml --file=build/docker-compose.${{ matrix.version }}.yml --project-directory=. up --exit-code-from=tests --build

otlp-exporter-test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
version: [netcoreapp2.1,netcoreapp3.1]
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
steps:
- uses: actions/checkout@v2
- name: Run OTLP Exporter docker-compose.integration
run: docker-compose --file=test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/docker-compose.yml --file=build/docker-compose.${{ matrix.version }}.yml --project-directory=. up --exit-code-from=tests --build
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net452;net46;netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net452;net46;netstandard2.0</TargetFrameworks>

<!--
There is an integration test for the OTLP exporter that runs a test targeting netcoreapp2.1, netcoreapp3.1 and net5.0.
The .NET Core 2.1 SDK cannot handle the presence of a netstandard2.1 target, so this project fails to build for the
netcoreapp2.1 test with the following error:
/usr/share/dotnet/sdk/2.1.811/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.TargetFrameworkInference.targets(150,5):
error NETSDK1045: The current .NET SDK does not support targeting .NET Standard 2.1.
Either target .NET Standard 2.0 or lower, or use a version of the .NET SDK that supports .NET Standard 2.1.
[/repo/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OpenTelemetry.Exporter.OpenTelemetryProtocol.csproj]
When the CI runs the integration test, the TARGET_FRAMEWORK variable is set. The presence of this variable is used
to determine whether to include the netstandard2.1 target.
Ideally, this would check the version of the SDK used to build the project rather than a check that is dependent on
how the integration test works. I have yet to find an available MSBuild property for checking SDK version.
-->
<TargetFrameworks Condition="$(TARGET_FRAMEWORK) == ''">$(TargetFrameworks);netstandard2.1</TargetFrameworks>
<TargetFrameworks Condition="$(TARGET_FRAMEWORK) != '' AND $(TARGET_FRAMEWORK) != 'netcoreapp2.1'">netstandard2.1</TargetFrameworks>

<Description>OpenTelemetry protocol exporter for OpenTelemetry .NET</Description>
<PackageTags>$(PackageTags);OTLP</PackageTags>
</PropertyGroup>
Expand Down
17 changes: 17 additions & 0 deletions test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Create a container for running the OpenTelemetry Collector integration tests.
# This should be run from the root of the repo:
# docker build --file test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/Dockerfile

ARG SDK_VERSION=3.1
FROM mcr.microsoft.com/dotnet/core/sdk:${SDK_VERSION} AS build
ARG PUBLISH_CONFIGURATION=Release
ARG PUBLISH_FRAMEWORK=netcoreapp3.1
WORKDIR /repo
COPY . ./
WORKDIR "/repo/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests"
RUN dotnet publish "OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests.csproj" -c "${PUBLISH_CONFIGURATION}" -f "${PUBLISH_FRAMEWORK}" -o /drop -p:IntegrationBuild=true -p:TARGET_FRAMEWORK=${PUBLISH_FRAMEWORK}

FROM mcr.microsoft.com/dotnet/core/sdk:${SDK_VERSION} AS final
WORKDIR /test
COPY --from=build /drop .
ENTRYPOINT ["dotnet", "vstest", "OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests.dll"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// <copyright file="IntegrationTests.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;
using System.Diagnostics;
using OpenTelemetry.Tests;
using OpenTelemetry.Trace;
using Xunit;

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests
{
public class IntegrationTests
{
private const string CollectorEndpointEnvVarName = "OTEL_EXPORTER_OTLP_ENDPOINT";
private static readonly string CollectorEndpoint = SkipUnlessEnvVarFoundFactAttribute.GetEnvironmentVariable(CollectorEndpointEnvVarName);

[Trait("CategoryName", "CollectorIntegrationTests")]
[SkipUnlessEnvVarFoundFact(CollectorEndpointEnvVarName)]
public void ExportResultIsSuccess()
{
#if NETCOREAPP3_1
// Adding the OtlpExporter creates a GrpcChannel.
// This switch must be set before creating a GrpcChannel/HttpClient when calling an insecure gRPC service.
// See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
#endif

var exporterOptions = new OtlpExporterOptions
{
#if NETCOREAPP3_1 || NET5_0
Endpoint = new System.Uri($"http://{CollectorEndpoint}"),
#else
Endpoint = CollectorEndpoint,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since #1662 is merged, we need to conditionally use Uri/String here.

#endif
};

var otlpExporter = new OtlpExporter(exporterOptions);
var delegatingExporter = new DelegatingTestExporter<Activity>(otlpExporter);
var exportActivityProcessor = new SimpleActivityExportProcessor(delegatingExporter);

var activitySourceName = "otlp.collector.test";

var builder = Sdk.CreateTracerProviderBuilder()
.AddSource(activitySourceName)
.AddProcessor(exportActivityProcessor);

using var tracerProvider = builder.Build();

var source = new ActivitySource(activitySourceName);
var activity = source.StartActivity("Test Activity");
activity?.Stop();

Assert.Single(delegatingExporter.ExportResults);
Assert.Equal(ExportResult.Success, delegatingExporter.ExportResults[0]);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1;net5.0</TargetFrameworks>
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);net452;net46</TargetFrameworks>
<TargetFrameworks Condition="$(TARGET_FRAMEWORK) == ''">netcoreapp2.1;netcoreapp3.1;net5.0</TargetFrameworks>
<TargetFrameworks Condition="$(TARGET_FRAMEWORK) == '' AND $(OS) == 'Windows_NT'">$(TargetFrameworks);net452;net46</TargetFrameworks>
<TargetFrameworks Condition="$(TARGET_FRAMEWORK) != ''">$(TARGET_FRAMEWORK)</TargetFrameworks>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This third line seems kind of redundant 🤷

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not. This is basically allowing $(TARGET_FRAMEWORK) to override what is in csproj? So you can pass it on the command-line?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, TARGET_FRAMEWORK is a property that is set when building the project for running the integration tests

RUN dotnet publish "OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests.csproj" -c "${PUBLISH_CONFIGURATION}" -f "${PUBLISH_FRAMEWORK}" -o /drop -p:IntegrationBuild=true -p:TARGET_FRAMEWORK=${PUBLISH_FRAMEWORK}

<IsPackable>false</IsPackable>
</PropertyGroup>

Expand All @@ -22,7 +23,9 @@

<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\DateTimeOffsetExtensions.net452.cs" Link="Includes\DateTimeOffsetExtensions.net452.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\DelegatingTestExporter.cs" Link="Includes\DelegatingTestExporter.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\EventSourceTestHelper.cs" Link="Includes\EventSourceTestHelper.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\SkipUnlessEnvVarFoundFactAttribute.cs" Link="Includes\SkipUnlessEnvVarFoundFactAttribute.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\TestActivityProcessor.cs" Link="Includes\TestActivityProcessor.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\TestEventListener.cs" Link="Includes\TestEventListener.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\TestExporter.cs" Link="Includes\TestExporter.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This is a configuration file for the OpenTelemetry Collector intended to be
# used in conjunction with the OTLP Exporter integration tests.
#
# For more information about the OpenTelemetry Collector see:
# https://github.com/open-telemetry/opentelemetry-collector
#
receivers:
otlp:
protocols:
grpc:

exporters:
logging:
loglevel: debug

service:
pipelines:
traces:
receivers: [otlp]
exporters: [logging]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Starts an OpenTelemetry Collector and then runs the OTLP exporter integration tests.
# This should be run from the root of the repo:
# docker-compose --file=test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/docker-compose.yml --project-directory=. up --exit-code-from=tests --build

version: '3.7'

services:
otel-collector:
image: otel/opentelemetry-collector
volumes:
- ./test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests:/cfg
command: --config=/cfg/config.yaml
ports:
- "55680:55680"

tests:
build:
context: .
dockerfile: ./test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/Dockerfile
command: --TestCaseFilter:CategoryName=CollectorIntegrationTests
environment:
- OTEL_EXPORTER_OTLP_ENDPOINT=otel-collector:55680
depends_on:
- otel-collector
40 changes: 40 additions & 0 deletions test/OpenTelemetry.Tests/Shared/DelegatingTestExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// <copyright file="DelegatingTestExporter.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.Collections.Generic;

namespace OpenTelemetry.Tests
{
public class DelegatingTestExporter<T> : BaseExporter<T>
where T : class
{
public List<ExportResult> ExportResults = new List<ExportResult>();

private readonly BaseExporter<T> exporter;

public DelegatingTestExporter(BaseExporter<T> exporter)
{
this.exporter = exporter;
}

public override ExportResult Export(in Batch<T> batch)
{
var result = this.exporter.Export(batch);
this.ExportResults.Add(result);
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// <copyright file="SkipUnlessEnvVarFoundFactAttribute.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;
using Xunit;

namespace OpenTelemetry.Tests
{
internal class SkipUnlessEnvVarFoundFactAttribute : FactAttribute
{
public SkipUnlessEnvVarFoundFactAttribute(string environmentVariable)
{
if (string.IsNullOrEmpty(GetEnvironmentVariable(environmentVariable)))
{
this.Skip = $"Skipped because {environmentVariable} environment variable was not configured.";
}
}

public static string GetEnvironmentVariable(string environmentVariableName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to say you could call SkipUnlessEnvVarFoundTheoryAttribute.GetEnvironmentVariable to achieve some code reuse but then you would have to also include that file in the project which kind of sucks. Probably the duplication is better. So I guess what I'm saying is, nice job 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I had some similar thoughts then opted for duplication. We could play around with organizing things like this into a "xUnit extensions" namespace and then you could do something like

<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\xunit\**" />

{
string environmentVariableValue = Environment.GetEnvironmentVariable(environmentVariableName, EnvironmentVariableTarget.Process);

if (string.IsNullOrEmpty(environmentVariableValue))
{
environmentVariableValue = Environment.GetEnvironmentVariable(environmentVariableName, EnvironmentVariableTarget.Machine);
}

return environmentVariableValue;
}
}
}