Skip to content

Commit

Permalink
[player] .blackfire.ini configuration support
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-jean committed Feb 16, 2024
1 parent b2b2adf commit 5e7ebb6
Showing 1 changed file with 49 additions and 9 deletions.
58 changes: 49 additions & 9 deletions Player/Console/PlayerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ protected function configure(): void
->setName('run')
->setDefinition([
new InputArgument('file', InputArgument::OPTIONAL, 'The file defining the scenarios'),
new InputOption('config', '', InputOption::VALUE_OPTIONAL, 'The configuration file to retrieve configuration from', null),
new InputOption('concurrency', 'c', InputOption::VALUE_REQUIRED, 'The number of clients to create', 1),
new InputOption('endpoint', '', InputOption::VALUE_REQUIRED, 'Override the scenario endpoint', null),
new InputOption('json', '', InputOption::VALUE_NONE, 'Outputs execution report as JSON', null),
Expand Down Expand Up @@ -136,10 +137,43 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->ensureCommandIsRunInDockerContainer($output);

// The Blackfire SDK Adapter is always null in production. We only inject one for testing purpose.
if (!$this->blackfireSdkAdapter) {
$clientConfiguration = null;
if (null !== $configFile = $input->getOption('config')) {
$clientConfiguration = ClientConfiguration::createFromFile($configFile);
}

if (!$clientConfiguration) {
$clientConfiguration = new ClientConfiguration();
}

$blackfire = new Client($clientConfiguration);
$blackfire->getConfiguration()->setUserAgentSuffix(sprintf('Blackfire Player/%s', Player::version()));

$this->blackfireSdkAdapter = new BlackfireSdkAdapter($blackfire);
}

[
'client_id' => $clientId,
'client_token' => $clientToken,
'endpoint' => $endpoint,
] = $this->getConfig($this->blackfireSdkAdapter);

if (!$this->blackfireHttpClient) {
$errorMessagePattern = 'Missing required "%s" configuration. Either configure it using "%s" environment variable or in your .blackfire.ini file';

if (!$clientId) {
throw new \InvalidArgumentException(sprintf($errorMessagePattern, 'client_id', 'BLACKFIRE_CLIENT_ID'));
}

if (!$clientToken) {
throw new \InvalidArgumentException(sprintf($errorMessagePattern, 'client_token', 'BLACKFIRE_CLIENT_TOKEN'));
}

$this->blackfireHttpClient = HttpClient::create([
'base_uri' => $this->getEnvOrDefault('BLACKFIRE_ENDPOINT', 'https://blackfire.io'),
'auth_basic' => [$this->getEnvOrThrow('BLACKFIRE_CLIENT_ID'), $this->getEnvOrThrow('BLACKFIRE_CLIENT_TOKEN')],
'base_uri' => $endpoint,
'auth_basic' => [$clientId, $clientToken],
'headers' => [
'Content-Type' => 'application/json',
'User-Agent' => sprintf('Blackfire Player/%s', Player::version()),
Expand All @@ -148,13 +182,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
]);
}

if (!$this->blackfireSdkAdapter) {
$blackfire = new Client(new ClientConfiguration());
$blackfire->getConfiguration()->setUserAgentSuffix(sprintf('Blackfire Player/%s', Player::version()));

$this->blackfireSdkAdapter = new BlackfireSdkAdapter($blackfire);
}

$json = $input->getOption('json');
$sslNoVerify = $input->getOption('ssl-no-verify');
$concurrency = $input->getOption('concurrency');
Expand Down Expand Up @@ -339,4 +366,17 @@ private function getEnvOrThrow(string $envVar): string

return $env;
}

private function getConfig(BlackfireSdkAdapterInterface $adapter): array
{
$clientId = $adapter->getConfiguration()->getClientId();
$clientToken = $adapter->getConfiguration()->getClientToken();
$endpoint = $adapter->getConfiguration()->getEndPoint();

return [
'client_id' => $clientId,
'client_token' => $clientToken,
'endpoint' => $endpoint,
];
}
}

0 comments on commit 5e7ebb6

Please sign in to comment.