Skip to content

Commit

Permalink
aptos_latest_block_height metric added.
Browse files Browse the repository at this point in the history
  • Loading branch information
qedgardo committed Oct 24, 2024
1 parent 68590fc commit c6d962a
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 1 deletion.
73 changes: 72 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,72 @@
# aptos-fullnode-metrics
# Aptos Metrics Exporter

Atm **Aptos Metrics Exporter** is a Prometheus exporter that collects and exposes the latest block height in a prometheus format metric from an Aptos blockchain node.
By default, the exporter will fetch the block height in `http://localhost:8080/v1` and start an HTTP server on port `2112` to expose the metric.

## Prerequisites

- **Go**: Make sure you have Go installed to build the project.
- **Prometheus**: The exporter is designed to be scraped by a Prometheus instance.

## Installation

1. **Clone the repository**:

```bash
git clone https://github.com/qedgardo/aptos-metrics-exporter.git
cd aptos-metrics-exporter
```

2. **Build the binary**:

```bash
go get github.com/prometheus/client_golang/prometheus
go get github.com/prometheus/client_golang/prometheus/promhttp
go build -o aptos-metrics-exporter ./cmd/aptos-metrics-exporter
```

3. **Run the exporter**:

After building the binary, run it:

```bash
./aptos-metrics-exporter
```

4. **Prometheus Configuration**:

Add the following scrape job to your Prometheus configuration (`prometheus.yml`):

```yaml
scrape_configs:
- job_name: 'aptos-metrics-exporter'
static_configs:
- targets: ['localhost:2112']
```

Make sure to replace `localhost` with the actual IP address or domain where the exporter is running if it's on a remote machine.
## Usage
Once the exporter is running, it will expose metrics at the `/metrics` endpoint. Prometheus can then scrape this endpoint at regular intervals to collect metrics.
### Exposed Metrics
- **`aptos_latest_block_height`**: The current/latest block height of the Aptos blockchain node.
Example output from `/metrics`:
```
# HELP aptos_latest_block_height The latest block height of the Aptos blockchain.
# TYPE aptos_latest_block_height gauge
aptos_latest_block_height 28832207
```
### Testing
To test the project locally, you can use `curl` to query the `/metrics` endpoint:
```bash
curl http://localhost:2112/metrics
```
32 changes: 32 additions & 0 deletions cmd/aptos-metrics-exporter/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"log"
"net/http"
"time"

"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/qedgardo/aptos-metrics-exporter/internal/collector"
)

func main() {
// Set up the HTTP handler for Prometheus metrics
http.Handle("/metrics", promhttp.Handler())

// Create a ticker that will call FetchLatestBlockHeight every 5 seconds
ticker := time.NewTicker(5 * time.Second)
go func() {
for range ticker.C {
collector.FetchLatestBlockHeight()
}
}()

// Fetch the block height initially to set the initial value before scraping starts
collector.FetchLatestBlockHeight()

// Start the HTTP server
log.Println("Starting server on :2112")
if err := http.ListenAndServe(":2112", nil); err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
16 changes: 16 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/qedgardo/aptos-metrics-exporter

go 1.23.2

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
golang.org/x/sys v0.22.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc=
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
50 changes: 50 additions & 0 deletions internal/collector/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package collector

import (
"encoding/json"
"log"
"net/http"
"strconv"

"github.com/prometheus/client_golang/prometheus"
)

var (
aptosLatestBlockHeight = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "aptos_latest_block_height",
Help: "The latest block height of the Aptos blockchain.",
})
)

func init() {
prometheus.MustRegister(aptosLatestBlockHeight)
}

// FetchLatestBlockHeight retrieves the latest block height from the Aptos node.
func FetchLatestBlockHeight() {
resp, err := http.Get("http://localhost:8080/v1")
if err != nil {
log.Println("Error fetching latest block height:", err)
return
}
defer resp.Body.Close()

var result struct {
BlockHeight string `json:"block_height"`
}

if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
log.Println("Error decoding JSON:", err)
return
}

// Convert the block height string to int64 to avoid scientific notation
blockHeight, err := strconv.ParseInt(result.BlockHeight, 10, 64)
if err != nil {
log.Println("Error converting block height to int64:", err)
return
}

// Set the block height in the gauge as a float64, but still as an integer value
aptosLatestBlockHeight.Set(float64(blockHeight))
}

0 comments on commit c6d962a

Please sign in to comment.