github.com/wtfutil/wtf@v0.43.0/modules/newrelic/client/metrics.go (about)

     1  package newrelic
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // Metric describes a New Relic metric.
     8  type Metric struct {
     9  	Name   string   `json:"name,omitempty"`
    10  	Values []string `json:"values,omitempty"`
    11  }
    12  
    13  // MetricsOptions options allow filtering when getting lists of metric names
    14  // associated with an entity.
    15  type MetricsOptions struct {
    16  	Name string
    17  	Page int
    18  }
    19  
    20  // MetricTimeslice describes the period to which a Metric pertains.
    21  type MetricTimeslice struct {
    22  	From   time.Time          `json:"from,omitempty"`
    23  	To     time.Time          `json:"to,omitempty"`
    24  	Values map[string]float64 `json:"values,omitempty"`
    25  }
    26  
    27  // MetricData describes the data for a particular metric.
    28  type MetricData struct {
    29  	Name       string            `json:"name,omitempty"`
    30  	Timeslices []MetricTimeslice `json:"timeslices,omitempty"`
    31  }
    32  
    33  // MetricDataOptions allow filtering when getting data about a particular set
    34  // of New Relic metrics.
    35  type MetricDataOptions struct {
    36  	Names     Array
    37  	Values    Array
    38  	From      time.Time
    39  	To        time.Time
    40  	Period    int
    41  	Summarize bool
    42  	Raw       bool
    43  }
    44  
    45  // MetricDataResponse is the response received from New Relic for any request
    46  // for metric data.
    47  type MetricDataResponse struct {
    48  	From            time.Time    `json:"from,omitempty"`
    49  	To              time.Time    `json:"to,omitempty"`
    50  	MetricsNotFound []string     `json:"metrics_not_found,omitempty"`
    51  	MetricsFound    []string     `json:"metrics_found,omitempty"`
    52  	Metrics         []MetricData `json:"metrics,omitempty"`
    53  }
    54  
    55  func (o *MetricsOptions) String() string {
    56  	if o == nil {
    57  		return ""
    58  	}
    59  	return encodeGetParams(map[string]interface{}{
    60  		"name": o.Name,
    61  		"page": o.Page,
    62  	})
    63  }
    64  
    65  func (o *MetricDataOptions) String() string {
    66  	if o == nil {
    67  		return ""
    68  	}
    69  	return encodeGetParams(map[string]interface{}{
    70  		"names[]":   o.Names,
    71  		"values[]":  o.Values,
    72  		"from":      o.From,
    73  		"to":        o.To,
    74  		"period":    o.Period,
    75  		"summarize": o.Summarize,
    76  		"raw":       o.Raw,
    77  	})
    78  }
    79  
    80  // MetricClient implements a generic New Relic metrics client.
    81  // This is used as a general client for fetching metric names and data.
    82  type MetricClient struct {
    83  	newRelicClient *Client
    84  }
    85  
    86  // NewMetricClient creates and returns a new MetricClient.
    87  func NewMetricClient(newRelicClient *Client) *MetricClient {
    88  	return &MetricClient{
    89  		newRelicClient: newRelicClient,
    90  	}
    91  }
    92  
    93  // GetMetrics is a generic function for fetching a list of available metrics
    94  // from different parts of New Relic.
    95  // Example: Application metrics, Component metrics, etc.
    96  func (mc *MetricClient) GetMetrics(path string, options *MetricsOptions) ([]Metric, error) {
    97  	resp := &struct {
    98  		Metrics []Metric `json:"metrics,omitempty"`
    99  	}{}
   100  
   101  	err := mc.newRelicClient.doGet(path, options, resp)
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  
   106  	return resp.Metrics, nil
   107  }
   108  
   109  // GetMetricData is a generic function for fetching data for a specific metric.
   110  // from different parts of New Relic.
   111  // Example: Application metric data, Component metric data, etc.
   112  func (mc *MetricClient) GetMetricData(path string, names []string, options *MetricDataOptions) (*MetricDataResponse, error) {
   113  	resp := &struct {
   114  		MetricData MetricDataResponse `json:"metric_data,omitempty"`
   115  	}{}
   116  
   117  	if options == nil {
   118  		options = &MetricDataOptions{}
   119  	}
   120  
   121  	options.Names = Array{names}
   122  	err := mc.newRelicClient.doGet(path, options, resp)
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  
   127  	return &resp.MetricData, nil
   128  }