stats

package
v1.22.0-rc.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// QueueTimeHeader is the HTTP header used to propagate the queue enqueue
	// timestamp (UnixNano) from the scheduler to the querier.
	QueueTimeHeader = "X-Cortex-Queue-Enqueue-Time-Ns"
)

Variables

View Source
var (
	ErrInvalidLengthStats = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowStats   = fmt.Errorf("proto: integer overflow")
)

Functions

func ExtractQueueTimeHeader

func ExtractQueueTimeHeader(req *httpgrpc.HTTPRequest, s *QueryStats)

ExtractQueueTimeHeader reads the enqueue timestamp from the HTTP header, computes the queue wait duration using the current wall-clock as the dequeue time, and sets both join and leave times on the given QueryStats. The header is removed from the request after extraction.

func InjectQueueTimeHeader

func InjectQueueTimeHeader(req *httpgrpc.HTTPRequest, enqueueTime time.Time)

InjectQueueTimeHeader adds the enqueue timestamp as an HTTP header on the request so it can be propagated from the scheduler to the querier.

func IsEnabled added in v1.7.0

func IsEnabled(ctx context.Context) bool

IsEnabled returns whether stats tracking is enabled in the context.

func ShouldTrackHTTPGRPCResponse

func ShouldTrackHTTPGRPCResponse(r *httpgrpc.HTTPResponse) bool

Types

type PhaseTrackerConfig

type PhaseTrackerConfig struct {
	// TotalTimeout is the total time before the querier cancels the query context.
	TotalTimeout time.Duration

	// EvalTimeThreshold is the eval time above which a timeout is classified as user error (4XX).
	EvalTimeThreshold time.Duration

	// Enabled controls whether the 5XX-to-4XX conversion is active.
	Enabled bool
}

PhaseTrackerConfig holds configurable thresholds for timeout classification.

type QueryStats added in v1.15.0

type QueryStats struct {
	Stats
	QueryResponseSeries uint64
	PriorityAssigned    bool
	Priority            int64
	DataSelectMaxTime   int64
	DataSelectMinTime   int64
	SplitInterval       time.Duration
	// contains filtered or unexported fields
}

func ContextWithEmptyStats

func ContextWithEmptyStats(ctx context.Context) (*QueryStats, context.Context)

ContextWithEmptyStats returns a context with empty stats.

func FromContext

func FromContext(ctx context.Context) *QueryStats

FromContext gets the Stats out of the Context. Returns nil if stats have not been initialised in the context.

func (*QueryStats) AddExtraFields added in v1.15.0

func (s *QueryStats) AddExtraFields(fieldsVals ...any)

func (*QueryStats) AddFetchedChunkBytes added in v1.15.0

func (s *QueryStats) AddFetchedChunkBytes(bytes uint64)

func (*QueryStats) AddFetchedChunks added in v1.15.0

func (s *QueryStats) AddFetchedChunks(count uint64)

func (*QueryStats) AddFetchedDataBytes added in v1.15.0

func (s *QueryStats) AddFetchedDataBytes(bytes uint64)

func (*QueryStats) AddFetchedSamples added in v1.15.0

func (s *QueryStats) AddFetchedSamples(count uint64)

func (*QueryStats) AddFetchedSeries added in v1.15.0

func (s *QueryStats) AddFetchedSeries(series uint64)

func (*QueryStats) AddPeakSamples added in v1.19.0

func (s *QueryStats) AddPeakSamples(count uint64)

func (*QueryStats) AddQueryStorageWallTime added in v1.17.0

func (s *QueryStats) AddQueryStorageWallTime(t time.Duration)

AddQueryStorageWallTime adds some time to the counter.

func (*QueryStats) AddResponseSeries added in v1.19.0

func (s *QueryStats) AddResponseSeries(series uint64)

func (*QueryStats) AddScannedSamples added in v1.19.0

func (s *QueryStats) AddScannedSamples(count uint64)

func (*QueryStats) AddSplitQueries added in v1.17.0

func (s *QueryStats) AddSplitQueries(count uint64)

func (*QueryStats) AddStoreGatewayTouchedPostingBytes added in v1.18.0

func (s *QueryStats) AddStoreGatewayTouchedPostingBytes(bytes uint64)

func (*QueryStats) AddStoreGatewayTouchedPostings added in v1.18.0

func (s *QueryStats) AddStoreGatewayTouchedPostings(count uint64)

func (*QueryStats) AddWallTime added in v1.15.0

func (s *QueryStats) AddWallTime(t time.Duration)

AddWallTime adds some time to the counter.

func (*QueryStats) ComputeAndStoreTimingBreakdown

func (s *QueryStats) ComputeAndStoreTimingBreakdown()

ComputeAndStoreTimingBreakdown computes the timing breakdown from phase tracking fields and stores them as max values. This should be called after a sub-query completes, before stats are sent back to the frontend.

func (*QueryStats) Copy added in v1.20.0

func (s *QueryStats) Copy() *QueryStats

func (*QueryStats) LoadDataSelectMaxTime added in v1.17.0

func (s *QueryStats) LoadDataSelectMaxTime() int64

func (*QueryStats) LoadDataSelectMinTime added in v1.17.0

func (s *QueryStats) LoadDataSelectMinTime() int64

func (*QueryStats) LoadExtraFields added in v1.15.0

func (s *QueryStats) LoadExtraFields() []any

func (*QueryStats) LoadFetchedChunkBytes added in v1.15.0

func (s *QueryStats) LoadFetchedChunkBytes() uint64

func (*QueryStats) LoadFetchedChunks added in v1.15.0

func (s *QueryStats) LoadFetchedChunks() uint64

func (*QueryStats) LoadFetchedDataBytes added in v1.15.0

func (s *QueryStats) LoadFetchedDataBytes() uint64

func (*QueryStats) LoadFetchedSamples added in v1.15.0

func (s *QueryStats) LoadFetchedSamples() uint64

func (*QueryStats) LoadFetchedSeries added in v1.15.0

func (s *QueryStats) LoadFetchedSeries() uint64

func (*QueryStats) LoadMaxEvalTime

func (s *QueryStats) LoadMaxEvalTime() time.Duration

func (*QueryStats) LoadMaxFetchTime

func (s *QueryStats) LoadMaxFetchTime() time.Duration

func (*QueryStats) LoadMaxQueueWaitTime

func (s *QueryStats) LoadMaxQueueWaitTime() time.Duration

func (*QueryStats) LoadMaxTotalTime

func (s *QueryStats) LoadMaxTotalTime() time.Duration

func (*QueryStats) LoadPeakSamples added in v1.19.0

func (s *QueryStats) LoadPeakSamples() uint64

func (*QueryStats) LoadPriority added in v1.17.0

func (s *QueryStats) LoadPriority() (int64, bool)

func (*QueryStats) LoadQueryEnd

func (s *QueryStats) LoadQueryEnd() time.Time

LoadQueryEnd returns the query end time.

func (*QueryStats) LoadQueryStart

func (s *QueryStats) LoadQueryStart() time.Time

LoadQueryStart returns the query start time.

func (*QueryStats) LoadQueryStorageWallTime added in v1.17.0

func (s *QueryStats) LoadQueryStorageWallTime() time.Duration

LoadQueryStorageWallTime returns current query storage wall time.

func (*QueryStats) LoadQueueJoinTime

func (s *QueryStats) LoadQueueJoinTime() time.Time

LoadQueueJoinTime returns the queue join time.

func (*QueryStats) LoadQueueLeaveTime

func (s *QueryStats) LoadQueueLeaveTime() time.Time

LoadQueueLeaveTime returns the queue leave time.

func (*QueryStats) LoadResponseSeries added in v1.19.0

func (s *QueryStats) LoadResponseSeries() uint64

func (*QueryStats) LoadScannedSamples added in v1.19.0

func (s *QueryStats) LoadScannedSamples() uint64

func (*QueryStats) LoadSplitInterval added in v1.20.0

func (s *QueryStats) LoadSplitInterval() time.Duration

func (*QueryStats) LoadSplitQueries added in v1.17.0

func (s *QueryStats) LoadSplitQueries() uint64

func (*QueryStats) LoadStoreGatewayTouchedPostingBytes added in v1.18.0

func (s *QueryStats) LoadStoreGatewayTouchedPostingBytes() uint64

func (*QueryStats) LoadStoreGatewayTouchedPostings added in v1.18.0

func (s *QueryStats) LoadStoreGatewayTouchedPostings() uint64

func (*QueryStats) LoadWallTime added in v1.15.0

func (s *QueryStats) LoadWallTime() time.Duration

LoadWallTime returns current wall time.

func (*QueryStats) Merge added in v1.15.0

func (s *QueryStats) Merge(other *QueryStats)

Merge the provided Stats into this one.

func (*QueryStats) SetDataSelectMaxTime added in v1.17.0

func (s *QueryStats) SetDataSelectMaxTime(dataSelectMaxTime int64)

func (*QueryStats) SetDataSelectMinTime added in v1.17.0

func (s *QueryStats) SetDataSelectMinTime(dataSelectMinTime int64)

func (*QueryStats) SetPeakSamples added in v1.19.0

func (s *QueryStats) SetPeakSamples(count uint64)

func (*QueryStats) SetPriority added in v1.17.0

func (s *QueryStats) SetPriority(priority int64)

func (*QueryStats) SetQueryEnd

func (s *QueryStats) SetQueryEnd(t time.Time)

SetQueryEnd records when the query finished execution.

func (*QueryStats) SetQueryStart

func (s *QueryStats) SetQueryStart(t time.Time)

SetQueryStart records when the query began execution.

func (*QueryStats) SetQueueJoinTime

func (s *QueryStats) SetQueueJoinTime(t time.Time)

SetQueueJoinTime records when the request entered the scheduler queue.

func (*QueryStats) SetQueueLeaveTime

func (s *QueryStats) SetQueueLeaveTime(t time.Time)

SetQueueLeaveTime records when the request left the scheduler queue.

func (*QueryStats) UpdateMaxEvalTime

func (s *QueryStats) UpdateMaxEvalTime(t time.Duration)

func (*QueryStats) UpdateMaxFetchTime

func (s *QueryStats) UpdateMaxFetchTime(t time.Duration)

UpdateMaxFetchTime updates the max fetch time if the provided value is larger.

func (*QueryStats) UpdateMaxQueueWaitTime

func (s *QueryStats) UpdateMaxQueueWaitTime(t time.Duration)

func (*QueryStats) UpdateMaxTotalTime

func (s *QueryStats) UpdateMaxTotalTime(t time.Duration)

type Stats

type Stats struct {
	// The sum of all wall time spent in the querier to execute the query.
	WallTime time.Duration `protobuf:"bytes,1,opt,name=wall_time,json=wallTime,proto3,stdduration" json:"wall_time"`
	// The number of series fetched for the query
	FetchedSeriesCount uint64 `protobuf:"varint,2,opt,name=fetched_series_count,json=fetchedSeriesCount,proto3" json:"fetched_series_count,omitempty"`
	// The number of bytes of the chunks fetched for the query
	FetchedChunkBytes uint64 `protobuf:"varint,3,opt,name=fetched_chunk_bytes,json=fetchedChunkBytes,proto3" json:"fetched_chunk_bytes,omitempty"`
	// The number of bytes of data fetched for the query
	FetchedDataBytes uint64 `protobuf:"varint,4,opt,name=fetched_data_bytes,json=fetchedDataBytes,proto3" json:"fetched_data_bytes,omitempty"`
	// Extra fields to be reported on the stats log
	ExtraFields map[string]string `` /* 182-byte string literal not displayed */
	// The number of chunks fetched for the query
	FetchedChunksCount uint64 `protobuf:"varint,6,opt,name=fetched_chunks_count,json=fetchedChunksCount,proto3" json:"fetched_chunks_count,omitempty"`
	// The number of samples fetched for the query
	FetchedSamplesCount uint64 `protobuf:"varint,7,opt,name=fetched_samples_count,json=fetchedSamplesCount,proto3" json:"fetched_samples_count,omitempty"`
	// The limit hit when executing the query
	LimitHit string `protobuf:"bytes,8,opt,name=limit_hit,json=limitHit,proto3" json:"limit_hit,omitempty"`
	// The total number of split queries sent after going through all the middlewares.
	// It includes the number of requests that might be discarded by the queue.
	SplitQueries uint64 `protobuf:"varint,9,opt,name=split_queries,json=splitQueries,proto3" json:"split_queries,omitempty"`
	// The sum of wall time spent in the querier to fetch and merge data from storage.
	QueryStorageWallTime time.Duration `` /* 128-byte string literal not displayed */
	// The total number of postings touched in store gateway for a specific query.
	// Only successful requests from querier to store gateway are included.
	StoreGatewayTouchedPostingsCount uint64 `` /* 165-byte string literal not displayed */
	// The total size of postings touched in store gateway for a specific query, in bytes.
	// Only successful requests from querier to store gateway are included.
	StoreGatewayTouchedPostingBytes uint64 `` /* 162-byte string literal not displayed */
	// The total number of samples scanned while evaluating a query.
	// Equal to TotalSamples in https://github.com/prometheus/prometheus/blob/main/util/stats/query_stats.go
	ScannedSamples uint64 `protobuf:"varint,13,opt,name=scanned_samples,json=scannedSamples,proto3" json:"scanned_samples,omitempty"`
	// The highest count of samples considered while evaluating a query.
	// Equal to PeakSamples in https://github.com/prometheus/prometheus/blob/main/util/stats/query_stats.go
	PeakSamples uint64 `protobuf:"varint,14,opt,name=peak_samples,json=peakSamples,proto3" json:"peak_samples,omitempty"`
	// Max timing breakdown across sub-queries for timeout classification.
	// These use max() semantics during Merge rather than sum.
	MaxFetchTime     time.Duration `protobuf:"bytes,15,opt,name=max_fetch_time,json=maxFetchTime,proto3,stdduration" json:"max_fetch_time"`
	MaxEvalTime      time.Duration `protobuf:"bytes,16,opt,name=max_eval_time,json=maxEvalTime,proto3,stdduration" json:"max_eval_time"`
	MaxQueueWaitTime time.Duration `protobuf:"bytes,17,opt,name=max_queue_wait_time,json=maxQueueWaitTime,proto3,stdduration" json:"max_queue_wait_time"`
	MaxTotalTime     time.Duration `protobuf:"bytes,18,opt,name=max_total_time,json=maxTotalTime,proto3,stdduration" json:"max_total_time"`
}

func (*Stats) Descriptor

func (*Stats) Descriptor() ([]byte, []int)

func (*Stats) Equal

func (this *Stats) Equal(that interface{}) bool

func (*Stats) GetExtraFields added in v1.15.0

func (m *Stats) GetExtraFields() map[string]string

func (*Stats) GetFetchedChunkBytes added in v1.11.0

func (m *Stats) GetFetchedChunkBytes() uint64

func (*Stats) GetFetchedChunksCount added in v1.15.0

func (m *Stats) GetFetchedChunksCount() uint64

func (*Stats) GetFetchedDataBytes added in v1.14.0

func (m *Stats) GetFetchedDataBytes() uint64

func (*Stats) GetFetchedSamplesCount added in v1.15.0

func (m *Stats) GetFetchedSamplesCount() uint64

func (*Stats) GetFetchedSeriesCount added in v1.11.0

func (m *Stats) GetFetchedSeriesCount() uint64

func (*Stats) GetLimitHit added in v1.16.0

func (m *Stats) GetLimitHit() string

func (*Stats) GetMaxEvalTime

func (m *Stats) GetMaxEvalTime() time.Duration

func (*Stats) GetMaxFetchTime

func (m *Stats) GetMaxFetchTime() time.Duration

func (*Stats) GetMaxQueueWaitTime

func (m *Stats) GetMaxQueueWaitTime() time.Duration

func (*Stats) GetMaxTotalTime

func (m *Stats) GetMaxTotalTime() time.Duration

func (*Stats) GetPeakSamples added in v1.19.0

func (m *Stats) GetPeakSamples() uint64

func (*Stats) GetQueryStorageWallTime added in v1.17.0

func (m *Stats) GetQueryStorageWallTime() time.Duration

func (*Stats) GetScannedSamples added in v1.19.0

func (m *Stats) GetScannedSamples() uint64

func (*Stats) GetSplitQueries added in v1.17.0

func (m *Stats) GetSplitQueries() uint64

func (*Stats) GetStoreGatewayTouchedPostingBytes added in v1.18.0

func (m *Stats) GetStoreGatewayTouchedPostingBytes() uint64

func (*Stats) GetStoreGatewayTouchedPostingsCount added in v1.18.0

func (m *Stats) GetStoreGatewayTouchedPostingsCount() uint64

func (*Stats) GetWallTime

func (m *Stats) GetWallTime() time.Duration

func (*Stats) GoString

func (this *Stats) GoString() string

func (*Stats) Marshal

func (m *Stats) Marshal() (dAtA []byte, err error)

func (*Stats) MarshalTo

func (m *Stats) MarshalTo(dAtA []byte) (int, error)

func (*Stats) MarshalToSizedBuffer

func (m *Stats) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (*Stats) ProtoMessage

func (*Stats) ProtoMessage()

func (*Stats) Reset

func (m *Stats) Reset()

func (*Stats) Size

func (m *Stats) Size() (n int)

func (*Stats) String

func (this *Stats) String() string

func (*Stats) Unmarshal

func (m *Stats) Unmarshal(dAtA []byte) error

func (*Stats) XXX_DiscardUnknown

func (m *Stats) XXX_DiscardUnknown()

func (*Stats) XXX_Marshal

func (m *Stats) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Stats) XXX_Merge

func (m *Stats) XXX_Merge(src proto.Message)

func (*Stats) XXX_Size

func (m *Stats) XXX_Size() int

func (*Stats) XXX_Unmarshal

func (m *Stats) XXX_Unmarshal(b []byte) error

type TimeoutDecision

type TimeoutDecision int

TimeoutDecision represents the classification of a query timeout.

const (
	// Default5XX means return 503 (current behavior).
	Default5XX TimeoutDecision = iota
	// UserError4XX means the query is too expensive, return 422.
	UserError4XX
)

func DecideTimeoutResponse

func DecideTimeoutResponse(stats *QueryStats, cfg PhaseTrackerConfig) TimeoutDecision

DecideTimeoutResponse inspects QueryStats phase timings and returns a TimeoutDecision. It returns UserError4XX if eval time exceeds the threshold, Default5XX otherwise. It is a pure function that does not modify stats or cfg.

type WallTimeMiddleware

type WallTimeMiddleware struct{}

WallTimeMiddleware tracks the wall time.

func NewWallTimeMiddleware

func NewWallTimeMiddleware() WallTimeMiddleware

NewWallTimeMiddleware makes a new WallTimeMiddleware.

func (WallTimeMiddleware) Wrap

Wrap implements middleware.Interface.

Jump to

Keyboard shortcuts

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