Files
firefly-iii/app/Support/Telemetry.php

90 lines
2.9 KiB
PHP
Raw Normal View History

2020-03-13 08:02:38 +01:00
<?php
2020-06-30 19:05:35 +02:00
2020-03-13 08:02:38 +01:00
/**
* Telemetry.php
2020-06-30 19:05:35 +02:00
* Copyright (c) 2020 james@firefly-iii.org
2020-03-13 08:02:38 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-06-30 19:05:35 +02:00
declare(strict_types=1);
2020-03-13 08:02:38 +01:00
namespace FireflyIII\Support;
use Carbon\Carbon;
2020-06-06 06:57:44 +02:00
use FireflyIII\Support\System\GeneratesInstallationId;
2021-05-29 11:30:13 +02:00
use Sentry\Severity;
use Sentry\State\Scope;
use function Sentry\captureMessage;
use function Sentry\configureScope;
2020-03-13 08:02:38 +01:00
/**
* Class Telemetry
*/
class Telemetry
{
2020-06-06 06:57:44 +02:00
use GeneratesInstallationId;
2021-05-29 11:30:13 +02:00
2020-03-13 08:02:38 +01:00
/**
2020-03-21 15:42:37 +01:00
* Feature telemetry stores a $value for the given $feature.
* Will only store the given $feature / $value combination once.
*
2020-03-13 08:02:38 +01:00
*
* Examples:
2020-03-21 15:42:37 +01:00
* - execute-cli-command [value]
2020-03-13 08:02:38 +01:00
* - use-help-pages
* - has-created-bill
* - first-time-install
* - more
*
* Its use should be limited to exotic and strange use cases in Firefly III.
* Because time and date are logged as well, useful to track users' evolution in Firefly III.
*
* Any meta-data stored is strictly non-financial.
*
2020-03-21 15:42:37 +01:00
* @param string $key
* @param string $value
2020-03-13 08:02:38 +01:00
*/
2020-03-21 15:42:37 +01:00
public function feature(string $key, string $value): void
2020-03-13 08:02:38 +01:00
{
if (false === config('firefly.send_telemetry') || false === config('firefly.feature_flags.telemetry')) {
2020-03-13 08:02:38 +01:00
// hard stop if not allowed to do telemetry.
// do nothing!
return;
}
2021-05-29 11:30:13 +02:00
// send to Sentry.
$this->generateInstallationId();
$installationId = app('fireflyconfig')->get('installation_id');
2020-03-13 08:02:38 +01:00
2021-05-29 11:30:13 +02:00
// add some context:
configureScope(
function (Scope $scope) use ($installationId, $key, $value): void {
$scope->setContext(
'telemetry', [
'installation_id' => $installationId->data,
'version' => config('firefly.version'),
'collected_at' => Carbon::now()->format('r'),
'key' => $key,
'value' => $value,
]
);
}
);
captureMessage(sprintf('FIT: %s/%s', $key, $value), Severity::info());
}
}