promwriter

package module
v0.0.0-...-5fa3130 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2026 License: MIT Imports: 13 Imported by: 0

README

Promwriter

Simple lib for writing Prometheus data


	cfg := Config{
		URL:              srv.URL,
		//MaxBatchDuration: time.Second * 3,
		//MaxBatchLength:   1000,
		//Logger:           zapSugaredLogger, // or 
	}
    writer, err := promwriter.New(cfg)
    metric := promwriter.Metric{
		Name:    "test_metric",
		Labels:  map[string]string{"host": "unit-test", "zone": "us-east-1"},
		Value:   42.5,
		Counter: true, // adds "_total" to metric name
		TS:      Time.Now(),
	}
    err := writer.WriteMetric(metric)

will error out if queue is full, WriteMetric waits up to MaxBatchDuration for timeout

Alternatively you can use text representation

    metric := promwriter.Metric{
		Name:    "test_metric",
		Labels:  map[string]string{"host": "unit-test", "zone": "us-east-1"},
		Value:   42.5,
		Counter: true, // adds "_total" to metric name
		TS:      Time.Now(),
	}
    n,err := metric.Write(ioWriter)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	InstanceName     string `yaml:"-"`
	URL              string
	Timeout          time.Duration      `yaml:"timeout"`
	MaxBatchDuration time.Duration      `yaml:"max_batch_duration"`
	MaxBatchLength   int                `yaml:"max_batch_length"`
	HostLabelFile    string             `yaml:"host_label_file"`
	Logger           *zap.SugaredLogger `yaml:"-"`
}

type Metric

type Metric struct {
	Name    string
	Labels  map[string]string
	Value   float64
	Counter bool
	TS      time.Time
}

func (*Metric) Write

func (p *Metric) Write(f io.Writer) (n int, err error)

Write writes text format Prometheus data.

type PromWriter

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

func New

func New(cfg Config) (*PromWriter, error)

func (*PromWriter) WriteChannel

func (p *PromWriter) WriteChannel() chan Metric

func (*PromWriter) WriteMetric

func (p *PromWriter) WriteMetric(m Metric) error

type QueueFullError

type QueueFullError struct{}

func (QueueFullError) Error

func (e QueueFullError) Error() string

Jump to

Keyboard shortcuts

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