Issue #449 API endpoint for Peer Monitor metrics

Support the new endpoint for later metrics in `rest/api/client`

Support the new method created in `rest/api/client` in
ipfs-cluster-ctl. i.e. `ipfs-cluster-ctl health metrics <name>` would
show the peers and the last list of metrics logged for each as returned
by the Peer Monitor, in a friendly way.

License: MIT
Signed-off-by: Kishan Mohanbhai Sagathiya <kishansagathiya@gmail.com>
This commit is contained in:
Kishan Sagathiya 2018-10-07 22:02:46 +05:30
parent fafbf7b929
commit 3ac7d6c9cc
4 changed files with 48 additions and 0 deletions

View File

@ -103,6 +103,10 @@ type Client interface {
// GetConnectGraph returns an ipfs-cluster connection graph. The
// serialized version, strings instead of pids, is returned
GetConnectGraph() (api.ConnectGraphSerial, error)
// PeerMonitorLatestMetrics returns a map with the latest metrics of matching name
// for the current cluster peers.
PeerMonitorLatestMetrics(name string) ([]api.Metric, error)
}
// Config allows to configure the parameters to connect

View File

@ -204,6 +204,14 @@ func (c *defaultClient) GetConnectGraph() (api.ConnectGraphSerial, error) {
return graphS, err
}
// PeerMonitorLatestMetrics returns a map with the latest metrics of matching name
// for the current cluster peers.
func (c *defaultClient) PeerMonitorLatestMetrics(name string) ([]api.Metric, error) {
var metrics []api.Metric
err := c.do("GET", fmt.Sprintf("/health/metrics/%s", name), nil, nil, &metrics)
return metrics, err
}
// WaitFor is a utility function that allows for a caller to wait for a
// paticular status for a CID (as defined by StatusFilterParams).
// It returns the final status for that CID and an error, if there was.

View File

@ -24,6 +24,9 @@ func jsonFormatObject(resp interface{}) {
jsonFormatPrint(resp.(api.AddedOutput))
case api.Version:
jsonFormatPrint(resp.(api.Version))
case api.Metric:
serial := resp.(api.Metric)
textFormatPrintMetric(&serial)
case api.Error:
jsonFormatPrint(resp.(api.Error))
case []api.ID:
@ -51,6 +54,9 @@ func jsonFormatObject(resp interface{}) {
case []api.AddedOutput:
serials := resp.([]api.AddedOutput)
jsonFormatPrint(serials)
case []api.Metric:
serials := resp.([]api.Metric)
jsonFormatPrint(serials)
default:
checkErr("", errors.New("unsupported type returned"))
}
@ -84,6 +90,9 @@ func textFormatObject(resp interface{}) {
case api.Error:
serial := resp.(api.Error)
textFormatPrintError(&serial)
case api.Metric:
serial := resp.(api.Metric)
textFormatPrintMetric(&serial)
case []api.ID:
for _, item := range resp.([]api.ID) {
textFormatObject(item)
@ -100,6 +109,10 @@ func textFormatObject(resp interface{}) {
for _, item := range resp.([]api.AddedOutput) {
textFormatObject(item)
}
case []api.Metric:
for _, item := range resp.([]api.Metric) {
textFormatObject(item)
}
default:
checkErr("", errors.New("unsupported type returned"))
}
@ -202,6 +215,15 @@ func textFormatPrintAddedOutput(obj *api.AddedOutput) {
fmt.Printf("added %s %s\n", obj.Cid, obj.Name)
}
func textFormatPrintMetric(obj *api.Metric) {
fmt.Printf("{\n")
fmt.Printf(" Peer: %s\n", (obj.Peer).String())
fmt.Printf(" Value: %s\n", obj.Value)
fmt.Printf(" Expire: %d\n", obj.Expire)
fmt.Printf(" Expire: %t\n", obj.Valid)
fmt.Printf("}\n")
}
func textFormatPrintError(obj *api.Error) {
fmt.Printf("An error occurred:\n")
fmt.Printf(" Code: %d\n", obj.Code)

View File

@ -762,6 +762,20 @@ graph of the connections. Output is a dot file encoding the cluster's connectio
return nil
},
},
{
Name: "metrics",
Usage: "Get latest metrics of matching name for the current cluster peers.",
Description: `
This command would show the peers and the last list of metrics logged for each as
returned by the Peer Monitor, in a friendly way.
`,
ArgsUsage: "Metric name",
Action: func(c *cli.Context) error {
resp, cerr := globalClient.PeerMonitorLatestMetrics(c.Args().First())
formatResponse(c, resp, cerr)
return nil
},
},
},
},
{