cryptocurrency-prices-grafana
Fetching Cryptocurrency Prices from Coingecko and Displaying them on Grafana
About
This stack consists of:
- Prometheus (timeseries database)
- Pushgateway (introduces a /metrics endpoint for prometheus to scrape)
- Grafana (the best)
- Redis (k/v in memory store)
- Python Script that pulls crypto market data, publishes to pushgateway for prometheus to scrape, and stores market data in Redis
- Flask API to fetch market prices for other projects
I'm using coingecko's api which gives me 50 free calls per minute.
Walkthrough
Clone the repo:
git clone https://github.com/ruanbekker/cryptocurrency-prices-grafana
Change to the directory and build/start the containers:
cd cryptocurrency-prices-grafana
docker-compose up -d --build
Accessing Grafana, for me its locally on http://grafana.127.0.0.1.nip.io:3000
Using username admin and password admin.
Accessing Prometheus on http://prometheus.127.0.0.1.nip.io:9090, we can see we can fetch our metrics using promql:
cryptocurrency_price{provider="coingecko"}
When we access Pushgateway on http://pushgateway.127.0.0.1.nip.io:9091, we can see that prometheus is scraping pushgateway correctly:
When we create a Grafana Dashboard:
- Query:
cryptocurrency_price{provider="coingecko"}
- Legend:
{{coin}}
To add dashboard variables: Settings -> Variables -> Add New Variable
Head back to the dashboard and update your query to include the coin that you select from the dropdown at the top, which will be referenced as the variable:
cryptocurrency_price{provider="coingecko", coin=~"$coin"}
Which will introduce this:
Then it should look like this:
After some customization:
Coingecko allows 50 calls a minute, so for my own use-case I am fetching the data once a minute and store it in the redis cache, so that the api fetches the data from cache, so I can call my api as much as I want, the only downside is that the data will be a minute old.
We can see from redis that our data was cached:
docker exec -it rates-cache sh -c "redis-cli -n 1 keys \*"
1) "MATIC_TO_USD"
2) "LINK_TO_USD"
3) "DOGE_TO_USD"
4) "XRP_TO_USD"
5) "ADA_TO_USD"
6) "VET_TO_USD"
7) "TRX_TO_USD"
8) "ETH_TO_USD"
9) "BTC_TO_USD"
And to get the value of BTC in USD:
docker exec -it rates-cache sh -c "redis-cli -n 1 get BTC_TO_USD"
"49272"
Our Flask API uses the redis cache to fetch the values, and can be accessed like this:
curl http://api.127.0.0.1.nip.io:5000/coins/btc
{"acronymm":"BTC","current_price_in_usd":"49533"}
curl http://api.127.0.0.1.nip.io:5000/coins/matic
{"acronymm":"MATIC","current_price_in_usd":"2.32"}