Write metrics to the given path. This is intended for use with the Node exporter textfile collector. The path must end in .prom for the textfile collector to process it. An optional tmpdir parameter can be set to determine where the metrics will be temporarily written to. If not se
(path: str, registry: Collector, escaping: str = openmetrics.ALLOWUTF8, tmpdir: Optional[str] = None)
| 455 | |
| 456 | |
| 457 | def write_to_textfile(path: str, registry: Collector, escaping: str = openmetrics.ALLOWUTF8, tmpdir: Optional[str] = None) -> None: |
| 458 | """Write metrics to the given path. |
| 459 | |
| 460 | This is intended for use with the Node exporter textfile collector. |
| 461 | The path must end in .prom for the textfile collector to process it. |
| 462 | |
| 463 | An optional tmpdir parameter can be set to determine where the |
| 464 | metrics will be temporarily written to. If not set, it will be in |
| 465 | the same directory as the .prom file. If provided, the path MUST be |
| 466 | on the same filesystem.""" |
| 467 | if tmpdir is not None: |
| 468 | filename = os.path.basename(path) |
| 469 | tmppath = f'{os.path.join(tmpdir, filename)}.{os.getpid()}.{threading.current_thread().ident}' |
| 470 | else: |
| 471 | tmppath = f'{path}.{os.getpid()}.{threading.current_thread().ident}' |
| 472 | try: |
| 473 | with open(tmppath, 'wb') as f: |
| 474 | f.write(generate_latest(registry, escaping)) |
| 475 | |
| 476 | # rename(2) is atomic but fails on Windows if the destination file exists |
| 477 | if os.name == 'nt': |
| 478 | os.replace(tmppath, path) |
| 479 | else: |
| 480 | os.rename(tmppath, path) |
| 481 | except Exception: |
| 482 | if os.path.exists(tmppath): |
| 483 | os.remove(tmppath) |
| 484 | raise |
| 485 | |
| 486 | |
| 487 | def _make_handler( |
nothing calls this directly
no test coverage detected