Skip to content

Commit

Permalink
Don't throw exception when host not found
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryangr0 committed Oct 21, 2024
1 parent 3ad5010 commit b299f32
Showing 1 changed file with 40 additions and 21 deletions.
61 changes: 40 additions & 21 deletions src/Infrastructure/Factories/TelemetryServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@

use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Promise\CancellationException;
use Monolog\Logger;
use OpenTelemetry\API\Logs\NoopLoggerProvider;
use OpenTelemetry\API\Trace\NoopTracerProvider;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SemConv\ResourceAttributes;
use OpenTelemetry\SemConv\TraceAttributes;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -32,15 +29,34 @@ public function __construct(
}

/**
* @throws ContainerExceptionInterface
* @throws GuzzleException
* @throws NotFoundExceptionInterface
*/
public function create(
ContainerInterface $configuration,
LoggerInterface $logger
): TelemetryService {
$resourceInfo = ResourceInfo::create(
$resourceInfo = $this->createResourceInfo($configuration);

$otelCollectorHost = $configuration->get('otelCollectorHost');

if (
empty($otelCollectorHost) ||
!$this->collectorIsHealthy($otelCollectorHost, $logger)
) {
$logger->error('Telemetry collector host (otelCollectorHost) is not configured.');
return $this->createNoopTelemetryService($logger);
}

return new TelemetryService(
$this->loggerProviderFactory->create($resourceInfo),
$this->tracerProviderFactory->create($resourceInfo),
$logger

Check failure on line 53 in src/Infrastructure/Factories/TelemetryServiceFactory.php

View workflow job for this annotation

GitHub Actions / static-analysis / Static Analysis

Parameter #3 $logger of class Webgrip\TelemetryService\Infrastructure\Services\TelemetryService constructor expects Monolog\Logger, Psr\Log\LoggerInterface given.
);
}

private function createResourceInfo(ContainerInterface $configuration): ResourceInfo
{
return ResourceInfo::create(
Attributes::create([
ResourceAttributes::DEPLOYMENT_ENVIRONMENT_NAME => $configuration->get('applicationEnvironmentName'),

Expand All @@ -51,7 +67,7 @@ public function create(
ResourceAttributes::OS_VERSION => php_uname('v') ?? null,

Check failure on line 67 in src/Infrastructure/Factories/TelemetryServiceFactory.php

View workflow job for this annotation

GitHub Actions / static-analysis / Static Analysis

Expression on left side of ?? is not nullable.

ResourceAttributes::PROCESS_COMMAND => $_SERVER['argv'][0] ?? null,
ResourceAttributes::PROCESS_COMMAND_LINE => implode(' ', $_SERVER['argv']) ?? null,
ResourceAttributes::PROCESS_COMMAND_LINE => isset($_SERVER['argv']) ? implode(' ', $_SERVER['argv']) : null,
ResourceAttributes::PROCESS_OWNER => get_current_user() ?? null,

Check failure on line 71 in src/Infrastructure/Factories/TelemetryServiceFactory.php

View workflow job for this annotation

GitHub Actions / static-analysis / Static Analysis

Expression on left side of ?? is not nullable.
ResourceAttributes::PROCESS_PID => getmypid() ?? null,

Check failure on line 72 in src/Infrastructure/Factories/TelemetryServiceFactory.php

View workflow job for this annotation

GitHub Actions / static-analysis / Static Analysis

Expression on left side of ?? is not nullable.
ResourceAttributes::PROCESS_RUNTIME_DESCRIPTION => php_uname('m') ?? null,

Check failure on line 73 in src/Infrastructure/Factories/TelemetryServiceFactory.php

View workflow job for this annotation

GitHub Actions / static-analysis / Static Analysis

Expression on left side of ?? is not nullable.
Expand All @@ -62,7 +78,6 @@ public function create(
ResourceAttributes::SERVICE_NAME => $configuration->get('applicationName'),
ResourceAttributes::SERVICE_VERSION => $configuration->get('applicationVersion'),


TraceAttributes::CLIENT_ADDRESS => $_SERVER['REMOTE_ADDR'] ?? null,
TraceAttributes::CLIENT_PORT => $_SERVER['REMOTE_PORT'] ?? null,

Expand All @@ -75,7 +90,7 @@ public function create(
TraceAttributes::SERVER_ADDRESS => $_SERVER['SERVER_NAME'] ?? null,
TraceAttributes::SERVER_PORT => $_SERVER['SERVER_PORT'] ?? null,

TraceAttributes::SESSION_ID => session_id() ?? null,
TraceAttributes::SESSION_ID => session_id() ?: null,

TraceAttributes::URL_SCHEME => $_SERVER['REQUEST_SCHEME'] ?? null,
TraceAttributes::URL_DOMAIN => $_SERVER['HTTP_HOST'] ?? null,
Expand All @@ -86,32 +101,36 @@ public function create(
TraceAttributes::USER_AGENT_NAME => $_SERVER['HTTP_USER_AGENT'] ?? null,
])
);
}

$healthCheckUrl = 'http://' . $configuration->get('otelCollectorHost') . ':13133';
private function collectorIsHealthy(string $otelCollectorHost, LoggerInterface $logger): bool
{
$healthCheckUrl = 'http://' . $otelCollectorHost . ':13133';

try {
$response = $this->client->get($healthCheckUrl);

Check failure on line 111 in src/Infrastructure/Factories/TelemetryServiceFactory.php

View workflow job for this annotation

GitHub Actions / static-analysis / Static Analysis

Call to an undefined method Psr\Http\Client\ClientInterface::get().
$statusCode = $response->getStatusCode();

if (!in_array($statusCode, [Response::HTTP_OK, Response::HTTP_NO_CONTENT])) {
throw new CancellationException('Health check failed. Status code: ' . $statusCode);
if (!in_array($statusCode, [Response::HTTP_OK, Response::HTTP_NO_CONTENT], true)) {
$logger->warning('Health check failed. Status code: ' . $statusCode);
return false;
}
} catch (GuzzleException | CancellationException $e) {
$logger->warning(
'Health check for telemetry collector to ' . $healthCheckUrl . ' failed with exception ' . $e->getMessage(),
'Health check for telemetry collector to ' . $healthCheckUrl . ' failed with exception: ' . $e->getMessage(),
['exception' => $e]
);

return new TelemetryService(
new NoopLoggerProvider(),
new NoopTracerProvider(),
$logger
);
return false;
}

return true;
}

private function createNoopTelemetryService(LoggerInterface $logger): TelemetryService
{
return new TelemetryService(
$this->loggerProviderFactory->create($resourceInfo),
$this->tracerProviderFactory->create($resourceInfo),
new NoopLoggerProvider(),
new NoopTracerProvider(),
$logger
);
}
Expand Down

0 comments on commit b299f32

Please sign in to comment.