util

package
v0.32.2 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2023 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteMatchingLabels

func DeleteMatchingLabels(c CollectorVec, filter map[string]string) error

DeleteMatchingLabels removes metric with labels matching the filter.

func DurationWithJitter

func DurationWithJitter(input time.Duration, variancePerc float64) time.Duration

DurationWithJitter returns random duration from "input - input*variance" to "input + input*variance" interval.

func DurationWithPositiveJitter

func DurationWithPositiveJitter(input time.Duration, variancePerc float64) time.Duration

DurationWithPositiveJitter returns random duration from "input" to "input + input*variance" interval.

func FormatTimeMillis

func FormatTimeMillis(ms int64) string

FormatTimeMillis returns a human readable version of the input time (in milliseconds).

func FormatTimeModel

func FormatTimeModel(t model.Time) string

FormatTimeModel returns a human readable version of the input time.

func FromLabelPairsToLabels

func FromLabelPairsToLabels(pairs []*dto.LabelPair) labels.Labels

FromLabelPairsToLabels converts dto.LabelPair into labels.Labels.

func GetLabels

func GetLabels(c prometheus.Collector, filter map[string]string) ([]labels.Labels, error)

GetLables returns list of label combinations used by this collector at the time of call. This can be used to find and delete unused metrics.

func GetSumOfHistogramSampleCount

func GetSumOfHistogramSampleCount(families []*dto.MetricFamily, metricName string, matchers labels.Selector) uint64

GetSumOfHistogramSampleCount returns the sum of samples count of histograms matching the provided metric name and optional label matchers. Returns 0 if no metric matches.

func IsRequestBodyTooLarge

func IsRequestBodyTooLarge(err error) bool

IsRequestBodyTooLarge returns true if the error is "http: request body too large".

func LabelsToMetric

func LabelsToMetric(ls labels.Labels) model.Metric

LabelsToMetric converts a Labels to Metric Don't do this on any performance sensitive paths.

func MergeNSampleSets

func MergeNSampleSets(sampleSets ...[]model.SamplePair) []model.SamplePair

MergeNSampleSets merges and dedupes n sets of already sorted sample pairs.

func MergeSampleSets

func MergeSampleSets(a, b []model.SamplePair) []model.SamplePair

MergeSampleSets merges and dedupes two sets of already sorted sample pairs.

func NewDisableableTicker

func NewDisableableTicker(interval time.Duration) (func(), <-chan time.Time)

NewDisableableTicker essentially wraps NewTicker but allows the ticker to be disabled by passing zero duration as the interval. Returns a function for stopping the ticker, and the ticker channel.

func ParseProtoReader

func ParseProtoReader(ctx context.Context, reader io.Reader, expectedSize, maxSize int, req proto.Message, compression CompressionType) error

ParseProtoReader parses a compressed proto from an io.Reader.

func ParseTime

func ParseTime(s string) (int64, error)

ParseTime parses the string into an int64, milliseconds since epoch.

func RenderHTTPResponse

func RenderHTTPResponse(w http.ResponseWriter, v interface{}, t *template.Template, r *http.Request)

RenderHTTPResponse either responds with json or a rendered html page using the passed in template by checking the Accepts header

func SerializeProtoResponse

func SerializeProtoResponse(w http.ResponseWriter, resp proto.Message, compression CompressionType) error

SerializeProtoResponse serializes a protobuf response into an HTTP response.

func StreamWriteYAMLResponse

func StreamWriteYAMLResponse(w http.ResponseWriter, iter chan interface{}, logger log.Logger)

StreamWriteYAMLResponse stream writes data as http response

func TimeFromMillis

func TimeFromMillis(ms int64) time.Time

TimeFromMillis is a helper to turn milliseconds -> time.Time

func TimeToMillis

func TimeToMillis(t time.Time) int64

func WriteHTMLResponse

func WriteHTMLResponse(w http.ResponseWriter, message string)

Sends message as text/html response with 200 status code.

func WriteJSONResponse

func WriteJSONResponse(w http.ResponseWriter, v interface{})

WriteJSONResponse writes some JSON as a HTTP response.

func WriteTextResponse

func WriteTextResponse(w http.ResponseWriter, message string)

Sends message as text/plain response with 200 status code.

func WriteYAMLResponse

func WriteYAMLResponse(w http.ResponseWriter, v interface{})

WriteYAMLResponse writes some YAML as a HTTP response.

Types

type ActiveUsers

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

ActiveUsers keeps track of latest user's activity timestamp, and allows purging users that are no longer active.

func NewActiveUsers

func NewActiveUsers() *ActiveUsers

func (*ActiveUsers) PurgeInactiveUsers

func (m *ActiveUsers) PurgeInactiveUsers(deadline int64) []string

PurgeInactiveUsers removes users that were last active before given deadline, and returns removed users.

func (*ActiveUsers) UpdateUserTimestamp

func (m *ActiveUsers) UpdateUserTimestamp(userID string, ts int64)

type ActiveUsersCleanupService

type ActiveUsersCleanupService struct {
	services.Service
	// contains filtered or unexported fields
}

ActiveUsersCleanupService tracks active users, and periodically purges inactive ones while running.

func NewActiveUsersCleanupService

func NewActiveUsersCleanupService(cleanupInterval, inactiveTimeout time.Duration, cleanupFn func(string)) *ActiveUsersCleanupService

func NewActiveUsersCleanupWithDefaultValues

func NewActiveUsersCleanupWithDefaultValues(cleanupFn func(string)) *ActiveUsersCleanupService

func (*ActiveUsersCleanupService) UpdateUserTimestamp

func (s *ActiveUsersCleanupService) UpdateUserTimestamp(user string, now time.Time)

type BasicAuth

type BasicAuth struct {
	Username string `yaml:"basic_auth_username"`
	Password string `yaml:"basic_auth_password"`
}

BasicAuth configures basic authentication for HTTP clients.

func (BasicAuth) IsEnabled

func (b BasicAuth) IsEnabled() bool

IsEnabled returns false if basic authentication isn't enabled.

func (*BasicAuth) RegisterFlagsWithPrefix

func (b *BasicAuth) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet)

type CollectorVec

type CollectorVec interface {
	prometheus.Collector
	Delete(labels prometheus.Labels) bool
}

CollectorVec is a collector that can delete metrics by labels. Implemented by *prometheus.MetricVec (used by CounterVec, GaugeVec, SummaryVec, and HistogramVec).

type CompressionType

type CompressionType int

CompressionType for encoding and decoding requests and responses.

const (
	NoCompression CompressionType = iota
	RawSnappy
)

Values for CompressionType

type HistogramData

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

HistogramData keeps data required to build histogram Metric.

func (*HistogramData) AddHistogram

func (d *HistogramData) AddHistogram(histo *dto.Histogram)

AddHistogram adds histogram from gathered metrics to this histogram data. Do not call this function after Metric() has been invoked, because histogram created by Metric is using the buckets map (doesn't make a copy), and it's not allowed to change the buckets after they've been passed to a prometheus.Metric.

func (*HistogramData) AddHistogramData

func (d *HistogramData) AddHistogramData(histo HistogramData)

AddHistogramData merges another histogram data into this one. Do not call this function after Metric() has been invoked, because histogram created by Metric is using the buckets map (doesn't make a copy), and it's not allowed to change the buckets after they've been passed to a prometheus.Metric.

func (*HistogramData) Copy

func (d *HistogramData) Copy() *HistogramData

Copy returns a copy of this histogram data.

func (*HistogramData) Metric

func (d *HistogramData) Metric(desc *prometheus.Desc, labelValues ...string) prometheus.Metric

Metric returns prometheus metric from this histogram data.

Note that returned metric shares bucket with this HistogramData, so avoid doing more modifications to this HistogramData after calling Metric.

type HistogramDataCollector

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

HistogramDataCollector combines histogram data, with prometheus descriptor. It can be registered into prometheus to report histogram with stored data. Data can be updated via Add method.

func NewHistogramDataCollector

func NewHistogramDataCollector(desc *prometheus.Desc) *HistogramDataCollector

NewHistogramDataCollector creates new histogram data collector.

func (*HistogramDataCollector) Add

func (*HistogramDataCollector) Collect

func (h *HistogramDataCollector) Collect(out chan<- prometheus.Metric)

func (*HistogramDataCollector) Describe

func (h *HistogramDataCollector) Describe(out chan<- *prometheus.Desc)

type MetricFamiliesPerUser

type MetricFamiliesPerUser []struct {
	// contains filtered or unexported fields
}

MetricFamiliesPerUser is a collection of metrics gathered via calling Gatherer.Gather() method on different gatherers, one per user.

func (MetricFamiliesPerUser) GetSumOfCounters

func (d MetricFamiliesPerUser) GetSumOfCounters(counter string) float64

func (MetricFamiliesPerUser) GetSumOfGauges

func (d MetricFamiliesPerUser) GetSumOfGauges(gauge string) float64

func (MetricFamiliesPerUser) SendMaxOfGauges

func (d MetricFamiliesPerUser) SendMaxOfGauges(out chan<- prometheus.Metric, desc *prometheus.Desc, gauge string)

func (MetricFamiliesPerUser) SendMaxOfGaugesPerUser

func (d MetricFamiliesPerUser) SendMaxOfGaugesPerUser(out chan<- prometheus.Metric, desc *prometheus.Desc, gauge string)

func (MetricFamiliesPerUser) SendSumOfCounters

func (d MetricFamiliesPerUser) SendSumOfCounters(out chan<- prometheus.Metric, desc *prometheus.Desc, counter string)

func (MetricFamiliesPerUser) SendSumOfCountersPerUser

func (d MetricFamiliesPerUser) SendSumOfCountersPerUser(out chan<- prometheus.Metric, desc *prometheus.Desc, counter string)

func (MetricFamiliesPerUser) SendSumOfCountersPerUserWithLabels

func (d MetricFamiliesPerUser) SendSumOfCountersPerUserWithLabels(out chan<- prometheus.Metric, desc *prometheus.Desc, metric string, labelNames ...string)

SendSumOfCountersPerUserWithLabels provides metrics with the provided label names on a per-user basis. This function assumes that `user` is the first label on the provided metric Desc

func (MetricFamiliesPerUser) SendSumOfCountersWithLabels

func (d MetricFamiliesPerUser) SendSumOfCountersWithLabels(out chan<- prometheus.Metric, desc *prometheus.Desc, counter string, labelNames ...string)

func (MetricFamiliesPerUser) SendSumOfGauges

func (d MetricFamiliesPerUser) SendSumOfGauges(out chan<- prometheus.Metric, desc *prometheus.Desc, gauge string)

func (MetricFamiliesPerUser) SendSumOfGaugesPerUser

func (d MetricFamiliesPerUser) SendSumOfGaugesPerUser(out chan<- prometheus.Metric, desc *prometheus.Desc, gauge string)

func (MetricFamiliesPerUser) SendSumOfGaugesPerUserWithLabels

func (d MetricFamiliesPerUser) SendSumOfGaugesPerUserWithLabels(out chan<- prometheus.Metric, desc *prometheus.Desc, metric string, labelNames ...string)

SendSumOfGaugesPerUserWithLabels provides metrics with the provided label names on a per-user basis. This function assumes that `user` is the first label on the provided metric Desc

func (MetricFamiliesPerUser) SendSumOfGaugesWithLabels

func (d MetricFamiliesPerUser) SendSumOfGaugesWithLabels(out chan<- prometheus.Metric, desc *prometheus.Desc, gauge string, labelNames ...string)

func (MetricFamiliesPerUser) SendSumOfHistograms

func (d MetricFamiliesPerUser) SendSumOfHistograms(out chan<- prometheus.Metric, desc *prometheus.Desc, histogramName string)

func (MetricFamiliesPerUser) SendSumOfHistogramsWithLabels

func (d MetricFamiliesPerUser) SendSumOfHistogramsWithLabels(out chan<- prometheus.Metric, desc *prometheus.Desc, histogramName string, labelNames ...string)

func (MetricFamiliesPerUser) SendSumOfSummaries

func (d MetricFamiliesPerUser) SendSumOfSummaries(out chan<- prometheus.Metric, desc *prometheus.Desc, summaryName string)

func (MetricFamiliesPerUser) SendSumOfSummariesPerUser

func (d MetricFamiliesPerUser) SendSumOfSummariesPerUser(out chan<- prometheus.Metric, desc *prometheus.Desc, summaryName string)

func (MetricFamiliesPerUser) SendSumOfSummariesWithLabels

func (d MetricFamiliesPerUser) SendSumOfSummariesWithLabels(out chan<- prometheus.Metric, desc *prometheus.Desc, summaryName string, labelNames ...string)

type MetricFamilyMap

type MetricFamilyMap map[string]*dto.MetricFamily

MetricFamilyMap is a map of metric names to their family (metrics with same name, but different labels) Keeping map of metric name to its family makes it easier to do searches later.

func NewMetricFamilyMap

func NewMetricFamilyMap(metrics []*dto.MetricFamily) (MetricFamilyMap, error)

NewMetricFamilyMap sorts output from Gatherer.Gather method into a map. Gatherer.Gather specifies that there metric families are uniquely named, and we use that fact here. If they are not, this method returns error.

func (MetricFamilyMap) MaxGauges

func (mfm MetricFamilyMap) MaxGauges(name string) float64

func (MetricFamilyMap) SumCounters

func (mfm MetricFamilyMap) SumCounters(name string) float64

func (MetricFamilyMap) SumGauges

func (mfm MetricFamilyMap) SumGauges(name string) float64

func (MetricFamilyMap) SumHistograms

func (mfm MetricFamilyMap) SumHistograms(name string) HistogramData

func (MetricFamilyMap) SumHistogramsTo

func (mfm MetricFamilyMap) SumHistogramsTo(name string, output *HistogramData)

func (MetricFamilyMap) SumSummaries

func (mfm MetricFamilyMap) SumSummaries(name string) SummaryData

func (MetricFamilyMap) SumSummariesTo

func (mfm MetricFamilyMap) SumSummariesTo(name string, output *SummaryData)

type SummaryData

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

SummaryData keeps all data needed to create summary metric

func (*SummaryData) AddSummary

func (s *SummaryData) AddSummary(sum *dto.Summary)

func (*SummaryData) Metric

func (s *SummaryData) Metric(desc *prometheus.Desc, labelValues ...string) prometheus.Metric

type UserRegistries

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

UserRegistries holds Prometheus registries for multiple users, guaranteeing multi-thread safety and stable ordering.

func NewUserRegistries

func NewUserRegistries() *UserRegistries

NewUserRegistries makes new UserRegistries.

func (*UserRegistries) AddUserRegistry

func (r *UserRegistries) AddUserRegistry(user string, reg *prometheus.Registry)

AddUserRegistry adds an user registry. If user already has a registry, previous registry is removed, but latest metric values are preserved in order to avoid counter resets.

func (*UserRegistries) BuildMetricFamiliesPerUser

func (r *UserRegistries) BuildMetricFamiliesPerUser() MetricFamiliesPerUser

func (*UserRegistries) Registries

func (r *UserRegistries) Registries() []UserRegistry

Registries returns a copy of the user registries list.

func (*UserRegistries) RemoveUserRegistry

func (r *UserRegistries) RemoveUserRegistry(user string, hard bool)

RemoveUserRegistry removes all Prometheus registries for a given user. If hard is true, registry is removed completely. If hard is false, latest registry values are preserved for future aggregations.

type UserRegistry

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

UserRegistry holds a Prometheus registry associated to a specific user.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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