-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathlogger.py
More file actions
375 lines (324 loc) · 14.1 KB
/
logger.py
File metadata and controls
375 lines (324 loc) · 14.1 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
from __future__ import annotations
import logging
import time
from datetime import datetime
from typing import TYPE_CHECKING, Any, Dict, List, Optional
import pandas as pd
from feast.mlflow_integration.config import (
MLFLOW_TAG_TRUNCATION_LIMIT,
MLFLOW_TAG_TRUNCATION_SLICE,
)
if TYPE_CHECKING:
from feast import FeatureStore
from feast.feature_service import FeatureService
_logger = logging.getLogger(__name__)
_WARNING_INTERVAL_SECONDS = 300
class FeastMlflowLogger:
"""Handles all MLflow logging for Feast feature retrieval and operations.
Instantiated once inside :class:`FeastMlflowClient` and reuses its
``mlflow`` module reference and ``MlflowClient`` — no duplicate
``import mlflow`` or client construction.
"""
def __init__(self, store: "FeatureStore", mlflow_mod: Any, client: Any):
self._store = store
self._mlflow = mlflow_mod
self._client = client
self._tracking_uri = store.config.mlflow.get_tracking_uri()
self._consecutive_failures = 0
self._last_warning_time = 0.0
def _truncate_for_tag(self, value: str) -> str:
if len(value) > MLFLOW_TAG_TRUNCATION_LIMIT:
return value[:MLFLOW_TAG_TRUNCATION_SLICE] + "..."
return value
def _report_failure(self, msg: str, exc: Exception) -> None:
self._consecutive_failures += 1
now = time.monotonic()
if (
self._consecutive_failures == 1
or (now - self._last_warning_time) >= _WARNING_INTERVAL_SECONDS
):
_logger.warning(
"%s (failures=%d): %s", msg, self._consecutive_failures, exc
)
self._last_warning_time = now
else:
_logger.debug("%s (failures=%d): %s", msg, self._consecutive_failures, exc)
def _report_success(self) -> None:
self._consecutive_failures = 0
def log_feature_retrieval(
self,
feature_refs: List[str],
entity_count: int,
duration_seconds: float,
retrieval_type: str = "historical",
feature_service: Optional["FeatureService"] = None,
feature_service_name: Optional[str] = None,
) -> bool:
"""Log feature retrieval metadata to the active MLflow run."""
active_run = self._mlflow.active_run()
if active_run is None:
return False
try:
run_id = active_run.info.run_id
if self._store.project:
self._client.set_tag(run_id, "feast.project", self._store.project)
self._client.set_tag(run_id, "feast.retrieval_type", retrieval_type)
fs_name = None
if feature_service is not None:
fs_name = feature_service.name
elif feature_service_name is not None:
fs_name = feature_service_name
if fs_name:
self._client.set_tag(run_id, "feast.feature_service", fs_name)
fv_names = sorted({ref.split(":")[0] for ref in feature_refs if ":" in ref})
if fv_names:
fv_str = self._truncate_for_tag(",".join(fv_names))
self._client.set_tag(run_id, "feast.feature_views", fv_str)
refs_str = self._truncate_for_tag(",".join(feature_refs))
self._client.set_tag(run_id, "feast.feature_refs", refs_str)
self._client.set_tag(run_id, "feast.entity_count", str(entity_count))
self._client.set_tag(run_id, "feast.feature_count", str(len(feature_refs)))
self._client.log_metric(
run_id, "feast.job_submission_sec", round(duration_seconds, 4)
)
self._report_success()
return True
except Exception as e:
self._report_failure("Failed to log feature retrieval to MLflow", e)
return False
def log_training_dataset(
self,
df: pd.DataFrame,
dataset_name: str = "feast_training_data",
source: Optional[str] = None,
) -> bool:
"""Log a training DataFrame as an MLflow dataset input on the active run."""
active_run = self._mlflow.active_run()
if active_run is None:
return False
try:
dataset = self._mlflow.data.from_pandas(
df,
name=dataset_name,
source=source or "feast.get_historical_features",
)
self._mlflow.log_input(dataset, context="training")
return True
except Exception as e:
self._report_failure("Failed to log training dataset to MLflow", e)
return False
def _get_or_create_experiment(self, experiment_name: str) -> str:
exp = self._client.get_experiment_by_name(experiment_name)
if exp is not None:
return exp.experiment_id
return self._client.create_experiment(experiment_name)
def log_apply(
self,
changed_objects: List[Any],
transition_types: Optional[Dict[str, str]] = None,
) -> bool:
"""Log a feast apply operation to a dedicated MLflow experiment."""
try:
from feast import Entity, FeatureService
from feast.feature_view import FeatureView
project = self._store.project
mlflow_cfg = self._store.config.mlflow
ops_suffix = mlflow_cfg.ops_experiment_suffix
experiment_name = f"{project}{ops_suffix}"
experiment_id = self._get_or_create_experiment(experiment_name)
fv_names: List[str] = []
fs_names: List[str] = []
entity_names: List[str] = []
for obj in changed_objects:
if isinstance(obj, FeatureView):
fv_names.append(obj.name)
elif isinstance(obj, FeatureService):
fs_names.append(obj.name)
elif isinstance(obj, Entity) and obj.name != "__dummy":
entity_names.append(obj.name)
run = self._client.create_run(experiment_id, run_name=f"apply_{project}")
run_id = run.info.run_id
try:
self._client.set_tag(run_id, "feast.operation", "apply")
self._client.set_tag(run_id, "feast.project", project)
if fv_names:
self._client.set_tag(
run_id,
"feast.feature_views_changed",
self._truncate_for_tag(",".join(fv_names)),
)
if fs_names:
self._client.set_tag(
run_id,
"feast.feature_services_changed",
self._truncate_for_tag(",".join(fs_names)),
)
if entity_names:
self._client.set_tag(
run_id,
"feast.entities_changed",
self._truncate_for_tag(",".join(entity_names)),
)
self._client.log_metric(
run_id, "feast.apply.feature_views_count", len(fv_names)
)
self._client.log_metric(
run_id, "feast.apply.feature_services_count", len(fs_names)
)
self._client.log_metric(
run_id, "feast.apply.entities_count", len(entity_names)
)
if transition_types:
self._log_transition_tags(
run_id,
transition_types,
fv_names,
fs_names,
entity_names,
)
finally:
self._client.set_terminated(run_id)
self._report_success()
return True
except Exception as e:
self._report_failure("Failed to log apply to MLflow", e)
return False
def _log_transition_tags(
self,
run_id: str,
transition_types: Dict[str, str],
fv_names: List[str],
fs_names: List[str],
entity_names: List[str],
) -> None:
buckets: Dict[str, Dict[str, List[str]]] = {
"feature_views": {"created": [], "updated": [], "deleted": []},
"feature_services": {"created": [], "updated": [], "deleted": []},
"entities": {"created": [], "updated": [], "deleted": []},
}
for name in fv_names:
tt = transition_types.get(name, "").upper()
if tt in ("CREATE", "UPDATE", "DELETE"):
buckets["feature_views"][tt.lower() + "d"].append(name)
for name in fs_names:
tt = transition_types.get(name, "").upper()
if tt in ("CREATE", "UPDATE", "DELETE"):
buckets["feature_services"][tt.lower() + "d"].append(name)
for name in entity_names:
tt = transition_types.get(name, "").upper()
if tt in ("CREATE", "UPDATE", "DELETE"):
buckets["entities"][tt.lower() + "d"].append(name)
for obj_type, transitions in buckets.items():
for transition, names in transitions.items():
if names:
self._client.set_tag(
run_id,
f"feast.{obj_type}_{transition}",
self._truncate_for_tag(",".join(names)),
)
def log_materialize(
self,
feature_view_names: List[str],
start_date: Optional[datetime],
end_date: datetime,
duration_seconds: float,
incremental: bool = False,
) -> bool:
"""Log a feast materialize operation to a dedicated MLflow experiment."""
try:
project = self._store.project
mlflow_cfg = self._store.config.mlflow
ops_suffix = mlflow_cfg.ops_experiment_suffix
experiment_name = f"{project}{ops_suffix}"
experiment_id = self._get_or_create_experiment(experiment_name)
op_type = "materialize_incremental" if incremental else "materialize"
run = self._client.create_run(
experiment_id, run_name=f"{op_type}_{project}"
)
run_id = run.info.run_id
try:
self._client.set_tag(run_id, "feast.operation", op_type)
self._client.set_tag(run_id, "feast.project", project)
self._client.set_tag(
run_id,
"feast.materialize.feature_views",
self._truncate_for_tag(",".join(feature_view_names)),
)
if start_date:
self._client.log_param(
run_id,
"feast.materialize.start_date",
start_date.isoformat(),
)
self._client.log_param(
run_id,
"feast.materialize.end_date",
end_date.isoformat(),
)
self._client.log_metric(
run_id,
"feast.materialize.duration_sec",
round(duration_seconds, 4),
)
finally:
self._client.set_terminated(run_id)
self._report_success()
return True
except Exception as e:
self._report_failure("Failed to log materialize to MLflow", e)
return False
def log_entity_df_metadata(
self, entity_df: Any, start_date: Any = None, end_date: Any = None
) -> None:
"""Log lightweight entity_df metadata to MLflow.
Uses ``set_tag`` (not ``log_param``) so the metadata can safely be
updated when ``get_historical_features`` is called multiple times
within the same MLflow run.
"""
try:
if self._mlflow.active_run() is None:
return
run_id = self._mlflow.active_run().info.run_id
if isinstance(entity_df, str):
if len(entity_df) > MLFLOW_TAG_TRUNCATION_LIMIT:
query = entity_df[:MLFLOW_TAG_TRUNCATION_SLICE] + "..."
else:
query = entity_df
self._client.set_tag(run_id, "feast.entity_df_query", query)
self._client.set_tag(run_id, "feast.entity_df_type", "sql")
elif isinstance(entity_df, pd.DataFrame):
self._client.set_tag(run_id, "feast.entity_df_type", "dataframe")
self._client.set_tag(
run_id, "feast.entity_df_rows", str(len(entity_df))
)
cols = ",".join(entity_df.columns)
if len(cols) > MLFLOW_TAG_TRUNCATION_LIMIT:
cols = cols[:MLFLOW_TAG_TRUNCATION_SLICE] + "..."
self._client.set_tag(run_id, "feast.entity_df_columns", cols)
elif entity_df is None and (start_date or end_date):
self._client.set_tag(run_id, "feast.entity_df_type", "range")
if start_date:
self._client.set_tag(run_id, "feast.start_date", str(start_date))
if end_date:
self._client.set_tag(run_id, "feast.end_date", str(end_date))
except Exception as e:
_logger.debug("Failed to log entity_df metadata to MLflow: %s", e)
def log_entity_df_artifact(self, entity_df: Any) -> None:
"""Upload entity DataFrame as a parquet artifact to MLflow."""
try:
import os
import tempfile
if self._mlflow.active_run() is None:
return
if not isinstance(entity_df, pd.DataFrame):
return
mlflow_cfg = self._store.config.mlflow
run_id = self._mlflow.active_run().info.run_id
max_rows = mlflow_cfg.entity_df_max_rows
if len(entity_df) <= max_rows:
with tempfile.TemporaryDirectory() as tmp_dir:
path = os.path.join(tmp_dir, "entity_df.parquet")
entity_df.to_parquet(path, index=False)
self._client.log_artifact(run_id, path)
except Exception as e:
_logger.debug("Failed to log entity_df artifact to MLflow: %s", e)