github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/api/metricsdebug/client.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // The metricsdebug package contains the implementation of a client to
     5  // access metrics debug functions within state.
     6  package metricsdebug
     7  
     8  import (
     9  	"github.com/juju/errors"
    10  
    11  	"github.com/juju/juju/api"
    12  	"github.com/juju/juju/api/base"
    13  	"github.com/juju/juju/apiserver/params"
    14  )
    15  
    16  // Client provides access to the metric debug api
    17  type Client struct {
    18  	base.ClientFacade
    19  	st     api.Connection
    20  	facade base.FacadeCaller
    21  }
    22  
    23  // MetricsDebugClient defines the methods on the metricsdebug API end point.
    24  type MetricsDebugClient interface {
    25  	// GetMetrics will receive metrics collected by the given entity tag
    26  	GetMetrics(tag string) ([]params.MetricResult, error)
    27  }
    28  
    29  // MeterStatusClient defines methods on the metricsdebug API end point.
    30  type MeterStatusClient interface {
    31  	// SetMeterStatus will set the meter status on the given entity tag.
    32  	SetMeterStatus(tag, code, info string) error
    33  }
    34  
    35  var _ MetricsDebugClient = (*Client)(nil)
    36  var _ MeterStatusClient = (*Client)(nil)
    37  
    38  // NewClient creates a new client for accessing the metricsdebug api
    39  func NewClient(st base.APICallCloser) *Client {
    40  	frontend, backend := base.NewClientFacade(st, "MetricsDebug")
    41  	return &Client{ClientFacade: frontend, facade: backend}
    42  }
    43  
    44  // GetMetrics will receive metrics collected by the given entity
    45  func (c *Client) GetMetrics(tag string) ([]params.MetricResult, error) {
    46  	p := params.Entities{Entities: []params.Entity{
    47  		{tag},
    48  	}}
    49  	results := new(params.MetricResults)
    50  	if err := c.facade.FacadeCall("GetMetrics", p, results); err != nil {
    51  		return nil, errors.Trace(err)
    52  	}
    53  	if err := results.OneError(); err != nil {
    54  		return nil, errors.Trace(err)
    55  	}
    56  	metrics := []params.MetricResult{}
    57  	for _, r := range results.Results {
    58  		metrics = append(metrics, r.Metrics...)
    59  	}
    60  	return metrics, nil
    61  }
    62  
    63  // SetMeterStatus will set the meter status on the given entity tag.
    64  func (c *Client) SetMeterStatus(tag, code, info string) error {
    65  	args := params.MeterStatusParams{
    66  		Statuses: []params.MeterStatusParam{{
    67  			Tag:  tag,
    68  			Code: code,
    69  			Info: info,
    70  		},
    71  		},
    72  	}
    73  	results := new(params.ErrorResults)
    74  	if err := c.facade.FacadeCall("SetMeterStatus", args, results); err != nil {
    75  		return errors.Trace(err)
    76  	}
    77  	if err := results.OneError(); err != nil {
    78  		return errors.Trace(err)
    79  	}
    80  	return nil
    81  }