Skip to content
Open
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
144 changes: 80 additions & 64 deletions docs/core/diagnostics/observability-otlp-example.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/core/diagnostics/observability-with-otel.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ The following table describes the main packages.

This topic is continued with a couple of example walkthroughs for using OpenTelemetry in .NET:

- [Example: Use OTLP and the standalone Aspire Dashboard](./observability-otlp-example.md)
- [Use OTLP and the Aspire Dashboard](./observability-otlp-example.md)
- [Example: Use OpenTelemetry with Azure Monitor and Application Insights](./observability-applicationinsights.md)
- [Example: Use OpenTelemetry with Prometheus, Grafana, and Jaeger](./observability-prgrja-example.md)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// <Usings>
using System.Diagnostics;
using System.Diagnostics.Metrics;
using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
// </Usings>

// <CustomMetrics>
// Custom metrics for the application
var greeterMeter = new Meter("OTel.Example", "1.0.0");
var countGreetings = greeterMeter.CreateCounter<int>("greetings.count", description: "Counts the number of greetings");

// Custom ActivitySource for the application
var greeterActivitySource = new ActivitySource("OTel.Example");
// </CustomMetrics>

var builder = WebApplication.CreateBuilder(args);

// <OTEL>
// Configure the shared OTLP connection used by logs, metrics, and traces.
var otlpEndpoint = new Uri(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]!);
Action<OtlpExporterOptions> configureOtlp = options =>
{
options.Endpoint = otlpEndpoint;
options.Protocol = OtlpExportProtocol.Grpc;
options.Headers = builder.Configuration["OTEL_EXPORTER_OTLP_HEADERS"]; // To secure endpoint (not in this example)
};

// Setup logging to be exported via OpenTelemetry
builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
logging.AddOtlpExporter(configureOtlp);
});

var otel = builder.Services.AddOpenTelemetry();

// Identify this application as a single service in the Aspire dashboard.
otel.ConfigureResource(resource => resource.AddService(builder.Configuration["OTEL_SERVICE_NAME"]!));

// Add Metrics for ASP.NET Core and our custom metrics and export via OTLP
otel.WithMetrics(metrics =>
{
// Metrics provider from OpenTelemetry
metrics.AddAspNetCoreInstrumentation();

// Our custom metrics
metrics.AddMeter(greeterMeter.Name);

// Metrics provided by ASP.NET Core in .NET
metrics.AddMeter("Microsoft.AspNetCore.Hosting");
metrics.AddMeter("Microsoft.AspNetCore.Server.Kestrel");

// Export the metrics via OTLP
metrics.AddOtlpExporter(configureOtlp);
});

// Add Tracing for ASP.NET Core and our custom ActivitySource and export via OTLP
otel.WithTracing(tracing =>
{
tracing.AddAspNetCoreInstrumentation();
tracing.AddHttpClientInstrumentation();
tracing.AddSource(greeterActivitySource.Name);
tracing.AddOtlpExporter(configureOtlp);
});
// </OTEL>

var app = builder.Build();

// <MapGet>
app.MapGet("/", SendGreeting);
// </MapGet>

app.Run();

// <SendGreeting>
async Task<string> SendGreeting(ILogger<Program> logger)
{
// Create a new Activity scoped to the method
using var activity = greeterActivitySource.StartActivity("GreeterActivity");

// Log a message
logger.LogInformation("Sending greeting");

// Increment the custom counter
countGreetings.Add(1);

// Add a tag to the Activity
activity?.SetTag("greeting", "Hello World!");

return "Hello World!";
}
// </SendGreeting>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5245",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7147;http://localhost:5245",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"OTEL_EXPORTER_OTLP_ENDPOINT": "http://localhost:4317",
"OTEL_SERVICE_NAME": "OTLP-Example"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<!-- <PackageReferences> -->
<ItemGroup>
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.17.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.17.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.17.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.17.0" />
</ItemGroup>
<!-- </PackageReferences> -->

</Project>
2 changes: 1 addition & 1 deletion docs/navigate/tools-diagnostics/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ items:
items:
- name: Overview
href: ../../core/diagnostics/observability-with-otel.md
- name: "Example: Use OpenTelemetry with OTLP and the standalone Aspire Dashboard"
- name: "Use OpenTelemetry with OTLP and the Aspire Dashboard"
href: ../../core/diagnostics/observability-otlp-example.md
- name: "Example: Use OpenTelemetry with Prometheus, Grafana, and Jaeger"
href: ../../core/diagnostics/observability-prgrja-example.md
Expand Down
Loading