{"modulePath":"github.com/bitechdev/ResolveSpec","version":"v1.3.14","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package metrics\n\n## Functions\n\n```go\nfunc NormalizePath(path string) string\n```\nNormalizePath replaces dynamic-looking path segments (numeric IDs, UUIDs, long hex strings and other long opaque tokens) with \":id\" so that /users/123 and /users/456 share one label value.\n\n```go\nfunc Reset() bool\n```\nReset clears the current provider's stats if it supports resetting. It returns false if the provider does not implement Resetter.\n\n```go\nfunc SetProvider(p Provider)\n```\nSetProvider sets the global metrics provider.\n\n## Types\n\n```go\ntype AIProxyRecorder interface {\n\t// RecordAIProxy records one proxied (or refused) request. statusClass is \"2xx\"..\"5xx\",\n\t// outcome is ok, upstream_error, denied or rate_limited, model may be empty.\n\tRecordAIProxy(upstream, kind, model, statusClass, outcome string, duration time.Duration, promptTokens, completionTokens int64)\n}\n```\nAIProxyRecorder is optionally implemented by providers that record aiproxy traffic. pkg/aiproxy uses it when the global provider implements it.\n\n```go\ntype Config struct {\n\t// Enabled determines whether metrics collection is enabled\n\tEnabled bool `mapstructure:\"enabled\"`\n\n\t// Provider specifies which metrics provider to use (prometheus, noop)\n\tProvider string `mapstructure:\"provider\"`\n\n\t// Namespace is an optional prefix for all metric names\n\tNamespace string `mapstructure:\"namespace\"`\n\n\t// HTTPRequestBuckets defines histogram buckets for HTTP request duration (in seconds)\n\t// Default: [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10]\n\tHTTPRequestBuckets []float64 `mapstructure:\"http_request_buckets\"`\n\n\t// DBQueryBuckets defines histogram buckets for database query duration (in seconds)\n\t// Default: [0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5]\n\tDBQueryBuckets []float64 `mapstructure:\"db_query_buckets\"`\n\n\t// HTTPMaxPaths caps the number of distinct values of the \"path\" label on HTTP\n\t// metrics. Paths beyond the cap are reported as \"other\". Paths are already\n\t// normalized (route pattern, or dynamic segments replaced with \":id\").\n\t// Default: 1024. Set to a negative value to disable the cap.\n\tHTTPMaxPaths int `mapstructure:\"http_max_paths\"`\n\n\t// HTTPPathNormalizer optionally maps a request to its \"path\" label (e.g. the\n\t// matched route template of your router). Return \"\" to fall back to the\n\t// default behaviour (ServeMux pattern, then generic ID normalization).\n\tHTTPPathNormalizer func(*http.Request) string `mapstructure:\"-\"`\n\n\t// PushgatewayURL is the URL of the Prometheus Pushgateway (optional)\n\t// If set, metrics will be pushed to this gateway instead of only being scraped\n\t// Example: \"http://pushgateway:9091\"\n\tPushgatewayURL string `mapstructure:\"pushgateway_url\"`\n\n\t// PushgatewayJobName is the job name to use when pushing metrics to Pushgateway\n\t// Default: \"resolvespec\"\n\tPushgatewayJobName string `mapstructure:\"pushgateway_job_name\"`\n\n\t// PushgatewayInterval is the interval at which to push metrics to Pushgateway\n\t// Only used if PushgatewayURL is set. If 0, automatic pushing is disabled.\n\t// Default: 0 (no automatic pushing)\n\tPushgatewayInterval int `mapstructure:\"pushgateway_interval\"`\n\n\t// PushEndpointURL is a custom HTTP endpoint that metrics are POSTed to\n\t// (independent of Pushgateway). Example: \"https://collector.example.com/metrics\"\n\tPushEndpointURL string `mapstructure:\"push_endpoint_url\"`\n\n\t// PushEndpointFormat is the request body format: \"text\" (Prometheus text\n\t// exposition, Content-Type text/plain; version=0.0.4) or \"json\".\n\t// Default: \"text\"\n\tPushEndpointFormat string `mapstructure:\"push_endpoint_format\"`\n\n\t// PushEndpointHeaders are extra headers sent with each POST (e.g. Authorization).\n\tPushEndpointHeaders map[string]string `mapstructure:\"push_endpoint_headers\"`\n\n\t// PushEndpointInterval is the interval in seconds for automatic POSTs.\n\t// If 0, automatic posting is disabled (PushToEndpoint can still be called manually).\n\tPushEndpointInterval int `mapstructure:\"push_endpoint_interval\"`\n\n\t// PushEndpointTimeout is the per-request timeout in seconds. Default: 10\n\tPushEndpointTimeout int `mapstructure:\"push_endpoint_timeout\"`\n\n\t// PushEndpointResetOnSuccess clears local counters and histograms after the\n\t// endpoint answers with a 2xx status. Default: false.\n\tPushEndpointResetOnSuccess bool `mapstructure:\"push_endpoint_reset_on_success\"`\n\n\t// PushgatewayResetOnPush clears the local counters and histograms after each\n\t// successful push (automatic or via PushAndReset), so each push carries only\n\t// the activity since the previous one. Default: false.\n\tPushgatewayResetOnPush bool `mapstructure:\"pushgateway_reset_on_push\"`\n}\n```\nConfig holds configuration for the metrics provider\n\n```go\nfunc DefaultConfig() *Config\n```\nDefaultConfig returns a Config with sensible defaults\n\n```go\nfunc (c *Config) ApplyDefaults()\n```\nApplyDefaults fills in any missing values with defaults\n\n```go\ntype NoOpProvider struct{}\n```\nNoOpProvider is a no-op implementation of Provider\n\n```go\nfunc (n *NoOpProvider) DecRequestsInFlight()\n```\n\n```go\nfunc (n *NoOpProvider) Handler() http.Handler\n```\n\n```go\nfunc (n *NoOpProvider) IncRequestsInFlight()\n```\n\n```go\nfunc (n *NoOpProvider) RecordCacheHit(provider string)\n```\n\n```go\nfunc (n *NoOpProvider) RecordCacheMiss(provider string)\n```\n\n```go\nfunc (n *NoOpProvider) RecordDBQuery(operation, schema, entity, table string, duration time.Duration, err error)\n```\n\n```go\nfunc (n *NoOpProvider) RecordEventProcessed(source, eventType, status string, duration time.Duration)\n```\n\n```go\nfunc (n *NoOpProvider) RecordEventPublished(source, eventType string)\n```\n\n```go\nfunc (n *NoOpProvider) RecordHTTPRequest(method, path, status string, duration time.Duration)\n```\n\n```go\nfunc (n *NoOpProvider) RecordPanic(methodName string)\n```\n\n```go\nfunc (n *NoOpProvider) UpdateCacheSize(provider string, size int64)\n```\n\n```go\nfunc (n *NoOpProvider) UpdateEventQueueSize(size int64)\n```\n\n```go\ntype PrometheusProvider struct {\n\t// contains filtered or unexported fields\n}\n```\nPrometheusProvider implements the Provider interface using Prometheus\n\n```go\nfunc NewPrometheusProvider(cfg *Config) *PrometheusProvider\n```\nNewPrometheusProvider creates a new Prometheus metrics provider If cfg is nil, default configuration will be used\n\n```go\nfunc (p *PrometheusProvider) DecRequestsInFlight()\n```\nDecRequestsInFlight implements Provider interface\n\n```go\nfunc (p *PrometheusProvider) Handler() http.Handler\n```\nHandler implements Provider interface It responds 404 when metrics are disabled.\n\n```go\nfunc (p *PrometheusProvider) IncRequestsInFlight()\n```\nIncRequestsInFlight implements Provider interface\n\n```go\nfunc (p *PrometheusProvider) JSONHandler() http.Handler\n```\nJSONHandler returns an HTTP handler serving the current metrics as JSON (same shape as the \"json\" push endpoint format). Only GET and HEAD are accepted, and it responds 404 when metrics are disabled. It performs no authentication; mount it on an internal/protected route.\n\n```go\nfunc (p *PrometheusProvider) Middleware(next http.Handler) http.Handler\n```\nMiddleware returns an HTTP middleware that collects metrics When metrics are disabled it returns next unchanged.\n\n```go\nfunc (p *PrometheusProvider) Push() error\n```\nPush manually pushes metrics to the configured Pushgateway Returns an error if pushing fails or if Pushgateway is not configured\n\n```go\nfunc (p *PrometheusProvider) PushAndReset() error\n```\nPushAndReset pushes metrics to the Pushgateway and, only if the push succeeded, clears the local stats. Returns an error if Pushgateway is not configured, so stats are never discarded without being delivered. Observations recorded between the push and the reset are lost.\n\n```go\nfunc (p *PrometheusProvider) PushToEndpoint(ctx context.Context) error\n```\nPushToEndpoint POSTs the current metrics to the configured PushEndpointURL. If PushEndpointResetOnSuccess is set, local stats are cleared after a 2xx reply. Returns an error if no endpoint is configured.\n\n```go\nfunc (p *PrometheusProvider) RecordAIProxy(upstream, kind, model, statusClass, outcome string, duration time.Duration, promptTokens, completionTokens int64)\n```\nRecordAIProxy implements AIProxyRecorder\n\n```go\nfunc (p *PrometheusProvider) RecordCacheHit(provider string)\n```\nRecordCacheHit implements Provider interface\n\n```go\nfunc (p *PrometheusProvider) RecordCacheMiss(provider string)\n```\nRecordCacheMiss implements Provider interface\n\n```go\nfunc (p *PrometheusProvider) RecordDBQuery(operation, schema, entity, table string, duration time.Duration, err error)\n```\nRecordDBQuery implements Provider interface\n\n```go\nfunc (p *PrometheusProvider) RecordEventProcessed(source, eventType, status string, duration time.Duration)\n```\nRecordEventProcessed implements Provider interface\n\n```go\nfunc (p *PrometheusProvider) RecordEventPublished(source, eventType string)\n```\nRecordEventPublished implements Provider interface\n\n```go\nfunc (p *PrometheusProvider) RecordHTTPRequest(method, path, status string, duration time.Duration)\n```\nRecordHTTPRequest implements Provider interface The path is normalized and capped to keep label cardinality bounded.\n\n```go\nfunc (p *PrometheusProvider) RecordPanic(methodName string)\n```\nRecordPanic implements the Provider interface\n\n```go\nfunc (p *PrometheusProvider) Reset()\n```\nReset clears all recorded counters, histograms and labelled gauges (cache size) and forgets the tracked HTTP path labels. Live gauges (requests in flight, event queue size) are left untouched since they reflect current state. Prometheus treats the drop in counters as a counter reset, so rate() and increase() keep working on the scraper side.\n\n```go\nfunc (p *PrometheusProvider) ResetHandler() http.Handler\n```\nResetHandler returns an HTTP handler that clears local stats on POST. With ?push=true it first pushes to the Pushgateway and only resets on success. The handler performs no authentication; mount it on an internal/protected route.\n\n```go\nfunc (p *PrometheusProvider) StopAutoPush()\n```\nStopAutoPush stops the automatic push goroutine This should be called when shutting down the application\n\n```go\nfunc (p *PrometheusProvider) UpdateCacheSize(provider string, size int64)\n```\nUpdateCacheSize implements Provider interface\n\n```go\nfunc (p *PrometheusProvider) UpdateEventQueueSize(size int64)\n```\nUpdateEventQueueSize implements Provider interface\n\n```go\ntype Provider interface {\n\t// RecordHTTPRequest records metrics for an HTTP request\n\tRecordHTTPRequest(method, path, status string, duration time.Duration)\n\n\t// IncRequestsInFlight increments the in-flight requests counter\n\tIncRequestsInFlight()\n\n\t// DecRequestsInFlight decrements the in-flight requests counter\n\tDecRequestsInFlight()\n\n\t// RecordDBQuery records metrics for a database query\n\tRecordDBQuery(operation, schema, entity, table string, duration time.Duration, err error)\n\n\t// RecordCacheHit records a cache hit\n\tRecordCacheHit(provider string)\n\n\t// RecordCacheMiss records a cache miss\n\tRecordCacheMiss(provider string)\n\n\t// UpdateCacheSize updates the cache size metric\n\tUpdateCacheSize(provider string, size int64)\n\n\t// RecordEventPublished records an event publication\n\tRecordEventPublished(source, eventType string)\n\n\t// RecordEventProcessed records an event processing with its status\n\tRecordEventProcessed(source, eventType, status string, duration time.Duration)\n\n\t// UpdateEventQueueSize updates the event queue size metric\n\tUpdateEventQueueSize(size int64)\n\n\t// RecordPanic records a panic event\n\tRecordPanic(methodName string)\n\n\t// Handler returns an HTTP handler for exposing metrics (e.g., /metrics endpoint)\n\tHandler() http.Handler\n}\n```\nProvider defines the interface for metric collection\n\n```go\nfunc GetProvider() Provider\n```\nGetProvider returns the current metrics provider.\n\n```go\ntype Resetter interface {\n\tReset()\n}\n```\nResetter is optionally implemented by providers that can clear their recorded stats.\n\n```go\ntype ResponseWriter struct {\n\thttp.ResponseWriter\n\t// contains filtered or unexported fields\n}\n```\nResponseWriter wraps http.ResponseWriter to capture status code\n\n```go\nfunc NewResponseWriter(w http.ResponseWriter) *ResponseWriter\n```\n\n```go\nfunc (rw *ResponseWriter) WriteHeader(code int)\n```\n\n","path":"github.com/bitechdev/ResolveSpec/pkg/metrics","name":"metrics","synopsis":"","isRedistributable":true}
