langfun 0.1.2.dev202509300805__py3-none-any.whl → 0.1.2.dev202510020804__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.

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