promwrite

package module
v0.0.0-...-ba83378 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: MIT Imports: 9 Imported by: 1

README

promwrite

Prometheus Remote Write Go client with minimal dependencies. Supports Prometheus, Cortex, VictoriaMetrics etc.

Install
go get github.com/eryajf/promwrite
Example Usage
	client := promwrite.NewClient("http://prometheus:8428/api/v1/write")
	resp, err := client.Write(context.Background(), &promwrite.WriteRequest{
		TimeSeries: []promwrite.TimeSeries{
			{
				Labels: []promwrite.Label{
					{
						Name:  "__name__",
						Value: "my_metric_name",
					},
				},
				Sample: promwrite.Sample{
					Time:  time.Now(),
					Value: 123,
				},
			},
		},
	})

If your remote write has authentication enabled, you can initialize it using the following method:

	client := promwrite.NewClient("http://prometheus:8428/api/v1/write",
		promwrite.HttpClientWithAuth(
			&http.Client{},
			&promwrite.BasicAuth{
				Username: "admin",
				Password: "xxxxxx",
			}))

	resp, err := client.Write(context.Background(), &promwrite.WriteRequest{
		TimeSeries: []promwrite.TimeSeries{
			{
				Labels: []promwrite.Label{
					{
						Name:  "__name__",
						Value: "my_metric_name",
					},
				},
				Sample: promwrite.Sample{
					Time:  time.Now(),
					Value: 123,
				},
			},
		},
	})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BasicAuth

type BasicAuth struct {
	UserName string
	PassWord string
}

type Client

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

Client is Prometheus Remote Write client.

func NewClient

func NewClient(endpoint string, options ...ClientOption) *Client

func (*Client) Write

func (p *Client) Write(ctx context.Context, req *WriteRequest, options ...WriteOption) (*WriteResponse, error)

Write sends HTTP requests to Prometheus Remote Write compatible API endpoint including Prometheus, Cortex and VictoriaMetrics.

type ClientOption

type ClientOption func(opts *clientOptions)

ClientOption is used to set custom client options.

func HttpClient

func HttpClient(client *http.Client) ClientOption

HttpClient option allows configuring custom HTTP client.

func HttpClientWithAuth

func HttpClientWithAuth(client *http.Client, auth *BasicAuth) ClientOption

HttpClient option with basic auth

type Label

type Label struct {
	Name  string
	Value string
}

type Sample

type Sample struct {
	Time  time.Time
	Value float64
}

type TimeSeries

type TimeSeries struct {
	Labels []Label
	Sample Sample
}

type WriteError

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

WriteError returned if HTTP call is finished with response status code, but it was not successful.

func (*WriteError) Error

func (e *WriteError) Error() string

func (*WriteError) StatusCode

func (e *WriteError) StatusCode() int

type WriteOption

type WriteOption func(opts *writeOptions)

func WriteHeaders

func WriteHeaders(headers map[string]string) WriteOption

WriteHeaders allows passing custom HTTP headers. Once common use case is to pass `X-Scope-OrgID` header for Cortex tenant.

type WriteRequest

type WriteRequest struct {
	TimeSeries []TimeSeries
}

type WriteResponse

type WriteResponse struct {
}

Jump to

Keyboard shortcuts

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