From 5b020fbdc556a64a71914c3fa928360d1f140ccb Mon Sep 17 00:00:00 2001 From: Edgardo Quiroga Date: Thu, 24 Oct 2024 16:13:24 -0300 Subject: [PATCH 1/2] makefile added to build the binary --- Makefile | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c801a40 --- /dev/null +++ b/Makefile @@ -0,0 +1,37 @@ +# Binary name +BINARY_NAME=aptos-metrics-exporter +# Compressed binary name +COMPRESSED_NAME=ame-linux-amd64 +# Output directories +BUILD_DIR=build +BIN_DIR=$(BUILD_DIR)/bin + +# Commands +GO_BUILD=go build +GO_GET=go get +TAR=tar +GZIP=gzip + +# Install required Go dependencies +.PHONY: deps +deps: + @echo "Installing dependencies..." + $(GO_GET) github.com/prometheus/client_golang/prometheus + $(GO_GET) github.com/prometheus/client_golang/prometheus/promhttp + +# Build binary for Linux amd64 +.PHONY: build +build: deps + @echo "Building the binary for Linux amd64..." + mkdir -p $(BIN_DIR) + GOOS=linux GOARCH=amd64 $(GO_BUILD) -o $(BIN_DIR)/$(BINARY_NAME) ./cmd/$(BINARY_NAME) + +# Compress the binary with gzip +.PHONY: compress-gzip +compress-gzip: build + @echo "Compressing the binary with gzip..." + $(TAR) -cvzf $(BUILD_DIR)/$(COMPRESSED_NAME).tar.gz -C $(BIN_DIR) $(BINARY_NAME) + +# Default target (build) +.PHONY: all +all: build From a79c51a645b7fc955a2ccbe2522e2e1f88cab479 Mon Sep 17 00:00:00 2001 From: Edgardo Quiroga Date: Thu, 24 Oct 2024 16:13:54 -0300 Subject: [PATCH 2/2] -p option added to specify a different port --- README.md | 11 +++++++---- cmd/aptos-metrics-exporter/main.go | 13 +++++++++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 13c0df4..b36e41b 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,9 @@ By default, the exporter will fetch the block height in `http://localhost:8080/v 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 + make build ``` - + You can find the built binary in the `build/bin` directory. 3. **Run the exporter**: After building the binary, run it: @@ -33,6 +31,11 @@ By default, the exporter will fetch the block height in `http://localhost:8080/v ./aptos-metrics-exporter ``` + You can specify a different port by adding the `-p` flag followed by the desired port number. For example, to run the exporter on port `9090`: + ```bash + ./aptos-metrics-exporter -p 9090 + ``` + 4. **Prometheus Configuration**: Add the following scrape job to your Prometheus configuration (`prometheus.yml`): diff --git a/cmd/aptos-metrics-exporter/main.go b/cmd/aptos-metrics-exporter/main.go index bb7253a..c06b427 100644 --- a/cmd/aptos-metrics-exporter/main.go +++ b/cmd/aptos-metrics-exporter/main.go @@ -1,6 +1,8 @@ package main import ( + "flag" + "fmt" "log" "net/http" "time" @@ -10,6 +12,12 @@ import ( ) func main() { + // -p flag for the server port. Default value: 2112 + var port int + + flag.IntVar(&port, "p", 2112, "Server port") + flag.Parse() + // Set up the HTTP handler for Prometheus metrics http.Handle("/metrics", promhttp.Handler()) @@ -24,9 +32,10 @@ func main() { // Fetch the block height initially to set the initial value before scraping starts collector.FetchLatestBlockHeight() + address := fmt.Sprintf(":%d", port) // Start the HTTP server - log.Println("Starting server on :2112") - if err := http.ListenAndServe(":2112", nil); err != nil { + log.Printf("Starting server on %s", address) + if err := http.ListenAndServe(address, nil); err != nil { log.Fatal("ListenAndServe: ", err) } }