Skip to content

jaaywags/grafana-dotnet-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

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.

Folders

  • 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.

Running the project

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.

  1. Run the Docker Compose file for Grafana
cd grafana
docker compose up
  1. Run the API
cd TodoApi
dotnet build
dotnet run

Alternatively you can use the run & debug menu in VSCode.

Creating and Viewing Logs and Metrics

  1. Follow the steps in the previous section
  2. Go to the Grafana page
  3. Enter your default credentials

username: admin password: admin

  1. You will be prompted to update your password. Do so.
  2. After the API starts up, go to the swagger page.
  3. Run a few endpoints
  4. Go back to Grafana and go to the explore tab.
  5. Click on the labels dropdown and select any of the suggested labels.
  6. Do the same for the value dropdown.
  7. Click the blue Run Query button and you will see your logs!
  8. Now go to the dashboards tab
  9. Click the blue Create Dashboard button
  10. Click Import a dashboard
  • If prompted that you have unsaved changes, click Discard
  1. Paste the json from this file, ./dashboards/ASPNETCoreDashboard.json
  2. Click Load, then Import.
  3. There are your first metrics!
  4. Go back to the dashboards tab
  5. Click the new dropdown and click import
  6. Add the other dashboard json, ./dashboards/ASPNETCoreEndpointsDashboard.json
  7. Click Load then Import
  8. Go back to the dashboards tab
  9. Open up the dashboard, ASP.NET Core
  10. 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.
  11. To stop the API, just press ctrl + c
  12. To stop the docker containers, just press ctrl + c

Notes

Source Control

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.

Running the Docker Containers

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.

Setting up a Remote Server

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.

Other Dashboards

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages