plugin

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2024 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	QueryTypeResourceList = "resource-list"
	QueryTypeMetrics      = "metrics"
)
View Source
const (
	LabelID                = "id"
	LabelName              = "name"
	LabelSeriesName        = "series_name"
	LabelSeriesDisplayName = "series_display_name"
)
View Source
const (
	AutoLegendFormat = "{{ series_display_name }} {{ name }}"

	// DefaultBufferPeriod is the default buffer period for the QueryRunner.
	DefaultBufferPeriod = 200 * time.Millisecond

	InvalidAPITokenErrorMessage = "" /* 128-byte string literal not displayed */
)

Variables

This section is empty.

Functions

func NewDatasource

NewDatasource creates a new datasource instance.

func NicerErrorMessages added in v0.2.0

func NicerErrorMessages(err error) error

NicerErrorMessages replaces some error messages from the hetzner cloud API with more user-friendly messages.

Types

type APIRequestFn

type APIRequestFn[M HCloudMetrics] func(ctx context.Context, id int64, opts RequestOpts) (*M, error)

type Datasource

type Datasource struct {
	// contains filtered or unexported fields
}

Datasource is an example datasource which can respond to data queries, reports its health and has streaming skills.

func (*Datasource) CallResource

CallResource handles additional API calls. These are used to fill the resource dropdowns in the query editor.

func (*Datasource) CheckHealth

CheckHealth handles health checks sent from Grafana to the plugin. The main use case for these health checks is the test button on the datasource configuration page which allows users to verify that a datasource is working as expected.

func (*Datasource) GetResourceIDs

func (d *Datasource) GetResourceIDs(ctx context.Context, qm QueryModel) ([]int64, error)

func (*Datasource) QueryData

QueryData handles multiple queries and returns multiple responses. req contains the queries []DataQuery (where each query contains RefID as a unique identifier). The QueryDataResponse contains a map of RefID to the response for each query, and each response contains Frames ([]*Frame).

type FilterMetricsFn

type FilterMetricsFn[M HCloudMetrics] func(metrics *M, metricsTypes []MetricsType) *M

type GetResourceFn

type GetResourceFn[R HCloudResource] func(ctx context.Context, id int64) (*R, error)

type HCloudMetrics

type HCloudMetrics interface {
	hcloud.ServerMetrics | hcloud.LoadBalancerMetrics
}

type HCloudResource

type HCloudResource interface {
	hcloud.Server | hcloud.LoadBalancer
}

type IdentifierFn

type IdentifierFn[R HCloudResource] func(resource *R) (int64, string)

type Label

type Label string

type MetricsType

type MetricsType string
const (
	MetricsTypeServerCPU              MetricsType = "cpu"
	MetricsTypeServerDiskBandwidth    MetricsType = "disk-bandwidth"
	MetricsTypeServerDiskIOPS         MetricsType = "disk-iops"
	MetricsTypeServerNetworkBandwidth MetricsType = "network-bandwidth"
	MetricsTypeServerNetworkPPS       MetricsType = "network-pps"

	MetricsTypeLoadBalancerOpenConnections      MetricsType = "open-connections"
	MetricsTypeLoadBalancerConnectionsPerSecond MetricsType = "connections-per-second"
	MetricsTypeLoadBalancerRequestsPerSecond    MetricsType = "requests-per-second"
	MetricsTypeLoadBalancerBandwidth            MetricsType = "bandwidth"
)

type NameCache

type NameCache[R HCloudResource] struct {
	sync.Mutex
	// contains filtered or unexported fields
}

NameCache is a cache for resource names. It is used to avoid sending unnecessary API requests. Right now there is no expiry for entries, so if names are changed this is not reflected in queries.

func NewNameCache

func NewNameCache[R HCloudResource](client *hcloud.Client, getFn GetResourceFn[R], identifierFn IdentifierFn[R]) *NameCache[R]

func (*NameCache[R]) Get

func (c *NameCache[R]) Get(ctx context.Context, id int64) (string, error)

Get will retrieve the name from the cache or query the API in case it is unknown.

func (*NameCache[R]) Insert

func (c *NameCache[R]) Insert(resources ...*R)

Insert will insert the given resources into the cache, updating any existing entries. This should be called whenever API requests are made, to keep the cache reasonable full & up to date.

type Options

type Options struct {
	Debug bool `json:"debug"`
}

type QueryModel

type QueryModel struct {
	ResourceType ResourceType `json:"resourceType"`
	MetricsType  MetricsType  `json:"metricsType"`

	SelectBy       SelectBy `json:"selectBy"`
	LabelSelectors []string `json:"labelSelectors"`
	ResourceIDs    []int64  `json:"resourceIds"`

	LegendFormat string `json:"legendFormat"`
}

type QueryRunner

type QueryRunner[M HCloudMetrics] struct {
	// contains filtered or unexported fields
}

QueryRunner is responsible for getting the Metrics from the Hetzner Cloud API.

The Hetzner Cloud API has endpoints that expose all metrics for a single resource (server/load-balancer). This runs counter to the way you would use the metrics in Grafana, where you would like to see a single metrics for multiple resources.

The naive solution to this would send one request per resource per incoming query to the API, but this can easily exhaust the API rate limit. The QueryRunner instead buffers incoming requests and only sends a single request to the API per resource requested during the buffer period. If you show metrics from the same resource in ie. 5 panels, this will only send 1 request to the API instead of 5.

The downside is that responses are slower, because we always wait for the buffer period to end before sending the requests.

func NewQueryRunner

func NewQueryRunner[M HCloudMetrics](bufferPeriod time.Duration, apiRequestFn APIRequestFn[M], filterMetrics FilterMetricsFn[M]) *QueryRunner[M]

func (*QueryRunner[M]) RequestMetrics

func (q *QueryRunner[M]) RequestMetrics(ctx context.Context, ids []int64, opts RequestOpts) (map[int64]*M, error)

RequestMetrics requests metrics matching the arguments given. It will return a slice of metrics for each id in the same order

type RequestOpts

type RequestOpts struct {
	MetricsTypes []MetricsType
	TimeRange    backend.TimeRange
	Step         int
}

type ResourceType

type ResourceType string
const (
	ResourceTypeServer       ResourceType = "server"
	ResourceTypeLoadBalancer ResourceType = "load-balancer"
)

type SelectBy

type SelectBy string
const (
	SelectByLabel SelectBy = "label"
	SelectByID    SelectBy = "id"
)

type SelectableValue

type SelectableValue struct {
	Value int64  `json:"value"`
	Label string `json:"label"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL