metric

package
v0.0.0-...-4a72ded Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2024 License: Apache-2.0 Imports: 35 Imported by: 1

Documentation

Index

Constants

View Source
const (
	RequestCounter                = "router.http.requests"                      // Incoming request count total
	ServerLatencyHistogram        = "router.http.request.duration_milliseconds" // Incoming end to end duration, milliseconds
	RequestContentLengthCounter   = "router.http.request.content_length"        // Incoming request bytes total
	ResponseContentLengthCounter  = "router.http.response.content_length"       // Outgoing response bytes total
	InFlightRequestsUpDownCounter = "router.http.requests.in_flight"            // Number of requests in flight
	RequestError                  = "router.http.requests.error"                // Total request error count

	OperationPlanningTime = "router.graphql.operation.planning_time" // Time taken to plan the operation

)

Server HTTP metrics.

View Source
const (
	DefaultMinimumReadMemStatsInterval = 15 * time.Second

	AttributeGoInfoVersion = attribute.Key("version")
)
View Source
const DefaultCardinalityLimit = 2000

DefaultCardinalityLimit is the hard limit on the number of metric streams that can be collected for a single instrument.

View Source
const DefaultServerName = "cosmo-router"

DefaultServerName Default resource name.

Variables

View Source
var (
	RequestCounterDescription = "Total number of requests"
	RequestCounterOptions     = []otelmetric.Int64CounterOption{
		otelmetric.WithDescription(RequestCounterDescription),
	}
	RequestErrorCounterDescription = "Total number of failed requests"
	RequestErrorCounterOptions     = []otelmetric.Int64CounterOption{
		otelmetric.WithDescription(RequestErrorCounterDescription),
	}
	ServerLatencyHistogramDescription = "Server latency in milliseconds"
	ServerLatencyHistogramOptions     = []otelmetric.Float64HistogramOption{
		otelmetric.WithUnit("ms"),
		otelmetric.WithDescription(ServerLatencyHistogramDescription),
	}
	RequestContentLengthCounterDescription = "Total number of request bytes"
	RequestContentLengthCounterOptions     = []otelmetric.Int64CounterOption{
		otelmetric.WithUnit("bytes"),
		otelmetric.WithDescription(RequestContentLengthCounterDescription),
	}
	ResponseContentLengthCounterDescription = "Total number of response bytes"
	ResponseContentLengthCounterOptions     = []otelmetric.Int64CounterOption{
		otelmetric.WithUnit("bytes"),
		otelmetric.WithDescription(ResponseContentLengthCounterDescription),
	}
	InFlightRequestsUpDownCounterDescription = "Number of requests in flight"
	InFlightRequestsUpDownCounterOptions     = []otelmetric.Int64UpDownCounterOption{
		otelmetric.WithDescription(InFlightRequestsUpDownCounterDescription),
	}

	OperationPlanningTimeHistogramDescription = "Operation planning time in milliseconds"
	OperationPlanningTimeHistogramOptions     = []otelmetric.Float64HistogramOption{
		otelmetric.WithUnit("ms"),
		otelmetric.WithDescription(OperationPlanningTimeHistogramDescription),
	}
)

Functions

func NewOtlpMeterProvider

func NewOtlpMeterProvider(ctx context.Context, log *zap.Logger, c *Config, serviceInstanceID string) (*sdkmetric.MeterProvider, error)

func NewPrometheusMeterProvider

func NewPrometheusMeterProvider(ctx context.Context, c *Config, serviceInstanceID string) (*sdkmetric.MeterProvider, *prometheus.Registry, error)

func NewPrometheusServer

func NewPrometheusServer(logger *zap.Logger, listenAddr string, path string, registry *prometheus.Registry) *http.Server

Types

type CacheMetricInfo

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

CacheMetricInfo is a struct that aggregates information to provide metrics for a single cache implementation.

func NewCacheMetricInfo

func NewCacheMetricInfo(cacheType string, maxCost int64, cacheMetrics *ristretto.Metrics) CacheMetricInfo

NewCacheMetricInfo creates a new CacheMetricInfo instance.

type CacheMetrics

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

CacheMetrics is a struct that holds the metrics for various graphql operation caches.

func NewCacheMetrics

func NewCacheMetrics(logger *zap.Logger, baseAttributes []attribute.KeyValue, provider *metric.MeterProvider) (*CacheMetrics, error)

NewCacheMetrics creates a new CacheMetrics instance.

func (*CacheMetrics) RegisterObservers

func (c *CacheMetrics) RegisterObservers(
	operationCacheMetrics []CacheMetricInfo,
) error

RegisterObservers creates observer callbacks for the OTEL metrics, which will be invoked in intervals.

func (*CacheMetrics) Shutdown

func (c *CacheMetrics) Shutdown() error

type Config

type Config struct {
	// Name represents the service name for metrics. The default value is cosmo-router.
	Name string

	// Version represents the service version for metrics. The default value is dev.
	Version string

	// OpenTelemetry includes the OpenTelemetry configuration
	OpenTelemetry OpenTelemetry

	// Prometheus includes the Prometheus configuration
	Prometheus PrometheusConfig

	// ResourceAttributes added to the global resource attributes for all metrics.
	ResourceAttributes []attribute.KeyValue

	Attributes []config.CustomAttribute
}

Config represents the configuration for the agent.

func DefaultConfig

func DefaultConfig(serviceVersion string) *Config

DefaultConfig returns the default config.

func (*Config) IsEnabled

func (c *Config) IsEnabled() bool

type Measurements

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

Measurements holds the metrics for the request.

type Metrics

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

func (*Metrics) Flush

func (h *Metrics) Flush(ctx context.Context) error

Flush flushes the metrics to the backend synchronously.

func (*Metrics) MeasureInFlight

func (h *Metrics) MeasureInFlight(ctx context.Context, sliceAttr []attribute.KeyValue, opt otelmetric.AddOption) func()

func (*Metrics) MeasureLatency

func (h *Metrics) MeasureLatency(ctx context.Context, latency time.Duration, sliceAttr []attribute.KeyValue, opt otelmetric.RecordOption)

func (*Metrics) MeasureOperationPlanningTime

func (h *Metrics) MeasureOperationPlanningTime(ctx context.Context, planningTime time.Duration, sliceAttr []attribute.KeyValue, opt otelmetric.RecordOption)

func (*Metrics) MeasureRequestCount

func (h *Metrics) MeasureRequestCount(ctx context.Context, sliceAttr []attribute.KeyValue, opt otelmetric.AddOption)

func (*Metrics) MeasureRequestError

func (h *Metrics) MeasureRequestError(ctx context.Context, sliceAttr []attribute.KeyValue, opt otelmetric.AddOption)

func (*Metrics) MeasureRequestSize

func (h *Metrics) MeasureRequestSize(ctx context.Context, contentLength int64, sliceAttr []attribute.KeyValue, opt otelmetric.AddOption)

func (*Metrics) MeasureResponseSize

func (h *Metrics) MeasureResponseSize(ctx context.Context, size int64, sliceAttr []attribute.KeyValue, opt otelmetric.AddOption)

func (*Metrics) Shutdown

func (h *Metrics) Shutdown(ctx context.Context) error

Shutdown flushes the metrics and stops the runtime metrics.

type NoopMetrics

type NoopMetrics struct{}

func (NoopMetrics) Flush

func (n NoopMetrics) Flush(ctx context.Context) error

func (NoopMetrics) MeasureInFlight

func (n NoopMetrics) MeasureInFlight(ctx context.Context, sliceAttr []attribute.KeyValue, opt otelmetric.AddOption) func()

func (NoopMetrics) MeasureLatency

func (n NoopMetrics) MeasureLatency(ctx context.Context, latency time.Duration, sliceAttr []attribute.KeyValue, opt otelmetric.RecordOption)

func (NoopMetrics) MeasureOperationPlanningTime

func (n NoopMetrics) MeasureOperationPlanningTime(ctx context.Context, planningTime time.Duration, sliceAttr []attribute.KeyValue, opt otelmetric.RecordOption)

func (NoopMetrics) MeasureRequestCount

func (n NoopMetrics) MeasureRequestCount(ctx context.Context, sliceAttr []attribute.KeyValue, opt otelmetric.AddOption)

func (NoopMetrics) MeasureRequestError

func (n NoopMetrics) MeasureRequestError(ctx context.Context, sliceAttr []attribute.KeyValue, opt otelmetric.AddOption)

func (NoopMetrics) MeasureRequestSize

func (n NoopMetrics) MeasureRequestSize(ctx context.Context, contentLength int64, sliceAttr []attribute.KeyValue, opt otelmetric.AddOption)

func (NoopMetrics) MeasureResponseSize

func (n NoopMetrics) MeasureResponseSize(ctx context.Context, size int64, sliceAttr []attribute.KeyValue, opt otelmetric.AddOption)

func (NoopMetrics) Shutdown

func (n NoopMetrics) Shutdown(ctx context.Context) error

type OpenTelemetry

type OpenTelemetry struct {
	Enabled       bool
	RouterRuntime bool
	GraphqlCache  bool
	Exporters     []*OpenTelemetryExporter
	// Metrics to exclude from the OTLP exporter.
	ExcludeMetrics []*regexp.Regexp
	// Metric labels to exclude from the OTLP exporter.
	ExcludeMetricLabels []*regexp.Regexp
	// TestReader is used for testing purposes. If set, the reader will be used instead of the configured exporters.
	TestReader sdkmetric.Reader
}

type OpenTelemetryExporter

type OpenTelemetryExporter struct {
	Disabled bool
	Exporter otelconfig.Exporter
	Endpoint string
	// Headers represents the headers for HTTP transport.
	// For example:
	//  Authorization: 'Bearer <token>'
	Headers map[string]string
	// HTTPPath represents the path for OTLP HTTP transport.
	// For example
	// /v1/metrics
	HTTPPath    string
	Temporality otelconfig.ExporterTemporality
}

func GetDefaultExporter

func GetDefaultExporter(cfg *Config) *OpenTelemetryExporter

type Option

type Option func(svr *Metrics)

func WithBaseAttributes

func WithBaseAttributes(baseAttributes []attribute.KeyValue) Option

func WithCardinalityLimit

func WithCardinalityLimit(cardinalityLimit int) Option

func WithLogger

func WithLogger(logger *zap.Logger) Option

func WithOtlpMeterProvider

func WithOtlpMeterProvider(otelMeterProvider *metric.MeterProvider) Option

func WithProcessStartTime

func WithProcessStartTime(processStartTime time.Time) Option

func WithPromMeterProvider

func WithPromMeterProvider(promMeterProvider *metric.MeterProvider) Option

type OtlpMetricStore

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

func (*OtlpMetricStore) Flush

func (h *OtlpMetricStore) Flush(ctx context.Context) error

func (*OtlpMetricStore) MeasureInFlight

func (h *OtlpMetricStore) MeasureInFlight(ctx context.Context, opts ...otelmetric.AddOption) func()

func (*OtlpMetricStore) MeasureLatency

func (h *OtlpMetricStore) MeasureLatency(ctx context.Context, latency float64, opts ...otelmetric.RecordOption)

func (*OtlpMetricStore) MeasureOperationPlanningTime

func (h *OtlpMetricStore) MeasureOperationPlanningTime(ctx context.Context, planningTime float64, opts ...otelmetric.RecordOption)

func (*OtlpMetricStore) MeasureRequestCount

func (h *OtlpMetricStore) MeasureRequestCount(ctx context.Context, opts ...otelmetric.AddOption)

func (*OtlpMetricStore) MeasureRequestError

func (h *OtlpMetricStore) MeasureRequestError(ctx context.Context, opts ...otelmetric.AddOption)

func (*OtlpMetricStore) MeasureRequestSize

func (h *OtlpMetricStore) MeasureRequestSize(ctx context.Context, contentLength int64, opts ...otelmetric.AddOption)

func (*OtlpMetricStore) MeasureResponseSize

func (h *OtlpMetricStore) MeasureResponseSize(ctx context.Context, size int64, opts ...otelmetric.AddOption)

type PromMetricStore

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

func (*PromMetricStore) Flush

func (h *PromMetricStore) Flush(ctx context.Context) error

func (*PromMetricStore) MeasureInFlight

func (h *PromMetricStore) MeasureInFlight(ctx context.Context, opts ...otelmetric.AddOption) func()

func (*PromMetricStore) MeasureLatency

func (h *PromMetricStore) MeasureLatency(ctx context.Context, latency float64, opts ...otelmetric.RecordOption)

func (*PromMetricStore) MeasureOperationPlanningTime

func (h *PromMetricStore) MeasureOperationPlanningTime(ctx context.Context, planningTime float64, opts ...otelmetric.RecordOption)

func (*PromMetricStore) MeasureRequestCount

func (h *PromMetricStore) MeasureRequestCount(ctx context.Context, opts ...otelmetric.AddOption)

func (*PromMetricStore) MeasureRequestError

func (h *PromMetricStore) MeasureRequestError(ctx context.Context, opts ...otelmetric.AddOption)

func (*PromMetricStore) MeasureRequestSize

func (h *PromMetricStore) MeasureRequestSize(ctx context.Context, contentLength int64, opts ...otelmetric.AddOption)

func (*PromMetricStore) MeasureResponseSize

func (h *PromMetricStore) MeasureResponseSize(ctx context.Context, size int64, opts ...otelmetric.AddOption)

func (*PromMetricStore) Shutdown

func (h *PromMetricStore) Shutdown(ctx context.Context) error

type PrometheusConfig

type PrometheusConfig struct {
	Enabled      bool
	ListenAddr   string
	Path         string
	GraphqlCache bool
	// Metrics to exclude from Prometheus exporter
	ExcludeMetrics []*regexp.Regexp
	// Metric labels to exclude from Prometheus exporter
	ExcludeMetricLabels []*regexp.Regexp
	// TestRegistry is used for testing purposes. If set, the registry will be used instead of the default one.
	TestRegistry *prometheus.Registry
}

type Provider

type Provider interface {
	MeasureInFlight(ctx context.Context, opts ...otelmetric.AddOption) func()
	MeasureRequestCount(ctx context.Context, opts ...otelmetric.AddOption)
	MeasureRequestSize(ctx context.Context, contentLength int64, opts ...otelmetric.AddOption)
	MeasureResponseSize(ctx context.Context, size int64, opts ...otelmetric.AddOption)
	MeasureLatency(ctx context.Context, latency float64, opts ...otelmetric.RecordOption)
	MeasureRequestError(ctx context.Context, opts ...otelmetric.AddOption)
	MeasureOperationPlanningTime(ctx context.Context, planningTime float64, opts ...otelmetric.RecordOption)
	Flush(ctx context.Context) error
}

Provider is the interface that wraps the basic metric methods. We maintain two providers, one for OTEL and one for Prometheus.

func NewOtlpMetricStore

func NewOtlpMetricStore(logger *zap.Logger, meterProvider *metric.MeterProvider) (Provider, error)

func NewPromMetricStore

func NewPromMetricStore(logger *zap.Logger, meterProvider *metric.MeterProvider) (Provider, error)

type RuntimeMetrics

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

func NewRuntimeMetrics

func NewRuntimeMetrics(logger *zap.Logger, meterProvider *metric.MeterProvider, baseAttributes []attribute.KeyValue, processStartTime time.Time) *RuntimeMetrics

NewRuntimeMetrics creates a new instance of RuntimeMetrics. Runtime metrics are metrics that are collected from the Go and process runtime. These metrics are shared across feature flags.

func (*RuntimeMetrics) Shutdown

func (r *RuntimeMetrics) Shutdown() error

func (*RuntimeMetrics) Start

func (r *RuntimeMetrics) Start() error

type Store

type Store interface {
	MeasureInFlight(ctx context.Context, sliceAttr []attribute.KeyValue, opt otelmetric.AddOption) func()
	MeasureRequestCount(ctx context.Context, sliceAttr []attribute.KeyValue, opt otelmetric.AddOption)
	MeasureRequestSize(ctx context.Context, contentLength int64, sliceAttr []attribute.KeyValue, opt otelmetric.AddOption)
	MeasureResponseSize(ctx context.Context, size int64, sliceAttr []attribute.KeyValue, opt otelmetric.AddOption)
	MeasureLatency(ctx context.Context, latency time.Duration, sliceAttr []attribute.KeyValue, opt otelmetric.RecordOption)
	MeasureRequestError(ctx context.Context, sliceAttr []attribute.KeyValue, opt otelmetric.AddOption)
	MeasureOperationPlanningTime(ctx context.Context, planningTime time.Duration, sliceAttr []attribute.KeyValue, opt otelmetric.RecordOption)
	Flush(ctx context.Context) error
	Shutdown(ctx context.Context) error
}

Store is the unified metric interface for OTEL and Prometheus metrics. The interface can vary depending on if additional information is required for the provider e.g. slice attributes.

func NewNoopMetrics

func NewNoopMetrics() Store

func NewStore

func NewStore(opts ...Option) (Store, error)

NewStore creates a new metrics store instance. The store abstract OTEL and Prometheus metrics with a single interface.

Jump to

Keyboard shortcuts

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