|
| 1 | +import logging |
| 2 | +import sys |
| 3 | +from typing import ContextManager, Dict, List, Optional |
| 4 | + |
| 5 | +import pluggy |
| 6 | + |
| 7 | +metrics_reporter_plugin_name = "access_metrics_reporter" |
| 8 | +hookspec = pluggy.HookspecMarker(metrics_reporter_plugin_name) |
| 9 | +hookimpl = pluggy.HookimplMarker(metrics_reporter_plugin_name) |
| 10 | + |
| 11 | +_cached_metrics_reporter_hook: Optional[pluggy.HookRelay] = None |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +class MetricsReporterPluginSpec: |
| 17 | + @hookspec |
| 18 | + def record_counter( |
| 19 | + self, |
| 20 | + metric_name: str, |
| 21 | + value: float = 1.0, |
| 22 | + tags: Optional[Dict[str, str]] = None, |
| 23 | + monotonic: bool = True, |
| 24 | + ) -> None: |
| 25 | + """ |
| 26 | + Record a counter metric value. |
| 27 | +
|
| 28 | + Args: |
| 29 | + metric_name: The metric name |
| 30 | + value: The value to add (default 1.0) |
| 31 | + tags: Optional tags |
| 32 | + monotonic: If True, counter only increases. If False, can decrease. |
| 33 | + """ |
| 34 | + |
| 35 | + @hookspec |
| 36 | + def record_gauge( |
| 37 | + self, |
| 38 | + metric_name: str, |
| 39 | + value: float, |
| 40 | + tags: Optional[Dict[str, str]] = None, |
| 41 | + ) -> None: |
| 42 | + """Record a gauge metric value (snapshot/current value).""" |
| 43 | + |
| 44 | + @hookspec |
| 45 | + def record_histogram( |
| 46 | + self, |
| 47 | + metric_name: str, |
| 48 | + value: float, |
| 49 | + tags: Optional[Dict[str, str]] = None, |
| 50 | + buckets: Optional[List[float]] = None, |
| 51 | + ) -> None: |
| 52 | + """ |
| 53 | + Record a value in a histogram/distribution. |
| 54 | +
|
| 55 | + Args: |
| 56 | + metric_name: The metric name |
| 57 | + value: The value to record |
| 58 | + tags: Optional tags |
| 59 | + buckets: Optional bucket boundaries for the histogram |
| 60 | + """ |
| 61 | + |
| 62 | + @hookspec |
| 63 | + def record_summary( |
| 64 | + self, |
| 65 | + metric_name: str, |
| 66 | + value: float, |
| 67 | + tags: Optional[Dict[str, str]] = None, |
| 68 | + ) -> None: |
| 69 | + """ |
| 70 | + Record a value for summary statistics (percentiles, min, max, etc). |
| 71 | + This is similar to histogram but may be implemented differently by backends. |
| 72 | + """ |
| 73 | + |
| 74 | + @hookspec |
| 75 | + def batch_metrics(self) -> ContextManager[None]: |
| 76 | + """ |
| 77 | + Context manager for batching multiple metric operations. |
| 78 | +
|
| 79 | + Returns a context manager that batches metric operations for efficiency. |
| 80 | + Particularly useful for HTTP-based backends to reduce network calls. |
| 81 | +
|
| 82 | + Example: |
| 83 | + with metrics.batch_metrics(): |
| 84 | + metrics.record_counter("requests", 1) |
| 85 | + metrics.record_gauge("queue_size", 42) |
| 86 | + metrics.record_histogram("response_time", 0.123) |
| 87 | + # All metrics sent in one batch here |
| 88 | + """ |
| 89 | + return NotImplemented |
| 90 | + |
| 91 | + @hookspec |
| 92 | + def set_global_tags( |
| 93 | + self, |
| 94 | + tags: Dict[str, str], |
| 95 | + ) -> None: |
| 96 | + """Set global tags to be included with all metrics.""" |
| 97 | + |
| 98 | + @hookspec |
| 99 | + def flush(self) -> None: |
| 100 | + """Force flush any buffered metrics to the backend.""" |
| 101 | + |
| 102 | + |
| 103 | +def get_metrics_reporter_hook() -> pluggy.HookRelay: |
| 104 | + global _cached_metrics_reporter_hook |
| 105 | + |
| 106 | + if _cached_metrics_reporter_hook is not None: |
| 107 | + return _cached_metrics_reporter_hook |
| 108 | + |
| 109 | + pm = pluggy.PluginManager(metrics_reporter_plugin_name) |
| 110 | + pm.add_hookspecs(MetricsReporterPluginSpec) |
| 111 | + |
| 112 | + # Register the hook wrappers |
| 113 | + pm.register(sys.modules[__name__]) |
| 114 | + |
| 115 | + count = pm.load_setuptools_entrypoints(metrics_reporter_plugin_name) |
| 116 | + logger.debug(f"Count of loaded metrics reporter plugins: {count}") |
| 117 | + _cached_metrics_reporter_hook = pm.hook |
| 118 | + |
| 119 | + return _cached_metrics_reporter_hook |
0 commit comments