|
9 | 9 | import uuid
|
10 | 10 |
|
11 | 11 | import pytest
|
12 |
| -from prometheus_client import REGISTRY |
| 12 | +from prometheus_client import REGISTRY, CollectorRegistry |
13 | 13 |
|
14 | 14 | import litellm
|
15 | 15 | from litellm import completion
|
@@ -79,3 +79,91 @@ async def test_async_prometheus_success_logging():
|
79 | 79 | assert metrics["litellm_deployment_success_responses_total"] == 1.0
|
80 | 80 | assert metrics["litellm_deployment_total_requests_total"] == 1.0
|
81 | 81 | assert metrics["litellm_deployment_latency_per_output_token_bucket"] == 1.0
|
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.asyncio() |
| 85 | +async def test_async_prometheus_success_logging_with_callbacks(): |
| 86 | + run_id = str(uuid.uuid4()) |
| 87 | + litellm.set_verbose = True |
| 88 | + |
| 89 | + litellm.success_callback = [] |
| 90 | + litellm.failure_callback = [] |
| 91 | + litellm.callbacks = ["prometheus"] |
| 92 | + |
| 93 | + # Get initial metric values |
| 94 | + initial_metrics = {} |
| 95 | + for metric in REGISTRY.collect(): |
| 96 | + for sample in metric.samples: |
| 97 | + initial_metrics[sample.name] = sample.value |
| 98 | + |
| 99 | + response = await litellm.acompletion( |
| 100 | + model="claude-instant-1.2", |
| 101 | + messages=[{"role": "user", "content": "what llm are u"}], |
| 102 | + max_tokens=10, |
| 103 | + mock_response="hi", |
| 104 | + temperature=0.2, |
| 105 | + metadata={ |
| 106 | + "id": run_id, |
| 107 | + "tags": ["tag1", "tag2"], |
| 108 | + "user_api_key": "6eb81e014497d89f3cc1aa9da7c2b37bda6b7fea68e4b710d33d94201e68970c", |
| 109 | + "user_api_key_alias": "ishaans-prometheus-key", |
| 110 | + "user_api_end_user_max_budget": None, |
| 111 | + "litellm_api_version": "1.40.19", |
| 112 | + "global_max_parallel_requests": None, |
| 113 | + "user_api_key_user_id": "admin", |
| 114 | + "user_api_key_org_id": None, |
| 115 | + "user_api_key_team_id": "dbe2f686-a686-4896-864a-4c3924458709", |
| 116 | + "user_api_key_team_alias": "testing-team", |
| 117 | + }, |
| 118 | + ) |
| 119 | + print(response) |
| 120 | + await asyncio.sleep(3) |
| 121 | + |
| 122 | + # get prometheus logger |
| 123 | + from litellm.litellm_core_utils.litellm_logging import _in_memory_loggers |
| 124 | + |
| 125 | + for callback in _in_memory_loggers: |
| 126 | + if isinstance(callback, PrometheusLogger): |
| 127 | + test_prometheus_logger = callback |
| 128 | + |
| 129 | + print("done with success request") |
| 130 | + |
| 131 | + print( |
| 132 | + "vars of test_prometheus_logger", |
| 133 | + vars(test_prometheus_logger.litellm_requests_metric), |
| 134 | + ) |
| 135 | + |
| 136 | + # Get the updated metrics |
| 137 | + updated_metrics = {} |
| 138 | + for metric in REGISTRY.collect(): |
| 139 | + for sample in metric.samples: |
| 140 | + updated_metrics[sample.name] = sample.value |
| 141 | + |
| 142 | + print("metrics from prometheus", updated_metrics) |
| 143 | + |
| 144 | + # Assert the delta for each metric |
| 145 | + assert ( |
| 146 | + updated_metrics["litellm_requests_metric_total"] |
| 147 | + - initial_metrics.get("litellm_requests_metric_total", 0) |
| 148 | + == 1.0 |
| 149 | + ) |
| 150 | + assert ( |
| 151 | + updated_metrics["litellm_total_tokens_total"] |
| 152 | + - initial_metrics.get("litellm_total_tokens_total", 0) |
| 153 | + == 30.0 |
| 154 | + ) |
| 155 | + assert ( |
| 156 | + updated_metrics["litellm_deployment_success_responses_total"] |
| 157 | + - initial_metrics.get("litellm_deployment_success_responses_total", 0) |
| 158 | + == 1.0 |
| 159 | + ) |
| 160 | + assert ( |
| 161 | + updated_metrics["litellm_deployment_total_requests_total"] |
| 162 | + - initial_metrics.get("litellm_deployment_total_requests_total", 0) |
| 163 | + == 1.0 |
| 164 | + ) |
| 165 | + assert ( |
| 166 | + updated_metrics["litellm_deployment_latency_per_output_token_bucket"] |
| 167 | + - initial_metrics.get("litellm_deployment_latency_per_output_token_bucket", 0) |
| 168 | + == 1.0 |
| 169 | + ) |
0 commit comments