Skip to content

Commit

Permalink
Simplify/improve Prometheus tutorial (#4878)
Browse files Browse the repository at this point in the history
  • Loading branch information
reyang authored Sep 25, 2023
1 parent 2a480cd commit 9302891
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 112 deletions.
21 changes: 13 additions & 8 deletions docs/metrics/getting-started-prometheus-grafana/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,42 @@

using System.Diagnostics.Metrics;
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;

namespace GettingStartedPrometheusGrafana;

public class Program
{
private static readonly Meter MyMeter = new("MyCompany.MyProduct.MyLibrary", "1.0");
private static readonly Counter<long> MyFruitCounter = MyMeter.CreateCounter<long>("MyFruitCounter");

public static void Main()
{
using var meterProvider = Sdk.CreateMeterProviderBuilder()
var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter("MyCompany.MyProduct.MyLibrary")
.AddConsoleExporter()
.AddOtlpExporter(options =>
.AddOtlpExporter((exporterOptions, metricReaderOptions) =>
{
options.Endpoint = new Uri("http://localhost:9090/api/v1/otlp/v1/metrics");
options.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
exporterOptions.Endpoint = new Uri("http://localhost:9090/api/v1/otlp/v1/metrics");
exporterOptions.Protocol = OtlpExportProtocol.HttpProtobuf;
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 1000;
})
.Build();

Console.WriteLine("Press any key to exit");

while (!Console.KeyAvailable)
{
Thread.Sleep(1000);
MyFruitCounter.Add(1, new("name", "apple"), new("color", "red"));
MyFruitCounter.Add(2, new("name", "lemon"), new("color", "yellow"));
MyFruitCounter.Add(1, new("name", "lemon"), new("color", "yellow"));
MyFruitCounter.Add(2, new("name", "apple"), new("color", "green"));
MyFruitCounter.Add(5, new("name", "apple"), new("color", "red"));
MyFruitCounter.Add(4, new("name", "lemon"), new("color", "yellow"));

Thread.Sleep(300);
}

// Dispose meter provider before the application ends.
// This will flush the remaining metrics and shutdown the metrics pipeline.
meterProvider?.Dispose();
}
}
118 changes: 15 additions & 103 deletions docs/metrics/getting-started-prometheus-grafana/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Getting Started with Prometheus and Grafana

- [Export metrics from the application](#export-metrics-from-the-application)
- [Check results in the console](#check-results-in-the-console)
- [Collect metrics using Prometheus](#collect-metrics-using-prometheus)
- [Install and run Prometheus](#install-and-run-prometheus)
- [View results in Prometheus](#view-results-in-prometheus)
- [Explore metrics using Grafana](#explore-metrics-using-grafana)
- [Final cleanup](#final-cleanup)
- [Learn more](#learn-more)

## Export metrics from the application
Expand All @@ -18,77 +16,32 @@ this document.
Create a new console application and run it:

```sh
dotnet new console --output getting-started-prometheus
cd getting-started-prometheus
dotnet new console --output getting-started-prometheus-grafana
cd getting-started-prometheus-grafana
dotnet run
```

Add reference to [Console
Exporter](../../../src/OpenTelemetry.Exporter.Console/README.md) and [OTLP
Add reference to [OTLP
Exporter](../../../src/OpenTelemetry.Exporter.OpenTelemetryProtocol/README.md):

```sh
dotnet add package OpenTelemetry.Exporter.Console
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
```

Now copy the code from [Program.cs](./Program.cs).
Now copy the code from [Program.cs](./Program.cs) and run the application again.

### Check results in the console

Run the application again and we should see the metrics output from the console:

```text
> dotnet run
Press any key to exit
Resource associated with Metric:
telemetry.sdk.name: opentelemetry
telemetry.sdk.language: dotnet
telemetry.sdk.version: 1.6.1-alpha.0.23
service.name: unknown_service:getting-started-prometheus-grafana
Export MyFruitCounter, Meter: MyCompany.MyProduct.MyLibrary/1.0
(2023-09-22T20:40:22.2586791Z, 2023-09-22T20:40:31.1582923Z] color: red name: apple LongSum
Value: 54
(2023-09-22T20:40:22.2586791Z, 2023-09-22T20:40:31.1582923Z] color: yellow name: lemon LongSum
Value: 63
(2023-09-22T20:40:22.2586791Z, 2023-09-22T20:40:31.1582923Z] color: green name: apple LongSum
Value: 18
...
```

Note that we have configured two exporters in the code:

```csharp
using var meterProvider = Sdk.CreateMeterProviderBuilder()
...
.AddConsoleExporter()
.AddOtlpExporter(options =>
{
options.Endpoint = new Uri("http://localhost:9090/api/v1/otlp/v1/metrics");
options.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
})
.Build();
```

When we ran the application, the `ConsoleExporter` was printing the metrics on
console, and the `OtlpExporter` was attempting to send the metrics to
`http://localhost:9090/api/v1/otlp/v1/metrics`.

Since we didn't have Prometheus server running, the metrics received by
`OtlpExporter` were simply dropped on the floor. In the next step, we are going
to learn about how to use Prometheus to collect and visualize the metrics.
When we ran the application, the OTLP Exporter was attempting to export the
metrics to `http://localhost:9090/api/v1/otlp/v1/metrics`. Since Prometheus
server was not running, the metrics received by `OtlpExporter` were simply
dropped on the floor. In the next step, we are going to learn about how to use
Prometheus to collect and visualize the metrics.

```mermaid
graph LR
subgraph SDK
MeterProvider
MetricReader[BaseExportingMetricReader]
MetricReader2[BaseExportingMetricReader]
ConsoleExporter
OtlpExporter
end
Expand All @@ -98,25 +51,22 @@ end
Instrument --> | Measurements | MeterProvider
MeterProvider --> | Metrics | MetricReader --> | Push | OtlpExporter
MeterProvider --> | Metrics | MetricReader2 --> | Push | ConsoleExporter
MeterProvider --> | Metrics | MetricReader --> | Push | OtlpExporter --> | HTTP/protobuf | PrometheusServer[Prometheus server]
```

Also, for our learning purpose, use a while-loop to keep increasing the counter
value until any key is pressed.

```csharp
Console.WriteLine("Press any key to exit");

while (!Console.KeyAvailable)
{
Thread.Sleep(1000);
MyFruitCounter.Add(1, new("name", "apple"), new("color", "red"));
MyFruitCounter.Add(2, new("name", "lemon"), new("color", "yellow"));
MyFruitCounter.Add(1, new("name", "lemon"), new("color", "yellow"));
...
...
...

Thread.Sleep(300);
}
```

Expand Down Expand Up @@ -188,48 +138,10 @@ of increase of `MyFruitCounter_total` over the past 5 minutes:
![Grafana
UI](https://user-images.githubusercontent.com/17327289/151636769-138ecb4f-b44f-477b-88eb-247fc4340252.png)

## Final cleanup

In the end, remove the Console Exporter so we only have OTLP Exporter in the
final application:

```csharp
using var meterProvider = Sdk.CreateMeterProviderBuilder()
...
// Remove Console Exporter from the final application
// .AddConsoleExporter()
.AddOtlpExporter(options =>
{
options.Endpoint = new Uri("http://localhost:9090/api/v1/otlp/v1/metrics");
options.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
})
.Build();
```

```sh
dotnet remove package OpenTelemetry.Exporter.Console
```

```mermaid
graph LR
subgraph SDK
MeterProvider
MetricReader[BaseExportingMetricReader]
OtlpExporter
end
subgraph API
Instrument["Meter(#quot;MyCompany.MyProduct.MyLibrary#quot;, #quot;1.0#quot;)<br/>Counter(#quot;MyFruitCounter#quot;)"]
end
Instrument --> | Measurements | MeterProvider
MeterProvider --> | Metrics | MetricReader --> | Push | OtlpExporter
```

## Learn more

- [What is Prometheus?](https://prometheus.io/docs/introduction/overview/)
- [Prometheus now supports OpenTelemetry
Metrics](https://horovits.medium.com/prometheus-now-supports-opentelemetry-metrics-83f85878e46a)
- [Grafana support for
Prometheus](https://prometheus.io/docs/visualization/grafana/#creating-a-prometheus-graph)
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Console\OpenTelemetry.Exporter.Console.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.OpenTelemetryProtocol\OpenTelemetry.Exporter.OpenTelemetryProtocol.csproj" />
</ItemGroup>
</Project>

0 comments on commit 9302891

Please sign in to comment.