github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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 tags
    26  	// The tags act as a filter over what is to be returned. If no tags are
    27  	// supplied GetMetrics will return all the metrics recorded in the
    28  	// current model.
    29  	GetMetrics(tags ...string) ([]params.MetricResult, error)
    30  }
    31  
    32  // MeterStatusClient defines methods on the metricsdebug API end point.
    33  type MeterStatusClient interface {
    34  	// SetMeterStatus will set the meter status on the given entity tag.
    35  	SetMeterStatus(tag, code, info string) error
    36  }
    37  
    38  var _ MetricsDebugClient = (*Client)(nil)
    39  var _ MeterStatusClient = (*Client)(nil)
    40  
    41  // NewClient creates a new client for accessing the metricsdebug api
    42  func NewClient(st base.APICallCloser) *Client {
    43  	frontend, backend := base.NewClientFacade(st, "MetricsDebug")
    44  	return &Client{ClientFacade: frontend, facade: backend}
    45  }
    46  
    47  // GetMetrics will receive metrics collected by the given entity
    48  func (c *Client) GetMetrics(tags ...string) ([]params.MetricResult, error) {
    49  	entities := make([]params.Entity, len(tags))
    50  	for i, tag := range tags {
    51  		entities[i] = params.Entity{Tag: tag}
    52  	}
    53  	p := params.Entities{Entities: entities}
    54  	results := new(params.MetricResults)
    55  	if err := c.facade.FacadeCall("GetMetrics", p, results); err != nil {
    56  		return nil, errors.Trace(err)
    57  	}
    58  	if err := results.OneError(); err != nil {
    59  		return nil, errors.Trace(err)
    60  	}
    61  	metrics := []params.MetricResult{}
    62  	for _, r := range results.Results {
    63  		metrics = append(metrics, r.Metrics...)
    64  	}
    65  	return metrics, nil
    66  }
    67  
    68  // SetMeterStatus will set the meter status on the given entity tag.
    69  func (c *Client) SetMeterStatus(tag, code, info string) error {
    70  	args := params.MeterStatusParams{
    71  		Statuses: []params.MeterStatusParam{{
    72  			Tag:  tag,
    73  			Code: code,
    74  			Info: info,
    75  		},
    76  		},
    77  	}
    78  	results := new(params.ErrorResults)
    79  	if err := c.facade.FacadeCall("SetMeterStatus", args, results); err != nil {
    80  		return errors.Trace(err)
    81  	}
    82  	if err := results.OneError(); err != nil {
    83  		return errors.Trace(err)
    84  	}
    85  	return nil
    86  }