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

feat: revert telemetry server changes #2325

Merged
merged 8 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* [2305](https://github.com/zeta-chain/node/pull/2305) - add new messages `MsgAddAuthorization` and `MsgRemoveAuthorization` that can be used to update the authorization list
* [2313](https://github.com/zeta-chain/node/pull/2313) - add `CheckAuthorization` function to replace the `IsAuthorized` function. The new function uses the authorization list to verify the signer's authorization.
* [2312](https://github.com/zeta-chain/node/pull/2312) - add queries `ShowAuthorization` and `ListAuthorizations`
* [2325](https://github.com/zeta-chain/node/pull/2325) - revert telemetry server changes

### Refactor

Expand Down
5 changes: 3 additions & 2 deletions zetaclient/chains/bitcoin/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@

func (ob *Observer) SetLastBlockHeightScanned(height int64) {
atomic.StoreInt64(&ob.lastBlockScanned, height)
metrics.LastScannedBlockNumber.WithLabelValues(ob.chain.ChainName.String()).Set(float64(height))
// #nosec G701 checked as positive
ob.ts.SetLastScannedBlockNumber(ob.chain, uint64(height))

Check warning on line 360 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L360

Added line #L360 was not covered by tests
}

func (ob *Observer) GetLastBlockHeightScanned() int64 {
Expand Down Expand Up @@ -594,7 +595,7 @@
}

ob.Mu.Lock()
metrics.NumberOfUTXO.Set(float64(len(utxosFiltered)))
ob.ts.SetNumberOfUTXOs(len(utxosFiltered))

Check warning on line 598 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L598

Added line #L598 was not covered by tests
ob.utxos = utxosFiltered
ob.Mu.Unlock()
return nil
Expand Down
2 changes: 1 addition & 1 deletion zetaclient/chains/evm/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@
// SetLastBlockHeightScanned set last block height scanned (not necessarily caught up with external block; could be slow/paused)
func (ob *Observer) SetLastBlockHeightScanned(height uint64) {
atomic.StoreUint64(&ob.lastBlockScanned, height)
metrics.LastScannedBlockNumber.WithLabelValues(ob.chain.ChainName.String()).Set(float64(height))
ob.ts.SetLastScannedBlockNumber(ob.chain, height)

Check warning on line 425 in zetaclient/chains/evm/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/observer.go#L425

Added line #L425 was not covered by tests
}

// GetLastBlockHeightScanned get last block height scanned (not necessarily caught up with external block; could be slow/paused)
Expand Down
128 changes: 119 additions & 9 deletions zetaclient/metrics/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
Expand All @@ -11,23 +12,33 @@
"github.com/gorilla/mux"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"

"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/pkg/constant"
"github.com/zeta-chain/zetacore/zetaclient/types"
)

// TelemetryServer provide http endpoint for Tss server
// TelemetryServer provides http endpoint for Tss server
type TelemetryServer struct {
logger zerolog.Logger
s *http.Server
p2pid string
mu sync.Mutex
ipAddress string
HotKeyBurnRate *BurnRate
logger zerolog.Logger
s *http.Server
p2pid string
lastScannedBlockNumber map[int64]uint64 // chainID => block number
lastCoreBlockNumber int64
mu sync.Mutex
lastStartTimestamp time.Time
status types.Status
ipAddress string
HotKeyBurnRate *BurnRate
}

// NewTelemetryServer should only listen to the loopback
func NewTelemetryServer() *TelemetryServer {
hs := &TelemetryServer{
logger: log.With().Str("module", "http").Logger(),
HotKeyBurnRate: NewBurnRate(100),
logger: log.With().Str("module", "http").Logger(),
lastScannedBlockNumber: make(map[int64]uint64),
lastStartTimestamp: time.Now(),
HotKeyBurnRate: NewBurnRate(100),

Check warning on line 41 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L38-L41

Added lines #L38 - L41 were not covered by tests
}
s := &http.Server{
Addr: ":8123",
Expand Down Expand Up @@ -67,6 +78,51 @@
return t.ipAddress
}

// GetLastStartTimestamp returns last start timestamp
func (t *TelemetryServer) GetLastStartTimestamp() time.Time {
t.mu.Lock()
defer t.mu.Unlock()
return t.lastStartTimestamp

Check warning on line 85 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L82-L85

Added lines #L82 - L85 were not covered by tests
}

// SetLastScannedBlockNumber last scanned block number for chain in telemetry and metrics
func (t *TelemetryServer) SetLastScannedBlockNumber(chain chains.Chain, blockNumber uint64) {
t.mu.Lock()
t.lastScannedBlockNumber[chain.ChainId] = blockNumber
LastScannedBlockNumber.WithLabelValues(chain.ChainName.String()).Set(float64(blockNumber))
t.mu.Unlock()

Check warning on line 93 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L89-L93

Added lines #L89 - L93 were not covered by tests
}

// GetLastScannedBlockNumber returns last scanned block number for chain
func (t *TelemetryServer) GetLastScannedBlockNumber(chainID int64) uint64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.lastScannedBlockNumber[chainID]

Check warning on line 100 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L97-L100

Added lines #L97 - L100 were not covered by tests
}

// SetCoreBlockNumber sets core block number in telemetry and metrics
func (t *TelemetryServer) SetCoreBlockNumber(blockNumber int64) {
t.mu.Lock()
t.lastCoreBlockNumber = blockNumber
LastCoreBlockNumber.Set(float64(blockNumber))
t.mu.Unlock()

Check warning on line 108 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L104-L108

Added lines #L104 - L108 were not covered by tests
}

// GetCoreBlockNumber returns core block number
func (t *TelemetryServer) GetCoreBlockNumber() int64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.lastCoreBlockNumber

Check warning on line 115 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L112-L115

Added lines #L112 - L115 were not covered by tests
}

// SetNumberOfUTXOs sets number of UTXOs in telemetry and metrics
func (t *TelemetryServer) SetNumberOfUTXOs(numberOfUTXOs int) {
skosito marked this conversation as resolved.
Show resolved Hide resolved
t.mu.Lock()
t.status.BTCNumberOfUTXOs = numberOfUTXOs
NumberOfUTXO.Set(float64(numberOfUTXOs))
t.mu.Unlock()

Check warning on line 123 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L119-L123

Added lines #L119 - L123 were not covered by tests
}

// AddFeeEntry adds fee entry
func (t *TelemetryServer) AddFeeEntry(block int64, amount int64) {
t.mu.Lock()
Expand All @@ -82,6 +138,11 @@
router := mux.NewRouter()
router.Handle("/ping", http.HandlerFunc(t.pingHandler)).Methods(http.MethodGet)
router.Handle("/p2p", http.HandlerFunc(t.p2pHandler)).Methods(http.MethodGet)
router.Handle("/version", http.HandlerFunc(t.versionHandler)).Methods(http.MethodGet)
router.Handle("/lastscannedblock", http.HandlerFunc(t.lastScannedBlockHandler)).Methods(http.MethodGet)
router.Handle("/laststarttimestamp", http.HandlerFunc(t.lastStartTimestampHandler)).Methods(http.MethodGet)
router.Handle("/lastcoreblock", http.HandlerFunc(t.lastCoreBlockHandler)).Methods(http.MethodGet)
router.Handle("/status", http.HandlerFunc(t.statusHandler)).Methods(http.MethodGet)

Check warning on line 145 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L141-L145

Added lines #L141 - L145 were not covered by tests
router.Handle("/ip", http.HandlerFunc(t.ipHandler)).Methods(http.MethodGet)
router.Handle("/hotkeyburnrate", http.HandlerFunc(t.hotKeyFeeBurnRate)).Methods(http.MethodGet)

Expand All @@ -90,6 +151,7 @@
return router
}

// Start starts telemetry server
func (t *TelemetryServer) Start() error {
if t.s == nil {
return errors.New("invalid http server instance")
Expand All @@ -103,6 +165,7 @@
return nil
}

// Stop stops telemetry server
func (t *TelemetryServer) Stop() error {
c, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
Expand Down Expand Up @@ -131,6 +194,53 @@
fmt.Fprintf(w, "%s", t.ipAddress)
}

func (t *TelemetryServer) lastScannedBlockHandler(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")

Check warning on line 198 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L197-L198

Added lines #L197 - L198 were not covered by tests

t.mu.Lock()
defer t.mu.Unlock()

Check warning on line 201 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L200-L201

Added lines #L200 - L201 were not covered by tests
// Convert map to JSON
jsonBytes, err := json.Marshal(t.lastScannedBlockNumber)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return

Check warning on line 206 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L203-L206

Added lines #L203 - L206 were not covered by tests
}
_, err = w.Write(jsonBytes)
if err != nil {
t.logger.Error().Err(err).Msg("Failed to write response")

Check warning on line 210 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L208-L210

Added lines #L208 - L210 were not covered by tests
}
}

func (t *TelemetryServer) lastCoreBlockHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
t.mu.Lock()
defer t.mu.Unlock()
fmt.Fprintf(w, "%d", t.lastCoreBlockNumber)

Check warning on line 218 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L214-L218

Added lines #L214 - L218 were not covered by tests
}

func (t *TelemetryServer) statusHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
t.mu.Lock()
defer t.mu.Unlock()
s, err := json.MarshalIndent(t.status, "", "\t")
if err != nil {
t.logger.Error().Err(err).Msg("Failed to marshal status")

Check warning on line 227 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L221-L227

Added lines #L221 - L227 were not covered by tests
}
fmt.Fprintf(w, "%s", s)

Check warning on line 229 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L229

Added line #L229 was not covered by tests
skosito marked this conversation as resolved.
Show resolved Hide resolved
}

func (t *TelemetryServer) versionHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "%s", constant.Version)

Check warning on line 234 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L232-L234

Added lines #L232 - L234 were not covered by tests
}

func (t *TelemetryServer) lastStartTimestampHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
t.mu.Lock()
defer t.mu.Unlock()
fmt.Fprintf(w, "%s", t.lastStartTimestamp.Format(time.RFC3339))

Check warning on line 241 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L237-L241

Added lines #L237 - L241 were not covered by tests
}

func (t *TelemetryServer) hotKeyFeeBurnRate(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
t.mu.Lock()
Expand Down
2 changes: 1 addition & 1 deletion zetaclient/orchestrator/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@

// update last processed block number
lastBlockNum = bn
metrics.LastCoreBlockNumber.Set(float64(lastBlockNum))
oc.ts.SetCoreBlockNumber(lastBlockNum)

Check warning on line 328 in zetaclient/orchestrator/orchestrator.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/orchestrator/orchestrator.go#L328

Added line #L328 was not covered by tests
}
}
}
Expand Down
Loading