meters

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DimsContainedIn

func DimsContainedIn(checkDims Dimensions, eventDims Dimensions) bool

DimsContains checks if the given dimensions contain all the dimensions in checkDims.

func InjectContext

func InjectContext(ctx context.Context, m MeterRecorder) context.Context

InjectContext adds a MeterRecorder to the context, allowing it to be retrieved later.

func PeriodSpan

func PeriodSpan(period string) (time.Time, time.Time, error)

PeriodSpan returns the start and end time for a given period.

func WithMeter

func WithMeter(apiMeter MeterProvider, meterName string, meterValue float64, dims Dimensions) func(http.Handler) http.Handler

WithMeter is a middleware function that wraps an http.Handler to provide metering functionality. It checks the rate limits for a given meter and records events if the request is successful. It uses the provided MeterProvider to create a Meterer for the current user context. The meterName is the name of the meter, meterValue is the value to be recorded, and dims are the dimensions associated with the meter event. If the rate limit is exceeded, it responds with a 429 Too Many Requests status code. If the request is successful (status code < 400), it meters the event using the Meterer.

Types

type Dimension

type Dimension struct {
	Key   string
	Value string
}

Dimension represents a key-value pair used for metering dimensions.

type Dimensions

type Dimensions []Dimension

type MeterEvent

type MeterEvent struct {
	EventID      string
	Name         string
	Value        float64
	Timestamp    time.Time
	Dimensions   Dimensions
	RequestID    string        // Request ID associated with the event, useful for tracing
	StatusCode   int           // HTTP status code of the request that generated this event
	Success      bool          // Indicates if the event was successful (e.g., HTTP status < 400)
	Duration     time.Duration // Duration of the request
	ResponseSize int64         // Size of the response body in bytes
}

MeterEvent represents a metered event with a name, value, timestamp, dimensions, and request ID.

func NewMeterEvent

func NewMeterEvent(name string, value float64, dims Dimensions) MeterEvent

NewMeterEvent creates a new MeterEvent with the current time in UTC.

type MeterProvider

type MeterProvider interface {
	// NewMeter creates a new Meterer for the given MeterUser.
	NewMeter(MeterUser) Meterer
	// Close and Flush are used to clean up resources and ensure all data is written out.
	Close() error
	// Flush is used to ensure all buffered data is written out.
	Flush() error
}

MeterProvider is an interface for creating new Meterers. It also provides methods for closing the provider and flushing any buffered data. The NewMeter method takes a MeterUser, which provides user-specific context for metering. The Close method is used to clean up resources, and Flush is used to ensure all data is written out.

type MeterReader

type MeterReader interface {
	GetValue(context.Context, string, time.Time, time.Time, Dimensions) (float64, bool)
	Check(context.Context, string, float64, Dimensions) (bool, error)
}

MeterReader is an interface for reading metered values and checking rate limits.

type MeterRecorder

type MeterRecorder interface {
	Meter(context.Context, MeterEvent) error
	ApplyDimension(key, value string)
}

MeterRecorder is an interface for recording metered events. ApplyDimension mutates the MeterRecorder and adds a dimension that will be applied to all future Meter calls.

func ForContext

func ForContext(ctx context.Context) MeterRecorder

ForContext retrieves the MeterRecorder from the context.

type MeterUser

type MeterUser interface {
	ID() string
	GetExternalData(string) (string, bool)
}

MeterUser is an interface representing a user for metering purposes. It provides an ID method to get the user's identifier and a GetExternalData method to retrieve external data associated with the user, which can be used for metering purposes.

type Meterer

type Meterer interface {
	MeterReader
	MeterRecorder
}

Meterer combines both MeterReader and MeterRecorder interfaces.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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