From 5c600360b5067e1f93e20ad8d5f024c5c0723a59 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Tue, 16 Jul 2024 10:25:14 +0200 Subject: [PATCH] Ship auto instrumentation plugin as part of Elastic.OpenTelemetry directly (#122) * Ship auto instrumentation plugin as part of Elastic.OpenTelemetry directly * rename Plugin to AutoInstrumentationPlugin --- Elastic.OpenTelemetry.sln | 7 --- .../Example.AutoInstrumentation/Dockerfile | 53 ++++++++----------- .../Example.AutoInstrumentation/README.md | 20 +++++++ ...Telemetry.AutoInstrumentationPlugin.csproj | 17 ------ .../AutoInstrumentationPlugin.cs} | 8 +-- .../Elastic.OpenTelemetry.csproj | 1 + .../PluginLoaderTests.cs | 2 +- 7 files changed, 48 insertions(+), 60 deletions(-) create mode 100644 examples/Example.AutoInstrumentation/README.md delete mode 100644 src/Elastic.OpenTelemetry.AutoInstrumentationPlugin/Elastic.OpenTelemetry.AutoInstrumentationPlugin.csproj rename src/{Elastic.OpenTelemetry.AutoInstrumentationPlugin/ElasticAutoInstrumentationPlugin.cs => Elastic.OpenTelemetry/AutoInstrumentationPlugin.cs} (92%) diff --git a/Elastic.OpenTelemetry.sln b/Elastic.OpenTelemetry.sln index 159ffe9..688c3f2 100644 --- a/Elastic.OpenTelemetry.sln +++ b/Elastic.OpenTelemetry.sln @@ -41,8 +41,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AppHost", "examples\AppHost EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceDefaults", "examples\ServiceDefaults\ServiceDefaults.csproj", "{A3D1ED4D-863B-45D7-9829-305DD33B4CE5}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.OpenTelemetry.AutoInstrumentationPlugin", "src\Elastic.OpenTelemetry.AutoInstrumentationPlugin\Elastic.OpenTelemetry.AutoInstrumentationPlugin.csproj", "{B61D749B-21E5-430D-B50D-CA02EBAA7F2F}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.AutoInstrumentation", "examples\Example.AutoInstrumentation\Example.AutoInstrumentation.csproj", "{F3AA76EC-C7D8-42DA-947D-4376B6562772}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoInstrumentation.IntegrationTests", "tests\AutoInstrumentation.IntegrationTests\AutoInstrumentation.IntegrationTests.csproj", "{782E4DC1-8186-4BAC-B2F4-89E6DF22A4DD}" @@ -101,10 +99,6 @@ Global {A3D1ED4D-863B-45D7-9829-305DD33B4CE5}.Debug|Any CPU.Build.0 = Debug|Any CPU {A3D1ED4D-863B-45D7-9829-305DD33B4CE5}.Release|Any CPU.ActiveCfg = Release|Any CPU {A3D1ED4D-863B-45D7-9829-305DD33B4CE5}.Release|Any CPU.Build.0 = Release|Any CPU - {B61D749B-21E5-430D-B50D-CA02EBAA7F2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B61D749B-21E5-430D-B50D-CA02EBAA7F2F}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B61D749B-21E5-430D-B50D-CA02EBAA7F2F}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B61D749B-21E5-430D-B50D-CA02EBAA7F2F}.Release|Any CPU.Build.0 = Release|Any CPU {F3AA76EC-C7D8-42DA-947D-4376B6562772}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {F3AA76EC-C7D8-42DA-947D-4376B6562772}.Debug|Any CPU.Build.0 = Debug|Any CPU {F3AA76EC-C7D8-42DA-947D-4376B6562772}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -127,7 +121,6 @@ Global {863CAB86-5EB0-4E9F-B01D-F51687EC6597} = {4E95C87B-655B-4BC3-8F2A-DF06B7AAB7E9} {206203BD-3EBA-4E9A-8881-1189D95AB037} = {4E95C87B-655B-4BC3-8F2A-DF06B7AAB7E9} {A3D1ED4D-863B-45D7-9829-305DD33B4CE5} = {4E95C87B-655B-4BC3-8F2A-DF06B7AAB7E9} - {B61D749B-21E5-430D-B50D-CA02EBAA7F2F} = {E622CFF2-C6C4-40FB-BE42-7C4F2B38B75A} {F3AA76EC-C7D8-42DA-947D-4376B6562772} = {4E95C87B-655B-4BC3-8F2A-DF06B7AAB7E9} {782E4DC1-8186-4BAC-B2F4-89E6DF22A4DD} = {AAD39891-0B70-47FA-A212-43E1AAE5DF56} EndGlobalSection diff --git a/examples/Example.AutoInstrumentation/Dockerfile b/examples/Example.AutoInstrumentation/Dockerfile index 01665fc..357365d 100644 --- a/examples/Example.AutoInstrumentation/Dockerfile +++ b/examples/Example.AutoInstrumentation/Dockerfile @@ -20,28 +20,28 @@ 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 +ENV _PROJECT="Example.AutoInstrumentation" +ENV _PROJECTPATH="${_PROJECT}/${_PROJECT}.csproj" +WORKDIR /work +COPY ["examples/${_PROJECTPATH}", "examples/${_PROJECT}/"] +RUN dotnet restore "examples/${_PROJECT}" +COPY .git .git +COPY examples/${_PROJECT} examples/${_PROJECT} +WORKDIR "/work/examples/${_PROJECT}" +RUN dotnet build "${_PROJECT}.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" +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build_distro +ENV _PROJECT="Elastic.OpenTelemetry" +ENV _PROJECTPATH="${_PROJECT}/${_PROJECT}.csproj" +WORKDIR /work +COPY ["src/${_PROJECTPATH}", "src/${_PROJECT}/"] +RUN dotnet restore "src/${_PROJECTPATH}" 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 +WORKDIR "/work/src/${_PROJECT}" +RUN dotnet build "${_PROJECT}.csproj" -c Release FROM otel AS final ARG TARGETPLATFORM @@ -49,24 +49,13 @@ 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/ +COPY --from=build_distro /work/.artifacts/bin/Elastic.OpenTelemetry/release_netstandard2.1/Elastic.OpenTelemetry.dll /app/otel/net/ +COPY --from=build_distro /work/.artifacts/bin/Elastic.OpenTelemetry/release_netstandard2.1/Elastic.OpenTelemetry.pdb /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_DOTNET_AUTO_PLUGINS="Elastic.OpenTelemetry.AutoInstrumentationPlugin, Elastic.OpenTelemetry, Version=1.0.0.0, Culture=neutral, PublicKeyToken=069ca2728db333c1" ENV OTEL_TRACES_EXPORTER=none ENV OTEL_METRICS_EXPORTER=none @@ -83,4 +72,6 @@ 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" +ENV OTEL_DOTNET_AUTO_FAIL_FAST_ENABLED=true + ENTRYPOINT ["dotnet", "/app/example/Example.AutoInstrumentation.dll"] diff --git a/examples/Example.AutoInstrumentation/README.md b/examples/Example.AutoInstrumentation/README.md new file mode 100644 index 0000000..15468bf --- /dev/null +++ b/examples/Example.AutoInstrumentation/README.md @@ -0,0 +1,20 @@ +# Example auto instrumentation plugin for OpenTelemetry .NET + +This is a very minimal .NET application that we use to validate our OpenTelemetry plugin loads correctly + +This happens automated through our testing setup: + +```bash +$ ./build.sh test --test-suite=integration +``` + +Which ends up running the tests in `/tests/AutoInstrumentation.IntegrationTests` + + +To quickly see the `DockerFile` in action run the following from the root of this repository. + + +```bash +$ docker build -t example.autoinstrumentation:latest -f examples/Example.AutoInstrumentation/Dockerfile --no-cache . && \ + docker run -it --rm -p 5000:8080 --name autoin example.autoinstrumentation:latest +``` \ No newline at end of file diff --git a/src/Elastic.OpenTelemetry.AutoInstrumentationPlugin/Elastic.OpenTelemetry.AutoInstrumentationPlugin.csproj b/src/Elastic.OpenTelemetry.AutoInstrumentationPlugin/Elastic.OpenTelemetry.AutoInstrumentationPlugin.csproj deleted file mode 100644 index 7c6a754..0000000 --- a/src/Elastic.OpenTelemetry.AutoInstrumentationPlugin/Elastic.OpenTelemetry.AutoInstrumentationPlugin.csproj +++ /dev/null @@ -1,17 +0,0 @@ - - - - net8.0;net462 - enable - enable - False - - - - - - - - - - diff --git a/src/Elastic.OpenTelemetry.AutoInstrumentationPlugin/ElasticAutoInstrumentationPlugin.cs b/src/Elastic.OpenTelemetry/AutoInstrumentationPlugin.cs similarity index 92% rename from src/Elastic.OpenTelemetry.AutoInstrumentationPlugin/ElasticAutoInstrumentationPlugin.cs rename to src/Elastic.OpenTelemetry/AutoInstrumentationPlugin.cs index d5f78d4..9343d7c 100644 --- a/src/Elastic.OpenTelemetry.AutoInstrumentationPlugin/ElasticAutoInstrumentationPlugin.cs +++ b/src/Elastic.OpenTelemetry/AutoInstrumentationPlugin.cs @@ -11,20 +11,20 @@ using OpenTelemetry.Resources; using OpenTelemetry.Trace; -namespace Elastic.OpenTelemetry.AutoInstrumentationPlugin; +namespace Elastic.OpenTelemetry; /// /// Elastic Distribution for OpenTelemetry .NET plugin for Auto Instrumentation. /// Ensures all signals are rich enough to report to Elastic /// // ReSharper disable once UnusedType.Global -public class ElasticAutoInstrumentationPlugin +public class AutoInstrumentationPlugin { private readonly ILogger _logger; private readonly EventListener _eventListener; - /// - public ElasticAutoInstrumentationPlugin() + /// + public AutoInstrumentationPlugin() { var options = new ElasticOpenTelemetryBuilderOptions(); var (eventListener, logger) = ElasticOpenTelemetryBuilder.Bootstrap(options); diff --git a/src/Elastic.OpenTelemetry/Elastic.OpenTelemetry.csproj b/src/Elastic.OpenTelemetry/Elastic.OpenTelemetry.csproj index 3c67815..3bc3371 100644 --- a/src/Elastic.OpenTelemetry/Elastic.OpenTelemetry.csproj +++ b/src/Elastic.OpenTelemetry/Elastic.OpenTelemetry.csproj @@ -11,6 +11,7 @@ True false $(NoWarn);OTEL1000 + latest diff --git a/tests/AutoInstrumentation.IntegrationTests/PluginLoaderTests.cs b/tests/AutoInstrumentation.IntegrationTests/PluginLoaderTests.cs index 25bffc0..4a91c95 100644 --- a/tests/AutoInstrumentation.IntegrationTests/PluginLoaderTests.cs +++ b/tests/AutoInstrumentation.IntegrationTests/PluginLoaderTests.cs @@ -19,7 +19,7 @@ public async Task ObserveDistributionPluginLoad() var output = exampleApplicationContainer.FailureTestOutput(); output.Should() .NotBeNullOrWhiteSpace() - .And.Contain("Elastic OpenTelemetry Distribution:") + .And.Contain("Elastic Distribution for OpenTelemetry .NET:") .And.Contain("ElasticOpenTelemetryBuilder initialized") .And.Contain("Added 'Elastic.OpenTelemetry.Processors.ElasticCompatibilityProcessor'");