Skip to content

Commit

Permalink
feat: new metrics added
Browse files Browse the repository at this point in the history
Signed-off-by: Jose Ramon Mañes <[email protected]>
  • Loading branch information
tty47 committed Aug 11, 2023
1 parent 96ac7cc commit f123228
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.20.3-bullseye AS builder
FROM golang:1.21.0-bullseye AS builder
WORKDIR /
COPY go.mod go.sum ./
# Download dependencies
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile_local
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.20.3-bullseye AS builder
FROM golang:1.21.0-bullseye AS builder
WORKDIR /
COPY go.mod go.sum ./
# Download dependencies
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type MutualPeersConfig struct {

// MutualPeer represents a mutual peer structure.
type MutualPeer struct {
ConsensusNode string `yaml:"consensusNode,omitempty"`
// List of peers.
Peers []Peer `yaml:"peers"`
TrustedPeersPath string `yaml:"trustedPeersPath,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions deployment/overlays/local/config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mutualPeers:
- consensusNode: "consensus-validator-1"
- peers:
- nodeName: "da-bridge-1-0"
containerName: "da"
Expand Down
16 changes: 16 additions & 0 deletions pkg/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ func Run(cfg config.MutualPeersConfig) {
return
}

// Get the genesisHash
// check if the config has the consensusNode field defined
if cfg.MutualPeers[0].ConsensusNode != "" {
blockHash, earliestBlockTime := k8s.GenesisHash(cfg)
err = metrics.WithMetricsBlockHeight(
blockHash,
earliestBlockTime,
cfg.MutualPeers[0].ConsensusNode,
os.Getenv("POD_NAMESPACE"),
)
if err != nil {
log.Errorf("Error registering metric block_height_1: %v", err)
return
}
}

// Create the server
server := &http.Server{
Addr: ":" + httpPort,
Expand Down
51 changes: 51 additions & 0 deletions pkg/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package k8s
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"sync"

"github.com/jrmanes/torch/config"
Expand Down Expand Up @@ -274,6 +277,54 @@ func BulkTrustedPeers(pods config.MutualPeer) {
}
}

// GenesisHash
func GenesisHash(pods config.MutualPeersConfig) (string, string) {
consensusNode := pods.MutualPeers[0].ConsensusNode
c := exec.Command("wget", "-q", "-O", "-", fmt.Sprintf("http://%s:26657/block?height=1", consensusNode))

// Create a buffer to capture the command's output
var outputBuffer bytes.Buffer
c.Stdout = &outputBuffer

// Run the command
err := c.Run()
if err != nil {
log.Error("Error:", err)
return "", ""
}

// Convert the output buffer to a string
outputString := outputBuffer.String()

// Parse the JSON response into a generic map
var response map[string]interface{}
err = json.Unmarshal([]byte(outputString), &response)
if err != nil {
log.Error("Error parsing JSON:", err)
return "", ""
}

// Access and print the .block_id.hash field
blockIDHash, ok := response["result"].(map[string]interface{})["block_id"].(map[string]interface{})["hash"].(string)
if !ok {
log.Error("Unable to access .block_id.hash")
return "", ""
}

// Access and print the .block.header.time field
blockTime, ok := response["result"].(map[string]interface{})["block"].(map[string]interface{})["header"].(map[string]interface{})["time"].(string)
if !ok {
log.Error("Unable to access .block.header.time")
return "", ""
}

log.Info("Block ID Hash: ", blockIDHash)
log.Info("Block Time: ", blockTime)
log.Info("Full output: ", outputString)

return blockIDHash, blockTime
}

// RunRemoteCommand executes a remote command on the specified node.
func RunRemoteCommand(nodeName, container, namespace string, command []string) (string, error) {
clusterConfig, err := rest.InClusterConfig()
Expand Down
60 changes: 59 additions & 1 deletion pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package metrics

import (
"context"
"fmt"
"time"

log "github.com/sirupsen/logrus"

Expand All @@ -11,7 +13,7 @@ import (
)

// Get the meter from the global meter provider with the name "torch".
var meter = otel.GetMeterProvider().Meter("multiaddr")
var meter = otel.GetMeterProvider().Meter("torch")

// MultiAddrs represents the information for a multiaddress.
type MultiAddrs struct {
Expand Down Expand Up @@ -56,3 +58,59 @@ func WithMetricsMultiAddress(multiAddrs []MultiAddrs) error {
_, err = meter.RegisterCallback(callback, multiAddressesGauge)
return err
}

// BlockHeight represents the information for the block height 1.
type BlockHeight struct {
ServiceName string // ServiceName Name of the service associated with the multiaddress.
BlockHeight string // Namespace where the service is deployed.
Value float64 // Value to be observed for the multiaddress.
}

// WithMetricsBlockHeight creates a callback function to observe metrics for block_height_1.
// consensus-node:26657/block?height=1
func WithMetricsBlockHeight(blockHeight, earliestBlockTime, serviceName, namespace string) error {
log.Info("registering metric: ", blockHeight)
// Create a Float64ObservableGauge named "block_height_1" with a description for the metric.
blockHeightGauge, err := meter.Float64ObservableGauge(
"block_height_1",
metric.WithDescription("Torch - BlockHeight"),
)
if err != nil {
log.Fatalf(err.Error())
return err
}
callback := func(ctx context.Context, observer metric.Observer) error {
// Define the callback function that will be called periodically to observe metrics.
// Create labels with attributes for each block_height_1.
labels := metric.WithAttributes(
attribute.String("service_name", serviceName),
attribute.String("block_height_1", blockHeight),
attribute.String("earliest_block_time", earliestBlockTime),
attribute.Int("days_running", CalculateDaysDifference(earliestBlockTime)),
attribute.String("namespace", namespace),
)
// Observe the float64 value for the current block_height_1 with the associated labels.
observer.ObserveFloat64(blockHeightGauge, 1, labels)

return nil
}

// Register the callback with the meter and the Float64ObservableGauge.
_, err = meter.RegisterCallback(callback, blockHeightGauge)
return err
}

func CalculateDaysDifference(inputTimeString string) int {
layout := "2006-01-02T15:04:05.999999999Z"
inputTime, err := time.Parse(layout, inputTimeString)
if err != nil {
fmt.Println("Error parsing time:", err)
return -1
}

currentTime := time.Now()
timeDifference := currentTime.Sub(inputTime)
daysDifference := int(timeDifference.Hours() / 24)

return daysDifference
}

0 comments on commit f123228

Please sign in to comment.