metrics

package
v0.14.3-rc2 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

README

Task Metrics using Prometheus in Go

Table of Contents

Introduction

This project provides a comprehensive metrics collection and reporting framework integrated with Prometheus for Go applications.

Features

  • Different types of metrics collectors including Counter, Gauge, and Histogram.
  • Built-in Prometheus server.
  • Strong typing to prevent metrics misuse.
  • Error handling and logging integrated with Uber's Zap library.
  • Support for custom labels.
  • Thread-safe metric updates with read-write mutexes.
  • Auto-registering of metrics based on type.

Code Structure

  • /metrics/collectors/: Contains the implementation of various metrics collectors (Counter, Gauge, Histogram).
  • /metrics/handler.go: Contains the MetricHandler interface and taskMetrics UpdateMetric, IncrementMetric, and DecrementMetric methods for updating metrics.
  • /metrics/prometheus.go: Defines the Prometheus server.
  • /metrics/register.go: Responsible for registering metrics with the Prometheus server.

Metrics Collectors

Counter
  • File: /metrics/collectors/counter.go
  • Methods: Update
  • Usage: Counters are cumulative metrics that can only increase.
Gauge
  • File: /metrics/collectors/gauge.go
  • Methods: Update, Inc, Dec
  • Usage: Gauges are metrics that can arbitrarily go up and down.
Histogram
  • File: /metrics/collectors/histogram.go
  • Methods: Update
  • Usage: Histograms count observations (like request durations or response sizes) and place them in configurable buckets.

Usage

Starting the Server
metricsServer := metrics.NewTaskMetrics("/metrics", "9090")
err := metricsServer.Start()
if err != nil {
    log.Fatal(err)
}
Registering Metrics
// Without labels
err = metricsServer.RegisterMetric("my_counter", "This is a counter metric", nil, &collectors.Counter{})

// With labels
err = metricsServer.RegisterMetric("my_counter_with_labels", "This is a counter metric with labels", []string{"label1", "label2"}, &collectors.Counter{})

// Register a gauge
err = metricsServer.RegisterMetric("my_gauge", "This is a gauge metric", nil, &collectors.Gauge{})
Updating Metrics
// Without labels
metricsServer.UpdateMetric("my_counter", 1)

// With labels
metricsServer.UpdateMetric("my_counter_with_labels", 1, "label1_value", "label2_value")

// Increment a gauge
metricsServer.IncrementMetric("my_gauge")

// Decrement a gauge
metricsServer.DecrementMetric("my_gauge")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IncrementDecrementHandler added in v0.6.0

type IncrementDecrementHandler interface {
	MetricHandler
	Inc(collector prometheus.Collector, labels ...string) error
	Dec(collector prometheus.Collector, labels ...string) error
}

type MetricDetail added in v0.4.0

type MetricDetail struct {
	Collector prometheus.Collector
	Handler   MetricHandler
}

type MetricHandler added in v0.4.0

type MetricHandler interface {
	Update(collector prometheus.Collector, value float64, labels ...string) error
	Type() string
}

type TaskMetrics added in v0.3.0

type TaskMetrics interface {
	Start() error
	RegisterMetric(name string, help string, labels []string, handler MetricHandler) error
	UpdateMetric(name string, value float64, labels ...string) error
	IncrementMetric(name string, labels ...string) error
	DecrementMetric(name string, labels ...string) error
	Name() string
	Stop() error
}

func NewTaskMetrics added in v0.3.0

func NewTaskMetrics(path string, port string) TaskMetrics

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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