prometheus

package
v1.0.4-alpha Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package prometheus implements prometheus client

References:

[Go Reference]: https://pkg.go.dev/badge/github.com/mo-silent/go-devops.svg

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DataValueVec

func DataValueVec(name string, label []string) *prometheus.GaugeVec

DataValueVec implements a new prometheus metrics that includes label.

Types

type MatrixResult

type MatrixResult struct {
	Metric string `json:"metric"`
	//Labels []MetricLabel  `json:"labels"`
	Values []MetricValues `json:"values"`
}

MatrixResult obtains the matrix result y from the prometheus query.

type MetricLabel

type MetricLabel struct {
	Label string
	Value string
}

A MetricLabel is a prometheus metric label structure.

type MetricValues

type MetricValues struct {
	Timestamp int64
	Value     float64
}

MetricValues gets the metric value from the prometheus query.

type MetricsInterface

type MetricsInterface interface {
	Push(context.Context, PushMetrics, string) error
	QueryRange(context.Context, api.Client, string, v1.Range, ...Option) ([]MatrixResult, error)
}

MetricsInterface is the interface that implements prometheus push metrics and range queries.

type Option

type Option func(c *apiOptions)

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout can be used to provide an optional query evaluation timeout for Query and QueryRange. https://prometheus.io/docs/prometheus/latest/querying/api/#instant-queries

type PromMetrics

type PromMetrics struct {
	Values []string // values of metrics label
	Data   float64  // metrics value
}

PromMetrics is a prometheus metrics values.

type Prometheus

type Prometheus struct{}

Prometheus implements MetricsInterface.

func (*Prometheus) Push

func (p *Prometheus) Push(ctx context.Context, pm PushMetrics, addr string) error

Push implements a new prometheus metrics pushed to PushGateway.

Example
// prometheus push metrics example
log.SetLevel(log.DebugLevel)
log.SetOutput(os.Stdout)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
p := devops.NewDevops().Prometheus()
pm := prometheus.PushMetrics{
	Name:  "test",
	Label: []string{"sample1", "sample2"},
	Metrics: []prometheus.PromMetrics{
		{
			Values: []string{"s1", "s2"},
			Data:   99.99,
		},
	},
}
if err := p.Push(ctx, pm, "localhost:9091"); err != nil {
	log.Errorf("push metrics error, err:  %v", err)
}

func (*Prometheus) QueryRange

func (p *Prometheus) QueryRange(ctx context.Context, client api.Client, query string, r v1.Range, opts ...Option) (res []MatrixResult, err error)

QueryRange implements prometheus range query.

Example
// An example of how to query Prometheus metrics by range.
log.SetLevel(log.DebugLevel)
log.SetOutput(os.Stdout)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()

// new prometheus client
client, err := api.NewClient(api.Config{
	Address: "http://localhost:80",
})
if err != nil {
	log.Errorf("Error creating client: %v\n", err)
	return
}

end := time.Now().UTC()

r := v1.Range{
	Start: end.Add(-4 * time.Minute),
	End:   end,
	Step:  time.Minute,
}
log.Debugf("start: %v, end time: %v", end.Add(-6*time.Minute), end)

p := devops.NewDevops().Prometheus()
res, err := p.QueryRange(ctx, client, "test", r, prometheus.WithTimeout(5*time.Second))
if err != nil {
	log.Errorf("Error querying Prometheus: %v\n", err)
}
fmt.Println(res)

type PushMetrics

type PushMetrics struct {
	Name    string        // description of metrics name
	Label   []string      // description of metrics label
	Metrics []PromMetrics // values of metrics label and metrics value
}

PushMetrics implements a new Prometheus metrics.

type ResultT

type ResultT interface {
	MetricValues | []MatrixResult | []VectorResult
}

type RewriteAPI

type RewriteAPI interface {
	// QueryRange performs a query for the given range.
	QueryRange(ctx context.Context, query string, r v1.Range, opts ...Option) ([]byte, Warnings, error)
}

RewriteAPI provides bindings for refactoring the v1 API for Prometheus.

func NewAPI

func NewAPI(c api.Client) RewriteAPI

NewAPI returns a new API for the client.

It is safe to use the returned API from multiple goroutines.

type VectorResult

type VectorResult struct {
	Metric string       `json:"metric"`
	Values MetricValues `json:"values"`
}

type Warnings

type Warnings []string

Warnings is an array of non-critical errors

Jump to

Keyboard shortcuts

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