fakemetrics

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2025 License: MIT Imports: 6 Imported by: 0

README

FakeMetrics

FakeMetrics is a Go package designed for generating fake metrics that can be integrated with Prometheus or Victoria Metrics. This package is ideal for testing and demo purposes, providing a simple way to simulate metric data without needing a live system.

Features

  • Fake Counters: Automatically increments counters with random values.
  • Fake Gauges: Generates gauges that return random float values.
  • Fake Histograms: Updates histograms with random values.
  • Customizable: Configure the number of each metric type, update intervals, prefixes, and static labels.
  • Update Metrics Toggle: Choose whether to periodically update generated metrics via the UpdateMetrics configuration flag.

Installation

To use FakeMetrics in your Go project, install it using go get:

bash

go get github.com/wabel-v0/fakemetrics

Then import it in your code:

go

import (
    "github.com/wabel-v0/fakemetrics"
)

Usage

Below is a basic example of how to use FakeMetrics:

go

package fakemetrics

  

 import (

 "net/http"

 "time"

  

 "github.com/VictoriaMetrics/metrics"

"github.com/wabel-v0/fakemetrics"

 )

  

 func main() {

//  Configure metrics generation

cfg := fakemetrics.Config{

 MetricPrefix: "app_",

 NumCounters: 5,

 NumGauges: 3,

 NumHistograms: 2,

 UpdateInterval: 2 * time.Second,

 Labels: map[string]string{

 "environment": "production",

 },
// Set UpdateMetrics to true to periodically update generated metrics.
// Set to false if you want to only register metrics  and not update them.
UpdateMetrics: true,

 }

  

// Create and start generator

 gen := fakemetrics.New(cfg)

 gen.Start()

 defer gen.Stop()

  

 // Set up metrics endpoint

 http.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {

 metrics.WritePrometheus(w, true)

 })

  

 // Start server

 http.ListenAndServe(":8080", nil)

 }

Configuration

The Config struct allows you to customize the behavior of the metrics generator:

go

type Config struct {
    MetricPrefix   string            // Prefix added to each metric name (default: "fake_")
    NumCounters    int               // Number of counters to create (default: 10)
    NumGauges      int               // Number of gauges to create (default: 10)
    NumHistograms  int               // Number of histograms to create (default: 10)
    UpdateInterval time.Duration     // Interval for updating metric values (default: 2s)
    Labels         map[string]string // A set of labels to be appended to each metric (default: {"environment": "lazy"})
	UpdateMetrics  bool              // If true, periodically update generated metrics (default: false)

}

How It Works

  1. Metric Creation: Upon starting, the generator creates the specified number of counters, gauges, and histograms. Each metric name is prefixed with MetricPrefix, and additional labels are appended in Prometheus style (e.g., key="value").

  2. Metric Updates: If enabled by the UpdateMetrics flag, a goroutine runs on a ticker based on the UpdateInterval. Each tick performs the following:

  • Counters are incremented by a random value.
  • Gauges provide a new random float value (via a dynamic function).
  • Histograms are updated with new random float values.
  1. Graceful Stop: The generator can be stopped gracefully, ensuring that the background goroutine exits cleanly.

Dependencies

This project uses the following libraries:

Feel free to contribute or open issues if you have any questions. Enjoy testing!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	MetricPrefix   string
	NumCounters    int
	NumGauges      int
	NumHistograms  int
	UpdateInterval time.Duration
	Labels         map[string]string
	UpdateMetrics  bool
}

Config defines the fake metrics generation parameters

type Generator

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

Generator creates and manages fake metrics

func New

func New(cfg Config) *Generator

New creates a metrics generator with default values

func (*Generator) Start

func (g *Generator) Start()

Start begins updating metric values

func (*Generator) Stop

func (g *Generator) Stop()

Stop halts metric updates

Jump to

Keyboard shortcuts

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