github.com/newrelic/newrelic-client-go@v1.1.0/pkg/plugins/components.go (about)

     1  package plugins
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"time"
     7  )
     8  
     9  // ListComponentsParams represents a set of filters to be
    10  // used when querying New Relic applications.
    11  type ListComponentsParams struct {
    12  	Name         string `url:"filter[name],omitempty"`
    13  	IDs          []int  `url:"filter[ids],omitempty,comma"`
    14  	PluginID     int    `url:"filter[plugin_id],omitempty"`
    15  	HealthStatus bool   `url:"health_status,omitempty"`
    16  }
    17  
    18  // ListComponents is used to retrieve the components associated with
    19  // a New Relic account.
    20  func (p *Plugins) ListComponents(params *ListComponentsParams) ([]*Component, error) {
    21  	c := []*Component{}
    22  	nextURL := p.config.Region().RestURL("components.json")
    23  
    24  	for nextURL != "" {
    25  		response := componentsResponse{}
    26  		resp, err := p.client.Get(nextURL, &params, &response)
    27  
    28  		if err != nil {
    29  			return nil, err
    30  		}
    31  
    32  		c = append(c, response.Components...)
    33  
    34  		paging := p.pager.Parse(resp)
    35  		nextURL = paging.Next
    36  	}
    37  
    38  	return c, nil
    39  
    40  }
    41  
    42  // GetComponent is used to retrieve a specific New Relic component.
    43  func (p *Plugins) GetComponent(componentID int) (*Component, error) {
    44  	response := componentResponse{}
    45  	url := fmt.Sprintf("/components/%d.json", componentID)
    46  
    47  	_, err := p.client.Get(p.config.Region().RestURL(url), nil, &response)
    48  
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	return &response.Component, nil
    54  }
    55  
    56  // ListComponentMetricsParams represents a set of parameters to be
    57  // used when querying New Relic component metrics.
    58  type ListComponentMetricsParams struct {
    59  	// Name allows for filtering the returned list of metrics by name.
    60  	Name string `url:"name,omitempty"`
    61  }
    62  
    63  // ListComponentMetrics is used to retrieve the metrics for a specific New Relic component.
    64  func (p *Plugins) ListComponentMetrics(componentID int, params *ListComponentMetricsParams) ([]*ComponentMetric, error) {
    65  	m := []*ComponentMetric{}
    66  	response := componentMetricsResponse{}
    67  	nextURL := p.config.Region().RestURL("components", strconv.Itoa(componentID), "metrics.json")
    68  
    69  	for nextURL != "" {
    70  		resp, err := p.client.Get(nextURL, &params, &response)
    71  
    72  		if err != nil {
    73  			return nil, err
    74  		}
    75  
    76  		m = append(m, response.Metrics...)
    77  
    78  		paging := p.pager.Parse(resp)
    79  		nextURL = paging.Next
    80  	}
    81  
    82  	return m, nil
    83  }
    84  
    85  // GetComponentMetricDataParams represents a set of parameters to be
    86  // used when querying New Relic component metric data.
    87  type GetComponentMetricDataParams struct {
    88  	// Names allows retrieval of specific metrics by name.
    89  	// At least one metric name is required.
    90  	Names []string `url:"names[],omitempty"`
    91  
    92  	// Values allows retrieval of specific metric values.
    93  	Values []string `url:"values[],omitempty"`
    94  
    95  	// From specifies a begin time for the query.
    96  	From *time.Time `url:"from,omitempty"`
    97  
    98  	// To specifies an end time for the query.
    99  	To *time.Time `url:"to,omitempty"`
   100  
   101  	// Period represents the period of timeslices in seconds.
   102  	Period int `url:"period,omitempty"`
   103  
   104  	// Summarize will summarize the data when set to true.
   105  	Summarize bool `url:"summarize,omitempty"`
   106  
   107  	// Raw will return unformatted raw values when set to true.
   108  	Raw bool `url:"raw,omitempty"`
   109  }
   110  
   111  // GetComponentMetricData is used to retrieve the metric timeslice data for a specific component metric.
   112  func (p *Plugins) GetComponentMetricData(componentID int, params *GetComponentMetricDataParams) ([]*Metric, error) {
   113  	m := []*Metric{}
   114  	response := componentMetricDataResponse{}
   115  	nextURL := p.config.Region().RestURL("components", strconv.Itoa(componentID), "metrics/data.json")
   116  
   117  	for nextURL != "" {
   118  		resp, err := p.client.Get(nextURL, &params, &response)
   119  
   120  		if err != nil {
   121  			return nil, err
   122  		}
   123  
   124  		m = append(m, response.MetricData.Metrics...)
   125  
   126  		paging := p.pager.Parse(resp)
   127  		nextURL = paging.Next
   128  	}
   129  
   130  	return m, nil
   131  }
   132  
   133  type componentsResponse struct {
   134  	Components []*Component `json:"components,omitempty"`
   135  }
   136  
   137  type componentResponse struct {
   138  	Component Component `json:"component,omitempty"`
   139  }
   140  
   141  type componentMetricsResponse struct {
   142  	Metrics []*ComponentMetric `json:"metrics,omitempty"`
   143  }
   144  
   145  type componentMetricDataResponse struct {
   146  	MetricData struct {
   147  		Metrics []*Metric `json:"metrics,omitempty"`
   148  	} `json:"metric_data,omitempty"`
   149  }