langfun 0.1.2.dev202509290805__py3-none-any.whl → 0.1.2.dev202510010805__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of langfun might be problematic. Click here for more details.

@@ -0,0 +1,607 @@
1
+ # Copyright 2025 The Langfun Authors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """Environment event logger."""
15
+
16
+ from typing import Annotated
17
+ from langfun.env.event_handlers import base
18
+ import pyglove as pg
19
+
20
+
21
+ _METRIC_NAMESPACE = '/langfun/env'
22
+
23
+
24
+ class MetricWriter(pg.Object, base.EventHandler):
25
+ """Event handler for streamz metrics."""
26
+
27
+ app: Annotated[
28
+ str,
29
+ 'Application name that will be used as a metric parameter.'
30
+ ] = ''
31
+
32
+ def _get_counter(
33
+ self,
34
+ name: str,
35
+ description: str,
36
+ parameters: dict[str, type[str]] | None = None
37
+ ) -> pg.monitoring.Counter:
38
+ return self._metric_collection.get_counter(
39
+ name=name,
40
+ description=description,
41
+ parameters=parameters
42
+ )
43
+
44
+ def _get_scalar(
45
+ self,
46
+ name: str,
47
+ description: str,
48
+ parameters: dict[str, type[str]] | None = None
49
+ ) -> pg.monitoring.Metric:
50
+ return self._metric_collection.get_scalar(
51
+ name=name,
52
+ description=description,
53
+ parameters=parameters
54
+ )
55
+
56
+ def _error_tag(self, error: BaseException | None) -> str:
57
+ if error is None:
58
+ return ''
59
+ return pg.utils.ErrorInfo.from_exception(error).tag
60
+
61
+ def _initialize_metrics(self) -> None:
62
+ """Initializes metrics."""
63
+
64
+ self._metric_collection = pg.monitoring.metric_collection(_METRIC_NAMESPACE)
65
+
66
+ #
67
+ # Environment metrics.
68
+ #
69
+
70
+ self._environment_housekeep_duration_ms = self._get_scalar(
71
+ 'environment_housekeep_duration_ms',
72
+ description='Environment housekeeping duration in milliseconds',
73
+ parameters={
74
+ 'app': str,
75
+ 'environment_id': str,
76
+ 'error': str,
77
+ }
78
+ )
79
+
80
+ #
81
+ # Sandbox metrics.
82
+ #
83
+
84
+ # Sandbox counters.
85
+ self._sandbox_start = self._get_counter(
86
+ 'sandbox_start',
87
+ description='Sandbox start counter',
88
+ parameters={
89
+ 'app': str,
90
+ 'environment_id': str,
91
+ 'error': str,
92
+ }
93
+ )
94
+ self._sandbox_shutdown = self._get_counter(
95
+ 'sandbox_shutdown',
96
+ description='Sandbox shutdown counter',
97
+ parameters={
98
+ 'app': str,
99
+ 'environment_id': str,
100
+ 'error': str
101
+ }
102
+ )
103
+ self._sandbox_housekeep = self._get_counter(
104
+ 'sandbox_housekeep',
105
+ description='Sandbox housekeeping counter',
106
+ parameters={
107
+ 'app': str,
108
+ 'environment_id': str,
109
+ 'error': str,
110
+ }
111
+ )
112
+ self._sandbox_activity = self._get_counter(
113
+ 'sandbox_activity',
114
+ description='Sandbox activity counter',
115
+ parameters={
116
+ 'app': str,
117
+ 'environment_id': str,
118
+ 'activity': str,
119
+ 'error': str,
120
+ }
121
+ )
122
+
123
+ # Sandbox scalars.
124
+ self._sandbox_lifetime_ms = self._get_scalar(
125
+ 'sandbox_lifetime_ms',
126
+ description='Sandbox life time in milliseconds',
127
+ parameters={
128
+ 'app': str,
129
+ 'environment_id': str,
130
+ 'error': str,
131
+ }
132
+ )
133
+ self._sandbox_start_duration_ms = self._get_scalar(
134
+ 'sandbox_start_duration_ms',
135
+ description='Sandbox start duration in milliseconds',
136
+ parameters={
137
+ 'app': str,
138
+ 'environment_id': str,
139
+ 'error': str,
140
+ }
141
+ )
142
+ self._sandbox_shutdown_duration_ms = self._get_scalar(
143
+ 'sandbox_shutdown_duration_ms',
144
+ description='Sandbox shutdown duration in milliseconds',
145
+ parameters={
146
+ 'app': str,
147
+ 'environment_id': str,
148
+ 'error': str,
149
+ }
150
+ )
151
+ self._sandbox_housekeep_duration_ms = self._get_scalar(
152
+ 'sandbox_housekeep_duration_ms',
153
+ description='Sandbox housekeeping duration in milliseconds',
154
+ parameters={
155
+ 'app': str,
156
+ 'environment_id': str,
157
+ 'error': str,
158
+ }
159
+ )
160
+ self._sandbox_status_duration_ms = self._get_scalar(
161
+ 'sandbox_status_duration_ms',
162
+ description='Sandbox duration of specific status in milliseconds',
163
+ parameters={
164
+ 'app': str,
165
+ 'environment_id': str,
166
+ 'status': str,
167
+ }
168
+ )
169
+ self._sandbox_activity_duration_ms = self._get_scalar(
170
+ 'sandbox_activity_duration_ms',
171
+ description='Sandbox activity duration in milliseconds',
172
+ parameters={
173
+ 'app': str,
174
+ 'environment_id': str,
175
+ 'activity': str,
176
+ 'error': str,
177
+ }
178
+ )
179
+
180
+ #
181
+ # Feature metrics.
182
+ #
183
+
184
+ # Feature counters.
185
+ self._feature_setup = self._get_counter(
186
+ 'feature_setup',
187
+ description='Feature setup counter',
188
+ parameters={
189
+ 'app': str,
190
+ 'environment_id': str,
191
+ 'feature_name': str,
192
+ 'error': str,
193
+ }
194
+ )
195
+ self._feature_teardown = self._get_counter(
196
+ 'feature_teardown',
197
+ description='Feature teardown counter',
198
+ parameters={
199
+ 'app': str,
200
+ 'environment_id': str,
201
+ 'feature_name': str,
202
+ 'error': str,
203
+ }
204
+ )
205
+ self._feature_setup_session = self._get_counter(
206
+ 'feature_setup_session',
207
+ description='Feature setup session counter',
208
+ parameters={
209
+ 'app': str,
210
+ 'environment_id': str,
211
+ 'feature_name': str,
212
+ 'error': str,
213
+ }
214
+ )
215
+ self._feature_teardown_session = self._get_counter(
216
+ 'feature_teardown_session',
217
+ description='Feature teardown session counter',
218
+ parameters={
219
+ 'app': str,
220
+ 'environment_id': str,
221
+ 'feature_name': str,
222
+ 'error': str,
223
+ }
224
+ )
225
+ self._feature_housekeep = self._get_counter(
226
+ 'feature_housekeep',
227
+ description='Feature housekeeping counter',
228
+ parameters={
229
+ 'app': str,
230
+ 'environment_id': str,
231
+ 'feature_name': str,
232
+ 'error': str,
233
+ }
234
+ )
235
+
236
+ # Feature scalars.
237
+ self._feature_setup_duration_ms = self._get_scalar(
238
+ 'feature_setup_duration_ms',
239
+ description='Feature setup duration in milliseconds',
240
+ parameters={
241
+ 'app': str,
242
+ 'environment_id': str,
243
+ 'feature_name': str,
244
+ 'error': str,
245
+ }
246
+ )
247
+ self._feature_teardown_duration_ms = self._get_scalar(
248
+ 'feature_teardown_duration_ms',
249
+ description='Feature teardown duration in milliseconds',
250
+ parameters={
251
+ 'app': str,
252
+ 'environment_id': str,
253
+ 'feature_name': str,
254
+ 'error': str,
255
+ }
256
+ )
257
+ self._feature_setup_session_duration_ms = self._get_scalar(
258
+ 'feature_setup_session_duration_ms',
259
+ description='Feature setup session duration in milliseconds',
260
+ parameters={
261
+ 'app': str,
262
+ 'environment_id': str,
263
+ 'feature_name': str,
264
+ 'error': str,
265
+ }
266
+ )
267
+ self._feature_teardown_session_duration_ms = self._get_scalar(
268
+ 'feature_teardown_session_duration_ms',
269
+ description='Feature teardown session duration in milliseconds',
270
+ parameters={
271
+ 'app': str,
272
+ 'environment_id': str,
273
+ 'feature_name': str,
274
+ 'error': str,
275
+ }
276
+ )
277
+ self._feature_housekeep_duration_ms = self._get_scalar(
278
+ 'feature_housekeep_duration_ms',
279
+ description='Feature housekeeping duration in milliseconds',
280
+ parameters={
281
+ 'app': str,
282
+ 'environment_id': str,
283
+ 'feature_name': str,
284
+ 'error': str,
285
+ }
286
+ )
287
+
288
+ #
289
+ # Session metrics.
290
+ #
291
+
292
+ self._session_start_duration_ms = self._get_scalar(
293
+ 'session_start_duration_ms',
294
+ description='Session start duration in milliseconds',
295
+ parameters={
296
+ 'app': str,
297
+ 'environment_id': str,
298
+ 'error': str,
299
+ }
300
+ )
301
+ self._session_end_duration_ms = self._get_scalar(
302
+ 'session_end_duration_ms',
303
+ description='Session end duration in milliseconds',
304
+ parameters={
305
+ 'app': str,
306
+ 'environment_id': str,
307
+ 'error': str,
308
+ }
309
+ )
310
+ self._session_lifetime_ms = self._get_scalar(
311
+ 'session_lifetime_ms',
312
+ description='Session lifetime in milliseconds',
313
+ parameters={
314
+ 'app': str,
315
+ 'environment_id': str,
316
+ 'error': str,
317
+ }
318
+ )
319
+
320
+ def on_environment_starting(
321
+ self,
322
+ environment: base.Environment
323
+ ) -> None:
324
+ """Called when the environment is starting."""
325
+ self._initialize_metrics()
326
+
327
+ def on_environment_housekeep(
328
+ self,
329
+ environment: base.Environment,
330
+ counter: int,
331
+ duration: float,
332
+ error: BaseException | None,
333
+ **kwargs
334
+ ) -> None:
335
+ """Called when the environment is housekeeping."""
336
+ self._environment_housekeep_duration_ms.record(
337
+ int(duration * 1000),
338
+ app=self.app,
339
+ environment_id=str(environment.id),
340
+ error=self._error_tag(error)
341
+ )
342
+
343
+ def on_sandbox_start(
344
+ self,
345
+ environment: base.Environment,
346
+ sandbox: base.Sandbox,
347
+ duration: float,
348
+ error: BaseException | None
349
+ ) -> None:
350
+ self._sandbox_start.increment(
351
+ app=self.app,
352
+ environment_id=str(environment.id),
353
+ error=self._error_tag(error)
354
+ )
355
+ self._sandbox_start_duration_ms.record(
356
+ int(duration * 1000),
357
+ app=self.app,
358
+ environment_id=str(environment.id),
359
+ error=self._error_tag(error)
360
+ )
361
+
362
+ def on_sandbox_status_change(
363
+ self,
364
+ environment: base.Environment,
365
+ sandbox: base.Sandbox,
366
+ old_status: base.Sandbox.Status,
367
+ new_status: base.Sandbox.Status,
368
+ span: float
369
+ ) -> None:
370
+ self._sandbox_status_duration_ms.record(
371
+ int(span * 1000),
372
+ app=self.app,
373
+ environment_id=str(environment.id),
374
+ status=old_status.value
375
+ )
376
+
377
+ def on_sandbox_shutdown(
378
+ self,
379
+ environment: base.Environment,
380
+ sandbox: base.Sandbox,
381
+ duration: float,
382
+ lifetime: float,
383
+ error: BaseException | None
384
+ ) -> None:
385
+ self._sandbox_shutdown.increment(
386
+ app=self.app,
387
+ environment_id=str(environment.id),
388
+ error=self._error_tag(error)
389
+ )
390
+ self._sandbox_shutdown_duration_ms.record(
391
+ int(duration * 1000),
392
+ app=self.app,
393
+ environment_id=str(environment.id),
394
+ error=self._error_tag(error)
395
+ )
396
+ self._sandbox_lifetime_ms.record(
397
+ int(lifetime * 1000),
398
+ app=self.app,
399
+ environment_id=str(environment.id),
400
+ error=self._error_tag(error)
401
+ )
402
+
403
+ def on_sandbox_housekeep(
404
+ self,
405
+ environment: base.Environment,
406
+ sandbox: base.Sandbox,
407
+ counter: int,
408
+ duration: float,
409
+ error: BaseException | None,
410
+ **kwargs
411
+ ) -> None:
412
+ """Called when a sandbox feature is housekeeping."""
413
+ self._sandbox_housekeep.increment(
414
+ app=self.app,
415
+ environment_id=str(environment.id),
416
+ error=self._error_tag(error)
417
+ )
418
+ self._sandbox_housekeep_duration_ms.record(
419
+ int(duration * 1000),
420
+ app=self.app,
421
+ environment_id=str(environment.id),
422
+ error=self._error_tag(error)
423
+ )
424
+
425
+ def on_feature_setup(
426
+ self,
427
+ environment: base.Environment,
428
+ sandbox: base.Sandbox,
429
+ feature: base.Feature,
430
+ duration: float,
431
+ error: BaseException | None
432
+ ) -> None:
433
+ """Called when a sandbox feature is setup."""
434
+ self._feature_setup.increment(
435
+ app=self.app,
436
+ environment_id=str(environment.id),
437
+ feature_name=feature.name,
438
+ error=self._error_tag(error)
439
+ )
440
+ self._feature_setup_duration_ms.record(
441
+ int(duration * 1000),
442
+ app=self.app,
443
+ environment_id=str(environment.id),
444
+ feature_name=feature.name,
445
+ error=self._error_tag(error)
446
+ )
447
+
448
+ def on_feature_teardown(
449
+ self,
450
+ environment: base.Environment,
451
+ sandbox: base.Sandbox,
452
+ feature: base.Feature,
453
+ duration: float,
454
+ error: BaseException | None
455
+ ) -> None:
456
+ """Called when a sandbox feature is teardown."""
457
+ self._feature_teardown.increment(
458
+ app=self.app,
459
+ environment_id=str(environment.id),
460
+ feature_name=feature.name,
461
+ error=self._error_tag(error)
462
+ )
463
+ self._feature_teardown_duration_ms.record(
464
+ int(duration * 1000),
465
+ app=self.app,
466
+ environment_id=str(environment.id),
467
+ feature_name=feature.name,
468
+ error=self._error_tag(error)
469
+ )
470
+
471
+ def on_feature_setup_session(
472
+ self,
473
+ environment: base.Environment,
474
+ sandbox: base.Sandbox,
475
+ feature: base.Feature,
476
+ session_id: str | None,
477
+ duration: float,
478
+ error: BaseException | None
479
+ ) -> None:
480
+ """Called when a sandbox feature is setup."""
481
+ self._feature_setup_session.increment(
482
+ app=self.app,
483
+ environment_id=str(environment.id),
484
+ feature_name=feature.name,
485
+ error=self._error_tag(error)
486
+ )
487
+ self._feature_setup_session_duration_ms.record(
488
+ int(duration * 1000),
489
+ app=self.app,
490
+ environment_id=str(environment.id),
491
+ feature_name=feature.name,
492
+ error=self._error_tag(error)
493
+ )
494
+
495
+ def on_feature_teardown_session(
496
+ self,
497
+ environment: base.Environment,
498
+ sandbox: base.Sandbox,
499
+ feature: base.Feature,
500
+ session_id: str,
501
+ duration: float,
502
+ error: BaseException | None
503
+ ) -> None:
504
+ """Called when a sandbox feature is teardown."""
505
+ self._feature_teardown_session.increment(
506
+ app=self.app,
507
+ environment_id=str(environment.id),
508
+ feature_name=feature.name,
509
+ error=self._error_tag(error)
510
+ )
511
+ self._feature_teardown_session_duration_ms.record(
512
+ int(duration * 1000),
513
+ app=self.app,
514
+ environment_id=str(environment.id),
515
+ feature_name=feature.name,
516
+ error=self._error_tag(error)
517
+ )
518
+
519
+ def on_feature_housekeep(
520
+ self,
521
+ environment: base.Environment,
522
+ sandbox: base.Sandbox,
523
+ feature: base.Feature,
524
+ counter: int,
525
+ duration: float,
526
+ error: BaseException | None,
527
+ **kwargs
528
+ ) -> None:
529
+ """Called when a sandbox feature is housekeeping."""
530
+ self._feature_housekeep.increment(
531
+ app=self.app,
532
+ environment_id=str(environment.id),
533
+ feature_name=feature.name,
534
+ error=self._error_tag(error)
535
+ )
536
+ self._feature_housekeep_duration_ms.record(
537
+ int(duration * 1000),
538
+ app=self.app,
539
+ environment_id=str(environment.id),
540
+ feature_name=feature.name,
541
+ error=self._error_tag(error)
542
+ )
543
+
544
+ def on_session_start(
545
+ self,
546
+ environment: base.Environment,
547
+ sandbox: base.Sandbox,
548
+ session_id: str,
549
+ duration: float,
550
+ error: BaseException | None
551
+ ) -> None:
552
+ """Called when a sandbox session starts."""
553
+ self._session_start_duration_ms.record(
554
+ int(duration * 1000),
555
+ app=self.app,
556
+ environment_id=str(environment.id),
557
+ error=self._error_tag(error)
558
+ )
559
+
560
+ def on_session_end(
561
+ self,
562
+ environment: base.Environment,
563
+ sandbox: base.Sandbox,
564
+ session_id: str,
565
+ duration: float,
566
+ lifetime: float,
567
+ error: BaseException | None
568
+ ) -> None:
569
+ """Called when a sandbox session ends."""
570
+ self._session_end_duration_ms.record(
571
+ int(duration * 1000),
572
+ app=self.app,
573
+ environment_id=str(environment.id),
574
+ error=self._error_tag(error)
575
+ )
576
+ self._session_lifetime_ms.record(
577
+ int(lifetime * 1000),
578
+ app=self.app,
579
+ environment_id=str(environment.id),
580
+ error=self._error_tag(error)
581
+ )
582
+
583
+ def on_sandbox_activity(
584
+ self,
585
+ name: str,
586
+ environment: base.Environment,
587
+ sandbox: base.Sandbox,
588
+ feature: base.Feature | None,
589
+ session_id: str | None,
590
+ duration: float,
591
+ error: BaseException | None,
592
+ **kwargs
593
+ ) -> None:
594
+ """Called when a sandbox activity is performed."""
595
+ self._sandbox_activity.increment(
596
+ app=self.app,
597
+ environment_id=str(environment.id),
598
+ activity=name,
599
+ error=self._error_tag(error)
600
+ )
601
+ self._sandbox_activity_duration_ms.record(
602
+ int(duration * 1000),
603
+ app=self.app,
604
+ environment_id=str(environment.id),
605
+ activity=name,
606
+ error=self._error_tag(error)
607
+ )