Skip to content

Commit 48295c3

Browse files
committed
Stop using Timer class due to memory leaks. Log and SpanMeta now use Timekeeper directly
1 parent 6de6f78 commit 48295c3

File tree

3 files changed

+30
-60
lines changed

3 files changed

+30
-60
lines changed

src/Log.php

+13-14
Original file line numberDiff line numberDiff line change
@@ -468,20 +468,23 @@ public static function dt(TimeKeeper $oTimer, array $aContext = []): void {
468468
}
469469

470470
/**
471-
* @param string $sLabel
472-
* @return TimeKeeper
471+
* @param TimeKeeper $oTimer
472+
* @param array $aContext
473473
*/
474-
public static function startTimer(string $sLabel): TimeKeeper {
475-
return self::$aSpanMetas[self::getCurrentRequestHash()]->Timer->start($sLabel);
474+
public static function et(TimeKeeper $oTimer, array $aContext = []): void {
475+
$aContext['--ms'] = $oTimer->stop();
476+
477+
self::e($oTimer->label(), $aContext);
476478
}
477479

478480
/**
479481
* @param string $sLabel
480-
*
481-
* @return float|null
482+
* @return TimeKeeper
482483
*/
483-
public static function stopTimer(string $sLabel): ?float {
484-
return self::$aSpanMetas[self::getCurrentRequestHash()]->Timer->stop($sLabel);
484+
public static function startTimer(string $sLabel): TimeKeeper {
485+
$oTimer = new TimeKeeper($sLabel);
486+
$oTimer->start();
487+
return $oTimer;
485488
}
486489

487490
/**
@@ -514,7 +517,6 @@ public static function startChildRequest(?string $sThreadHash = null, ?string $s
514517
* Retrieves the previous request hash
515518
*/
516519
public static function endChildRequest(): void {
517-
self::stopTimer('_REQUEST');
518520
self::summary();
519521
array_pop(self::$aSpans);
520522
}
@@ -683,7 +685,7 @@ public static function initSpan(ServerRequestInterface $oRequest, array $aUser =
683685
'--r' => $sRequestHash
684686
];
685687

686-
self::$aSpanMetas[$sRequestHash] = new SpanMeta($oStartTime, self::$bMetrics ? SpanMeta::METRICS_ON : SpanMeta::METRICS_OFF);
688+
self::$aSpanMetas[$sRequestHash] = new SpanMeta($oStartTime);
687689

688690
if ($sThreadHash = $sIncomingThreadHash ?? self::getThreadHash()) {
689691
$aSpan['--t'] = $sThreadHash;
@@ -710,8 +712,6 @@ public static function initSpan(ServerRequestInterface $oRequest, array $aUser =
710712
}
711713

712714
self::$aSpans[] = $aSpan;
713-
714-
self::startTimer('_REQUEST');
715715
}
716716

717717
/**
@@ -720,12 +720,11 @@ public static function initSpan(ServerRequestInterface $oRequest, array $aUser =
720720
*/
721721
public static function summary(string $sOverrideName = 'Summary'): void {
722722
self::incrementCurrentIndex();
723-
$iTimer = self::stopTimer('_REQUEST');
724723
$aMessage = array_merge(
725724
self::prepareContext(
726725
self::$sService . '.' . $sOverrideName,
727726
[
728-
'--ms' => $iTimer,
727+
'--ms' => self::$aSpanMetas[self::getCurrentRequestHash()]->Timer->stop(),
729728
'--summary' => true,
730729
'--span' => self::$aSpanMetas[self::getCurrentRequestHash()]->getMessage(self::$sService),
731730
],

src/SpanMeta.php

+8-15
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,32 @@
11
<?php
2-
3-
42
namespace Enobrev;
53

64
use DateTime;
75
use Adbar\Dot;
86

97
class SpanMeta {
10-
public const METRICS_ON = 'metrics-on';
11-
public const METRICS_OFF = 'metrics-off';
12-
138
private const TIMESTAMP_FORMAT = DATE_RFC3339_EXTENDED;
149

15-
// const VERSION = 1: included tags, which were not used
16-
private const VERSION = 2;
10+
// private const VERSION = 1: included tags, which were not used
11+
// private const VERSION = 2; included metrics and a memory-leaking timer
12+
private const VERSION = 3;
1713

1814
private string $sName = '';
1915

20-
private bool $bMetrics;
21-
2216
private DateTime $oStart;
2317

2418
private bool $bError;
2519

2620
public Dot $Context;
2721

28-
public Timer $Timer;
22+
public TimeKeeper $Timer;
2923

30-
public function __construct(DateTime $oStart, $sMetrics = self::METRICS_OFF) {
31-
$this->bMetrics = $sMetrics === self::METRICS_ON;
24+
public function __construct(DateTime $oStart) {
3225
$this->oStart = $oStart;
3326
$this->bError = false;
3427
$this->Context = new Dot();
35-
$this->Timer = new Timer();
28+
$this->Timer = new TimeKeeper('_REQUEST');
29+
$this->Timer->start();
3630
}
3731

3832
public function getName():string {
@@ -60,8 +54,7 @@ public function getMessage(string $sService): array {
6054
'start_timestamp' => $this->oStart->format(self::TIMESTAMP_FORMAT),
6155
'end_timestamp' => notNowButRightNow()->format(self::TIMESTAMP_FORMAT),
6256
'error' => $this->bError,
63-
'context' => $this->Context->all(),
64-
'metrics' => $this->bMetrics ? json_encode($this->Timer->stats()) : null
57+
'context' => $this->Context->all()
6558
];
6659
}
6760
}

src/Timer.php

+9-31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22
namespace Enobrev;
33

4+
/**
5+
* @deprecated for causing memory leaks
6+
*/
47
class Timer {
58
/**
69
* @var TimeKeeper[][]
@@ -49,51 +52,26 @@ public function stats($bReturnTimers = false): array {
4952
return $aReturn;
5053
}
5154

52-
/**
53-
* @param string $sLabel
54-
* @return TimeKeeper|null
55-
*/
56-
public function &get(string $sLabel): ?TimeKeeper {
57-
if (!isset($this->aTimers[$sLabel])) {
58-
return $this->start($sLabel);
59-
}
60-
61-
$oTimer = null;
62-
$iTimers = count($this->aTimers[$sLabel]);
63-
if ($iTimers > 0) {
64-
$oTimer = &$this->aTimers[$sLabel][$iTimers - 1];
65-
}
66-
67-
return $oTimer;
68-
}
69-
7055
/**
7156
* @param string $sLabel
7257
* @return TimeKeeper
7358
*/
7459
public function &start(string $sLabel): TimeKeeper {
75-
if (!isset($this->aTimers[$sLabel])) {
76-
$this->aTimers[$sLabel] = [];
77-
}
60+
$this->aTimers[$sLabel] = new TimeKeeper($sLabel);
61+
$this->aTimers[$sLabel]->start();
7862

79-
$oTimer = new TimeKeeper($sLabel);
80-
$oTimer->start();
81-
82-
$this->aTimers[$sLabel][] = &$oTimer;
83-
84-
return $oTimer;
63+
return $this->aTimers[$sLabel];
8564
}
8665

8766
/**
8867
* @param string $sLabel
8968
* @return float|null
9069
*/
9170
public function stop(string $sLabel): ?float {
92-
$oTimeKeeper = &$this->get($sLabel);
93-
if ($oTimeKeeper) {
94-
return $oTimeKeeper->stop();
71+
if (!isset($this->aTimers[$sLabel])) {
72+
return null;
9573
}
9674

97-
return null;
75+
return $this->aTimers[$sLabel]->stop();
9876
}
9977
}

0 commit comments

Comments
 (0)