This is a demo repo to show you how to integrate dotnet APIs into Grafana.
In this project, we send logs (using serilog) to Loki and OpenTelemetry metrics to Promethus. Between our API and Prometheus, we have something called a collector. It is in charge of receiving the metrics and traces, deciding how to process them and where to send them.
- Dashboards - Holds all of the dashboards for Grafana.
- Grafana - Holds all of the configurations and docker stuff for standing up Grafana with Loki and Prometheus.
- TodoApi - This is our sample API.
You will need dotnet SDK 8 or higher. You will also need Docker installed and Docker Desktop running. I use VS Code to view this project and run the API. I recommend you use the same.
- Run the Docker Compose file for Grafana
cd grafana
docker compose up- Run the API
cd TodoApi
dotnet build
dotnet runAlternatively you can use the run & debug menu in VSCode.
- Follow the steps in the previous section
- Go to the Grafana page
- Enter your default credentials
username: admin password: admin
- You will be prompted to update your password. Do so.
- After the API starts up, go to the swagger page.
- Run a few endpoints
- Go back to Grafana and go to the explore tab.
- Click on the labels dropdown and select any of the suggested labels.
- Do the same for the value dropdown.
- Click the blue Run Query button and you will see your logs!
- Now go to the dashboards tab
- Click the blue
Create Dashboardbutton - Click
Import a dashboard
- If prompted that you have unsaved changes, click Discard
- Paste the json from this file,
./dashboards/ASPNETCoreDashboard.json - Click Load, then Import.
- There are your first metrics!
- Go back to the dashboards tab
- Click the new dropdown and click import
- Add the other dashboard json,
./dashboards/ASPNETCoreEndpointsDashboard.json - Click Load then Import
- Go back to the dashboards tab
- Open up the dashboard,
ASP.NET Core - Now any endpoints that show up in the bottom 3 tables, you can click and you will be redirected to the dashboard we set up in step 19.
- To stop the API, just press
ctrl + c - To stop the docker containers, just press
ctrl + c
I purposely left a few folders out of source control.
./grafana/grafana/./grafana/prometheus/./grafana/loki/
You may want to include these so that your grafana config, metrics, and logs all are saved into source control. I did not need to do that for this demo project.
In the steps above, I ran the docker containers using docker compose up. You may choose to run the in detached mode by using docker compose up -d. If you do so, in order to stop the containers, just run docker compose down.
If you want to put this somewhere like on a VPS or Droplet, you definitely can! Here are a few things to keep in mind though.
- This is an unsecured logging server. So anyone with the loki or prometheus endpoints can flood your servers. Very unlikely but you never know.
- The way I am sending logs to Loki is through an http endpoint. So all you need to do is set up a domain with ProxyPass to
http://127.0.0.1:3100. I also turn off all CORS policies. - The way metrics and traces are sent are through gRPC, not HTTP. So you don't need a virtual host. Instead, you should just open up a port for
4317. - If you do stand this stuff up in a VPS somewhere, remember to update the endpoints in your API project. Loki can point to a domain but Prometheus needs to point to an IP address with the port.
Grafana provides so many dashboards. You can find more here. The one I used in this demo project is a modified version of this dashboard.