Collector implementations, scrape errors, const metrics, and cardinality controls.
7 min readGo for SREBuild
Collector Interface
The Collector interface lets you generate metrics on demand. Implement Describe and Collect to expose metrics that depend on external state.
Gocustom-collector-implementation.go
55 linesLn 1, Col 1Go
Describe sends metric descriptors. Collect sends current metric values. Reset clears old label values. This pattern ensures metrics are fresh on every scrape and handles dynamic label values.
Dynamic Metrics
Godynamic-metric-generation.go
38 linesLn 1, Col 1Go
Dynamic collectors generate metrics based on current state. Reset clears stale label values before setting new ones. UpdateQueues changes the metric set dynamically. This handles changing infrastructure without restarting.
Scrape Error Handling
Goerror-handling-in-collectors.go
41 linesLn 1, Col 1Go
Report scrape errors as metrics. This lets you alert on scrape failures. Use prometheus.NewInvalidDesc for error metrics. Exporter_scrape_error = 1 indicates the exporter cannot reach its target.
Const Metrics
Goconst-metrics-for-static-values.go
33 linesLn 1, Col 1Go
Const metrics expose static information like version, build time, and configuration. The metric value is always 1 — the labels carry the information. This enables queries like count(build_info) by (version).
Cardinality Controls
Gocontrolling-label-cardinality.go
46 linesLn 1, Col 1Go
Cardinality controls prevent metric explosion. BoundedCounterVec limits unique label combinations. Hash bucketing reduces cardinality by grouping similar values. Monitor metric count to detect cardinality growth.
Testing Custom Exporters
Gotesting-custom-exporters.go
54 linesLn 1, Col 1Go
Test that collectors produce expected metrics. Test error handling by creating failing collectors. Use testutil.GatherAndCompare for precise assertions. Verify scrape errors are reported as metrics.
Always test your collectors
Custom exporters are easy to get wrong. Test every metric name, label combination, and value. Test error handling. Test dynamic label updates. A broken exporter is worse than no exporter.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.