Skip to content

Commit

Permalink
Merge pull request #2 from qedgardo/feat/custom-port
Browse files Browse the repository at this point in the history
Feat: custom port
  • Loading branch information
qedgardo authored Oct 24, 2024
2 parents c6d962a + a79c51a commit 44d061e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
37 changes: 37 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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`):
Expand Down
13 changes: 11 additions & 2 deletions cmd/aptos-metrics-exporter/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"flag"
"fmt"
"log"
"net/http"
"time"
Expand All @@ -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())

Expand All @@ -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)
}
}

0 comments on commit 44d061e

Please sign in to comment.