langfun 0.1.2.dev202510310805__py3-none-any.whl → 0.1.2.dev202511020804__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.

@@ -14,14 +14,14 @@
14
14
  """Environment event logger."""
15
15
 
16
16
  from typing import Annotated
17
- from langfun.env.event_handlers import base
17
+ from langfun.env import interface
18
18
  import pyglove as pg
19
19
 
20
20
 
21
21
  _METRIC_NAMESPACE = '/langfun/env'
22
22
 
23
23
 
24
- class MetricWriter(pg.Object, base.EventHandler):
24
+ class MetricWriter(pg.Object, interface.EventHandler):
25
25
  """Event handler for streamz metrics."""
26
26
 
27
27
  app: Annotated[
@@ -209,6 +209,41 @@ class MetricWriter(pg.Object, base.EventHandler):
209
209
  }
210
210
  )
211
211
 
212
+ #
213
+ # Sandbox session metrics.
214
+ #
215
+
216
+ self._sandbox_session_start_duration_ms = self._get_distribution(
217
+ 'sandbox_session_start_duration_ms',
218
+ description='Sandbox session start duration in milliseconds',
219
+ parameters={
220
+ 'app': str,
221
+ 'environment_id': str,
222
+ 'image_id': str,
223
+ 'error': str,
224
+ }
225
+ )
226
+ self._sandbox_session_end_duration_ms = self._get_distribution(
227
+ 'sandbox_session_end_duration_ms',
228
+ description='Sandbox session end duration in milliseconds',
229
+ parameters={
230
+ 'app': str,
231
+ 'environment_id': str,
232
+ 'image_id': str,
233
+ 'error': str,
234
+ }
235
+ )
236
+ self._sandbox_session_lifetime_ms = self._get_distribution(
237
+ 'sandbox_session_lifetime_ms',
238
+ description='Sandbox session lifetime in milliseconds',
239
+ parameters={
240
+ 'app': str,
241
+ 'environment_id': str,
242
+ 'image_id': str,
243
+ 'error': str,
244
+ }
245
+ )
246
+
212
247
  #
213
248
  # Feature metrics.
214
249
  #
@@ -258,6 +293,17 @@ class MetricWriter(pg.Object, base.EventHandler):
258
293
  'error': str,
259
294
  }
260
295
  )
296
+ self._feature_activity = self._get_counter(
297
+ 'feature_activity',
298
+ description='Feature activity counter',
299
+ parameters={
300
+ 'app': str,
301
+ 'environment_id': str,
302
+ 'image_id': str,
303
+ 'activity': str,
304
+ 'error': str,
305
+ }
306
+ )
261
307
  self._feature_housekeep = self._get_counter(
262
308
  'feature_housekeep',
263
309
  description='Feature housekeeping counter',
@@ -315,63 +361,39 @@ class MetricWriter(pg.Object, base.EventHandler):
315
361
  'error': str,
316
362
  }
317
363
  )
318
- self._feature_housekeep_duration_ms = self._get_distribution(
319
- 'feature_housekeep_duration_ms',
320
- description='Feature housekeeping duration in milliseconds',
321
- parameters={
322
- 'app': str,
323
- 'environment_id': str,
324
- 'image_id': str,
325
- 'feature_name': str,
326
- 'error': str,
327
- }
328
- )
329
-
330
- #
331
- # Session metrics.
332
- #
333
-
334
- self._session_start_duration_ms = self._get_distribution(
335
- 'session_start_duration_ms',
336
- description='Session start duration in milliseconds',
337
- parameters={
338
- 'app': str,
339
- 'environment_id': str,
340
- 'image_id': str,
341
- 'error': str,
342
- }
343
- )
344
- self._session_end_duration_ms = self._get_distribution(
345
- 'session_end_duration_ms',
346
- description='Session end duration in milliseconds',
364
+ self._feature_activity_duration_ms = self._get_distribution(
365
+ 'feature_activity_duration_ms',
366
+ description='Feature activity duration in milliseconds',
347
367
  parameters={
348
368
  'app': str,
349
369
  'environment_id': str,
350
370
  'image_id': str,
371
+ 'activity': str,
351
372
  'error': str,
352
373
  }
353
374
  )
354
- self._session_lifetime_ms = self._get_distribution(
355
- 'session_lifetime_ms',
356
- description='Session lifetime in milliseconds',
375
+ self._feature_housekeep_duration_ms = self._get_distribution(
376
+ 'feature_housekeep_duration_ms',
377
+ description='Feature housekeeping duration in milliseconds',
357
378
  parameters={
358
379
  'app': str,
359
380
  'environment_id': str,
360
381
  'image_id': str,
382
+ 'feature_name': str,
361
383
  'error': str,
362
384
  }
363
385
  )
364
386
 
365
387
  def on_environment_starting(
366
388
  self,
367
- environment: base.Environment
389
+ environment: interface.Environment
368
390
  ) -> None:
369
391
  """Called when the environment is starting."""
370
392
  self._initialize_metrics()
371
393
 
372
394
  def on_environment_housekeep(
373
395
  self,
374
- environment: base.Environment,
396
+ environment: interface.Environment,
375
397
  counter: int,
376
398
  duration: float,
377
399
  error: BaseException | None,
@@ -387,89 +409,151 @@ class MetricWriter(pg.Object, base.EventHandler):
387
409
 
388
410
  def on_sandbox_start(
389
411
  self,
390
- environment: base.Environment,
391
- sandbox: base.Sandbox,
412
+ sandbox: interface.Sandbox,
392
413
  duration: float,
393
414
  error: BaseException | None
394
415
  ) -> None:
395
416
  self._sandbox_start.increment(
396
417
  app=self.app,
397
- environment_id=str(environment.id),
418
+ environment_id=str(sandbox.environment.id),
398
419
  image_id=sandbox.image_id,
399
420
  error=self._error_tag(error)
400
421
  )
401
422
  self._sandbox_start_duration_ms.record(
402
423
  int(duration * 1000),
403
424
  app=self.app,
404
- environment_id=str(environment.id),
425
+ environment_id=str(sandbox.environment.id),
405
426
  image_id=sandbox.image_id,
406
427
  error=self._error_tag(error)
407
428
  )
408
429
 
409
430
  def on_sandbox_status_change(
410
431
  self,
411
- environment: base.Environment,
412
- sandbox: base.Sandbox,
413
- old_status: base.Sandbox.Status,
414
- new_status: base.Sandbox.Status,
432
+ sandbox: interface.Sandbox,
433
+ old_status: interface.Sandbox.Status,
434
+ new_status: interface.Sandbox.Status,
415
435
  span: float
416
436
  ) -> None:
417
437
  self._sandbox_status_duration_ms.record(
418
438
  int(span * 1000),
419
439
  app=self.app,
420
- environment_id=str(environment.id),
440
+ environment_id=str(sandbox.environment.id),
421
441
  image_id=sandbox.image_id,
422
442
  status=old_status.value
423
443
  )
424
- if old_status != base.Sandbox.Status.CREATED:
444
+ if old_status != interface.Sandbox.Status.CREATED:
425
445
  self._sandbox_count.increment(
426
446
  delta=-1,
427
447
  app=self.app,
428
- environment_id=str(environment.id),
448
+ environment_id=str(sandbox.environment.id),
429
449
  image_id=sandbox.image_id,
430
450
  status=old_status.value
431
451
  )
432
- if new_status != base.Sandbox.Status.OFFLINE:
452
+ if new_status != interface.Sandbox.Status.OFFLINE:
433
453
  self._sandbox_count.increment(
434
454
  app=self.app,
435
- environment_id=str(environment.id),
455
+ environment_id=str(sandbox.environment.id),
436
456
  image_id=sandbox.image_id,
437
457
  status=new_status.value
438
458
  )
439
459
 
440
460
  def on_sandbox_shutdown(
441
461
  self,
442
- environment: base.Environment,
443
- sandbox: base.Sandbox,
462
+ sandbox: interface.Sandbox,
444
463
  duration: float,
445
464
  lifetime: float,
446
465
  error: BaseException | None
447
466
  ) -> None:
448
467
  self._sandbox_shutdown.increment(
449
468
  app=self.app,
450
- environment_id=str(environment.id),
469
+ environment_id=str(sandbox.environment.id),
451
470
  image_id=sandbox.image_id,
452
471
  error=self._error_tag(error)
453
472
  )
454
473
  self._sandbox_shutdown_duration_ms.record(
455
474
  int(duration * 1000),
456
475
  app=self.app,
457
- environment_id=str(environment.id),
476
+ environment_id=str(sandbox.environment.id),
458
477
  image_id=sandbox.image_id,
459
478
  error=self._error_tag(error)
460
479
  )
461
480
  self._sandbox_lifetime_ms.record(
462
481
  int(lifetime * 1000),
463
482
  app=self.app,
464
- environment_id=str(environment.id),
483
+ environment_id=str(sandbox.environment.id),
484
+ image_id=sandbox.image_id,
485
+ error=self._error_tag(error)
486
+ )
487
+
488
+ def on_sandbox_session_start(
489
+ self,
490
+ sandbox: interface.Sandbox,
491
+ session_id: str,
492
+ duration: float,
493
+ error: BaseException | None
494
+ ) -> None:
495
+ """Called when a sandbox session starts."""
496
+ self._sandbox_session_start_duration_ms.record(
497
+ int(duration * 1000),
498
+ app=self.app,
499
+ environment_id=str(sandbox.environment.id),
500
+ image_id=sandbox.image_id,
501
+ error=self._error_tag(error)
502
+ )
503
+
504
+ def on_sandbox_session_end(
505
+ self,
506
+ sandbox: interface.Sandbox,
507
+ session_id: str,
508
+ duration: float,
509
+ lifetime: float,
510
+ error: BaseException | None
511
+ ) -> None:
512
+ """Called when a sandbox session ends."""
513
+ self._sandbox_session_end_duration_ms.record(
514
+ int(duration * 1000),
515
+ app=self.app,
516
+ environment_id=str(sandbox.environment.id),
517
+ image_id=sandbox.image_id,
518
+ error=self._error_tag(error)
519
+ )
520
+ self._sandbox_session_lifetime_ms.record(
521
+ int(lifetime * 1000),
522
+ app=self.app,
523
+ environment_id=str(sandbox.environment.id),
524
+ image_id=sandbox.image_id,
525
+ error=self._error_tag(error)
526
+ )
527
+
528
+ def on_sandbox_activity(
529
+ self,
530
+ name: str,
531
+ sandbox: interface.Sandbox,
532
+ session_id: str | None,
533
+ duration: float,
534
+ error: BaseException | None,
535
+ **kwargs
536
+ ) -> None:
537
+ """Called when a sandbox activity is performed."""
538
+ self._sandbox_activity.increment(
539
+ app=self.app,
540
+ environment_id=str(sandbox.environment.id),
541
+ image_id=sandbox.image_id,
542
+ activity=name,
543
+ error=self._error_tag(error)
544
+ )
545
+ self._sandbox_activity_duration_ms.record(
546
+ int(duration * 1000),
547
+ app=self.app,
548
+ environment_id=str(sandbox.environment.id),
465
549
  image_id=sandbox.image_id,
550
+ activity=name,
466
551
  error=self._error_tag(error)
467
552
  )
468
553
 
469
554
  def on_sandbox_housekeep(
470
555
  self,
471
- environment: base.Environment,
472
- sandbox: base.Sandbox,
556
+ sandbox: interface.Sandbox,
473
557
  counter: int,
474
558
  duration: float,
475
559
  error: BaseException | None,
@@ -478,213 +562,165 @@ class MetricWriter(pg.Object, base.EventHandler):
478
562
  """Called when a sandbox feature is housekeeping."""
479
563
  self._sandbox_housekeep.increment(
480
564
  app=self.app,
481
- environment_id=str(environment.id),
565
+ environment_id=str(sandbox.environment.id),
482
566
  image_id=sandbox.image_id,
483
567
  error=self._error_tag(error)
484
568
  )
485
569
  self._sandbox_housekeep_duration_ms.record(
486
570
  int(duration * 1000),
487
571
  app=self.app,
488
- environment_id=str(environment.id),
572
+ environment_id=str(sandbox.environment.id),
489
573
  image_id=sandbox.image_id,
490
574
  error=self._error_tag(error)
491
575
  )
492
576
 
493
577
  def on_feature_setup(
494
578
  self,
495
- environment: base.Environment,
496
- sandbox: base.Sandbox,
497
- feature: base.Feature,
579
+ feature: interface.Feature,
498
580
  duration: float,
499
581
  error: BaseException | None
500
582
  ) -> None:
501
583
  """Called when a sandbox feature is setup."""
584
+ image_id = feature.sandbox.image_id if feature.sandbox else ''
502
585
  self._feature_setup.increment(
503
586
  app=self.app,
504
- environment_id=str(environment.id),
505
- image_id=sandbox.image_id,
587
+ environment_id=str(feature.environment.id),
588
+ image_id=image_id,
506
589
  feature_name=feature.name,
507
590
  error=self._error_tag(error)
508
591
  )
509
592
  self._feature_setup_duration_ms.record(
510
593
  int(duration * 1000),
511
594
  app=self.app,
512
- environment_id=str(environment.id),
513
- image_id=sandbox.image_id,
595
+ environment_id=str(feature.environment.id),
596
+ image_id=image_id,
514
597
  feature_name=feature.name,
515
598
  error=self._error_tag(error)
516
599
  )
517
600
 
518
601
  def on_feature_teardown(
519
602
  self,
520
- environment: base.Environment,
521
- sandbox: base.Sandbox,
522
- feature: base.Feature,
603
+ feature: interface.Feature,
523
604
  duration: float,
524
605
  error: BaseException | None
525
606
  ) -> None:
526
607
  """Called when a sandbox feature is teardown."""
608
+ image_id = feature.sandbox.image_id if feature.sandbox else ''
527
609
  self._feature_teardown.increment(
528
610
  app=self.app,
529
- environment_id=str(environment.id),
530
- image_id=sandbox.image_id,
611
+ environment_id=str(feature.environment.id),
612
+ image_id=image_id,
531
613
  feature_name=feature.name,
532
614
  error=self._error_tag(error)
533
615
  )
534
616
  self._feature_teardown_duration_ms.record(
535
617
  int(duration * 1000),
536
618
  app=self.app,
537
- environment_id=str(environment.id),
538
- image_id=sandbox.image_id,
619
+ environment_id=str(feature.environment.id),
620
+ image_id=image_id,
539
621
  feature_name=feature.name,
540
622
  error=self._error_tag(error)
541
623
  )
542
624
 
543
625
  def on_feature_setup_session(
544
626
  self,
545
- environment: base.Environment,
546
- sandbox: base.Sandbox,
547
- feature: base.Feature,
627
+ feature: interface.Feature,
548
628
  session_id: str | None,
549
629
  duration: float,
550
630
  error: BaseException | None
551
631
  ) -> None:
552
632
  """Called when a sandbox feature is setup."""
633
+ image_id = feature.sandbox.image_id if feature.sandbox else ''
553
634
  self._feature_setup_session.increment(
554
635
  app=self.app,
555
- environment_id=str(environment.id),
556
- image_id=sandbox.image_id,
636
+ environment_id=str(feature.environment.id),
637
+ image_id=image_id,
557
638
  feature_name=feature.name,
558
639
  error=self._error_tag(error)
559
640
  )
560
641
  self._feature_setup_session_duration_ms.record(
561
642
  int(duration * 1000),
562
643
  app=self.app,
563
- environment_id=str(environment.id),
564
- image_id=sandbox.image_id,
644
+ environment_id=str(feature.environment.id),
645
+ image_id=image_id,
565
646
  feature_name=feature.name,
566
647
  error=self._error_tag(error)
567
648
  )
568
649
 
569
650
  def on_feature_teardown_session(
570
651
  self,
571
- environment: base.Environment,
572
- sandbox: base.Sandbox,
573
- feature: base.Feature,
652
+ feature: interface.Feature,
574
653
  session_id: str,
575
654
  duration: float,
576
655
  error: BaseException | None
577
656
  ) -> None:
578
657
  """Called when a sandbox feature is teardown."""
658
+ image_id = feature.sandbox.image_id if feature.sandbox else ''
579
659
  self._feature_teardown_session.increment(
580
660
  app=self.app,
581
- environment_id=str(environment.id),
582
- image_id=sandbox.image_id,
661
+ environment_id=str(feature.environment.id),
662
+ image_id=image_id,
583
663
  feature_name=feature.name,
584
664
  error=self._error_tag(error)
585
665
  )
586
666
  self._feature_teardown_session_duration_ms.record(
587
667
  int(duration * 1000),
588
668
  app=self.app,
589
- environment_id=str(environment.id),
590
- image_id=sandbox.image_id,
669
+ environment_id=str(feature.environment.id),
670
+ image_id=image_id,
591
671
  feature_name=feature.name,
592
672
  error=self._error_tag(error)
593
673
  )
594
674
 
595
- def on_feature_housekeep(
675
+ def on_feature_activity(
596
676
  self,
597
- environment: base.Environment,
598
- sandbox: base.Sandbox,
599
- feature: base.Feature,
600
- counter: int,
677
+ name: str,
678
+ feature: interface.Feature,
679
+ session_id: str | None,
601
680
  duration: float,
602
681
  error: BaseException | None,
603
682
  **kwargs
604
683
  ) -> None:
605
- """Called when a sandbox feature is housekeeping."""
606
- self._feature_housekeep.increment(
607
- app=self.app,
608
- environment_id=str(environment.id),
609
- image_id=sandbox.image_id,
610
- feature_name=feature.name,
611
- error=self._error_tag(error)
612
- )
613
- self._feature_housekeep_duration_ms.record(
614
- int(duration * 1000),
615
- app=self.app,
616
- environment_id=str(environment.id),
617
- image_id=sandbox.image_id,
618
- feature_name=feature.name,
619
- error=self._error_tag(error)
620
- )
621
-
622
- def on_session_start(
623
- self,
624
- environment: base.Environment,
625
- sandbox: base.Sandbox,
626
- session_id: str,
627
- duration: float,
628
- error: BaseException | None
629
- ) -> None:
630
- """Called when a sandbox session starts."""
631
- self._session_start_duration_ms.record(
632
- int(duration * 1000),
684
+ """Called when a feature activity is performed."""
685
+ image_id = feature.sandbox.image_id if feature.sandbox else ''
686
+ self._feature_activity.increment(
633
687
  app=self.app,
634
- environment_id=str(environment.id),
635
- image_id=sandbox.image_id,
688
+ environment_id=str(feature.environment.id),
689
+ image_id=image_id,
690
+ activity=name,
636
691
  error=self._error_tag(error)
637
692
  )
638
-
639
- def on_session_end(
640
- self,
641
- environment: base.Environment,
642
- sandbox: base.Sandbox,
643
- session_id: str,
644
- duration: float,
645
- lifetime: float,
646
- error: BaseException | None
647
- ) -> None:
648
- """Called when a sandbox session ends."""
649
- self._session_end_duration_ms.record(
693
+ self._feature_activity_duration_ms.record(
650
694
  int(duration * 1000),
651
695
  app=self.app,
652
- environment_id=str(environment.id),
653
- image_id=sandbox.image_id,
654
- error=self._error_tag(error)
655
- )
656
- self._session_lifetime_ms.record(
657
- int(lifetime * 1000),
658
- app=self.app,
659
- environment_id=str(environment.id),
660
- image_id=sandbox.image_id,
696
+ environment_id=str(feature.environment.id),
697
+ image_id=image_id,
698
+ activity=name,
661
699
  error=self._error_tag(error)
662
700
  )
663
701
 
664
- def on_sandbox_activity(
702
+ def on_feature_housekeep(
665
703
  self,
666
- name: str,
667
- environment: base.Environment,
668
- sandbox: base.Sandbox,
669
- feature: base.Feature | None,
670
- session_id: str | None,
704
+ feature: interface.Feature,
705
+ counter: int,
671
706
  duration: float,
672
707
  error: BaseException | None,
673
708
  **kwargs
674
709
  ) -> None:
675
- """Called when a sandbox activity is performed."""
676
- self._sandbox_activity.increment(
710
+ """Called when a sandbox feature is housekeeping."""
711
+ image_id = feature.sandbox.image_id if feature.sandbox else ''
712
+ self._feature_housekeep.increment(
677
713
  app=self.app,
678
- environment_id=str(environment.id),
679
- image_id=sandbox.image_id,
680
- activity=name,
714
+ environment_id=str(feature.environment.id),
715
+ image_id=image_id,
716
+ feature_name=feature.name,
681
717
  error=self._error_tag(error)
682
718
  )
683
- self._sandbox_activity_duration_ms.record(
719
+ self._feature_housekeep_duration_ms.record(
684
720
  int(duration * 1000),
685
721
  app=self.app,
686
- environment_id=str(environment.id),
687
- image_id=sandbox.image_id,
688
- activity=name,
722
+ environment_id=str(feature.environment.id),
723
+ image_id=image_id,
724
+ feature_name=feature.name,
689
725
  error=self._error_tag(error)
690
726
  )
@@ -33,7 +33,7 @@ class MetricWriterTest(unittest.TestCase):
33
33
  outage_retry_interval=0,
34
34
  housekeep_interval=10.0,
35
35
  sandbox_keepalive_interval=1.0,
36
- event_handlers=[writer],
36
+ event_handler=writer,
37
37
  )
38
38
  with env:
39
39
  with env.sandbox(session_id='session1') as sb: