This example shows how to use multiple registries for registering and unregistering groups of metrics.
()
| 1265 | // This example shows how to use multiple registries for registering and |
| 1266 | // unregistering groups of metrics. |
| 1267 | func ExampleRegistry_grouping() { |
| 1268 | // Create a global registry. |
| 1269 | globalReg := prometheus.NewRegistry() |
| 1270 | |
| 1271 | // Spawn 10 workers, each of which will have their own group of metrics. |
| 1272 | for i := 0; i < 10; i++ { |
| 1273 | // Create a new registry for each worker, which acts as a group of |
| 1274 | // worker-specific metrics. |
| 1275 | workerReg := prometheus.NewRegistry() |
| 1276 | globalReg.Register(workerReg) |
| 1277 | |
| 1278 | go func(workerID int) { |
| 1279 | // Once the worker is done, it can unregister itself. |
| 1280 | defer globalReg.Unregister(workerReg) |
| 1281 | |
| 1282 | workTime := prometheus.NewCounter(prometheus.CounterOpts{ |
| 1283 | Name: "worker_total_work_time_milliseconds", |
| 1284 | ConstLabels: prometheus.Labels{ |
| 1285 | // Generate a label unique to this worker so its metric doesn't |
| 1286 | // collide with the metrics from other workers. |
| 1287 | "worker_id": strconv.Itoa(workerID), |
| 1288 | }, |
| 1289 | }) |
| 1290 | workerReg.MustRegister(workTime) |
| 1291 | |
| 1292 | start := time.Now() |
| 1293 | time.Sleep(time.Millisecond * time.Duration(rand.Intn(100))) |
| 1294 | workTime.Add(float64(time.Since(start).Milliseconds())) |
| 1295 | }(i) |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | type customCollector struct { |
| 1300 | collectFunc func(ch chan<- prometheus.Metric) |
nothing calls this directly
no test coverage detected