-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add network wide metrics collection (#1606)
* add network wide metrics collection * lint and test fix --------- Co-authored-by: Jacob Gadikian <[email protected]>
- Loading branch information
Showing
10 changed files
with
121 additions
and
8 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
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
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,78 @@ | ||
package app | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/common/expfmt" | ||
"github.com/tendermint/tendermint/abci/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
var ( | ||
nodeID string | ||
nodeIDMutex sync.Mutex | ||
) | ||
|
||
func (app *Quicksilver) InitNodeID() (string, error) { | ||
nodeIDMutex.Lock() | ||
defer nodeIDMutex.Unlock() | ||
if nodeID == "" { | ||
query := tmservice.GetNodeInfoRequest{} | ||
queryRes := app.Query(types.RequestQuery{Data: app.appCodec.MustMarshal(&query), Path: "/cosmos.base.tendermint.v1beta1.Service/GetNodeInfo", Height: 0, Prove: false}) | ||
nodeInfo := queryRes.GetValue() | ||
result := tmservice.GetNodeInfoResponse{} | ||
err := app.appCodec.Unmarshal(nodeInfo, &result) | ||
if err != nil { | ||
return "", err | ||
} | ||
nodeID = result.DefaultNodeInfo.DefaultNodeID | ||
} | ||
return nodeID, nil | ||
} | ||
|
||
func (app *Quicksilver) ShipMetrics(ctx sdk.Context) { | ||
metricsFamilies, err := prometheus.DefaultGatherer.Gather() | ||
if err != nil { | ||
ctx.Logger().Error("Error gathering metrics", "error", err) | ||
} | ||
|
||
buf := &bytes.Buffer{} | ||
defer buf.Reset() | ||
|
||
e := expfmt.NewEncoder(buf, expfmt.FmtText) | ||
for _, mf := range metricsFamilies { | ||
if err := e.Encode(mf); err != nil { | ||
ctx.Logger().Error("Error encoding metrics", "error", err) | ||
return | ||
} | ||
} | ||
|
||
nodeID, err := app.InitNodeID() | ||
if err != nil { | ||
ctx.Logger().Error("Error getting node ID", "error", err) | ||
return | ||
} | ||
c, cancel := context.WithTimeout(ctx.Context(), time.Second*10) | ||
defer cancel() | ||
req, err := http.NewRequestWithContext(c, "POST", fmt.Sprintf("%s/chain_id/%s/node_id/%s", app.metricsURL, ctx.ChainID(), nodeID), bytes.NewReader(buf.Bytes())) | ||
if err != nil { | ||
ctx.Logger().Error("Error creating metrics request", "error", err) | ||
return | ||
} | ||
req.Header.Set("Content-Type", string(expfmt.FmtText)) | ||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
ctx.Logger().Error("Error sending metrics", "error", err) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
} |
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
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
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 |
---|---|---|
|
@@ -38,6 +38,7 @@ func newQuicksilver(t *testing.T) *app.Quicksilver { | |
app.EmptyAppOptions{}, | ||
true, | ||
false, | ||
"", | ||
) | ||
} | ||
|
||
|