mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-10-08 06:16:28 +00:00
feat(metrics): bound HTTP path labels, add reset, custom push endpoint and JSON pull
- normalize the HTTP path label (ServeMux pattern, custom normalizer, ID collapsing) and cap distinct values via HTTPMaxPaths (default 1024) - add Reset, PushAndReset, ResetHandler and reset-on-push options - add POST push to a custom endpoint (text or json) with optional reset - add JSONHandler for JSON pull - honour Config.Enabled; log Pushgateway push failures
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package metrics
|
||||
|
||||
import "net/http"
|
||||
|
||||
// Config holds configuration for the metrics provider
|
||||
type Config struct {
|
||||
// Enabled determines whether metrics collection is enabled
|
||||
@@ -19,6 +21,17 @@ type Config struct {
|
||||
// Default: [0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5]
|
||||
DBQueryBuckets []float64 `mapstructure:"db_query_buckets"`
|
||||
|
||||
// HTTPMaxPaths caps the number of distinct values of the "path" label on HTTP
|
||||
// metrics. Paths beyond the cap are reported as "other". Paths are already
|
||||
// normalized (route pattern, or dynamic segments replaced with ":id").
|
||||
// Default: 1024. Set to a negative value to disable the cap.
|
||||
HTTPMaxPaths int `mapstructure:"http_max_paths"`
|
||||
|
||||
// HTTPPathNormalizer optionally maps a request to its "path" label (e.g. the
|
||||
// matched route template of your router). Return "" to fall back to the
|
||||
// default behaviour (ServeMux pattern, then generic ID normalization).
|
||||
HTTPPathNormalizer func(*http.Request) string `mapstructure:"-"`
|
||||
|
||||
// PushgatewayURL is the URL of the Prometheus Pushgateway (optional)
|
||||
// If set, metrics will be pushed to this gateway instead of only being scraped
|
||||
// Example: "http://pushgateway:9091"
|
||||
@@ -32,6 +45,34 @@ type Config struct {
|
||||
// Only used if PushgatewayURL is set. If 0, automatic pushing is disabled.
|
||||
// Default: 0 (no automatic pushing)
|
||||
PushgatewayInterval int `mapstructure:"pushgateway_interval"`
|
||||
|
||||
// PushEndpointURL is a custom HTTP endpoint that metrics are POSTed to
|
||||
// (independent of Pushgateway). Example: "https://collector.example.com/metrics"
|
||||
PushEndpointURL string `mapstructure:"push_endpoint_url"`
|
||||
|
||||
// PushEndpointFormat is the request body format: "text" (Prometheus text
|
||||
// exposition, Content-Type text/plain; version=0.0.4) or "json".
|
||||
// Default: "text"
|
||||
PushEndpointFormat string `mapstructure:"push_endpoint_format"`
|
||||
|
||||
// PushEndpointHeaders are extra headers sent with each POST (e.g. Authorization).
|
||||
PushEndpointHeaders map[string]string `mapstructure:"push_endpoint_headers"`
|
||||
|
||||
// PushEndpointInterval is the interval in seconds for automatic POSTs.
|
||||
// If 0, automatic posting is disabled (PushToEndpoint can still be called manually).
|
||||
PushEndpointInterval int `mapstructure:"push_endpoint_interval"`
|
||||
|
||||
// PushEndpointTimeout is the per-request timeout in seconds. Default: 10
|
||||
PushEndpointTimeout int `mapstructure:"push_endpoint_timeout"`
|
||||
|
||||
// PushEndpointResetOnSuccess clears local counters and histograms after the
|
||||
// endpoint answers with a 2xx status. Default: false.
|
||||
PushEndpointResetOnSuccess bool `mapstructure:"push_endpoint_reset_on_success"`
|
||||
|
||||
// PushgatewayResetOnPush clears the local counters and histograms after each
|
||||
// successful push (automatic or via PushAndReset), so each push carries only
|
||||
// the activity since the previous one. Default: false.
|
||||
PushgatewayResetOnPush bool `mapstructure:"pushgateway_reset_on_push"`
|
||||
}
|
||||
|
||||
// DefaultConfig returns a Config with sensible defaults
|
||||
@@ -43,6 +84,7 @@ func DefaultConfig() *Config {
|
||||
HTTPRequestBuckets: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10},
|
||||
// DB queries are usually faster
|
||||
DBQueryBuckets: []float64{0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5},
|
||||
HTTPMaxPaths: defaultHTTPMaxPaths,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +99,17 @@ func (c *Config) ApplyDefaults() {
|
||||
if len(c.DBQueryBuckets) == 0 {
|
||||
c.DBQueryBuckets = []float64{0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5}
|
||||
}
|
||||
if c.PushEndpointURL != "" {
|
||||
if c.PushEndpointFormat == "" {
|
||||
c.PushEndpointFormat = "text"
|
||||
}
|
||||
if c.PushEndpointTimeout <= 0 {
|
||||
c.PushEndpointTimeout = 10
|
||||
}
|
||||
}
|
||||
if c.HTTPMaxPaths == 0 {
|
||||
c.HTTPMaxPaths = defaultHTTPMaxPaths
|
||||
}
|
||||
// Set default job name if pushgateway is configured but job name is empty
|
||||
if c.PushgatewayURL != "" && c.PushgatewayJobName == "" {
|
||||
c.PushgatewayJobName = "resolvespec"
|
||||
|
||||
Reference in New Issue
Block a user