Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pushgateway additional metrics #71

Merged
merged 39 commits into from
Jan 28, 2025
Merged
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
1186963
add gas wallet balance to pushgateway
d-fda Jan 23, 2025
4a134b5
chore: run go mod tidy
d-fda Jan 23, 2025
03e9d89
bug fixes
d-fda Jan 23, 2025
d598d73
add last update time to metrics pushed
d-fda Jan 26, 2025
cd2b84a
add last update time to collector
d-fda Jan 26, 2025
f3e513f
collector typo
d-fda Jan 26, 2025
6bcd617
improve metric error logging
d-fda Jan 27, 2025
5c741a1
Revert "improve metric error logging"
d-fda Jan 27, 2025
b38c66e
Revert "collector typo"
d-fda Jan 27, 2025
eadf181
Revert "add last update time to collector"
d-fda Jan 27, 2025
a667cf8
Revert "add last update time to metrics pushed"
d-fda Jan 27, 2025
06c1fb9
add last update time metric
d-fda Jan 27, 2025
21c24b1
fetch last update time from log txn receipt
d-fda Jan 27, 2025
02eca4d
debug logging
d-fda Jan 27, 2025
23bd2a5
get block from blockhash
d-fda Jan 27, 2025
c67677b
add buffer for confirmed blocks
d-fda Jan 27, 2025
2b383d4
get block header instead of block
d-fda Jan 28, 2025
262a8d4
Revert "add buffer for confirmed blocks"
d-fda Jan 28, 2025
b2f3fd3
remove debug logging
d-fda Jan 28, 2025
dda1476
add gas wallet balance to pushgateway
d-fda Jan 23, 2025
2994402
chore: run go mod tidy
d-fda Jan 23, 2025
df1a291
bug fixes
d-fda Jan 23, 2025
8cc6219
add last update time to metrics pushed
d-fda Jan 26, 2025
7c6a9e9
add last update time to collector
d-fda Jan 26, 2025
9f5d84e
collector typo
d-fda Jan 26, 2025
647cc05
improve metric error logging
d-fda Jan 27, 2025
6becb77
Revert "improve metric error logging"
d-fda Jan 27, 2025
5002889
Revert "collector typo"
d-fda Jan 27, 2025
400032d
Revert "add last update time to collector"
d-fda Jan 27, 2025
5200298
Revert "add last update time to metrics pushed"
d-fda Jan 27, 2025
3ae9f99
add last update time metric
d-fda Jan 27, 2025
58ee858
fetch last update time from log txn receipt
d-fda Jan 27, 2025
f755ee7
debug logging
d-fda Jan 27, 2025
90ecd83
get block from blockhash
d-fda Jan 27, 2025
2bac7e1
add buffer for confirmed blocks
d-fda Jan 27, 2025
6190877
get block header instead of block
d-fda Jan 28, 2025
dae584f
Revert "add buffer for confirmed blocks"
d-fda Jan 28, 2025
fcb4456
remove debug logging
d-fda Jan 28, 2025
d9139d8
Merge branch 'pushgateway-additional-metrics' of https://github.com/d…
d-fda Jan 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
bug fixes
d-fda committed Jan 28, 2025
commit df1a291d1a74539ad7a7be77c3f84cf40f2b93a1
21 changes: 10 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"context"
"flag"
"fmt"
"math"
"math/big"
"os"
@@ -19,6 +21,7 @@ import (
diaOracleV2MultiupdateService "github.com/diadata-org/diadata/pkg/dia/scraper/blockchain-scrapers/blockchains/ethereum/diaOracleV2MultiupdateService"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/crypto"
"crypto/ecdsa"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"
@@ -129,25 +132,21 @@ func NewMetrics(reg prometheus.Registerer, pushGatewayURL, jobName, authUser, au
return m
}

func getAddressBalance(client *ethclient.Client, privateKeyHex string) (float64, error) {
privateKey, err := crypto.HexToECDSA(privateKeyHex)
if err != nil {
return 0, log.Errorf("Failed to parse private key: %w", err)
}
func getAddressBalance(client *ethclient.Client, privateKey *ecdsa.PrivateKey) (float64, error) {

publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return 0, log.Errorf("Failed to cast public key to ECDSA")
return math.NaN(), fmt.Errorf("Failed to cast public key to ECDSA")
}

address := crypto.PubkeyToAddress(*publicKeyECDSA)
balance, err := client.BalanceAt(context.Background(), address, nil)
if err != nil {
return math.Nan(), log.Errorf("Failed to get balance: %w", err)
return math.NaN(), fmt.Errorf("Failed to get balance: %w", err)
}

balanceInDIA := new(big.Float).Quo(new(big.Float).SetInt(balance), big.NewFloat(1e18)).Float64()
balanceInDIA, _ := new(big.Float).Quo(new(big.Float).SetInt(balance), big.NewFloat(1e18)).Float64()
return balanceInDIA, nil
}

@@ -222,6 +221,7 @@ func main() {
startTime := time.Now()

// Initialize feeder env variables
deployedContract := utils.Getenv("DEPLOYED_CONTRACT", "")
privateKeyHex := utils.Getenv("PRIVATE_KEY", "")
blockchainNode := utils.Getenv("BLOCKCHAIN_NODE", "https://testnet-rpc.diadata.org")
backupNode := utils.Getenv("BACKUP_NODE", "https://testnet-rpc.diadata.org")
@@ -262,8 +262,7 @@ func main() {
log.Fatalf("Failed to Deploy or Bind primary and backup contract: %v", err)
}

// Get deployed contract and set the metric
deployedContract := utils.Getenv("DEPLOYED_CONTRACT", "")

// Set the static contract label for Prometheus monitoring
m.contract.WithLabelValues(deployedContract).Set(1) // The value is arbitrary; the label holds the address

@@ -318,7 +317,7 @@ func main() {
m.cpuUsage.Set(avgCPUUsage) // Update the metric with the smoothed value
}

gas_balance, err := getAddressBalance(conn, privateKeyHex)
gas_balance, err := getAddressBalance(conn, privateKey)
if err != nil {
log.Errorf("Failed to fetch address balance: %v", err)
} else {