metrics

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package metrics provides a unified and simplified interface for instrumenting applications with metrics, abstracting away the specifics of different monitoring backends like Datadog and OpenTelemetry.

This library allows developers to easily record common metric types such as counters, gauges, and histograms without needing to directly manage different client libraries.

Features

- Simple, clean API for recording metrics (`Increment`, `Gauge`, `Histogram`). - Unified configuration for service and business context. - Swappable backends (Datadog, OpenTelemetry) via configuration. - Automatic tag conversion to backend-specific formats.

Basic Usage

To get started, create a `ClientConfig` struct, then pass it to `NewMetricsClient`. The client can then be used to send metrics. It is crucial to call `Close()` on the client before the application terminates to ensure all buffered metrics are sent.

// 1. Configure the client
config := &metrics.ClientConfig{
	MetricCollectorType: metrics.DatadogCollector, // or metrics.OtelCollector
	Server: metrics.ServerConfig{
		Host:         "127.0.0.1",
		Port:         8125, // Port for Datadog Agent (statsd)
		MetricPrefix: "myapp",
	},
	Service: metrics.ServiceConfig{
		Name:    "my-awesome-service",
		Version: "1.0.2",
	},
}

// 2. Create a new client
client, err := metrics.NewMetricsClient(config)
if err != nil {
	log.Fatalf("Failed to create metrics client: %v", err)
}
defer client.Close() // Ensure metrics are flushed on exit

// 3. Use the client to send metrics
tags := types.Tags{
	{Name: "endpoint", Value: "/login"},
	{Name: "status", Value: 200},
}
client.Increment("http.requests.total", 1, tags)
client.Histogram("http.request.duration_ms", 150.5, tags)

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrHandlerNotFound is returned when the specified metric collector type is not supported.
	ErrHandlerNotFound = errors.New("metric handler was not found")
	// ErrConfigNotFound is returned when a nil configuration is provided to the client constructor.
	ErrConfigNotFound = errors.New("client configuration was not found")
)

Functions

This section is empty.

Types

type Client

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

Client is the main metrics client used to send data to a collector.

func NewMetricsClient

func NewMetricsClient(c *ClientConfig) (*Client, error)

NewMetricsClient creates and initializes a new metrics client based on the provided configuration. It selects the appropriate backend handler (e.g., Datadog, OpenTelemetry) based on the MetricCollectorType specified in the config.

func (*Client) Close

func (c *Client) Close() error

Close gracefully shuts down the metrics client and flushes any buffered metrics. It should be called before the application exits.

func (*Client) Gauge

func (c *Client) Gauge(metric string, value float64, tags types.Tags) error

Gauge sends a gauge metric, which represents a single numerical value that can arbitrarily go up and down.

func (*Client) Histogram

func (c *Client) Histogram(metric string, value float64, tags types.Tags) error

Histogram sends a histogram metric, which samples observations (e.g., request durations) and calculates statistical distributions.

func (*Client) Increment

func (c *Client) Increment(metric string, value int64, tags types.Tags) error

Increment sends a counter metric, which adds a value to a running total. The value is typically 1, but can be any integer.

type ClientConfig

type ClientConfig struct {
	// MetricCollectorType indicates which metrics backend will be used (e.g., Datadog, OTEL).
	MetricCollectorType CollectorType
	// Server holds the server connection details.
	Server ServerConfig `json:"server"`
	// Service holds information about the emitting service.
	Service ServiceConfig `json:"service"`
	// Solution holds business context for the metrics.
	Solution SolutionConfig `json:"solution"`
}

ClientConfig holds all configuration needed to initialize a metrics client.

type CollectorType

type CollectorType string

CollectorType defines the type of metrics collector backend.

const (
	// DatadogCollector represents the Datadog collector type.
	DatadogCollector CollectorType = "datadog"
	// OtelCollector represents the OpenTelemetry collector type.
	OtelCollector CollectorType = "otel"
)

type ServerConfig

type ServerConfig struct {
	// Host is the hostname or IP address of the metrics collector.
	Host string `json:"host"`
	// Port is the port number used by the metrics collector.
	Port int `json:"port"`
	// MetricPrefix is a string prefixed to all metric names.
	MetricPrefix string `json:"metricPrefix"`
}

ServerConfig holds the configuration for the metrics collector server.

type ServiceConfig

type ServiceConfig struct {
	// Name of the service responsible for the metrics.
	Name string `json:"service"`
	// Version is the current version of the service.
	Version string `json:"version"`
}

ServiceConfig contains information about the service emitting the metrics.

type SolutionConfig

type SolutionConfig struct {
	// Team indicates the name of the team/squad responsible for this service.
	Team string `json:"team"`
	// Solution indicates the name of the solution or application.
	Solution string `json:"solution"`
	// Domain indicates the business domain of the solution (DDD).
	Domain string `json:"domain"`
	// Product indicates the name of the product this service belongs to.
	Product string `json:"product"`
	// CustomTags is an additional field to create custom tags not covered by the default object.
	CustomTags types.Tags `json:"tags"`
}

SolutionConfig provides business context for the metrics.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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