-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rudimentary auto instrumentation integration tests (#120)
* Add example instrumentation project and dockerfile * Very rudimentary plugin tests * rename to .IntegrationTests * dotnet format * add integrate target * fix build usage missing pattern match * fix license header * move to TARGETARCH * put debug print of archs at the top * remove copy of netfx dll * Release => release * hack to enforce copy * more debugging why we can copy files from build_plugin on GithubActions * quote files * attempt #5 to get a copy from build_plugin to work on Github Action * attempt #6 to get a copy from build_plugin to work on Github Action * attempt #7 to get a copy from build_plugin to work on Github Action * attempt 8 to get a copy from build_plugin to work on Github Action * attempt 9 to get a copy from build_plugin to work on Github Action * attempt 10 to get a copy from build_plugin to work on Github Action * manually add TARGETARCH to TestContainer build * ensure arg is available in last step in build * debug message * remove debug message * remove debug message * skip on windows and dockerfile cleanup * share OTEL_VERSION properly over stages
- Loading branch information
Showing
16 changed files
with
337 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
**/.dockerignore | ||
**/.env | ||
**/.gitignore | ||
**/.project | ||
**/.settings | ||
**/.toolstarget | ||
**/.vs | ||
**/.vscode | ||
**/.idea | ||
**/*.*proj.user | ||
**/*.dbmdl | ||
**/*.jfm | ||
**/azds.yaml | ||
**/bin | ||
**/charts | ||
**/docker-compose* | ||
**/Dockerfile* | ||
**/node_modules | ||
**/npm-debug.log | ||
**/obj | ||
**/secrets.dev.yaml | ||
**/values.dev.yaml |
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
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,86 @@ | ||
ARG OTEL_VERSION=1.7.0 | ||
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base | ||
ARG TARGETPLATFORM | ||
ARG TARGETARCH | ||
ARG TARGETVARIANT | ||
RUN apt-get update && apt-get install -y unzip curl strace | ||
# Would love to run as non root but TestContainers does not utilize buildkit like `docker build` does OOTB | ||
#USER $APP_UID | ||
WORKDIR /app | ||
RUN chown app:app . | ||
|
||
FROM base AS otel | ||
ARG OTEL_VERSION | ||
# install OpenTelemetry .NET Automatic Instrumentation | ||
# the following commented line does not work from TestContainers because it does not utilize buildkit which `docker build` does OOTB | ||
#ADD --chown=$APP_UID --chmod=777 https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/download/v${OTEL_VERSION}/otel-dotnet-auto-install.sh otel-dotnet-auto-install.sh | ||
ADD https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/download/v${OTEL_VERSION}/otel-dotnet-auto-install.sh otel-dotnet-auto-install.sh | ||
RUN chmod +x otel-dotnet-auto-install.sh | ||
RUN OTEL_DOTNET_AUTO_HOME="/app/otel" sh otel-dotnet-auto-install.sh | ||
|
||
|
||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build_example | ||
WORKDIR /src | ||
COPY ["examples/Example.AutoInstrumentation/Example.AutoInstrumentation.csproj", "examples/Example.AutoInstrumentation/"] | ||
RUN dotnet restore "examples/Example.AutoInstrumentation/Example.AutoInstrumentation.csproj" | ||
COPY . . | ||
WORKDIR "/src/examples/Example.AutoInstrumentation" | ||
RUN dotnet build "Example.AutoInstrumentation.csproj" -c Release -o /app/build_example | ||
|
||
FROM build_example AS publish_example | ||
RUN dotnet publish "Example.AutoInstrumentation.csproj" -c Release -o /app/example /p:UseAppHost=false | ||
|
||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build_plugin | ||
WORKDIR /src | ||
COPY ["README.md", "."] | ||
COPY ["LICENSE.txt", "."] | ||
COPY ["NOTICE.txt", "."] | ||
COPY ["src/Elastic.OpenTelemetry.AutoInstrumentationPlugin/Elastic.OpenTelemetry.AutoInstrumentationPlugin.csproj", "src/Elastic.OpenTelemetry.AutoInstrumentationPlugin/"] | ||
RUN dotnet restore "src/Elastic.OpenTelemetry.AutoInstrumentationPlugin/Elastic.OpenTelemetry.AutoInstrumentationPlugin.csproj" | ||
COPY . . | ||
WORKDIR "/src/src/Elastic.OpenTelemetry.AutoInstrumentationPlugin" | ||
RUN dotnet build "Elastic.OpenTelemetry.AutoInstrumentationPlugin.csproj" -c release | ||
RUN mkdir -p /app/temp | ||
RUN cp -r /src/.artifacts/bin /app/temp | ||
|
||
FROM otel AS final | ||
ARG TARGETPLATFORM | ||
ARG TARGETARCH | ||
ARG TARGETVARIANT | ||
WORKDIR /app | ||
COPY --from=publish_example /app/example /app/example | ||
COPY --from=otel /app/otel /app/otel | ||
|
||
# This `RUN true` is a bit of magic that I don't care to understand | ||
# https://github.com/moby/moby/issues/37965 | ||
# Seems to relate to the fs driver on GitHub Actions failing to copy files from the `build_plugin` layer. | ||
# Hence we copy the whole .artifacts/bin folder manually and copy the files to /app/otel/net | ||
RUN true | ||
COPY --from=build_plugin /app/temp /app/temp | ||
RUN true | ||
RUN ls -al /app/temp/bin | ||
RUN mkdir -p /app/otel/net | ||
RUN cp "/app/temp/bin/Elastic.OpenTelemetry.AutoInstrumentationPlugin/release_net8.0/Elastic.OpenTelemetry.AutoInstrumentationPlugin.dll" /app/otel/net/ | ||
RUN cp "/app/temp/bin/Elastic.OpenTelemetry/release_net8.0/Elastic.OpenTelemetry.dll" /app/otel/net/ | ||
|
||
ENV CORECLR_ENABLE_PROFILING="1" | ||
ENV CORECLR_PROFILER="{918728DD-259F-4A6A-AC2B-B85E1B658318}" | ||
ENV CORECLR_PROFILER_PATH="/app/otel/linux-${TARGETARCH}/OpenTelemetry.AutoInstrumentation.Native.so" | ||
ENV OTEL_DOTNET_AUTO_PLUGINS="Elastic.OpenTelemetry.AutoInstrumentationPlugin.ElasticAutoInstrumentationPlugin, Elastic.OpenTelemetry.AutoInstrumentationPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=069ca2728db333c1" | ||
|
||
ENV OTEL_TRACES_EXPORTER=none | ||
ENV OTEL_METRICS_EXPORTER=none | ||
ENV OTEL_LOGS_EXPORTER=none | ||
ENV OTEL_SERVICE_NAME=ExampleInstrumentation | ||
|
||
ENV OTEL_LOG_LEVEL=info | ||
ENV ELASTIC_OTEL_LOG_LEVEL=trace | ||
ENV ELASTIC_OTEL_LOG_TARGETS=stdout | ||
ENV OTEL_DOTNET_AUTO_LOG_DIRECTORY=/app/logs | ||
|
||
ENV OTEL_DOTNET_AUTO_HOME="/app/otel" | ||
ENV DOTNET_ADDITIONAL_DEPS="/app/otel/AdditionalDeps" | ||
ENV DOTNET_SHARED_STORE="/app/otel/store" | ||
ENV DOTNET_STARTUP_HOOKS="/app/otel/net/OpenTelemetry.AutoInstrumentation.StartupHook.dll" | ||
|
||
ENTRYPOINT ["dotnet", "/app/example/Example.AutoInstrumentation.dll"] |
17 changes: 17 additions & 0 deletions
17
examples/Example.AutoInstrumentation/Example.AutoInstrumentation.csproj
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,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="..\..\.dockerignore"> | ||
<Link>.dockerignore</Link> | ||
</Content> | ||
</ItemGroup> | ||
|
||
</Project> |
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,14 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
Console.WriteLine("Hello, World!"); | ||
|
||
var httpClient = new HttpClient(); | ||
for (var i = 0; i < 10; i++) | ||
{ | ||
var response = await httpClient.GetAsync(new Uri("https://google.com")); | ||
Console.Write($"\rSent {i + 1} requests, last response: {response.StatusCode}."); | ||
await Task.Delay(TimeSpan.FromMilliseconds(100)); | ||
} | ||
Console.WriteLine(); |
3 changes: 1 addition & 2 deletions
3
...elemetry.AutoInstrumentationPlugin/Elastic.OpenTelemetry.AutoInstrumentationPlugin.csproj
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
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
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
32 changes: 32 additions & 0 deletions
32
src/Elastic.OpenTelemetry/Diagnostics/Logging/StandardOutLogger.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,32 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Elastic.OpenTelemetry.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Elastic.OpenTelemetry.Diagnostics.Logging; | ||
|
||
internal sealed class StandardOutLogger(ElasticOpenTelemetryOptions options) : ILogger | ||
{ | ||
private readonly LogLevel _configuredLogLevel = options.LogLevel; | ||
|
||
private readonly LoggerExternalScopeProvider _scopeProvider = new(); | ||
|
||
private bool StandardOutLoggingEnabled { get; } = options.GlobalLogEnabled && options.LogTargets.HasFlag(LogTargets.StdOut); | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) | ||
{ | ||
// We skip logging for any log level higher (numerically) than the configured log level | ||
if (!IsEnabled(logLevel)) | ||
return; | ||
|
||
var logLine = LogFormatter.Format(logLevel, eventId, state, exception, formatter); | ||
Console.WriteLine(logLine); | ||
|
||
} | ||
|
||
public bool IsEnabled(LogLevel logLevel) => StandardOutLoggingEnabled && _configuredLogLevel <= logLevel; | ||
|
||
public IDisposable BeginScope<TState>(TState state) where TState : notnull => _scopeProvider.Push(state); | ||
} |
16 changes: 16 additions & 0 deletions
16
tests/AutoInstrumentation.IntegrationTests/AutoInstrumentation.IntegrationTests.csproj
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,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<RootNamespace>Elastic.OpenTelemetry.AutoInstrumentation.IntegrationTests</RootNamespace> | ||
<NoWarn>xUnit1041</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Nullean.Xunit.Partitions" Version="0.5.0" /> | ||
<PackageReference Include="Testcontainers" Version="3.9.0"/> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.