First of all, my journey on this realm started with metrics-collection.
So this is my .Net code:
public class Prometheus
{
static Meter s_meter = new Meter("HatCo.HatStore", "1.0.0");
static Counter<int> s_hatsSold = s_meter.CreateCounter<int>(name: "hats_sold",
unit: "Hats",
description: "The number of hats sold in our store");
public void Run()
{
Random rnd = new Random();
using (var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter("HatCo.HatStore")
.AddPrometheusExporter(opt =>
{
opt.StartHttpListener = true;
opt.HttpListenerPrefixes = new string[] { $"http://127.0.0.1:9185" };
})
.Build())
{
Console.WriteLine("Press any key to exit");
while (!Console.KeyAvailable)
{
// Pretend our store has a transaction each second that sells 4 hats
Thread.Sleep(rnd.Next(200, 1000));
s_hatsSold.Add(rnd.Next(1, 20));
}
}
}
Main method contains only these couple of lines:
Prometheus prometheus = new Prometheus();
prometheus.Run();
Some very important thing to note here: http://127.0.0.1:9185 instead of http://localhost:9185.
And yes, I have changed the port also from 9184 to 9185, for some reason that was taken on my local host.
When configuring Prometheus in Docker, we will have to access this endpoint which is hosted on my localhost address.There was no way to access localhost inside the docker container and so, having the ip specified explicitly made it work as expected.
Some more details about this you can find here: https://www.cloudsavvyit.com/14114/how-to-connect-to-localhost-within-a-docker-container/
Check our exporter here:
Everything is fine until now. Let's try to configure Prometheus.
prometheus.yml will contain:
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
- job_name: 'OpenTelemetryTest'
scrape_interval: 5s # poll very quickly for a more responsive demo
static_configs:
- targets: ["host.docker.internal:9185"]
host.docker.internal did the trick to access back the localhost.
You may find it configured in your C:\Windows\System32\drivers\etc\hosts
# localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
::1 localhost
# Added by Docker Desktop
192.168.0.208 host.docker.internal
Spawn a Powershell window after you have defined prometheus.yml file and create the docker container.
docker run -d --name=prometheus -p 9090:9090 -v ${PWD}/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
We are now able to read the hats count.
Next is Grafana:
docker run -d --name=grafana --link prometheus -p 3000:3000 grafana/grafana
When configuring Grafana, login with user admin and password admin.
Configure your data source as prometheus and set the URL as depicted here:
You may choose that or : http://host.docker.internal:9090.
Don't forget, host.docker.internal is our local host ip.
There are plenty of tutorials on Youtube on how to use Grafana.
The entire flow should work by now, starting with the .net exporter and visualize data in Grafana.