Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejgordon committed Feb 11, 2025
1 parent 17cb7b5 commit f707563
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 81 deletions.
5 changes: 3 additions & 2 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>
<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.10.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"
}
}
}
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
2 changes: 1 addition & 1 deletion examples/Example.WorkerService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

var builder = Host.CreateApplicationBuilder(args);

builder.Services.AddOpenTelemetry()
builder.Services.AddElasticOpenTelemetry()
.ConfigureResource(r => r.AddService(serviceName: "MyService"))
.WithTracing(t => t.AddSource(Worker.ActivitySourceName).AddConsoleExporter())
.WithMetrics(m => m.AddMeter(Worker.MeterName).AddConsoleExporter());
Expand Down
24 changes: 3 additions & 21 deletions examples/ServiceDefaults/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -38,31 +36,15 @@ public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBu

public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder)
{
builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});

builder.Services.AddElasticOpenTelemetry(builder.Configuration)
.WithMetrics(metrics =>
{
metrics.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddProcessInstrumentation()
.AddRuntimeInstrumentation();
})
.WithMetrics()
.WithTracing(tracing =>
{
if (builder.Environment.IsDevelopment())
{
// We want to view all traces in development
tracing.SetSampler(new AlwaysOnSampler());
}

tracing.AddAspNetCoreInstrumentation()
.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation();
});

builder.AddOpenTelemetryExporters();
Expand All @@ -86,8 +68,8 @@ private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostAppli
// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
//{
// builder.Services.AddOpenTelemetry()
// .UseAzureMonitor();
//builder.Services.AddOpenTelemetry()
// .UseAzureMonitor();
//}
builder;

Expand Down
14 changes: 7 additions & 7 deletions examples/ServiceDefaults/ServiceDefaults.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />

<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.1.0" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="9.0.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.11.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.11.1" />
<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" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.11.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.11.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Process" Version="1.11.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.11.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit f707563

Please sign in to comment.