Merge pull request #572 from kishansagathiya/issue_449
Issue #449 API endpoint for Peer Monitor metrics
This commit is contained in:
commit
48c89fbe4d
|
@ -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)
|
||||
|
||||
// Metrics returns a map with the latest metrics of matching name
|
||||
// for the current cluster peers.
|
||||
Metrics(name string) ([]api.Metric, error)
|
||||
}
|
||||
|
||||
// Config allows to configure the parameters to connect
|
||||
|
|
|
@ -204,6 +204,14 @@ func (c *defaultClient) GetConnectGraph() (api.ConnectGraphSerial, error) {
|
|||
return graphS, err
|
||||
}
|
||||
|
||||
// Metrics returns a map with the latest metrics of matching name
|
||||
// for the current cluster peers.
|
||||
func (c *defaultClient) Metrics(name string) ([]api.Metric, error) {
|
||||
var metrics []api.Metric
|
||||
err := c.do("GET", fmt.Sprintf("/monitor/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.
|
||||
|
|
|
@ -385,6 +385,12 @@ func (api *API) routes() []route {
|
|||
"/health/graph",
|
||||
api.graphHandler,
|
||||
},
|
||||
{
|
||||
"Metrics",
|
||||
"GET",
|
||||
"/monitor/metrics/{name}",
|
||||
api.metricsHandler,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -506,6 +512,19 @@ func (api *API) graphHandler(w http.ResponseWriter, r *http.Request) {
|
|||
api.sendResponse(w, autoStatus, err, graph)
|
||||
}
|
||||
|
||||
func (api *API) metricsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
name := vars["name"]
|
||||
|
||||
var metrics []types.Metric
|
||||
err := api.rpcClient.Call("",
|
||||
"Cluster",
|
||||
"PeerMonitorLatestMetrics",
|
||||
name,
|
||||
&metrics)
|
||||
api.sendResponse(w, autoStatus, err, metrics)
|
||||
}
|
||||
|
||||
func (api *API) addHandler(w http.ResponseWriter, r *http.Request) {
|
||||
reader, err := r.MultipartReader()
|
||||
if err != nil {
|
||||
|
|
|
@ -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,10 @@ func textFormatPrintAddedOutput(obj *api.AddedOutput) {
|
|||
fmt.Printf("added %s %s\n", obj.Cid, obj.Name)
|
||||
}
|
||||
|
||||
func textFormatPrintMetric(obj *api.Metric) {
|
||||
fmt.Printf("%s: %s | Expire : %d\n", obj.Peer.Pretty(), obj.Value, obj.Expire)
|
||||
}
|
||||
|
||||
func textFormatPrintError(obj *api.Error) {
|
||||
fmt.Printf("An error occurred:\n")
|
||||
fmt.Printf(" Code: %d\n", obj.Code)
|
||||
|
|
|
@ -762,6 +762,20 @@ graph of the connections. Output is a dot file encoding the cluster's connectio
|
|||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "metrics",
|
||||
Usage: "List latest metrics logged by this peer",
|
||||
Description: `
|
||||
This commands displays the latest valid metrics of the given type logged
|
||||
by this peer for all current cluster peers.
|
||||
`,
|
||||
ArgsUsage: "Metric name",
|
||||
Action: func(c *cli.Context) error {
|
||||
resp, cerr := globalClient.Metrics(c.Args().First())
|
||||
formatResponse(c, resp, cerr)
|
||||
return nil
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue
Block a user