1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
---
stage: Monitor
group: Platform Insights
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://handbook.gitlab.com/handbook/product/ux/technical-writing/#assignments
---
# Tutorial: Use GitLab Observability with a .NET application
FLAG:
The availability of this feature is controlled by a feature flag.
For more information, see the history of the [**Distributed tracing** feature](../../operations/tracing.md).
<!-- Update this note when observability_features flag is removed -->
In this tutorial, you'll learn how to create, configure, instrument, and monitor a .NET Core application using GitLab Observability features.
## Before you begin
To follow along this tutorial, you must have:
- A GitLab Ultimate subscription for GitLab.com or GitLab self-managed
- A local installation of [.NET](https://dotnet.microsoft.com/en-us/)
- Basic knowledge of Git, .NET, and the core concepts of [OpenTelemetry](https://opentelemetry.io/)
## Create a GitLab project
First, create a GitLab project and a corresponding access token.
1. On the left sidebar, at the top, select **Create new** (**{plus}**) and **New project/repository**.
1. Select **Create blank project**.
1. Enter the project details.
- In the **Project name** field, enter `dotnet-O11y-tutorial`.
1. Select **Create project**.
1. In the `dotnet-O11y-tutorial` project, on the left sidebar, select **Settings > Access tokens**.
1. Create an access token with the `api` scope and Developer role. Store the token value somewhere safe.
You'll need it later.
## Create a .NET application
Next, we'll create a .NET web application that we can instrument. For this tutorial, let's create a toy application that returns animal emojis.
1. Clone the `dotnet-O11y-tutorial` project and `cd` to the `dotnet-O11y-tutorial` directory.
1. Create a web application by running:
```shell
dotnet new web
```
1. Create an animal controller file by running the following:
```shell
touch AnimalController.cs
```
1. Replace the contents of `AnimalController.cs` with the following:
```cs
using Microsoft.AspNetCore.Mvc;
public class AnimalsController : ControllerBase
{
private Dictionary<string, string> animals = new Dictionary<string, string>
{
{ "dog", "🐶" },
{ "cat", "🐱" },
{ "fish", "🐟" }
};
private ILogger<AnimalsController> logger;
public AnimalsController(ILogger<AnimalsController> logger)
{
this.logger = logger;
}
[HttpGet("/animals/{animal}")]
public IActionResult GetAnimal([FromRoute] string animal)
{
if (animals.TryGetValue(animal, out string? emoji))
{
logger.LogInformation("Animal emoji found for: {animal}", animal);
return Ok(emoji);
}
else
{
logger.LogInformation("Could not find animal emoji for: {animal}", animal);
return NotFound("Animal not found");
}
}
}
```
1. In the `Properties` subdirectory, replace the contents of `launchSettings.json` with the following:
```json
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:8080",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
```
1. Replace the contents of `Program.cs` with the following:
```cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
var app = builder.Build();
app.MapControllers();
app.Run();
```
1. Build and run the application:
```shell
dotnet build
dotnet run
```
1. Visit `http://localhost:8080/animals/dog`, and you should see the emoji 🐶.
## Instrument the application
1. Install the required OpenTelemetry packages:
```shell
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Exporter.Console
dotnet add package OpenTelemetry.Instrumentation.AspNetCore --prerelease
dotnet add package OpenTelemetry.Instrumentation.Http --prerelease
```
1. Replace the contents of `Program.cs` with the following:
```cs
using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
var builder = WebApplication.CreateBuilder(args);
const string serviceName = "dotnet-O11y-tutorial";
string otelHeaders = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS") ?? "empty";
string otelBaseUrl = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT") ?? "empty";
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService(serviceName))
.WithTracing(tracing => tracing
.AddSource(serviceName)
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation()
.AddOtlpExporter(options =>
{
options.Endpoint = new Uri(otelBaseUrl + "/traces");
options.Headers = otelHeaders;
options.Protocol = OtlpExportProtocol.HttpProtobuf;
}))
.WithMetrics(metrics => metrics
.AddMeter(serviceName)
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation()
.AddOtlpExporter(options =>
{
options.Endpoint = new Uri(otelBaseUrl + "/metrics");
options.Headers = otelHeaders;
options.Protocol = OtlpExportProtocol.HttpProtobuf;
}))
.WithLogging(logging => logging
.AddConsoleExporter()
.AddOtlpExporter(options =>
{
options.Endpoint = new Uri(otelBaseUrl + "/logs");
options.Headers = otelHeaders;
options.Protocol = OtlpExportProtocol.HttpProtobuf;
}));
builder.Services.AddControllers();
var app = builder.Build();
app.MapControllers();
app.Run();
```
1. Find your project ID:
1. On the `dotnet-O11y-tutorial` project overview page, in the upper-right corner, select **Actions** (**{ellipsis_v}**).
1. Select **Copy project ID**. Save the copied ID for later.
1. Configure your application with instrumentation.
If you're using self-managed GitLab, replace `gitlab.com` with your self-managed instance hostname.
1. Run your application.
```shell
env OTEL_EXPORTER_OTLP_ENDPOINT="https://gitlab.com/api/v4/projects/{{PROJECT_ID}}/observability" \
OTEL_EXPORTER_OTLP_HEADERS="PRIVATE-TOKEN={{ACCESS_TOKEN}}" \
OTEL_LOG_LEVEL="debug" \
dotnet run
```
1. Visit `http://localhost:8080/animals/dog` to generate some events.
## View the information in GitLab
To view the exported information from your test project:
1. On the left sidebar, select **Search or go to** and find your project.
1. Select **Monitor**, then either **Logs**, **Metrics**, or **Traces**.
|