-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ddfb43
commit 29f6f51
Showing
4 changed files
with
115 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM golang:1.22 as builder | ||
WORKDIR /app | ||
COPY go.mod go.sum ./ | ||
RUN go mod download | ||
COPY . . | ||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o swarm-batch-exporter . | ||
|
||
FROM scratch | ||
COPY --from=builder /app/swarm-batch-exporter /swarm-batch-exporter | ||
EXPOSE 1640 | ||
ENTRYPOINT ["/swarm-batch-exporter"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
build: | ||
go build -o swarm-exporter | ||
go build -o swarm-batch-exporter | ||
|
||
docker: | ||
docker build . -t swarm-batch-exporter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,126 +1,126 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"log" | ||
"math" | ||
"time" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"encoding/json" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"log" | ||
"math" | ||
"net/http" | ||
"os" | ||
"time" | ||
) | ||
|
||
type APIResponse struct { | ||
Stamps []Stamp `json:"stamps"` | ||
Stamps []Stamp `json:"stamps"` | ||
} | ||
|
||
type Stamp struct { | ||
BatchID string `json:"batchID"` | ||
Utilization int `json:"utilization"` | ||
Usable bool `json:"usable"` | ||
Label string `json:"label"` | ||
Depth int `json:"depth"` | ||
Amount string `json:"amount"` | ||
BucketDepth int `json:"bucketDepth"` | ||
BlockNumber int `json:"blockNumber"` | ||
ImmutableFlag bool `json:"immutableFlag"` | ||
Exists bool `json:"exists"` | ||
BatchTTL int `json:"batchTTL"` | ||
BatchID string `json:"batchID"` | ||
Utilization int `json:"utilization"` | ||
Usable bool `json:"usable"` | ||
Label string `json:"label"` | ||
Depth int `json:"depth"` | ||
//Amount string `json:"amount"` | ||
BucketDepth int `json:"bucketDepth"` | ||
BlockNumber int `json:"blockNumber"` | ||
ImmutableFlag bool `json:"immutableFlag"` | ||
Exists bool `json:"exists"` | ||
BatchTTL int `json:"batchTTL"` | ||
} | ||
|
||
// Declare the metrics | ||
var ( | ||
utilizationMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "swarm_stamp_batch_utilization", | ||
Help: "Stamp batch utilization.", | ||
}, []string{"batchID", "label"}) | ||
ttlMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "swarm_stamp_batch_ttl", | ||
Help: "Stamp batch TTL.", | ||
}, []string{"batchID", "label"}) | ||
depthMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "swarm_stamp_batch_depth", | ||
Help: "Stamp batch depth.", | ||
}, []string{"batchID", "label"}) | ||
amountMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "swarm_stamp_batch_amount", | ||
Help: "Stamp batch amount.", | ||
}, []string{"batchID", "label"}) | ||
capacityMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "swarm_stamp_batch_capacity_bytes", | ||
Help: "Stamp batch total capacity in bytes.", | ||
}, []string{"batchID", "label"}) | ||
availabilityMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "swarm_stamp_batch_available_bytes", | ||
Help: "Stamp batch available capacity in bytes.", | ||
}, []string{"batchID", "label"}) | ||
usageMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "swarm_stamp_batch_usage_percentage", | ||
Help: "Stamp batch usage percentage.", | ||
}, []string{"batchID", "label"}) | ||
utilizationMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "swarm_stamp_batch_utilization", | ||
Help: "Stamp batch utilization.", | ||
}, []string{"batchID", "label"}) | ||
ttlMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "swarm_stamp_batch_ttl", | ||
Help: "Stamp batch TTL.", | ||
}, []string{"batchID", "label"}) | ||
depthMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "swarm_stamp_batch_depth", | ||
Help: "Stamp batch depth.", | ||
}, []string{"batchID", "label"}) | ||
// amountMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
// Name: "swarm_stamp_batch_amount", | ||
// Help: "Stamp batch amount.", | ||
// }, []string{"batchID", "label"}) | ||
capacityMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "swarm_stamp_batch_capacity_bytes", | ||
Help: "Stamp batch total capacity in bytes.", | ||
}, []string{"batchID", "label"}) | ||
availabilityMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "swarm_stamp_batch_available_bytes", | ||
Help: "Stamp batch available capacity in bytes.", | ||
}, []string{"batchID", "label"}) | ||
usageMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "swarm_stamp_batch_usage_percentage", | ||
Help: "Stamp batch usage percentage.", | ||
}, []string{"batchID", "label"}) | ||
) | ||
|
||
func init() { | ||
prometheus.MustRegister(utilizationMetric) | ||
prometheus.MustRegister(ttlMetric) | ||
prometheus.MustRegister(depthMetric) | ||
prometheus.MustRegister(amountMetric) | ||
prometheus.MustRegister(capacityMetric) | ||
prometheus.MustRegister(availabilityMetric) | ||
prometheus.MustRegister(usageMetric) | ||
prometheus.MustRegister(utilizationMetric) | ||
prometheus.MustRegister(ttlMetric) | ||
prometheus.MustRegister(depthMetric) | ||
// prometheus.MustRegister(amountMetric) | ||
prometheus.MustRegister(capacityMetric) | ||
prometheus.MustRegister(availabilityMetric) | ||
prometheus.MustRegister(usageMetric) | ||
} | ||
|
||
func GetStampMaximumCapacityBytes(depth int) float64 { | ||
return math.Pow(2, float64(depth)) * 4096 | ||
return math.Pow(2, float64(depth)) * 4096 | ||
} | ||
|
||
func GetStampUsage(utilization, depth, bucketDepth int) float64 { | ||
return float64(utilization) / math.Pow(2, float64(depth-bucketDepth)) | ||
return float64(utilization) / math.Pow(2, float64(depth-bucketDepth)) | ||
|
||
} | ||
|
||
func fetchMetrics() { | ||
resp, err := http.Get("http://localhost:1633/stamps") | ||
if err != nil { | ||
log.Println("Error fetching data:", err) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
var apiResponse APIResponse | ||
if err := json.NewDecoder(resp.Body).Decode(&apiResponse); err != nil { | ||
log.Println("Error decoding JSON data:", err) | ||
return | ||
} | ||
|
||
// Update Prometheus metrics | ||
for _, stamp := range apiResponse.Stamps { | ||
utilizationMetric.With(prometheus.Labels{"batchID": stamp.BatchID, "label": stamp.Label}).Set(float64(stamp.Utilization)) | ||
ttlMetric.With(prometheus.Labels{"batchID": stamp.BatchID, "label": stamp.Label}).Set(float64(stamp.BatchTTL)) | ||
depthMetric.With(prometheus.Labels{"batchID": stamp.BatchID, "label": stamp.Label}).Set(float64(stamp.Depth)) | ||
//amountMetric.With(prometheus.Labels{"batchID": stamp.BatchID, "label": stamp.Label}).Set(float64(stamp.Amount)) | ||
url := os.Getenv("BEE_ENDPOINT") | ||
if url == "" { | ||
url = "http://localhost:1633/stamps" | ||
} | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
log.Println("Error fetching data:", err) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
var apiResponse APIResponse | ||
if err := json.NewDecoder(resp.Body).Decode(&apiResponse); err != nil { | ||
log.Println("Error decoding JSON data:", err) | ||
return | ||
} | ||
|
||
for _, stamp := range apiResponse.Stamps { | ||
utilizationMetric.With(prometheus.Labels{"batchID": stamp.BatchID, "label": stamp.Label}).Set(float64(stamp.Utilization)) | ||
ttlMetric.With(prometheus.Labels{"batchID": stamp.BatchID, "label": stamp.Label}).Set(float64(stamp.BatchTTL)) | ||
depthMetric.With(prometheus.Labels{"batchID": stamp.BatchID, "label": stamp.Label}).Set(float64(stamp.Depth)) | ||
//amountMetric.With(prometheus.Labels{"batchID": stamp.BatchID, "label": stamp.Label}).Set(float64(stamp.Amount)) | ||
|
||
capacity := GetStampMaximumCapacityBytes(stamp.Depth) | ||
capacityMetric.With(prometheus.Labels{"batchID": stamp.BatchID, "label": stamp.Label}).Set(capacity) | ||
capacityMetric.With(prometheus.Labels{"batchID": stamp.BatchID, "label": stamp.Label}).Set(capacity) | ||
|
||
stampUsage := GetStampUsage(stamp.Utilization, stamp.Depth, stamp.BucketDepth) | ||
usageMetric.With(prometheus.Labels{"batchID": stamp.BatchID, "label": stamp.Label}).Set(stampUsage) | ||
usageMetric.With(prometheus.Labels{"batchID": stamp.BatchID, "label": stamp.Label}).Set(stampUsage) | ||
|
||
available := int(stampUsage * capacity) | ||
availabilityMetric.With(prometheus.Labels{"batchID": stamp.BatchID, "label": stamp.Label}).Set(float64(available)) | ||
} | ||
availabilityMetric.With(prometheus.Labels{"batchID": stamp.BatchID, "label": stamp.Label}).Set(float64(available)) | ||
} | ||
} | ||
|
||
func main() { | ||
// Setup fetchMetrics to run periodically | ||
go func() { | ||
for { | ||
fetchMetrics() | ||
time.Sleep(5 * time.Minute) // Adjust fetch interval as needed | ||
} | ||
}() | ||
|
||
// Setup HTTP server | ||
http.Handle("/metrics", promhttp.Handler()) | ||
log.Fatal(http.ListenAndServe(":1640", nil)) | ||
go func() { | ||
for { | ||
fetchMetrics() | ||
time.Sleep(5 * time.Minute) | ||
} | ||
}() | ||
http.Handle("/metrics", promhttp.Handler()) | ||
log.Fatal(http.ListenAndServe(":1640", nil)) | ||
} |