Skip to content

Commit

Permalink
Merge pull request #26 from elastic/aspnetcore-lib
Browse files Browse the repository at this point in the history
Add AspNetCore package and registration methods
  • Loading branch information
stevejgordon authored Jan 18, 2024
2 parents 108e88f + e839231 commit 18653e4
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 54 deletions.
3 changes: 1 addition & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Authors>Elastic and contributors</Authors>
Expand All @@ -13,7 +13,6 @@
<PropertyGroup>
<MinVerDefaultPreReleaseIdentifiers>canary.0</MinVerDefaultPreReleaseIdentifiers>
<MinVerMinimumMajorMinor>0.1</MinVerMinimumMajorMinor>

<LangVersion>latest</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<UseArtifactsOutput>true</UseArtifactsOutput>
Expand Down
11 changes: 9 additions & 2 deletions Elastic.OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{AAD39891
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elastic.OpenTelemetry.Tests", "tests\Elastic.OpenTelemetry.Tests\Elastic.OpenTelemetry.Tests.csproj", "{22BF9223-3A6D-4197-8527-3E4E43A98A81}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = ".github", ".github\.github.csproj", "{B701E33D-D777-4BD4-A4EC-1F63FE69AF7A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = ".github", ".github\.github.csproj", "{B701E33D-D777-4BD4-A4EC-1F63FE69AF7A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.Elastic.OpenTelemetry.AspNetCore", "examples\Example.Elastic.OpenTelemetry.AspNetCore\Example.Elastic.OpenTelemetry.AspNetCore.csproj", "{EC81FA30-C765-4F04-8679-86F16DA3CC65}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.Elastic.OpenTelemetry.Worker", "examples\Example.Elastic.OpenTelemetry.Worker\Example.Elastic.OpenTelemetry.Worker.csproj", "{4377A059-16E0-4D5D-AC03-44C09BCE5BC4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.Elastic.OpenTelemetry.Worker", "examples\Example.Elastic.OpenTelemetry.Worker\Example.Elastic.OpenTelemetry.Worker.csproj", "{4377A059-16E0-4D5D-AC03-44C09BCE5BC4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elastic.OpenTelemetry.AspNetCore", "src\Elastic.OpenTelemetry.AspNetCore\Elastic.OpenTelemetry.AspNetCore.csproj", "{2139F902-B10D-475D-8A38-F78962CEBFD3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -70,6 +72,10 @@ Global
{4377A059-16E0-4D5D-AC03-44C09BCE5BC4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4377A059-16E0-4D5D-AC03-44C09BCE5BC4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4377A059-16E0-4D5D-AC03-44C09BCE5BC4}.Release|Any CPU.Build.0 = Release|Any CPU
{2139F902-B10D-475D-8A38-F78962CEBFD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2139F902-B10D-475D-8A38-F78962CEBFD3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2139F902-B10D-475D-8A38-F78962CEBFD3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2139F902-B10D-475D-8A38-F78962CEBFD3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -80,6 +86,7 @@ Global
{22BF9223-3A6D-4197-8527-3E4E43A98A81} = {AAD39891-0B70-47FA-A212-43E1AAE5DF56}
{EC81FA30-C765-4F04-8679-86F16DA3CC65} = {4E95C87B-655B-4BC3-8F2A-DF06B7AAB7E9}
{4377A059-16E0-4D5D-AC03-44C09BCE5BC4} = {4E95C87B-655B-4BC3-8F2A-DF06B7AAB7E9}
{2139F902-B10D-475D-8A38-F78962CEBFD3} = {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 @@ -7,11 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.0" />
<ProjectReference Include="..\..\src\Elastic.OpenTelemetry.AspNetCore\Elastic.OpenTelemetry.AspNetCore.csproj" />
</ItemGroup>

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


</Project>
10 changes: 4 additions & 6 deletions examples/Example.Elastic.OpenTelemetry.AspNetCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
// 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 Example.Elastic.OpenTelemetry.AspNetCore.Controllers;
using OpenTelemetry.Trace;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddHttpClient();
builder.Services.AddOtelElasticAgent(t => t
.AddSource(HomeController.ActivitySourceName)
.AddAspNetCoreInstrumentation());
builder.Services.AddControllersWithViews();
builder.Services
.AddHttpClient()
.AddElasticOpenTelemetryForAspNetCore(HomeController.ActivitySourceName)
.AddControllersWithViews();

var app = builder.Build();

Expand Down
2 changes: 1 addition & 1 deletion examples/Example.Elastic.OpenTelemetry.Worker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();

builder.Services.AddOtelElasticAgent("CustomActivitySource");
builder.Services.AddElasticOpenTelemetry("CustomActivitySource");

var host = builder.Build();
host.Run();
3 changes: 2 additions & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"sdk": {
"version": "8.0.100",
"rollForward": "latestMajor"
"rollForward": "latestMajor",
"allowPrerelease": false
}
}
18 changes: 18 additions & 0 deletions src/Elastic.OpenTelemetry.AspNetCore/AgentBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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 OpenTelemetry.Trace;

namespace Elastic.OpenTelemetry.AspNetCore;

/// <summary>
///
/// </summary>
public static class AgentBuilderExtensions
{
/// <summary>
///
/// </summary>
/// <param name="agentBuilder"></param>
public static AgentBuilder AddAspNetCore(this AgentBuilder agentBuilder) => agentBuilder.ConfigureTracer(tpb => tpb.AddAspNetCoreInstrumentation());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Content Remove="README.md" />
</ItemGroup>

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

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

</Project>
1 change: 1 addition & 0 deletions src/Elastic.OpenTelemetry.AspNetCore/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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;
using Elastic.OpenTelemetry.AspNetCore;
using OpenTelemetry.Trace;

namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// Extension methods for <see cref="IServiceProvider"/>.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds the Elastic OpenTelemetry distribution to an application via the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="serviceCollection">TODO</param>
/// <returns>TODO</returns>
public static IServiceCollection AddElasticOpenTelemetryForAspNetCore(this IServiceCollection serviceCollection) =>
new AgentBuilder().AddAspNetCore().Register(serviceCollection);

/// <summary>
/// TODO
/// </summary>
/// <param name="serviceCollection"></param>
/// <param name="activitySourceNames"></param>
/// <returns></returns>
public static IServiceCollection AddElasticOpenTelemetryForAspNetCore(this IServiceCollection serviceCollection, params string[] activitySourceNames) =>
new AgentBuilder(activitySourceNames).AddAspNetCore().Register(serviceCollection);

/// <summary>
/// TODO
/// </summary>
/// <param name="serviceCollection"></param>
/// <param name="configureTracerProvider"></param>
/// <returns></returns>
public static IServiceCollection AddElasticOpenTelemetryForAspNetCore(this IServiceCollection serviceCollection, Action<TracerProviderBuilder> configureTracerProvider) =>
new AgentBuilder().AddAspNetCore().ConfigureTracer(configureTracerProvider).Register(serviceCollection);
}
14 changes: 7 additions & 7 deletions src/Elastic.OpenTelemetry/AgentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public AgentBuilder ConfigureTracer(Action<TracerProviderBuilder> configure)
// Elastic exporter will be added after.

ArgumentNullException.ThrowIfNull(configure);
_tracerProviderBuilderAction = configure;
_tracerProviderBuilderAction += configure;
return this;
}

Expand All @@ -184,7 +184,12 @@ public IAgent Build()
return tracerProvider is not null ? new Agent(tracerProvider) : new Agent();
}

internal IServiceCollection Build(IServiceCollection serviceCollection)
/// <summary>
/// Register the OpenTelemetry SDK services and Elastic defaults into the supplied <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="serviceCollection">A <see cref="IServiceCollection"/> to which OpenTelemetry SDK services will be added.</param>
/// <returns>The supplied <see cref="IServiceCollection"/>.</returns>
public IServiceCollection Register(IServiceCollection serviceCollection)
{
_ = serviceCollection
.AddOpenTelemetry()
Expand All @@ -199,8 +204,6 @@ internal IServiceCollection Build(IServiceCollection serviceCollection)
if (_activitySourceNames is not null)
tracerProviderBuilder.AddSource(_activitySourceNames);

// Set up a default tracer provider.
// TODO - We need to decide which sources and how to handle conditional things such as ASP.NET Core.
tracerProviderBuilder
.AddHttpClientInstrumentation()
.AddGrpcClientInstrumentation()
Expand All @@ -220,9 +223,6 @@ internal IServiceCollection Build(IServiceCollection serviceCollection)

_tracerProviderBuilderAction?.Invoke(tracerProviderBuilder);

// Ensure the distro attributes are always added to the resource.
tracerProviderBuilder.ConfigureResource(r => r.AddDistroAttributes());

// Add the OTLP exporter configured to ship data to an Elastic backend.
// TODO - What about cases where users want to register processors/exporters after any exporters we add by default (OTLP)?
tracerProviderBuilder.AddElasticOtlpExporter(_otlpExporterConfiguration, _otlpExporterName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,36 @@
using Elastic.OpenTelemetry;
using OpenTelemetry.Trace;

namespace Microsoft.Extensions.DependencyInjection
namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// Extension methods for <see cref="IServiceProvider"/>.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Extension methods for <see cref="IServiceProvider"/>.
/// Adds the Elastic OpenTelemetry distribution to an application via the <see cref="IServiceCollection"/>.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds the Elastic OpenTelemetry distribution to an application via the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="serviceCollection">TODO</param>
/// <returns>TODO</returns>
public static IServiceCollection AddOtelElasticAgent(this IServiceCollection serviceCollection) =>
new AgentBuilder().Build(serviceCollection);
/// <param name="serviceCollection">TODO</param>
/// <returns>TODO</returns>
public static IServiceCollection AddElasticOpenTelemetry(this IServiceCollection serviceCollection) =>
new AgentBuilder().Register(serviceCollection);

/// <summary>
/// TODO
/// </summary>
/// <param name="serviceCollection"></param>
/// <param name="activitySourceNames"></param>
/// <returns></returns>
public static IServiceCollection AddOtelElasticAgent(this IServiceCollection serviceCollection, params string[] activitySourceNames) =>
new AgentBuilder(activitySourceNames).Build(serviceCollection);
/// <summary>
/// TODO
/// </summary>
/// <param name="serviceCollection"></param>
/// <param name="activitySourceNames"></param>
/// <returns></returns>
public static IServiceCollection AddElasticOpenTelemetry(this IServiceCollection serviceCollection, params string[] activitySourceNames) =>
new AgentBuilder(activitySourceNames).Register(serviceCollection);

/// <summary>
/// TODO
/// </summary>
/// <param name="serviceCollection"></param>
/// <param name="configureTracerProvider"></param>
/// <returns></returns>
public static IServiceCollection AddOtelElasticAgent(this IServiceCollection serviceCollection, Action<TracerProviderBuilder> configureTracerProvider) =>
new AgentBuilder().ConfigureTracer(configureTracerProvider).Build(serviceCollection);
}
/// <summary>
/// TODO
/// </summary>
/// <param name="serviceCollection"></param>
/// <param name="configureTracerProvider"></param>
/// <returns></returns>
public static IServiceCollection AddElasticOpenTelemetry(this IServiceCollection serviceCollection, Action<TracerProviderBuilder> configureTracerProvider) =>
new AgentBuilder().ConfigureTracer(configureTracerProvider).Register(serviceCollection);
}
2 changes: 1 addition & 1 deletion src/Elastic.OpenTelemetry/Elastic.OpenTelemetry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
<PackageReference Include="OpenTelemetry.Instrumentation.Process" Version="0.5.0-beta.4" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
</ItemGroup>

</Project>

0 comments on commit 18653e4

Please sign in to comment.