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
2323dotnet 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
6085graph LR
6186
6287subgraph SDK
6388 MeterProvider
6489 MetricReader[BaseExportingMetricReader]
65- PrometheusHttpListener["PrometheusHttpListener<br/>(http://localhost:9464/)"]
90+ MetricReader2[BaseExportingMetricReader]
91+ ConsoleExporter
92+ OtlpExporter
6693end
6794
6895subgraph API
7198
7299Instrument --> | Measurements | MeterProvider
73100
74- MeterProvider --> | Metrics | MetricReader --> | Pull | PrometheusHttpListener
101+ MeterProvider --> | Metrics | MetricReader --> | Push | OtlpExporter
102+
103+ MeterProvider --> | Metrics | MetricReader2 --> | Push | ConsoleExporter
75104```
76105
77106Also, 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+
109127Follow the [ first steps] ( https://prometheus.io/docs/introduction/first_steps/ )
110128to download the [ latest release] ( https://prometheus.io/download/ ) of Prometheus.
111129
112- ### Configuration
113-
114130After 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
146141To use the graphical interface for viewing our metrics with Prometheus, navigate
147142to [ 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
151146We 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-
158150Congratulations!
159151
160152Now 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
162154called 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
193185In 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
197189UI] ( 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;)"]
205224end
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
0 commit comments