Skip to content

Commit

Permalink
Merge pull request #572 from kishansagathiya/issue_449
Browse files Browse the repository at this point in the history
Issue #449 API endpoint for Peer Monitor metrics
  • Loading branch information
hsanjuan authored Oct 22, 2018
2 parents 50d4876 + ec4588a commit 48c89fb
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/rest/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions api/rest/client/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions api/rest/restapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,12 @@ func (api *API) routes() []route {
"/health/graph",
api.graphHandler,
},
{
"Metrics",
"GET",
"/monitor/metrics/{name}",
api.metricsHandler,
},
}
}

Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions cmd/ipfs-cluster-ctl/formatters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"))
}
Expand Down Expand Up @@ -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)
Expand All @@ -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"))
}
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions cmd/ipfs-cluster-ctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
},
},
},
{
Expand Down

0 comments on commit 48c89fb

Please # to comment.