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

Initial version of an Auto Instrumentation plugin #118

Merged
merged 4 commits into from
Jul 3, 2024
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
7 changes: 7 additions & 0 deletions Elastic.OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ 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}"
stevejgordon marked this conversation as resolved.
Show resolved Hide resolved
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -95,6 +97,10 @@ 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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -109,6 +115,7 @@ 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}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {573B2B5F-8CBB-4D52-A55A-4E65E282AAFB}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions examples/Example.Console/Example.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.0.0-rc9.14" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.0.0-rc9.15" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion examples/Example.MinimalApi/Example.MinimalApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.9.0" />
</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 7 additions & 7 deletions examples/ServiceDefaults/ServiceDefaults.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.2.0" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="8.0.0-preview.4.24156.9" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.8.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Process" Version="0.5.0-beta.5" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.8.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.9.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Process" Version="0.5.0-beta.6" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net8.0;net6.0;net462</TargetFrameworks>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>False</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Elastic.OpenTelemetry\Elastic.OpenTelemetry.csproj" />
</ItemGroup>

<ItemGroup>
<Content Remove="README.md" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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 System.Diagnostics.Tracing;
using Elastic.OpenTelemetry.Configuration;
using Elastic.OpenTelemetry.Extensions;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

namespace Elastic.OpenTelemetry.AutoInstrumentationPlugin;

/// <summary>
/// Elastic Distribution for OpenTelemetry .NET plugin for Auto Instrumentation.
/// <para>Ensures all signals are rich enough to report to Elastic</para>
/// </summary>
// ReSharper disable once UnusedType.Global
public class ElasticAutoInstrumentationPlugin
{
private readonly ILogger _logger;
private readonly EventListener _eventListener;

/// <inheritdoc cref="ElasticAutoInstrumentationPlugin"/>
public ElasticAutoInstrumentationPlugin()
{
var options = new ElasticOpenTelemetryBuilderOptions();
var (eventListener, logger) = ElasticOpenTelemetryBuilder.Bootstrap(options);
stevejgordon marked this conversation as resolved.
Show resolved Hide resolved

_logger = logger;
_eventListener = eventListener;
}

/// To access TracerProvider right after TracerProviderBuilder.Build() is executed.
public void TracerProviderInitialized(TracerProvider tracerProvider)
{
}

/// To access MeterProvider right after MeterProviderBuilder.Build() is executed.
public void MeterProviderInitialized(MeterProvider meterProvider)
{
}

/// To configure tracing SDK before Auto Instrumentation configured SDK
public TracerProviderBuilder BeforeConfigureTracerProvider(TracerProviderBuilder builder) =>
builder.UseElasticDefaults(_logger);


/// To configure tracing SDK after Auto Instrumentation configured SDK
public TracerProviderBuilder AfterConfigureTracerProvider(TracerProviderBuilder builder) =>
builder;

/// To configure metrics SDK before Auto Instrumentation configured SDK
public MeterProviderBuilder BeforeConfigureMeterProvider(MeterProviderBuilder builder) =>
builder.UseElasticDefaults(_logger);

/// To configure metrics SDK after Auto Instrumentation configured SDK
public MeterProviderBuilder AfterConfigureMeterProvider(MeterProviderBuilder builder) =>
builder;


/// To configure logs SDK (the method name is the same as for other logs options)
public void ConfigureLogsOptions(OpenTelemetryLoggerOptions options) =>
options.UseElasticDefaults(_logger);

/// To configure Resource
public ResourceBuilder ConfigureResource(ResourceBuilder builder) =>
builder.UseElasticDefaults(_logger);
}
16 changes: 8 additions & 8 deletions src/Elastic.OpenTelemetry/Elastic.OpenTelemetry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.8.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta.11" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.8.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Process" Version="0.5.0-beta.5" />
<PackageReference Include="OpenTelemetry" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.9.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta.12" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Process" Version="0.5.0-beta.6" />
<PackageReference Include="Polyfill" Version="4.4.0" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
</ItemGroup>

Expand Down
70 changes: 29 additions & 41 deletions src/Elastic.OpenTelemetry/ElasticOpenTelemetryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information

using System.Diagnostics;
using System.Diagnostics.Tracing;
using Elastic.OpenTelemetry.Configuration;
using Elastic.OpenTelemetry.Diagnostics;
using Elastic.OpenTelemetry.Diagnostics.Logging;
Expand Down Expand Up @@ -48,6 +49,23 @@ public class ElasticOpenTelemetryBuilder : IOpenTelemetryBuilder
/// <inheritdoc cref="IOpenTelemetryBuilder.Services"/>
public IServiceCollection Services { get; }

/// <summary>
/// Shared bootstrap routine for the Elastic OpenTelemetry Distribution.
/// Used to ensure auto instrumentation and manual instrumentation bootstrap the same way.
/// </summary>
public static (EventListener, ILogger) Bootstrap(ElasticOpenTelemetryBuilderOptions options)
stevejgordon marked this conversation as resolved.
Show resolved Hide resolved
{
var logger = new CompositeLogger(options);

// Enables logging of OpenTelemetry-SDK event source events
var eventListener = new LoggingEventListener(logger, options.DistroOptions);

logger.LogAgentPreamble();
logger.LogElasticOpenTelemetryBuilderInitialized(Environment.NewLine, new StackTrace(true));
options.DistroOptions.LogConfigSources(logger);
return (eventListener, logger);
}

/// <summary>
/// Creates an instance of the <see cref="ElasticOpenTelemetryBuilder" /> configured with default options.
/// </summary>
Expand All @@ -61,14 +79,10 @@ public ElasticOpenTelemetryBuilder()
/// </summary>
public ElasticOpenTelemetryBuilder(ElasticOpenTelemetryBuilderOptions options)
{
Logger = new CompositeLogger(options);
var (eventListener, logger) = Bootstrap(options);

// Enables logging of OpenTelemetry-SDK event source events
EventListener = new LoggingEventListener(Logger, options.DistroOptions);

Logger.LogAgentPreamble();
Logger.LogElasticOpenTelemetryBuilderInitialized(Environment.NewLine, new StackTrace(true));
options.DistroOptions.LogConfigSources(Logger);
Logger = (CompositeLogger)logger;
EventListener = (LoggingEventListener)eventListener;

Services = options.Services ?? new ServiceCollection();

Expand All @@ -81,6 +95,9 @@ public ElasticOpenTelemetryBuilder(ElasticOpenTelemetryBuilderOptions options)
var openTelemetry =
Microsoft.Extensions.DependencyInjection.OpenTelemetryServicesExtensions.AddOpenTelemetry(Services);

// We always add this so we can identify a distro is being used, even if all Elastic defaults are disabled.
openTelemetry.ConfigureResource(r => r.UseElasticDefaults());

if (options.DistroOptions.EnabledDefaults.Equals(ElasticOpenTelemetryOptions.EnabledElasticDefaults.None))
{
Logger.LogNoElasticDefaults();
Expand All @@ -90,52 +107,23 @@ public ElasticOpenTelemetryBuilder(ElasticOpenTelemetryBuilderOptions options)
return;
}

openTelemetry.ConfigureResource(r => r.AddElasticResourceDefaults(Logger));
openTelemetry.ConfigureResource(r => r.UseElasticDefaults(Logger));

//https://github.com/open-telemetry/opentelemetry-dotnet/pull/5400
if (!options.DistroOptions.SkipOtlpExporter)
openTelemetry.UseOtlpExporter();

if (options.DistroOptions.EnabledDefaults.HasFlag(ElasticOpenTelemetryOptions.EnabledElasticDefaults.Logging))
{
// TODO: Move to WithLogging once it gets stable.
Services.Configure<OpenTelemetryLoggerOptions>(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});

// Note: We use this log method for now as the WithLogging method is not yet stable.
Logger.LogConfiguredSignalProvider("logging", nameof(OpenTelemetryLoggerOptions));
//TODO Move to WithLogging once it gets stable
Services.Configure<OpenTelemetryLoggerOptions>(logging => logging.UseElasticDefaults());
}

if (options.DistroOptions.EnabledDefaults.HasFlag(ElasticOpenTelemetryOptions.EnabledElasticDefaults.Tracing))
{
openTelemetry.WithTracing(tracing =>
{
tracing
.AddHttpClientInstrumentation()
.AddGrpcClientInstrumentation()
.AddEntityFrameworkCoreInstrumentation();

tracing.AddElasticProcessors(Logger);
});

Logger.LogConfiguredSignalProvider("tracing", nameof(TracerProviderBuilder));
}
openTelemetry.WithTracing(tracing => tracing.UseElasticDefaults(Logger));

if (options.DistroOptions.EnabledDefaults.HasFlag(ElasticOpenTelemetryOptions.EnabledElasticDefaults.Metrics))
{
openTelemetry.WithMetrics(metrics =>
{
metrics
.AddProcessInstrumentation()
.AddRuntimeInstrumentation()
.AddHttpClientInstrumentation();

Logger.LogConfiguredSignalProvider("metrics", nameof(MeterProviderBuilder));
});
}
openTelemetry.WithMetrics(metrics => metrics.UseElasticDefaults(Logger));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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 Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using OpenTelemetry.Metrics;

namespace Elastic.OpenTelemetry.Extensions;

/// <summary>
/// Elastic extensions for <see cref="MeterProviderBuilder"/>.
/// </summary>
public static class MeterProviderBuilderExtensions
{
/// <summary> Use Elastic distribution defaults for <see cref="MeterProviderBuilder"/> </summary>
public static MeterProviderBuilder UseElasticDefaults(this MeterProviderBuilder builder, ILogger? logger = null)
{
logger ??= NullLogger.Instance;

builder
.AddProcessInstrumentation()
.AddRuntimeInstrumentation()
.AddHttpClientInstrumentation();

logger.LogConfiguredSignalProvider("metrics", nameof(MeterProviderBuilder));

return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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.Diagnostics.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using OpenTelemetry.Logs;

namespace Elastic.OpenTelemetry.Extensions;

/// <summary>
/// Elastic extensions for <see cref="OpenTelemetryLoggerOptions"/>.
/// </summary>
public static class OpenTelemetryLoggerOptionsExtensions
{
/// <summary>
/// Ensures Elastic distro options are set for <see cref="OpenTelemetryLoggerOptions"/>
/// </summary>
public static void UseElasticDefaults(this OpenTelemetryLoggerOptions options, ILogger? logger = null)
{
logger ??= NullLogger.Instance;
options.IncludeFormattedMessage = true;
options.IncludeScopes = true;
logger.LogConfiguredSignalProvider("logging", nameof(OpenTelemetryLoggerOptions));
}
}
Loading
Loading