telemetry

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package telemetry provides a global metrics registry with pluggable backends (expvar, Prometheus, or no-op) for recording anonymous usage statistics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(m Metrics)

Init sets the global metrics implementation. Call this once at startup with your chosen backend.

func InitFromConfig

func InitFromConfig(cfg config.Config)

InitFromConfig initializes the global telemetry based on config settings. Call this early in your main() or application setup.

func RecordBytes

func RecordBytes(stage string, n int64)

RecordBytes records n bytes processed at the named pipeline stage.

func RecordCapture

func RecordCapture(sourceKind string)

RecordCapture increments the capture event counter for the given source kind.

func RecordError

func RecordError(category string)

RecordError increments the error counter for the given category.

func RecordUpload

func RecordUpload(success bool, durationMs int64)

RecordUpload records the outcome and duration of an upload attempt.

func Snapshot

func Snapshot() map[string]any

Snapshot returns the current metric values as a plain map, useful for debugging.

func TimedUpload

func TimedUpload(ctx context.Context, fn func(context.Context) error) error

TimedUpload is a helper to measure and record upload duration

Types

type ExpvarMetrics

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

ExpvarMetrics implements Metrics using Go's built-in expvar package. Expvar publishes metrics at /debug/vars (HTTP handler can be registered separately).

func NewExpvarMetrics

func NewExpvarMetrics() *ExpvarMetrics

NewExpvarMetrics creates and registers an expvar-based metrics collector.

func (*ExpvarMetrics) RecordBytes

func (e *ExpvarMetrics) RecordBytes(stage string, n int64)

func (*ExpvarMetrics) RecordCapture

func (e *ExpvarMetrics) RecordCapture(sourceKind string)

func (*ExpvarMetrics) RecordError

func (e *ExpvarMetrics) RecordError(category string)

func (*ExpvarMetrics) RecordUpload

func (e *ExpvarMetrics) RecordUpload(success bool, durationMs int64)

func (*ExpvarMetrics) Snapshot

func (e *ExpvarMetrics) Snapshot() map[string]any

type Metrics

type Metrics interface {
	// RecordCapture increments capture event count and tracks source type
	RecordCapture(sourceKind string)

	// RecordBytes tracks bytes processed (ingested, normalized, uploaded)
	RecordBytes(stage string, n int64)

	// RecordUpload increments upload attempts and tracks success/failure
	RecordUpload(success bool, durationMs int64)

	// RecordError increments error count by category
	RecordError(category string)

	// Snapshot returns current metrics as a map for debugging/export
	Snapshot() map[string]any
}

Metrics represents the core telemetry interface for collecting anonymized metrics. Implementations include expvar (default) and prometheus (optional).

func Current

func Current() Metrics

Current returns the active metrics implementation

type NoopMetrics

type NoopMetrics struct{}

NoopMetrics is a silent implementation for when telemetry is disabled.

func (*NoopMetrics) RecordBytes

func (n *NoopMetrics) RecordBytes(stage string, bytes int64)

func (*NoopMetrics) RecordCapture

func (n *NoopMetrics) RecordCapture(sourceKind string)

func (*NoopMetrics) RecordError

func (n *NoopMetrics) RecordError(category string)

func (*NoopMetrics) RecordUpload

func (n *NoopMetrics) RecordUpload(success bool, durationMs int64)

func (*NoopMetrics) Snapshot

func (n *NoopMetrics) Snapshot() map[string]any

type PrometheusMetrics

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

PrometheusMetrics provides Prometheus-compatible metrics collection. Each instance owns its own prometheus.Registry for full isolation. The Prometheus counters are the single source of truth — Snapshot() reads directly from them, eliminating dual bookkeeping.

func NewPrometheusMetrics

func NewPrometheusMetrics() *PrometheusMetrics

NewPrometheusMetrics creates a new Prometheus metrics collector with its own registry.

func (*PrometheusMetrics) Handler

func (p *PrometheusMetrics) Handler() http.Handler

Handler returns an HTTP handler for the /metrics endpoint. The handler only exposes metrics from this instance's registry.

Example usage:

http.Handle("/metrics", metrics.Handler())
http.ListenAndServe(":2112", nil)

func (*PrometheusMetrics) RecordBytes

func (p *PrometheusMetrics) RecordBytes(stage string, n int64)

RecordBytes records the number of bytes processed at a given stage.

func (*PrometheusMetrics) RecordCapture

func (p *PrometheusMetrics) RecordCapture(sourceKind string)

RecordCapture increments the capture counter for a given source kind.

func (*PrometheusMetrics) RecordError

func (p *PrometheusMetrics) RecordError(category string)

RecordError increments the error counter for a given category.

func (*PrometheusMetrics) RecordUpload

func (p *PrometheusMetrics) RecordUpload(success bool, durationMs int64)

RecordUpload records an upload attempt with its success status and duration.

func (*PrometheusMetrics) Snapshot

func (p *PrometheusMetrics) Snapshot() map[string]any

Snapshot returns a point-in-time view of all metrics by reading directly from the Prometheus registry. This is the single source of truth.

Jump to

Keyboard shortcuts

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