Skip to content

Commit

Permalink
Refactor public API and align with 1.10 and 1.11 of the SDK (#210)
Browse files Browse the repository at this point in the history
This is quite a significant update from the previous code and includes a
lot of refactorings as a result. In short, as the SDK since 1.10
includes an `OpenTelemetryBuilder`, we can base many extension methods
directly on the interface it implements `IOpenTelemetryBuilder`.

A significant change in this PR is removing the "magic" global override
we used to replace `AddOpenTelemetry` with our behaviour. We no longer
do this to avoid surprises and prefer an explicit small code change from
consumers.

We now also track the calls to the various builders to avoid
bootstrapping components more than is required and log such
circumstances.

We expect the auto instrumentation tests to fail for now as those need
to be uncoupled from the SDK workflow.

Closes #164 
Closes #175
  • Loading branch information
stevejgordon authored Feb 13, 2025
1 parent be769bb commit 4b4a98f
Show file tree
Hide file tree
Showing 95 changed files with 3,116 additions and 2,234 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/bootstrap/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ runs:
uses: actions/setup-dotnet@v4
with:
global-json-file: ./global.json
# 7.x is required for the dotnet-project-licenses tool.
dotnet-version: |
7.x
- id: dotnet
shell: bash
Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/ci-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ jobs:
- run: 'echo "Not required for docs"'

# dummy steps that allow to bypass those mandatory checks for tests
build:
test-linux:
runs-on: ubuntu-latest
steps:
- run: 'echo "Not required for docs"'

# dummy steps that allow to bypass those mandatory checks for tests
release-build:
runs-on: ubuntu-latest
steps:
- run: 'echo "Not required for docs"'
24 changes: 18 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ env:
# update ci-docs.yml
jobs:
test-windows:
name: Windows Tests
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -36,11 +37,12 @@ jobs:
id: bootstrap
uses: ./.github/workflows/bootstrap

- name: Test
run: build.bat test --test-suite=skip-e2e
- name: Unit Tests
run: build.bat test --test-suite=unit
shell: cmd

build:
test-linux:
name: Linux Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -49,9 +51,19 @@ jobs:
id: bootstrap
uses: ./.github/workflows/bootstrap

- name: Test
run: ./build.sh test --test-suite=skip-e2e
- name: Unit Tests
run: ./build.sh test --test-suite=unit # For now, we limit to unit tests only, until we have a better way to run integration tests only for autoinstrumentation builds

# We still run the full release build on pull-requests this ensures packages are validated ahead of time
release-build:
name: Release Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Bootstrap Action Workspace
id: bootstrap
uses: ./.github/workflows/bootstrap

# We still run the full release build on pull-requests this ensures packages are validated ahead of time
- name: Release
run: ./build.sh release -c
34 changes: 18 additions & 16 deletions build/build.fsproj
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<OutputType>Exe</OutputType>
<NoWarn>$(NoWarn);NU1701</NoWarn>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Argu" Version="6.1.4"/>
<PackageReference Include="Bullseye" Version="4.2.1"/>
<PackageReference Include="Argu" Version="6.2.5" />
<PackageReference Include="Bullseye" Version="4.2.1" />
<PackageReference Include="Octokit" Version="13.0.1" />
<PackageReference Include="Proc.Fs" Version="0.8.1"/>
<PackageReference Include="Fake.Tools.Git" Version="5.20.3"/>
<PackageReference Remove="FSharp.Core"/>
<PackageReference Include="FSharp.Core" Version="8.0.101"/>
<PackageReference Include="Proc.Fs" Version="0.9.1" />
<PackageReference Include="Fake.Tools.Git" Version="5.23.1" />
<PackageReference Remove="FSharp.Core" />
<PackageReference Include="FSharp.Core" Version="9.0.201" />
</ItemGroup>

<ItemGroup>
<None Include="..\*" LinkBase="_root"/>
<None Include="..\*" LinkBase="_root" />
</ItemGroup>

<ItemGroup>
<Compile Include="scripts\BuildInformation.fs"/>
<Compile Include="scripts\CommandLine.fs"/>
<Compile Include="scripts\BuildInformation.fs" />
<Compile Include="scripts\CommandLine.fs" />
<Compile Include="scripts\Packaging.fs" />
<Compile Include="scripts\Targets.fs"/>
<Compile Include="scripts\Program.fs"/>
<None Include="**\*"/>
<None Remove="bin\**"/>
<None Remove="obj\**"/>
<None Remove="scripts\**"/>
<None Remove="output\**"/>
<Compile Include="scripts\Targets.fs" />
<Compile Include="scripts\Program.fs" />
<None Include="**\*" />
<None Remove="bin\**" />
<None Remove="obj\**" />
<None Remove="scripts\**" />
<None Remove="output\**" />
</ItemGroup>

</Project>
1 change: 0 additions & 1 deletion build/scripts/CommandLine.fs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ with
| Skip_Dirty_Check -> "Skip the clean checkout check that guards the release/publish targets"
| Test_Suite _ -> "Specify the test suite to run, defaults to all"


member this.StepName =
match FSharpValue.GetUnionFields(this, typeof<Build>) with
| case, _ -> case.Name.ToLowerInvariant()
Expand Down
7 changes: 4 additions & 3 deletions examples/Example.AspNetCore.Mvc/Example.AspNetCore.Mvc.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<UserSecretsId>1efafe93-6112-431d-b30f-786205a20ebe</UserSecretsId>
</PropertyGroup>

<ItemGroup>
Expand All @@ -13,7 +14,7 @@
</ItemGroup>

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

<ItemGroup>
Expand Down
17 changes: 14 additions & 3 deletions examples/Example.AspNetCore.Mvc/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@
// 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.AspNetCore.Mvc.Controllers;
using OpenTelemetry;
using OpenTelemetry.Resources;

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();
using var loggerFactory = LoggerFactory.Create(loggingBuilder => loggingBuilder
.SetMinimumLevel(LogLevel.Trace)
.AddConsole());

var logger = loggerFactory.CreateLogger("OpenTelemetry");

// Add services to the container.
builder.Services
.AddHttpClient()
.AddOpenTelemetry()
.WithTracing(t => t.AddSource(HomeController.ActivitySourceName));
.ConfigureResource(r => r.AddService("MyNewService1"))
.WithElasticDefaults(builder.Configuration);

builder.Services.AddOpenTelemetry()
.ConfigureResource(r => r.AddService("MyNewService2"))
.WithElasticDefaults(builder.Configuration);

//OpenTelemetrySdk.Create(b => b.WithElasticDefaults(builder.Configuration));

builder.Services
.AddControllersWithViews();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"https": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7295;http://localhost:5247"
},
Expand Down
11 changes: 8 additions & 3 deletions examples/Example.AspNetCore.Mvc/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Elastic.OpenTelemetry": "Information"
"Microsoft.AspNetCore": "Warning"
},
"OpenTelemetry": {
"IncludeFormattedMessage": true,
"IncludeScopes": true,
"ParseStateValues": true
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Elastic": {
"OpenTelemetry": {
"LogLevel": "Trace",
"LogDirectory": "C:\\Logs\\BrandNewLogs"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
Expand Down
2 changes: 1 addition & 1 deletion examples/Example.Console/Example.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.11.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.9.0-beta.1" />
</ItemGroup>

Expand Down
77 changes: 40 additions & 37 deletions examples/Example.Console/Usage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// See the LICENSE file in the project root for more information

using System.Diagnostics;
using Elastic.OpenTelemetry;
using Elastic.OpenTelemetry.Extensions;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
Expand All @@ -21,45 +19,50 @@ public static async Task BasicBuilderUsageAsync()
{
// NOTE: This sample assumes ENV VARs have been set to configure the Endpoint and Authorization header.

// Build an instrumentation session by creating an ElasticOpenTelemetryBuilder.
// The application will be instrumented until the session is disposed.
await using var session = new ElasticOpenTelemetryBuilder()
.WithTracing(b => b.AddSource(ActivitySourceName))
.Build();
//// Build an instrumentation session by creating an ElasticOpenTelemetryBuilder.
//// The application will be instrumented until the session is disposed.
//await using var session = new ElasticOpenTelemetryBuilder()
// .WithTracing(b => b.AddSource(ActivitySourceName))
// .Build();

await using var session2 = new ElasticOpenTelemetryBuilder().Build();
//await using var session2 = new ElasticOpenTelemetryBuilder().Build();

// This example adds the application activity source and fully customises the resource
await using var session3 = new ElasticOpenTelemetryBuilder()
.WithTracing(b => b
.AddSource(ActivitySourceName)
.ConfigureResource(r => r.Clear().AddService("CustomServiceName", serviceVersion: "2.2.2")))
.Build();
//// This example adds the application activity source and fully customises the resource
//await using var session3 = new ElasticOpenTelemetryBuilder()
// .WithTracing(b => b
// .AddSource(ActivitySourceName)
// .ConfigureResource(r => r.Clear().AddService("CustomServiceName", serviceVersion: "2.2.2")))
// .Build();

await using var session4 = new ElasticOpenTelemetryBuilder()
.WithTracing(t => t
.ConfigureResource(rb => rb.AddService("TracerProviderBuilder", "3.3.3"))
.AddRedisInstrumentation() // This can currently only be achieved using this overload or adding Elastic processors to the TPB (as below)
.AddSource(ActivitySourceName)
.AddConsoleExporter()
)
.WithTracing(tpb => tpb
.ConfigureResource(rb => rb.AddService("TracerProviderBuilder", "3.3.3"))
.AddRedisInstrumentation() // This can currently only be achieved using this overload or adding Elastic processors to the TPB (as below)
.AddSource(ActivitySourceName)
.AddConsoleExporter())
.Build();
//await using var session4 = new ElasticOpenTelemetryBuilder()
// .WithTracing(t => t
// .ConfigureResource(rb => rb.AddService("TracerProviderBuilder", "3.3.3"))
// .AddRedisInstrumentation() // This can currently only be achieved using this overload or adding Elastic processors to the TPB (as below)
// .AddSource(ActivitySourceName)
// .AddConsoleExporter()
// )
// .WithTracing(tpb => tpb
// .ConfigureResource(rb => rb.AddService("TracerProviderBuilder", "3.3.3"))
// .AddRedisInstrumentation() // This can currently only be achieved using this overload or adding Elastic processors to the TPB (as below)
// .AddSource(ActivitySourceName)
// .AddConsoleExporter())
// .Build();

//This is the most flexible approach for a consumer as they can include our processor(s) and exporter(s)
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource(ActivitySourceName)
.ConfigureResource(resource =>
resource.AddService(
serviceName: "OtelSdkApp",
serviceVersion: "1.0.0"))
.AddConsoleExporter()
.AddElasticProcessors()
.Build();
using var sdk = OpenTelemetrySdk.Create(builder => builder
.WithElasticMetrics()
.WithElasticMetrics()
.ConfigureResource(resource => resource.AddService("MyCustomServiceName")));

//This is the most flexible approach for a consumer as they can include our processor(s)
//using var tracerProvider = Sdk.CreateTracerProviderBuilder()
// .AddSource(ActivitySourceName)
// .ConfigureResource(resource =>
// resource.AddService(
// serviceName: "OtelSdkApp",
// serviceVersion: "1.0.0"))
// .AddConsoleExporter()
// .AddElasticProcessors()
// .Build();

await DoStuffAsync();

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.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.11.0" />
</ItemGroup>

</Project>
4 changes: 3 additions & 1 deletion examples/Example.MinimalApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

var builder = WebApplication.CreateBuilder(args);

//builder.AddElasticOpenTelemetry();

// This will add the OpenTelemetry services using Elastic defaults
builder.AddServiceDefaults();

builder.Services
.AddHttpClient() // Adds IHttpClientFactory
.AddOpenTelemetry() // Adds app specific tracing
.AddElasticOpenTelemetry() // Adds app specific tracing
.WithTracing(t => t.AddSource(Api.ActivitySourceName));

var app = builder.Build();
Expand Down
4 changes: 3 additions & 1 deletion examples/Example.MinimalApi/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"applicationUrl": "https://localhost:7140;http://localhost:5146",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"OTEL_RESOURCE_ATTRIBUTES": "service.name=minimal-api-example"
"OTEL_RESOURCE_ATTRIBUTES": "service.name=minimal-api-example",
"OTEL_EXPORTER_OTLP_ENDPOINT": "https://opentelemetry-playground-adbd73.apm.eu-west-1.aws.elastic.cloud:443",
"OTEL_EXPORTER_OTLP_HEADERS": "Authorization=ApiKey cVY3d3NKTUJXcWZFRWJwb2xURjA6bmNrZ0JiQ29SRWlEdUM1dzVORGYwZw=="
}
},
"IIS Express": {
Expand Down
3 changes: 2 additions & 1 deletion examples/Example.MinimalApi/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Microsoft.AspNetCore": "Warning",
"Elastic.OpenTelemetry": "Trace"
}
}
}
4 changes: 2 additions & 2 deletions examples/Example.WorkerService/Example.WorkerService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.9.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.11.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 4b4a98f

Please sign in to comment.