Skip to content

Commit

Permalink
Log install per OS stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Nov 6, 2024
1 parent 9332684 commit de42bd0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ public function trackDownloadsAction(Request $request, StatsDClient $statsd, str
$statsd->increment('installs.php_patch', 1, 1, [
'php_patch' => $uaParser->getPhpVersion() ?: 'unknown',
]);
$statsd->increment('installs.os', 1, 1, [
'os' => $uaParser->getOs() ?: 'unknown',
'php_minor' => $uaParser->getPhpMinorVersion(),
'ci' => $uaParser->getCI() ? 'true' : 'false',
]);
} elseif (
// log only if user-agent header is well-formed (it sometimes contains the header name itself in the value)
!str_starts_with($request->headers->get('User-Agent'), 'User-Agent:')
Expand Down
7 changes: 6 additions & 1 deletion src/Util/UserAgentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class UserAgentParser

public function __construct(?string $userAgent)
{
if ($userAgent && Preg::isMatch('#^Composer/(?P<composer>[a-z0-9.+-]+) \((?P<os>[^\s;]+)[^;]*?; [^;]*?; (?P<engine>HHVM|PHP) (?P<php>[0-9.]+)[^;]*(?:; (?P<http>streams|curl \d+\.\d+)[^;)]*)?(?:; Platform-PHP (?P<platform_php>[0-9.]+)[^;]*)?(?P<ci>; CI)?#i', $userAgent, $matches)) {
if ($userAgent && Preg::isMatch('#^Composer/(?P<composer>[a-z0-9.+-]+) \((?P<os>[^\s;]+)[^;]*?; (?P<osversion>[^;]*?); (?P<engine>HHVM|PHP) (?P<php>[0-9.]+)[^;]*(?:; (?P<http>streams|curl \d+\.\d+)[^;)]*)?(?:; Platform-PHP (?P<platform_php>[0-9.]+)[^;]*)?(?P<ci>; CI)?#i', $userAgent, $matches)) {
assert(isset($matches['composer'], $matches['engine'], $matches['os'], $matches['php']));
if ($matches['composer'] === 'source' || Preg::isMatch('{^[a-f0-9]{40}$}', $matches['composer'])) {
$matches['composer'] = 'pre-1.8.5';
Expand All @@ -34,6 +34,11 @@ public function __construct(?string $userAgent)
$this->phpVersion = (strtolower($matches['engine']) === 'hhvm' ? 'hhvm-' : '') . $matches['php'];
$this->platformPhpVersion = null !== $matches['platform_php'] ? (strtolower($matches['engine']) === 'hhvm' ? 'hhvm-' : '') . $matches['platform_php'] : null;
$this->os = Preg::replace('{^cygwin_nt-.*}', 'cygwin', strtolower($matches['os']));
if (str_contains('microsoft', strtolower((string) $matches['osversion']))) { // likely WSL 1 e.g. version-Microsoft
$this->os = 'wsl';
} elseif (str_contains('WSL', (string) $matches['osversion'])) { // likely WSL2 e.g. version-microsoft-standard-WSL2
$this->os = 'wsl';
}
$this->httpVersion = null !== $matches['http'] ? strtolower($matches['http']) : null;
$this->ci = (bool) ($matches['ci'] ?? null);
}
Expand Down

0 comments on commit de42bd0

Please sign in to comment.