prometheusmetrics

package module
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2023 License: MIT Imports: 22 Imported by: 0

README

go-prometheus-metrics

Package based on official Prometheus Go client library. It provides an interface to export metrics in JSON format through queue in RabbitMQ.

More documentation can be found here.

Install

go get git.debtfair.ru/external/go-prometheus-metrics

Configure

Mandatory Name Type Description
Yes Exchange string Name of exchange in RabbitMQ.
Yes RoutingKey string Routing key between exchange and queue in RabbitMQ.
Yes ExportPeriod time.Duration Period between metrics exporting.
Yes StopTimeout time.Duration Timeout for last metrics export after graceful stop.
Yes RetryPeriod time.Duration Period between reconnection retries.
No Namespace string Name prefix before Subsystem which will be added for each registered metric.
No Subsystem string Name prefix after Namespace which will be added for each registered metric.
No ConstLabels map[string]string Labels which will be added for each registered metric.
No RetryBackoffLimit int Number of retries to reconnect to RabbitMQ.
No ExportEnabled bool This setting allows you to disable export. May be helpful in development environment.

A basic example

package main

import (
	"context"
	"errors"
	"log"
	"time"

	rabbit "git.debtfair.ru/external/amqp-reconnect"
	prometheusmetrics "git.debtfair.ru/external/go-prometheus-metrics"
)

func main() {
	ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(3*time.Second))
	defer cancel()

	conn, err := rabbit.NewConnection(ctx, &rabbit.Config{
		Host:       "localhost",
		Port:       5672,
		User:       "guest",
		Password:   "guest",
		MaxRetries: 3,
		Heartbeat:  2 * time.Second,
	})
	if err != nil {
		log.Default().Fatal(err)
	}
	defer func() {
		if err := conn.Close(); err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
			log.Default().Fatal(err)
		}
	}()

	collector := prometheusmetrics.New(
		prometheusmetrics.Deps{
			Rabbit: conn,
		},
		prometheusmetrics.Config{
			Exchange:          "direct",
      ExchangeType:      "direct",
			RoutingKey:        "prometheus",
			Namespace:         "default",
			ExportPeriod:      1 * time.Second,
			ConstLabels:       prometheusmetrics.Labels{},
			RetryBackoffLimit: 5,
			RetryPeriod:       1 * time.Second,
			ExportEnabled:     true,
			StopTimeout:       1 * time.Second,
		},
	)
	defer collector.Stop()

	if err := collector.Register(
		prometheusmetrics.Metric{
			Type:       prometheusmetrics.CounterType,
			Name:       "processed_messages",
			Help:       "Number of processed messages.",
			LabelNames: []string{"status"},
		},
		prometheusmetrics.Metric{
			Type: prometheusmetrics.GaugeType,
			Name: "message_size",
			Help: "Total size of message (in kilobytes).",
		},
		prometheusmetrics.Metric{
			Type:    prometheusmetrics.HistogramType,
			Name:    "histogram_processing_duration",
			Help:    "Duration of message processing (in milliseconds).",
			Buckets: []float64{1, 10, 50, 100, 250, 500, 1000},
		},
		prometheusmetrics.Metric{
			Type: prometheusmetrics.SummaryType,
			Name: "summary_processing_duration",
			Help: "Duration of message processing (in milliseconds).",
			Objectives: map[float64]float64{
				0.99: 0.01,
				0.95: 0.02,
				0.90: 0.03,
				0.85: 0.02,
				0.5:  0.1,
				0.25: 0.1,
			},
		},
	); err != nil {
		log.Default().Fatal(err)
	}

	collector.CounterInc("processed_messages", prometheusmetrics.Labels{"status": "ok"})
	collector.CounterInc("processed_messages", prometheusmetrics.Labels{"status": "ok"})
	collector.CounterInc("processed_messages", prometheusmetrics.Labels{"status": "error"})

	collector.GaugeSet("message_size", prometheusmetrics.Labels{}, 104)

	collector.HistogramObserve("histogram_processing_duration", prometheusmetrics.Labels{}, 10)
	collector.HistogramObserve("histogram_processing_duration", prometheusmetrics.Labels{}, 12)

	collector.SummaryObserve("summary_processing_duration", prometheusmetrics.Labels{}, 0.5)
	collector.SummaryObserve("summary_processing_duration", prometheusmetrics.Labels{}, 15)

	if err := collector.Collect(ctx); err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
		log.Default().Fatal(err)
	}
}

Example of output JSON:

[
  {
    "n": "default_cpu_usage",
    "v": 0.1,
    "l": {
      "hostname": "localhost"
    }
  },
  {
    "n": "default_histogram_processing_duration_sum",
    "v": 22,
    "l": {
      "hostname": "localhost"
    }
  },
  {
    "n": "default_histogram_processing_duration_count",
    "v": 2,
    "l": {
      "hostname": "localhost"
    }
  },
  {
    "n": "default_histogram_processing_duration_bucket",
    "v": 0,
    "l": {
      "hostname": "localhost",
      "le": "1.00"
    }
  },
  {
    "n": "default_histogram_processing_duration_bucket",
    "v": 1,
    "l": {
      "hostname": "localhost",
      "le": "10.00"
    }
  },
  {
    "n": "default_histogram_processing_duration_bucket",
    "v": 2,
    "l": {
      "le": "50.00",
      "hostname": "localhost"
    }
  },
  {
    "n": "default_histogram_processing_duration_bucket",
    "v": 2,
    "l": {
      "hostname": "localhost",
      "le": "100.00"
    }
  },
  {
    "n": "default_histogram_processing_duration_bucket",
    "v": 2,
    "l": {
      "hostname": "localhost",
      "le": "250.00"
    }
  },
  {
    "n": "default_histogram_processing_duration_bucket",
    "v": 2,
    "l": {
      "hostname": "localhost",
      "le": "500.00"
    }
  },
  {
    "n": "default_histogram_processing_duration_bucket",
    "v": 2,
    "l": {
      "hostname": "localhost",
      "le": "1000.00"
    }
  },
  {
    "n": "default_histogram_processing_duration_bucket",
    "v": 2,
    "l": {
      "le": "+Inf",
      "hostname": "localhost"
    }
  },
  {
    "n": "default_memory_usage",
    "v": 8936,
    "l": {
      "hostname": "localhost"
    }
  },
  {
    "n": "default_message_size",
    "v": 104,
    "l": {
      "hostname": "localhost"
    }
  },
  {
    "n": "default_processed_messages",
    "v": 1,
    "l": {
      "hostname": "localhost",
      "status": "error"
    }
  },
  {
    "n": "default_processed_messages",
    "v": 2,
    "l": {
      "hostname": "localhost",
      "status": "ok"
    }
  },
  {
    "n": "default_running_goroutines",
    "v": 6,
    "l": {
      "hostname": "localhost"
    }
  },
  {
    "n": "default_summary_processing_duration_sum",
    "v": 15.5,
    "l": {
      "hostname": "localhost"
    }
  },
  {
    "n": "default_summary_processing_duration_count",
    "v": 2,
    "l": {
      "hostname": "localhost"
    }
  },
  {
    "n": "default_summary_processing_duration",
    "v": 0.5,
    "l": {
      "quantile": "0.25",
      "hostname": "localhost"
    }
  },
  {
    "n": "default_summary_processing_duration",
    "v": 0.5,
    "l": {
      "hostname": "localhost",
      "quantile": "0.50"
    }
  },
  {
    "n": "default_summary_processing_duration",
    "v": 15,
    "l": {
      "hostname": "localhost",
      "quantile": "0.85"
    }
  },
  {
    "n": "default_summary_processing_duration",
    "v": 15,
    "l": {
      "hostname": "localhost",
      "quantile": "0.90"
    }
  },
  {
    "n": "default_summary_processing_duration",
    "v": 15,
    "l": {
      "hostname": "localhost",
      "quantile": "0.95"
    }
  },
  {
    "n": "default_summary_processing_duration",
    "v": 15,
    "l": {
      "hostname": "localhost",
      "quantile": "0.99"
    }
  }
]

The MIT License (MIT) - see LICENSE for more details.

Documentation

Index

Constants

View Source
const (
	CounterType   = "counter"
	GaugeType     = "gauge"
	SummaryType   = "summary"
	HistogramType = "histogram"
)

Variables

View Source
var (
	ErrUnknownMetricType       = errors.New("unknown metric type")
	ErrMetricNotRegistered     = errors.New("metric not registered")
	ErrCollectorAlreadyStopped = errors.New("collector already stopped")
	ErrChannelNotRecovered     = errors.New("can`t recover RabbitMQ channel")
)

Functions

This section is empty.

Types

type Collector

type Collector interface {
	// Registers provided metrics.
	// If metric can`t be registered an error will be returned.
	Register(metrics ...Metric) error

	// Collects all registered metrics and sends them into queue in RabbitMQ.
	// Register method should be called before this method.
	Collect(ctx context.Context) error
	// Stops collector and frees associated resources.
	Stop() error

	// Increments provided counter and attaches specified labels.
	// If metric not registered the ErrMetricNotRegistered will be returned.
	CounterInc(name string, labels Labels) error
	// Adds provided value to the counter and attaches specified labels.
	// If metric not registered the ErrMetricNotRegistered will be returned.
	CounterAdd(name string, labels Labels, value float64) error

	// Increments provided gauge and attaches specified labels.
	// If metric not registered the ErrMetricNotRegistered will be returned.
	GaugeInc(name string, labels Labels) error
	// Decrements provided gauge and attaches specified labels.
	// If metric not registered the ErrMetricNotRegistered will be returned.
	GaugeDec(name string, labels Labels) error
	// Adds provided value to the gauge and attaches specified labels.
	// If metric not registered the ErrMetricNotRegistered will be returned.
	GaugeAdd(name string, labels Labels, value float64) error
	// Subtracts provided value to the gauge and attaches specified labels.
	// If metric not registered the ErrMetricNotRegistered will be returned.
	GaugeSub(name string, labels Labels, value float64) error
	// Sets provided value to the gauge and attaches specified labels.
	// If metric not registered the ErrMetricNotRegistered will be returned.
	GaugeSet(name string, labels Labels, value float64) error

	// Observes provided summary with specified value and labels.
	// If metric not registered the ErrMetricNotRegistered will be returned.
	SummaryObserve(name string, labels Labels, value float64) error

	// Observes provided histogram with specified value and labels.
	// If metric not registered the ErrMetricNotRegistered will be returned.
	HistogramObserve(name string, labels Labels, value float64) error
}

func New

func New(deps Deps, config Config) (Collector, error)

Returns a new metrics Collector.

type Config

type Config struct {
	Exchange          string        // RabbitMQ exchange
	RoutingKey        string        // RabbitMQ routing key queue and exchange
	Namespace         string        // default prefix for all metrics
	Subsystem         string        // subsystem between <namespace> and <name> in metric name
	RetryBackoffLimit int           // max number of retries to reconnect to RabbitMQ before collector panics
	ExportEnabled     bool          // is export enabled? if false, metrics will not be sent to RabbitMQ
	ExportPeriod      time.Duration // period, when collector collects all metrics into the batch
	StopTimeout       time.Duration // timeout for graceful stop
	RetryPeriod       time.Duration // period between retries
	ConstLabels       Labels        // default labels for all metrics
}

type DTO

type DTO struct {
	Name   string            `json:"n"`
	Type   string            `json:"t"`
	Value  float64           `json:"v"`
	Labels map[string]string `json:"l"`
}

func (DTO) MarshalEasyJSON

func (v DTO) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

type Deps

type Deps struct {
	Rabbit *rabbit.Connection // TCP connection for RabbitMQ
}

type Labels

type Labels = map[string]string

type Metric

type Metric struct {
	Type       string   // metric type (counter/gauge/summary/histogram)
	Name       string   // metric name
	Help       string   // description
	LabelNames []string // possible labels

	// Summary properties
	Objectives map[float64]float64 // percentile groups
	MaxAge     time.Duration       // max age of observations
	AgeBuckets uint32              // number of buckets used to exclude observations that are older than MaxAge from the summary
	BufCap     uint32              // default sample stream buffer size

	// Histogram properties
	Buckets []float64 // buckets into which observations are counted
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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