|
15 | 15 | import logging |
16 | 16 | import threading |
17 | 17 | from functools import wraps |
18 | | -from typing import TYPE_CHECKING, Dict, Optional, Set |
| 18 | +from typing import TYPE_CHECKING, Dict, Optional, Set, Union |
19 | 19 |
|
20 | 20 | from prometheus_client.core import REGISTRY, Counter, Gauge |
21 | 21 |
|
@@ -198,7 +198,7 @@ async def run(): |
198 | 198 | _background_process_start_count.labels(desc).inc() |
199 | 199 | _background_process_in_flight_count.labels(desc).inc() |
200 | 200 |
|
201 | | - with BackgroundProcessLoggingContext("%s-%s" % (desc, count)) as context: |
| 201 | + with BackgroundProcessLoggingContext(desc, count) as context: |
202 | 202 | try: |
203 | 203 | ctx = noop_context_manager() |
204 | 204 | if bg_start_span: |
@@ -243,8 +243,20 @@ class BackgroundProcessLoggingContext(LoggingContext): |
243 | 243 |
|
244 | 244 | __slots__ = ["_proc"] |
245 | 245 |
|
246 | | - def __init__(self, name: str): |
247 | | - super().__init__(name) |
| 246 | + def __init__(self, name: str, instance_id: Optional[Union[int, str]] = None): |
| 247 | + """ |
| 248 | +
|
| 249 | + Args: |
| 250 | + name: The name of the background process. Each distinct `name` gets a |
| 251 | + separate prometheus time series. |
| 252 | +
|
| 253 | + instance_id: an identifer to add to `name` to distinguish this instance of |
| 254 | + the named background process in the logs. If this is `None`, one is |
| 255 | + made up based on id(self). |
| 256 | + """ |
| 257 | + if instance_id is None: |
| 258 | + instance_id = id(self) |
| 259 | + super().__init__("%s-%s" % (name, instance_id)) |
248 | 260 | self._proc = _BackgroundProcess(name, self) |
249 | 261 |
|
250 | 262 | def start(self, rusage: "Optional[resource._RUsage]"): |
|
0 commit comments