prometheus

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2023 License: Apache-2.0 Imports: 10 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 Matrix added in v1.0.6

type Matrix []MatrixResult

Matrix is a list of time series.

func (Matrix) ConvertByte added in v1.0.6

func (m Matrix) ConvertByte() ([]byte, error)

type MatrixResult

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

MatrixResult obtains the matrix result 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
	Query(ctx context.Context, client api.Client, query string, endTime int64) (ResultValues, error)
	QueryRange(ctx context.Context, client api.Client, query string, r v1.Range, opts ...v1.Option) (ResultValues, error)
}

MetricsInterface is the interface that implements prometheus push metrics and range 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

ExamplePrometheus_Push demonstrates how to use Prometheus.Push. Open the link to see an example: https://go.dev/play/p/ViGSBJbtGKz

// 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)
}
Output:

func (*Prometheus) Query added in v1.0.6

func (p *Prometheus) Query(ctx context.Context, client api.Client, query string, endTime int64) (ResultValues, error)
Example (Scalar)

ExamplePrometheus_Query_vector demonstrates how to query scalar data using Prometheus.Query. Open the link to see an example: https://go.dev/play/p/cbqzfN1JDOE

// 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://10.158.215.90:80",
})
if err != nil {
	log.Errorf("Error creating client: %v\n", err)
	return
}

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

p := devops.NewDevops().Prometheus()
res, err := p.Query(ctx, client, "dynatrace_api_latency", end)
if err != nil {
	log.Errorf("Error querying Prometheus: %v\n", err)
}
vector, err := res.ConvertByte()
if err != nil {
	log.Errorf("Convert matrix result to byte error, err: %v", err)
}
var result prometheus.Vector
if err := jsoniter.Unmarshal(vector, &result); err != nil {
	log.Errorf("error jsoniter unmarshal result to prometheus.Matrix, err: %v", err)
}
fmt.Println(result)
Output:

Example (Vector)

ExamplePrometheus_Query_vector demonstrates how to query vector data using Prometheus.Query. Open the link to see an example: https://go.dev/play/p/cbqzfN1JDOE

// 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://10.158.215.90:80",
})
if err != nil {
	log.Errorf("Error creating client: %v\n", err)
	return
}

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

p := devops.NewDevops().Prometheus()
res, err := p.Query(ctx, client, "dynatrace_api_latency", end)
if err != nil {
	log.Errorf("Error querying Prometheus: %v\n", err)
}
vector, err := res.ConvertByte()
if err != nil {
	log.Errorf("Convert matrix result to byte error, err: %v", err)
}
var result prometheus.Vector
if err := jsoniter.Unmarshal(vector, &result); err != nil {
	log.Errorf("error jsoniter unmarshal result to prometheus.Matrix, err: %v", err)
}
fmt.Println(result)
Output:

func (*Prometheus) QueryRange

func (p *Prometheus) QueryRange(ctx context.Context, client api.Client, query string, r v1.Range, opts ...v1.Option) (ResultValues, error)

QueryRange implements prometheus range query.

Example

ExamplePrometheus_QueryRange demonstrates how to use Prometheus.QueryRange. Open the link to see an example: https://go.dev/play/p/32LVSjFs2hU

// 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://10.158.215.90: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(-4*time.Minute), end)

p := devops.NewDevops().Prometheus()
res, err := p.QueryRange(ctx, client, "dynatrace_api_latency", r, v1.WithTimeout(5*time.Second))
if err != nil {
	log.Errorf("Error querying Prometheus: %v\n", err)
}
matrix, err := res.ConvertByte()
if err != nil {
	log.Errorf("Convert matrix result to byte error, err: %v", err)
}
var result prometheus.Matrix
if err := jsoniter.Unmarshal(matrix, &result); err != nil {
	log.Errorf("error jsoniter unmarshal result to prometheus.Matrix, err: %v", err)
}
fmt.Println(result)
Output:

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 ResultValues added in v1.0.6

type ResultValues interface {
	ConvertByte() ([]byte, error)
}

type Scalar added in v1.0.6

type Scalar ScalarResult

Scalar is scalar result

func (Scalar) ConvertByte added in v1.0.6

func (s Scalar) ConvertByte() ([]byte, error)

type ScalarResult added in v1.0.6

type ScalarResult struct {
	Value MetricValues `json:"value"`
}

ScalarResult obtains the scalar result from the prometheus query.

type Vector added in v1.0.6

type Vector []VectorResult

Vector is a list of vector result from the prometheus query

func (Vector) ConvertByte added in v1.0.6

func (v Vector) ConvertByte() ([]byte, error)

type VectorResult

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

VectorResult obtains the vector result from the prometheus query.

Jump to

Keyboard shortcuts

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