prometheus

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package prometheus provides Prometheus metrics implementation for httpcache. This package is optional and only imported when Prometheus metrics are needed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collector

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

Collector implements metrics.Collector for Prometheus

func NewCollector

func NewCollector() *Collector

NewCollector creates a new Prometheus collector with default registry and configuration

func NewCollectorWithConfig

func NewCollectorWithConfig(config CollectorConfig) *Collector

NewCollectorWithConfig creates a new Prometheus collector with custom configuration

func NewCollectorWithRegistry

func NewCollectorWithRegistry(reg prometheus.Registerer) *Collector

NewCollectorWithRegistry creates a new Prometheus collector with a custom registry

func (*Collector) RecordCacheEntries

func (c *Collector) RecordCacheEntries(backend string, count int64)

RecordCacheEntries records current number of cache entries

func (*Collector) RecordCacheOperation

func (c *Collector) RecordCacheOperation(operation, backend, result string, duration time.Duration)

RecordCacheOperation records a cache operation

func (*Collector) RecordCacheSize

func (c *Collector) RecordCacheSize(backend string, sizeBytes int64)

RecordCacheSize records current cache size

func (*Collector) RecordHTTPRequest

func (c *Collector) RecordHTTPRequest(method, cacheStatus string, statusCode int, duration time.Duration)

RecordHTTPRequest records an HTTP request

func (*Collector) RecordHTTPResponseSize

func (c *Collector) RecordHTTPResponseSize(cacheStatus string, sizeBytes int64)

RecordHTTPResponseSize records HTTP response size

func (*Collector) RecordStaleResponse

func (c *Collector) RecordStaleResponse(errorType string)

RecordStaleResponse records a stale response served on error

type CollectorConfig

type CollectorConfig struct {
	// Registry is the Prometheus registry to use. If nil, uses prometheus.DefaultRegisterer
	Registry prometheus.Registerer

	// Namespace for metrics (default: "httpcache")
	Namespace string

	// Subsystem for metrics (optional)
	Subsystem string

	// ConstLabels are labels added to all metrics
	ConstLabels prometheus.Labels
}

CollectorConfig provides configuration options for the Prometheus collector

type InstrumentedCache

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

InstrumentedCache wraps an httpcache.Cache with Prometheus metrics

func NewInstrumentedCache

func NewInstrumentedCache(cache httpcache.Cache, backend string, collector metrics.Collector) *InstrumentedCache

NewInstrumentedCache creates a new instrumented cache that records metrics for all cache operations.

Parameters:

  • cache: the underlying cache implementation to wrap
  • backend: the name of the cache backend (e.g., "memory", "redis", "leveldb")
  • collector: the metrics collector (if nil, uses metrics.DefaultCollector)

Example:

collector := prometheus.NewCollector()
cache := prometheus.NewInstrumentedCache(
    httpcache.NewMemoryCache(),
    "memory",
    collector,
)

func (*InstrumentedCache) Delete

func (c *InstrumentedCache) Delete(key string)

Delete removes a value from the cache with metrics recording

func (*InstrumentedCache) Get

func (c *InstrumentedCache) Get(key string) ([]byte, bool)

Get retrieves a value from the cache with metrics recording

func (*InstrumentedCache) Set

func (c *InstrumentedCache) Set(key string, value []byte)

Set stores a value in the cache with metrics recording

type InstrumentedTransport

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

InstrumentedTransport wraps an httpcache.Transport with Prometheus metrics

func NewInstrumentedTransport

func NewInstrumentedTransport(transport *httpcache.Transport, collector metrics.Collector) *InstrumentedTransport

NewInstrumentedTransport creates a new instrumented transport that records metrics for all HTTP requests.

Parameters:

  • transport: the underlying httpcache.Transport to wrap
  • collector: the metrics collector (if nil, uses metrics.DefaultCollector)

Example:

collector := prometheus.NewCollector()
cache := prometheus.NewInstrumentedCache(
    httpcache.NewMemoryCache(),
    "memory",
    collector,
)
transport := httpcache.NewTransport(cache)
instrumentedTransport := prometheus.NewInstrumentedTransport(transport, collector)
client := instrumentedTransport.Client()

func (*InstrumentedTransport) Client

func (t *InstrumentedTransport) Client() *http.Client

Client returns an HTTP client with instrumented transport

func (*InstrumentedTransport) RoundTrip

func (t *InstrumentedTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip executes an HTTP request with metrics recording

Jump to

Keyboard shortcuts

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