-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add observeAll() to make it possible to instrument all function calls #158
Open
LionsAd
wants to merge
15
commits into
open-telemetry:main
Choose a base branch
from
LionsAd:untracked--wildcard-observer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add observeAll() to make it possible to instrument all function calls #158
LionsAd
wants to merge
15
commits into
open-telemetry:main
from
LionsAd:untracked--wildcard-observer
+338
−17
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry for the mess with #157. Here is a xhprof compatible profiler using this (unoptimized obviously): <?php
class XhprofCompatProfiler {
private static array $startTimes = [];
private static array $data = [];
private static array $callStack = ['main()'];
private static float $profileStartTime;
public static function start(): void {
self::$profileStartTime = microtime(true);
// Initialize main() entry
self::$data['main()'] = [
'ct' => 1,
'wt' => 0,
];
$preHook = function($object, $args, $scope, $function) {
$startTime = microtime(true);
$functionName = self::getFunctionName($object, $scope, $function);
$parent = end(self::$callStack);
$key = "$parent==>$functionName";
// Push to call stack
self::$callStack[] = $functionName;
// Store start time and memory
self::$startTimes[$key] = $startTime;
// Initialize data structure if not exists
if (!isset(self::$data[$key])) {
self::$data[$key] = [
'ct' => 0, // Call count
'wt' => 0, // Wall time
];
}
// Increment call count
self::$data[$key]['ct']++;
};
$postHook = function($class, array $params, $returnValue, ?Throwable $exception) {
// The current function we're exiting
$functionName = array_pop(self::$callStack);
// The parent function that called it
$parent = end(self::$callStack); // Last element without removing
$endTime = microtime(true);
$key = "$parent==>$functionName";
// Update metrics
if (isset(self::$startTimes[$key])) {
$startTime = self::$startTimes[$key];
// Wall time in microseconds
$wallTime = ($endTime - $startTime) * 1000;
// Update data
self::$data[$key]['wt'] += (int)$wallTime;
// Cleanup
unset(self::$startTimes[$key]);
}
};
\OpenTelemetry\Instrumentation\observeAll($preHook, $postHook);
}
public static function stop(): array {
\OpenTelemetry\Instrumentation\observeAll();
$endTime = microtime(true);
// Update main() wall time
self::$data['main()']['wt'] = (int)(($endTime - self::$profileStartTime) * 1000);
return self::$data;
}
private static function getFunctionName($object, ?string $scope, string $function): string {
if ($scope) {
return "$scope::$function";
}
if ($object) {
return get_class($object) . "::$function";
}
return $function;
}
}
// Usage example:
// Start profiling
XhprofCompatProfiler::start();
// Your code here
function test() {
static $rec = 0;
if ($rec == 100) {
return;
}
$rec++;
test();
$rec--;
if ($rec == 0) {
sleep(1);
}
return str_repeat('a', 1000000);
}
test();
// Stop profiling and get results
$data = XhprofCompatProfiler::stop();
// Print results
print_r($data); |
LionsAd
force-pushed
the
untracked--wildcard-observer
branch
from
January 17, 2025 04:00
b8c6a58
to
ebcd04b
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds an observeAll() function, which works in parallel to registered hooks.
I wanted to test what the overhead of a PHP based xhprof-compatible profiler would be in comparison to xhprof and xhprof without any processing (just the zend_execute_ex overhead).
Note: Claude 3.5 sonnet helped implement this.
--
Some notes