monitor_api_services.go raw

   1  // Functions in the file are making calls to the monitor-api instead of linode domain.
   2  // Please initialize a MonitorClient for using the endpoints below.
   3  
   4  package linodego
   5  
   6  import (
   7  	"context"
   8  	"encoding/json"
   9  	"time"
  10  )
  11  
  12  type MetricFilterOperator string
  13  
  14  const (
  15  	MetricFilterOperatorEq         MetricFilterOperator = "eq"
  16  	MetricFilterOperatorNeq        MetricFilterOperator = "neq"
  17  	MetricFilterOperatorStartsWith MetricFilterOperator = "startswith"
  18  	MetricFilterOperatorEndsWith   MetricFilterOperator = "endswith"
  19  	MetricFilterOperatorIn         MetricFilterOperator = "in"
  20  )
  21  
  22  type MetricTimeUnit string
  23  
  24  const (
  25  	MetricTimeUnitSec  MetricTimeUnit = "sec"
  26  	MetricTimeUnitMin  MetricTimeUnit = "min"
  27  	MetricTimeUnitHr   MetricTimeUnit = "hr"
  28  	MetricTimeUnitDays MetricTimeUnit = "days"
  29  )
  30  
  31  // EntityMetrics is the response body of the metrics for the entities requested
  32  type EntityMetrics struct {
  33  	Data      EntityMetricsData  `json:"data"`
  34  	IsPartial bool               `json:"is_partial"`
  35  	Stats     EntityMetricsStats `json:"stats"`
  36  	Status    string             `json:"status"`
  37  }
  38  
  39  // EntityMetricsData describes the result and type for the entity metrics
  40  type EntityMetricsData struct {
  41  	Result     []EntityMetricsDataResult `json:"result"`
  42  	ResultType string                    `json:"result_type"`
  43  }
  44  
  45  // EntityMetricsDataResult contains the information of a metric and values
  46  type EntityMetricsDataResult struct {
  47  	Metric map[string]any `json:"metric"`
  48  	Values [][]any        `json:"values"`
  49  }
  50  
  51  // EntityMetricsStats shows statistics info of the metrics fetched
  52  type EntityMetricsStats struct {
  53  	ExecutionTimeMsec int    `json:"executionTimeMsec"`
  54  	SeriesFetched     string `json:"seriesFetched"`
  55  }
  56  
  57  // EntityMetricsFetchOptions are the options used to fetch metrics with the entity ids provided
  58  type EntityMetricsFetchOptions struct {
  59  	// EntityIDs are expected to be type "any" as different service_types have different variable type for their entity_ids. For example, Linode has "int" entity_ids whereas object storage has "string" as entity_ids.
  60  	EntityIDs []any `json:"entity_ids"`
  61  
  62  	Filters              []MetricFilter              `json:"filters,omitempty"`
  63  	Metrics              []EntityMetric              `json:"metrics"`
  64  	TimeGranularity      []MetricTimeGranularity     `json:"time_granularity,omitempty"`
  65  	RelativeTimeDuration *MetricRelativeTimeDuration `json:"relative_time_duration,omitempty"`
  66  	AbsoluteTimeDuration *MetricAbsoluteTimeDuration `json:"absolute_time_duration,omitempty"`
  67  }
  68  
  69  // MetricFilter describes individual objects that define dimension filters for the query.
  70  type MetricFilter struct {
  71  	DimensionLabel string               `json:"dimension_label"`
  72  	Operator       MetricFilterOperator `json:"operator"`
  73  	Value          string               `json:"value"`
  74  }
  75  
  76  // EntityMetric specifies a metric name and its corresponding aggregation function.
  77  type EntityMetric struct {
  78  	Name              string            `json:"name"`
  79  	AggregateFunction AggregateFunction `json:"aggregate_function"`
  80  }
  81  
  82  // MetricTimeGranularity allows for an optional time granularity setting for metric data.
  83  type MetricTimeGranularity struct {
  84  	Unit  MetricTimeUnit `json:"unit"`
  85  	Value int            `json:"value"`
  86  }
  87  
  88  // MetricRelativeTimeDuration specifies a relative time duration for data queries
  89  type MetricRelativeTimeDuration struct {
  90  	Unit  MetricTimeUnit `json:"unit"`
  91  	Value int            `json:"value"`
  92  }
  93  
  94  // MetricAbsoluteTimeDuration specifies an absolute time range for data queries
  95  type MetricAbsoluteTimeDuration struct {
  96  	Start time.Time `json:"start"`
  97  	End   time.Time `json:"end"`
  98  }
  99  
 100  // FetchEntityMetrics returns metrics information for the individual entities within a specific service type
 101  func (mc *MonitorClient) FetchEntityMetrics(ctx context.Context, serviceType string, opts *EntityMetricsFetchOptions) (*EntityMetrics, error) {
 102  	endpoint := formatAPIPath("monitor/services/%s/metrics", serviceType)
 103  
 104  	req := mc.R(ctx).SetResult(&EntityMetrics{})
 105  
 106  	if opts != nil {
 107  		body, err := json.Marshal(opts)
 108  		if err != nil {
 109  			return nil, err
 110  		}
 111  
 112  		req.SetBody(string(body))
 113  	}
 114  
 115  	r, err := coupleAPIErrors(req.Post(endpoint))
 116  	if err != nil {
 117  		return nil, err
 118  	}
 119  
 120  	return r.Result().(*EntityMetrics), nil
 121  }
 122