-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathmonitoring.py
More file actions
399 lines (340 loc) · 14.9 KB
/
monitoring.py
File metadata and controls
399 lines (340 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import logging
from datetime import date
from typing import Any, Dict, List, Optional
from fastapi import APIRouter, HTTPException, Query
from pydantic import BaseModel, Field
from feast.infra.offline_stores.offline_store import OfflineStore
from feast.permissions.action import AuthzedAction
from feast.permissions.security_manager import assert_permissions
logger = logging.getLogger(__name__)
VALID_GRANULARITIES = OfflineStore.MONITORING_VALID_GRANULARITIES
class ComputeMetricsRequest(BaseModel):
project: str
feature_view_name: Optional[str] = None
feature_names: Optional[List[str]] = None
start_date: Optional[str] = None
end_date: Optional[str] = None
granularity: str = Field("daily")
set_baseline: bool = False
class AutoComputeRequest(BaseModel):
project: str
feature_view_name: Optional[str] = None
class ComputeLogMetricsRequest(BaseModel):
project: str
feature_service_name: str
start_date: Optional[str] = None
end_date: Optional[str] = None
granularity: str = Field("daily")
set_baseline: bool = False
class AutoComputeLogRequest(BaseModel):
project: str
feature_service_name: Optional[str] = None
class ComputeTransientRequest(BaseModel):
project: str
feature_view_name: str
feature_names: Optional[List[str]] = None
start_date: Optional[str] = None
end_date: Optional[str] = None
def get_monitoring_router(grpc_handler, store=None):
router = APIRouter()
_monitoring_service = None
def _get_monitoring_service():
nonlocal _monitoring_service
if _monitoring_service is None:
if store is None:
raise HTTPException(
status_code=503,
detail="Monitoring service is not available: no FeatureStore configured",
)
from feast.monitoring.monitoring_service import MonitoringService
_monitoring_service = MonitoringService(store)
return _monitoring_service
def _get_store():
if store is None:
raise HTTPException(
status_code=503,
detail="Monitoring service is not available: no FeatureStore configured",
)
return store
# ------------------------------------------------------------------ #
# DQM Job: submit and track
# ------------------------------------------------------------------ #
@router.post("/monitoring/compute", tags=["Monitoring"])
async def compute_metrics(request: ComputeMetricsRequest):
"""Submit a DQM job to compute and store metrics. Returns job_id.
When set_baseline is True and no date range is provided, computes
baseline from all available source data.
"""
store = _get_store()
if request.feature_view_name:
fv = store.registry.get_feature_view(
name=request.feature_view_name, project=request.project
)
assert_permissions(fv, actions=[AuthzedAction.UPDATE])
svc = _get_monitoring_service()
if request.set_baseline and not request.start_date and not request.end_date:
try:
result = svc.compute_baseline(
project=request.project,
feature_view_name=request.feature_view_name,
feature_names=request.feature_names,
)
return result
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if request.granularity not in VALID_GRANULARITIES:
raise HTTPException(
status_code=400,
detail=f"Invalid granularity '{request.granularity}'. "
f"Must be one of {VALID_GRANULARITIES}",
)
params: Dict[str, Any] = {}
if request.start_date:
params["start_date"] = request.start_date
if request.end_date:
params["end_date"] = request.end_date
if request.feature_names:
params["feature_names"] = request.feature_names
params["granularity"] = request.granularity
params["set_baseline"] = request.set_baseline
job_id = svc.submit_job(
project=request.project,
job_type="compute",
feature_view_name=request.feature_view_name,
parameters=params,
)
try:
result = svc.execute_job(job_id)
return {"job_id": job_id, **result}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.post("/monitoring/auto_compute", tags=["Monitoring"])
async def auto_compute(request: AutoComputeRequest):
"""Auto-detect date ranges and compute all granularities."""
store = _get_store()
if request.feature_view_name:
fv = store.registry.get_feature_view(
name=request.feature_view_name, project=request.project
)
assert_permissions(fv, actions=[AuthzedAction.UPDATE])
svc = _get_monitoring_service()
job_id = svc.submit_job(
project=request.project,
job_type="auto_compute",
feature_view_name=request.feature_view_name,
)
try:
result = svc.execute_job(job_id)
return {"job_id": job_id, **result}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# ------------------------------------------------------------------ #
# Log source: compute from feature serving logs
# ------------------------------------------------------------------ #
@router.post("/monitoring/compute/log", tags=["Monitoring"])
async def compute_log_metrics(request: ComputeLogMetricsRequest):
"""Compute metrics from feature serving logs for a feature service."""
if request.granularity not in VALID_GRANULARITIES:
raise HTTPException(
status_code=400,
detail=f"Invalid granularity '{request.granularity}'. "
f"Must be one of {VALID_GRANULARITIES}",
)
store = _get_store()
fs = store.registry.get_feature_service(
name=request.feature_service_name, project=request.project
)
assert_permissions(fs, actions=[AuthzedAction.UPDATE])
svc = _get_monitoring_service()
start_d = date.fromisoformat(request.start_date) if request.start_date else None
end_d = date.fromisoformat(request.end_date) if request.end_date else None
try:
result = svc.compute_log_metrics(
project=request.project,
feature_service_name=request.feature_service_name,
start_date=start_d,
end_date=end_d,
granularity=request.granularity,
set_baseline=request.set_baseline,
)
return result
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.post("/monitoring/auto_compute/log", tags=["Monitoring"])
async def auto_compute_log(request: AutoComputeLogRequest):
"""Auto-detect date ranges from log data and compute all granularities."""
store = _get_store()
if request.feature_service_name:
fs = store.registry.get_feature_service(
name=request.feature_service_name, project=request.project
)
assert_permissions(fs, actions=[AuthzedAction.UPDATE])
svc = _get_monitoring_service()
try:
result = svc.auto_compute_log_metrics(
project=request.project,
feature_service_name=request.feature_service_name,
)
return result
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/monitoring/jobs/{job_id}", tags=["Monitoring"])
async def get_job_status(job_id: str):
svc = _get_monitoring_service()
job = svc.get_job(job_id)
if job is None:
raise HTTPException(status_code=404, detail=f"Job '{job_id}' not found")
return job
# ------------------------------------------------------------------ #
# Transient compute (not stored)
# ------------------------------------------------------------------ #
@router.post("/monitoring/compute/transient", tags=["Monitoring"])
async def compute_transient(request: ComputeTransientRequest):
"""Compute metrics on-the-fly for an arbitrary date range. Results are
returned directly and NOT persisted to the monitoring tables."""
store = _get_store()
fv = store.registry.get_feature_view(
name=request.feature_view_name, project=request.project
)
assert_permissions(fv, actions=[AuthzedAction.DESCRIBE])
svc = _get_monitoring_service()
start_d = date.fromisoformat(request.start_date) if request.start_date else None
end_d = date.fromisoformat(request.end_date) if request.end_date else None
result = svc.compute_transient(
project=request.project,
feature_view_name=request.feature_view_name,
feature_names=request.feature_names,
start_date=start_d,
end_date=end_d,
)
return result
# ------------------------------------------------------------------ #
# Read endpoints
# ------------------------------------------------------------------ #
@router.get("/monitoring/metrics/features", tags=["Monitoring"])
async def get_feature_metrics(
project: str = Query(...),
feature_view_name: Optional[str] = Query(None),
feature_name: Optional[str] = Query(None),
feature_service_name: Optional[str] = Query(None),
granularity: Optional[str] = Query(None),
data_source_type: Optional[str] = Query(None),
start_date: Optional[str] = Query(None),
end_date: Optional[str] = Query(None),
):
store = _get_store()
if feature_view_name:
fv = store.registry.get_feature_view(
name=feature_view_name, project=project
)
assert_permissions(fv, actions=[AuthzedAction.DESCRIBE])
svc = _get_monitoring_service()
return svc.get_feature_metrics(
project=project,
feature_service_name=feature_service_name,
feature_view_name=feature_view_name,
feature_name=feature_name,
granularity=granularity,
data_source_type=data_source_type,
start_date=date.fromisoformat(start_date) if start_date else None,
end_date=date.fromisoformat(end_date) if end_date else None,
)
@router.get("/monitoring/metrics/feature_views", tags=["Monitoring"])
async def get_feature_view_metrics(
project: str = Query(...),
feature_view_name: Optional[str] = Query(None),
feature_service_name: Optional[str] = Query(None),
granularity: Optional[str] = Query(None),
data_source_type: Optional[str] = Query(None),
start_date: Optional[str] = Query(None),
end_date: Optional[str] = Query(None),
):
store = _get_store()
if feature_view_name:
fv = store.registry.get_feature_view(
name=feature_view_name, project=project
)
assert_permissions(fv, actions=[AuthzedAction.DESCRIBE])
svc = _get_monitoring_service()
return svc.get_feature_view_metrics(
project=project,
feature_service_name=feature_service_name,
feature_view_name=feature_view_name,
granularity=granularity,
data_source_type=data_source_type,
start_date=date.fromisoformat(start_date) if start_date else None,
end_date=date.fromisoformat(end_date) if end_date else None,
)
@router.get("/monitoring/metrics/feature_services", tags=["Monitoring"])
async def get_feature_service_metrics(
project: str = Query(...),
feature_service_name: Optional[str] = Query(None),
granularity: Optional[str] = Query(None),
data_source_type: Optional[str] = Query(None),
start_date: Optional[str] = Query(None),
end_date: Optional[str] = Query(None),
):
store = _get_store()
if feature_service_name:
fs = store.registry.get_feature_service(
name=feature_service_name, project=project
)
assert_permissions(fs, actions=[AuthzedAction.DESCRIBE])
svc = _get_monitoring_service()
return svc.get_feature_service_metrics(
project=project,
feature_service_name=feature_service_name,
granularity=granularity,
data_source_type=data_source_type,
start_date=date.fromisoformat(start_date) if start_date else None,
end_date=date.fromisoformat(end_date) if end_date else None,
)
@router.get("/monitoring/metrics/baseline", tags=["Monitoring"])
async def get_baseline(
project: str = Query(...),
feature_view_name: Optional[str] = Query(None),
feature_name: Optional[str] = Query(None),
data_source_type: Optional[str] = Query(None),
):
store = _get_store()
if feature_view_name:
fv = store.registry.get_feature_view(
name=feature_view_name, project=project
)
assert_permissions(fv, actions=[AuthzedAction.DESCRIBE])
svc = _get_monitoring_service()
return svc.get_baseline(
project=project,
feature_view_name=feature_view_name,
feature_name=feature_name,
data_source_type=data_source_type,
)
@router.get("/monitoring/metrics/timeseries", tags=["Monitoring"])
async def get_timeseries(
project: str = Query(...),
feature_view_name: Optional[str] = Query(None),
feature_name: Optional[str] = Query(None),
feature_service_name: Optional[str] = Query(None),
granularity: Optional[str] = Query(None),
data_source_type: Optional[str] = Query(None),
start_date: Optional[str] = Query(None),
end_date: Optional[str] = Query(None),
):
store = _get_store()
if feature_view_name:
fv = store.registry.get_feature_view(
name=feature_view_name, project=project
)
assert_permissions(fv, actions=[AuthzedAction.DESCRIBE])
svc = _get_monitoring_service()
return svc.get_timeseries(
project=project,
feature_view_name=feature_view_name,
feature_name=feature_name,
feature_service_name=feature_service_name,
granularity=granularity,
data_source_type=data_source_type,
start_date=date.fromisoformat(start_date) if start_date else None,
end_date=date.fromisoformat(end_date) if end_date else None,
)
return router