Skip to content

Commit 380b159

Browse files
authored
Merge branch 'main' into utpilla/Update-AggregatorStore-To-Reclaim-MetricPoints
2 parents 5abe869 + f468dd8 commit 380b159

File tree

4 files changed

+107
-84
lines changed

4 files changed

+107
-84
lines changed

docs/metrics/getting-started-prometheus-grafana/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ public static void Main()
2929
{
3030
using var meterProvider = Sdk.CreateMeterProviderBuilder()
3131
.AddMeter("MyCompany.MyProduct.MyLibrary")
32-
.AddPrometheusHttpListener()
32+
.AddConsoleExporter()
33+
.AddOtlpExporter(options =>
34+
{
35+
options.Endpoint = new Uri("http://localhost:9090/api/v1/otlp/v1/metrics");
36+
options.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
37+
})
3338
.Build();
3439

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

docs/metrics/getting-started-prometheus-grafana/README.md

Lines changed: 98 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Getting Started with Prometheus and Grafana
22

33
- [Export metrics from the application](#export-metrics-from-the-application)
4-
- [Check results in the browser](#check-results-in-the-browser)
4+
- [Check results in the console](#check-results-in-the-console)
55
- [Collect metrics using Prometheus](#collect-metrics-using-prometheus)
6-
- [Configuration](#configuration)
7-
- [Start Prometheus](#start-prometheus)
6+
- [Install and run Prometheus](#install-and-run-prometheus)
87
- [View results in Prometheus](#view-results-in-prometheus)
98
- [Explore metrics using Grafana](#explore-metrics-using-grafana)
9+
- [Final cleanup](#final-cleanup)
1010
- [Learn more](#learn-more)
1111

1212
## Export metrics from the application
@@ -23,46 +23,73 @@ cd getting-started-prometheus
2323
dotnet run
2424
```
2525

26-
Add a reference to [Prometheus
27-
Exporter Http Listener](../../../src/OpenTelemetry.Exporter.Prometheus.HttpListener/README.md):
26+
Add reference to [Console
27+
Exporter](../../../src/OpenTelemetry.Exporter.Console/README.md) and [OTLP
28+
Exporter](../../../src/OpenTelemetry.Exporter.OpenTelemetryProtocol/README.md):
2829

2930
```sh
30-
dotnet add package OpenTelemetry.Exporter.Prometheus.HttpListener --prerelease
31+
dotnet add package OpenTelemetry.Exporter.Console
32+
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
3133
```
3234

33-
Now, we are going to make some small tweaks to the example in the
34-
getting-started metrics `Program.cs` to make the metrics available via
35-
OpenTelemetry Prometheus Exporter.
35+
Now copy the code from [Program.cs](./Program.cs).
3636

37-
First, copy and paste everything from getting-started metrics
38-
[example](../getting-started-console/Program.cs) to the Program.cs file of the
39-
new console application (getting-started-prometheus) we've created.
37+
### Check results in the console
4038

41-
And replace the below line:
39+
Run the application again and we should see the metrics output from the console:
4240

43-
```csharp
44-
.AddConsoleExporter()
41+
```text
42+
> dotnet run
43+
Press any key to exit
44+
45+
Resource associated with Metric:
46+
telemetry.sdk.name: opentelemetry
47+
telemetry.sdk.language: dotnet
48+
telemetry.sdk.version: 1.6.1-alpha.0.23
49+
service.name: unknown_service:getting-started-prometheus-grafana
50+
51+
Export MyFruitCounter, Meter: MyCompany.MyProduct.MyLibrary/1.0
52+
(2023-09-22T20:40:22.2586791Z, 2023-09-22T20:40:31.1582923Z] color: red name: apple LongSum
53+
Value: 54
54+
(2023-09-22T20:40:22.2586791Z, 2023-09-22T20:40:31.1582923Z] color: yellow name: lemon LongSum
55+
Value: 63
56+
(2023-09-22T20:40:22.2586791Z, 2023-09-22T20:40:31.1582923Z] color: green name: apple LongSum
57+
Value: 18
58+
59+
...
4560
```
4661

47-
with
62+
Note that we have configured two exporters in the code:
4863

4964
```csharp
50-
.AddPrometheusHttpListener()
65+
using var meterProvider = Sdk.CreateMeterProviderBuilder()
66+
...
67+
.AddConsoleExporter()
68+
.AddOtlpExporter(options =>
69+
{
70+
options.Endpoint = new Uri("http://localhost:9090/api/v1/otlp/v1/metrics");
71+
options.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
72+
})
73+
.Build();
5174
```
5275

53-
`PrometheusHttpListener` is a wrapper that contains `PrometheusExporter`. With
54-
`AddPrometheusHttpListener()`, OpenTelemetry `PrometheusExporter` will export
55-
data via the endpoint defined by
56-
[PrometheusHttpListenerOptions.UriPrefixes](../../../src/OpenTelemetry.Exporter.Prometheus.HttpListener/README.md#uriprefixes),
57-
which is `http://localhost:9464/` by default.
76+
When we ran the application, the `ConsoleExporter` was printing the metrics on
77+
console, and the `OtlpExporter` was attempting to send the metrics to
78+
`http://localhost:9090/api/v1/otlp/v1/metrics`.
79+
80+
Since we didn't have Prometheus server running, the metrics received by
81+
`OtlpExporter` were simply dropped on the floor. In the next step, we are going
82+
to learn about how to use Prometheus to collect and visualize the metrics.
5883

5984
```mermaid
6085
graph LR
6186
6287
subgraph SDK
6388
MeterProvider
6489
MetricReader[BaseExportingMetricReader]
65-
PrometheusHttpListener["PrometheusHttpListener<br/>(http://localhost:9464/)"]
90+
MetricReader2[BaseExportingMetricReader]
91+
ConsoleExporter
92+
OtlpExporter
6693
end
6794
6895
subgraph API
@@ -71,7 +98,9 @@ end
7198
7299
Instrument --> | Measurements | MeterProvider
73100
74-
MeterProvider --> | Metrics | MetricReader --> | Pull | PrometheusHttpListener
101+
MeterProvider --> | Metrics | MetricReader --> | Push | OtlpExporter
102+
103+
MeterProvider --> | Metrics | MetricReader2 --> | Push | ConsoleExporter
75104
```
76105

77106
Also, for our learning purpose, use a while-loop to keep increasing the counter
@@ -91,74 +120,37 @@ while (!Console.KeyAvailable)
91120
}
92121
```
93122

94-
After the above modifications, now our `Program.cs` should look like [this](./Program.cs).
95-
96-
### Check results in the browser
97-
98-
Start the application and keep it running. Now we should be able to see the
99-
metrics at [http://localhost:9464/metrics](http://localhost:9464/metrics) from a
100-
web browser:
101-
102-
![Browser UI](https://user-images.githubusercontent.com/17327289/151633547-736c6d91-62d2-4e66-a53f-2e16c44bfabc.png)
103-
104-
Now, we understand how we can configure `PrometheusHttpListener` to export metrics.
105-
Next, we are going to learn about how to use Prometheus to collect the metrics.
106-
107123
## Collect metrics using Prometheus
108124

125+
### Install and run Prometheus
126+
109127
Follow the [first steps](https://prometheus.io/docs/introduction/first_steps/)
110128
to download the [latest release](https://prometheus.io/download/) of Prometheus.
111129

112-
### Configuration
113-
114130
After finished downloading, extract it to a local location that's easy to
115-
access. We will find the default Prometheus configuration YAML file in the
116-
folder, named `prometheus.yml`.
117-
118-
Let's create a new file in the same location as where `prometheus.yml` locates,
119-
and named the new file as `otel.yml` for this exercise. Then, copy and paste the
120-
entire content below into the `otel.yml` file we have created just now.
121-
122-
```yaml
123-
global:
124-
scrape_interval: 10s
125-
evaluation_interval: 10s
126-
scrape_configs:
127-
- job_name: "otel"
128-
static_configs:
129-
- targets: ["localhost:9464"]
130-
```
131+
access. Run the `prometheus(.exe)` server executable with feature flag
132+
[otlp-receiver](https://prometheus.io/docs/prometheus/latest/feature_flags/#otlp-receiver)
133+
enabled:
131134

132-
### Start Prometheus
133-
134-
Follow the instructions from
135-
[starting-prometheus](https://prometheus.io/docs/introduction/first_steps/#starting-prometheus)
136-
to start the Prometheus server and verify it has been started successfully.
137-
138-
Please note that we will need pass in `otel.yml` file as the argument:
139-
140-
```console
141-
./prometheus --config.file=otel.yml
135+
```sh
136+
./prometheus --enable-feature=otlp-write-receiver
142137
```
143138

144139
### View results in Prometheus
145140

146141
To use the graphical interface for viewing our metrics with Prometheus, navigate
147142
to [http://localhost:9090/graph](http://localhost:9090/graph), and type
148-
`MyFruitCounter` in the expression bar of the UI; finally, click the execute
149-
button.
143+
`MyFruitCounter_total` in the expression bar of the UI; finally, click the
144+
execute button.
150145

151146
We should be able to see the following chart from the browser:
152147

153148
![Prometheus UI](https://user-images.githubusercontent.com/17327289/151636225-6e4ce4c7-09f3-4996-8ca5-d404a88d9195.png)
154149

155-
From the legend, we can see that the `instance` name and the `job` name are the
156-
values we have set in `otel.yml`.
157-
158150
Congratulations!
159151

160152
Now we know how to configure Prometheus server and deploy OpenTelemetry
161-
`PrometheusHttpListener` to export our metrics. Next, we are going to explore a tool
153+
`OtlpExporter` to export our metrics. Next, we are going to explore a tool
162154
called Grafana, which has powerful visualizations for the metrics.
163155

164156
## Explore metrics using Grafana
@@ -191,24 +183,49 @@ Feel free to find some handy PromQL
191183
[here](https://promlabs.com/promql-cheat-sheet/).
192184

193185
In the below example, the query targets to find out what is the per-second rate
194-
of increase of myFruitCounter over the past 5 minutes:
186+
of increase of `MyFruitCounter_total` over the past 5 minutes:
195187

196188
![Grafana
197189
UI](https://user-images.githubusercontent.com/17327289/151636769-138ecb4f-b44f-477b-88eb-247fc4340252.png)
198190

191+
## Final cleanup
192+
193+
In the end, remove the Console Exporter so we only have OTLP Exporter in the
194+
final application:
195+
196+
```csharp
197+
using var meterProvider = Sdk.CreateMeterProviderBuilder()
198+
...
199+
// Remove Console Exporter from the final application
200+
// .AddConsoleExporter()
201+
.AddOtlpExporter(options =>
202+
{
203+
options.Endpoint = new Uri("http://localhost:9090/api/v1/otlp/v1/metrics");
204+
options.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
205+
})
206+
.Build();
207+
```
208+
209+
```sh
210+
dotnet remove package OpenTelemetry.Exporter.Console
211+
```
212+
199213
```mermaid
200-
graph TD
201214
202-
subgraph Prometheus
203-
PrometheusScraper
204-
PrometheusDatabase
215+
graph LR
216+
subgraph SDK
217+
MeterProvider
218+
MetricReader[BaseExportingMetricReader]
219+
OtlpExporter
220+
end
221+
222+
subgraph API
223+
Instrument["Meter(#quot;MyCompany.MyProduct.MyLibrary#quot;, #quot;1.0#quot;)<br/>Counter(#quot;MyFruitCounter#quot;)"]
205224
end
206225
207-
PrometheusHttpListener["PrometheusHttpListener<br/>(listening at #quot;http://localhost:9464/#quot;)"] -->|HTTP GET| PrometheusScraper{{"Prometheus scraper<br/>(polling #quot;http://localhost:9464/metrics#quot; every 10 seconds)"}}
208-
PrometheusScraper --> PrometheusDatabase[("Prometheus TSDB (time series database)")]
209-
PrometheusDatabase -->|http://localhost:9090/graph| PrometheusUI["Browser<br/>(Prometheus Dashboard)"]
210-
PrometheusDatabase -->|http://localhost:9090/api/| Grafana[Grafana Server]
211-
Grafana -->|http://localhost:3000/dashboard| GrafanaUI["Browser<br/>(Grafana Dashboard)"]
226+
Instrument --> | Measurements | MeterProvider
227+
228+
MeterProvider --> | Metrics | MetricReader --> | Push | OtlpExporter
212229
```
213230

214231
## Learn more
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<ItemGroup>
3-
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\OpenTelemetry.Exporter.Prometheus.HttpListener.csproj" />
3+
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Console\OpenTelemetry.Exporter.Console.csproj" />
4+
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.OpenTelemetryProtocol\OpenTelemetry.Exporter.OpenTelemetryProtocol.csproj" />
45
</ItemGroup>
56
</Project>

docs/trace/getting-started-jaeger/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ using var tracerProvider = Sdk.CreateTracerProviderBuilder()
7474
.Build();
7575
```
7676

77-
When we run the application, the `ConsoleExporter` was printing the traces on
77+
When we ran the application, the `ConsoleExporter` was printing the traces on
7878
console, and the `OtlpExporter` was attempting to send the traces to Jaeger
7979
Agent via the default endpoint `http://localhost:4317`.
8080

0 commit comments

Comments
 (0)