-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f9c1136
commit d2c3b9f
Showing
19 changed files
with
369 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
[[getting-started]] | ||
== Getting started | ||
|
||
This page will show you how to get started with Elastic OpenTelemetry distribution for .NET. | ||
|
||
As the distribution is a lightweight wrapper around the OpenTelemetry SDK, you should be broadly | ||
familiar with the OpenTelemetry SDK concepts and instrumenting applications using the Microsoft | ||
diagnostic APIs. If you are not, we recommend you read the | ||
https://opentelemetry.io/docs/languages/net[OpenTelemetry SDK documentation] first. | ||
|
||
[IMPORTANT] | ||
-- | ||
The Elastic distribution is currently in early alpha release status. It is not yet feature | ||
complete and may contain bugs. We are actively working on improving the distribution and | ||
adding new features. | ||
|
||
If you would like to experience the alpha and help us improve the distribution by providing | ||
early feedback, you can follow the steps below to get started. | ||
-- | ||
|
||
=== Prerequisites | ||
|
||
The current documentation and examples are written with .NET 6 and newer applications in mind. | ||
Before continuing, ensure that you have a supported | ||
https://dotnet.microsoft.com/en-us/download/dotnet[.NET SDK version] installed locally. | ||
|
||
=== Installation | ||
|
||
To get started with the Elastic OpenTelemetry distribution, you must add the Elastic OpenTelemetry | ||
NuGet package to your project. This can be achieved by adding the package reference to your project file. | ||
|
||
[source,xml] | ||
---- | ||
<PackageReference Include="Elastic.OpenTelemetry" Version="0.1.0-alpha.1" /> | ||
---- | ||
|
||
After adding the package reference, you can start using the Elastic OpenTelemetry distribution | ||
in your application. The distribution includes a transitive dependency on the OpenTelemetry SDK, | ||
so you do not need to add the OpenTelemetry SDK package to your project, although doing so will | ||
cause no harm and may be used to opt into newer SDK versions before the Elastic distribution | ||
references them. | ||
|
||
The Elastic OpenTelemetry distribution is designed to be easy to use and integrate into your | ||
applications. This includes applications which have previously used the OpenTelemetry SDK directly. | ||
In situations where the OpenTelemetry SDK is already used, the only required change is | ||
to add the `Elastic.OpenTelemetry` NuGet package to the project. Doing so will automatically | ||
switch to the opinionated configuration provided by the Elastic distribution. | ||
|
||
=== ASP.NET Core usage | ||
|
||
A common requirement is to instrument ASP.NET Core applications based on the `Microsoft.Extensions.Hosting` | ||
libraries which provide dependency injection via an `IServiceProvider`. | ||
|
||
The OpenTelemetry SDK and the Elastic distribution provide extension methods to enable observability | ||
features in your application by adding a few lines of code. | ||
|
||
In this section, we'll focus on instrumenting an ASP.NET Core minimal API application using the Elastic | ||
OpenTelemetry distribution. Similar steps can also be used to instrument other ASP.NET Core workloads | ||
and other host-based applications such as https://learn.microsoft.com/en-us/dotnet/core/extensions/workers[worker services]. | ||
|
||
[NOTE] | ||
-- | ||
These examples assume the use of the top-level statements feature introduced in C# 9.0 and the | ||
default choice for applications created using the latest templates. | ||
-- | ||
|
||
To take advantage of the OpenTelemetry SDK instrumentation for ASP.NET Core, add the following | ||
NuGet package to your project: | ||
|
||
[source,xml] | ||
---- | ||
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.0" /> | ||
---- | ||
|
||
This package includes instrumentation to collect traces for requests handled by ASP.NET Core endpoints. | ||
|
||
[NOTE] | ||
-- | ||
The ASP.NET Core instrumentation is not included by default in the Elastic OpenTelemetry distribution. | ||
As with all optional instrumentation libraries, you can choose to include them in your application by | ||
adding a suitable package reference. | ||
-- | ||
|
||
Inside the `Program.cs` file of the ASP.NET Core application, add the following two using directives: | ||
|
||
[source,csharp] | ||
---- | ||
using OpenTelemetry; | ||
using OpenTelemetry.Trace; | ||
---- | ||
|
||
The OpenTelemetry SDK provides extension methods on the `IServiceCollection` to support enabling the | ||
providers and configuring the SDK. The Elastic distribution overrides the default SDK registration, | ||
adding several opinionated defaults. | ||
|
||
In the minimal API template, the `WebApplicationBuilder` exposes a `Services` property that can be used | ||
to register services with the dependency injection container. To enable tracing and metrics collection, | ||
ensure that the OpenTelemetry SDK is registered. | ||
|
||
[source,csharp] | ||
---- | ||
var builder = WebApplication.CreateBuilder(args); | ||
builder.Services | ||
.AddHttpClient() <1> | ||
.AddOpenTelemetry() <2> | ||
.WithTracing(t => t.AddAspNetCoreInstrumentation()); <3> | ||
---- | ||
<1> The `AddHttpClient` method registers the `IHttpClientFactory` service with the dependency | ||
injection container. This is NOT required to enable OpenTelemetry, but the example endpoint will use it to | ||
send an HTTP request. | ||
<2> The `AddOpenTelemetry` method registers the OpenTelemetry SDK with the dependency injection | ||
container. When available, the Elastic distribution will override this to add opinionated defaults. | ||
<3> Configure tracing to instrument requests handled by ASP.NET Core. | ||
|
||
With these limited changes to the `Program.cs` file, the application is now configured to use the | ||
OpenTelemetry SDK and the Elastic distribution to collect traces and metrics, which are exported via | ||
OTLP. | ||
|
||
To demonstrate the tracing capabilities, add a simple endpoint to the application: | ||
|
||
[source,csharp] | ||
---- | ||
app.MapGet("/", async (IHttpClientFactory httpClientFactory) => | ||
{ | ||
using var client = httpClientFactory.CreateClient(); | ||
await Task.Delay(100); | ||
var response = await client.GetAsync("http://elastic.co"); <1> | ||
await Task.Delay(50); | ||
return response.StatusCode == System.Net.HttpStatusCode.OK ? Results.Ok() : Results.StatusCode(500); | ||
}); | ||
---- | ||
<1> Using this URL will require two redirects, allowing us to see multiple spans in the trace. | ||
|
||
The Elastic distribution will automatically enable the exporting of signals via the OTLP exporter. This | ||
exporter requires that endpoint(s) are configured. A common mechanism for configuring endpoints is | ||
via environment variables. | ||
|
||
This demo uses an Elastic Cloud deployment as the destination for our observability data. From Kibana | ||
running in Elastic Cloud, navigate to the observability set up guides. Select the OpenTelemetry option | ||
to view the configuration details that should be supplied to the application. | ||
|
||
image::./images/elastic-cloud-opentelemetry-configuration.png[Elastic Cloud OpenTelemetry configuration] | ||
|
||
Configure environment variables for the application either in `launchSettings.json` or in the environment | ||
where the application is running. | ||
|
||
Once configured, run the application and make a request to the root endpoint. A trace will be generated | ||
and exported to the OTLP endpoint. | ||
|
||
To view the traces, you can use the Elastic APM UI. | ||
|
||
image::./images/trace-sample-minimal-api.png[Minimal API request trace sample in the Elastic APM UI] |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
include::{asciidoc-dir}/../../shared/versions/stack/current.asciidoc[] | ||
include::{asciidoc-dir}/../../shared/attributes.asciidoc[] | ||
|
||
= Elastic OpenTelemetry Agent Distribution | ||
= Elastic OpenTelemetry Distribution | ||
|
||
Docs TODO | ||
include::./intro.asciidoc[] | ||
|
||
include::./getting-started.asciidoc[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[[introduction]] | ||
== Introduction | ||
|
||
The `Elastic.OpenTelemetry` package contains an Elastic distribution of the | ||
https://opentelemetry.io/docs/languages/net[OpenTelemetry SDK for .NET]. A distribution | ||
is a wrapper around an upstream OpenTelemetry repository with some customizations. For | ||
more details about distributions in general, visit the | ||
https://opentelemetry.io/docs/concepts/distributions[OpenTelemetry documentation]. | ||
|
||
The Elastic distribution includes some Elastic-specific processors to ensure the best | ||
compatibility when exporting OpenTelemetry signal data to an Elastic backend such | ||
as Elastic APM server. The distribution also preconfigures the collection of tracing | ||
and metrics signals, applying some opinionated defaults, such as which sources are | ||
collected by default. The distribution also ensures that the OTLP exporter is enabled | ||
by default. | ||
|
||
The distribution includes extension methods to fully control the creation of the | ||
underlying tracer and metric providers, providing a helpful set of defaults | ||
to get developers up and running quickly with collecting and exporting OpenTelemetry | ||
signals. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Elastic.OpenTelemetry\Elastic.OpenTelemetry.csproj" /> | ||
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System.Diagnostics; | ||
using Example.Api; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Trace; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services | ||
.AddHttpClient() // Adds IHttpClientFactory | ||
.AddOpenTelemetry() // Adds the OpenTelemetry SDK | ||
.WithTracing(t => { | ||
t.AddAspNetCoreInstrumentation(); // Configure tracing to instrument ASP.NET Core | ||
t.AddSource(Api.ActivitySourceName); | ||
}); | ||
|
||
var app = builder.Build(); | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.MapGet("/", (IHttpClientFactory httpClientFactory) => Api.HandleRoot(httpClientFactory)); | ||
|
||
app.Run(); | ||
|
||
namespace Example.Api | ||
{ | ||
internal static class Api | ||
{ | ||
public static string ActivitySourceName = "CustomActivitySource"; | ||
private static readonly ActivitySource ActivitySource = new(ActivitySourceName); | ||
|
||
public static async Task<IResult> HandleRoot(IHttpClientFactory httpClientFactory) | ||
{ | ||
using var client = httpClientFactory.CreateClient(); | ||
|
||
using var activity = ActivitySource.StartActivity("DoingStuff", ActivityKind.Internal); | ||
activity?.SetTag("custom-tag", "TagValue"); | ||
|
||
await Task.Delay(100); | ||
var response = await client.GetAsync("http://elastic.co"); // using this URL will require 2 redirects | ||
await Task.Delay(50); | ||
|
||
if (response.StatusCode == System.Net.HttpStatusCode.OK) | ||
{ | ||
activity?.SetStatus(ActivityStatusCode.Ok); | ||
return Results.Ok(); | ||
} | ||
|
||
activity?.SetStatus(ActivityStatusCode.Error); | ||
return Results.StatusCode(500); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
examples/Example.MinimalApi/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:4214", | ||
"sslPort": 44348 | ||
} | ||
}, | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "", | ||
"applicationUrl": "http://localhost:5146", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development", | ||
"OTEL_RESOURCE_ATTRIBUTES": "service.name=minimal-api-example" | ||
} | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "", | ||
"applicationUrl": "https://localhost:7140;http://localhost:5146", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development", | ||
"OTEL_RESOURCE_ATTRIBUTES": "service.name=minimal-api-example" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development", | ||
"OTEL_RESOURCE_ATTRIBUTES": "service.name=minimal-api-example" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
Oops, something went wrong.