Skip to content

Commit

Permalink
Merge branch 'main' into vendic-main
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.json
  • Loading branch information
Ryangr0 committed Oct 13, 2024
2 parents 25e8f4d + 9d0da7a commit e071ee0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vendic/telemetry-service",
"type": "library",
"license": "MIT",
"version": "0.2.0",
"version": "0.3.0",
"description": "A simple telemetry service for PHP applications",
"keywords": [
"telemetry",
Expand Down
15 changes: 15 additions & 0 deletions src/Core/Domain/Services/TelemetryServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@

namespace Webgrip\TelemetryService\Core\Domain\Services;

use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\API\Trace\TracerInterface;
use Psr\Log\LoggerInterface;

interface TelemetryServiceInterface
{
/**
* @return LoggerInterface
*/
public function logger(): LoggerInterface;

/**
* @return TracerInterface
*/
public function tracer(): TracerInterface;

/**
* @param \Throwable $exception
* @param SpanInterface $span
* @return void
*/
public function registerException(\Throwable $exception, SpanInterface $span): void;
}
39 changes: 39 additions & 0 deletions src/Infrastructure/Factories/TelemetryServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;
Expand Down Expand Up @@ -52,9 +53,47 @@ public function create(
$resourceInfo = ResourceInfo::create(
Attributes::create([
ResourceAttributes::DEPLOYMENT_ENVIRONMENT_NAME => $configuration->get('applicationEnvironmentName'),

ResourceAttributes::HOST_IP => $_SERVER['HOST_IP'] ?? null,
ResourceAttributes::HOST_NAME => $_SERVER['HOSTNAME'] ?? null,

ResourceAttributes::OS_NAME => php_uname('s') ?? null,
ResourceAttributes::OS_VERSION => php_uname('v') ?? null,

ResourceAttributes::PROCESS_COMMAND => $_SERVER['argv'][0] ?? null,
ResourceAttributes::PROCESS_COMMAND_LINE => implode(' ', $_SERVER['argv']) ?? null,
ResourceAttributes::PROCESS_OWNER => get_current_user() ?? null,
ResourceAttributes::PROCESS_PID => getmypid() ?? null,
ResourceAttributes::PROCESS_RUNTIME_DESCRIPTION => php_uname('m') ?? null,
ResourceAttributes::PROCESS_RUNTIME_NAME => php_sapi_name() ?? null,
ResourceAttributes::PROCESS_RUNTIME_VERSION => phpversion() ?? null,

ResourceAttributes::SERVICE_NAMESPACE => $configuration->get('applicationNamespace'),
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,

TraceAttributes::HTTP_ROUTE => $_SERVER['REQUEST_URI'] ?? null,
TraceAttributes::HTTP_REQUEST_METHOD => $_SERVER['REQUEST_METHOD'] ?? null,
TraceAttributes::HTTP_REQUEST_SIZE => $_SERVER['CONTENT_LENGTH'] ?? null,
TraceAttributes::HTTP_RESPONSE_SIZE => $_SERVER['CONTENT_LENGTH'] ?? null,
TraceAttributes::HTTP_RESPONSE_STATUS_CODE => $_SERVER['REDIRECT_STATUS'] ?? null,

TraceAttributes::SERVER_ADDRESS => $_SERVER['SERVER_NAME'] ?? null,
TraceAttributes::SERVER_PORT => $_SERVER['SERVER_PORT'] ?? null,

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

TraceAttributes::URL_SCHEME => $_SERVER['REQUEST_SCHEME'] ?? null,
TraceAttributes::URL_DOMAIN => $_SERVER['HTTP_HOST'] ?? null,
TraceAttributes::URL_EXTENSION => $_SERVER['REDIRECT_STATUS'] ?? null,
TraceAttributes::URL_PATH => $_SERVER['REQUEST_URI'] ?? null,
TraceAttributes::URL_QUERY => $_SERVER['QUERY_STRING'] ?? null,

TraceAttributes::USER_AGENT_NAME => $_SERVER['HTTP_USER_AGENT'] ?? null,
])
);

Expand Down
9 changes: 9 additions & 0 deletions src/Infrastructure/Services/TelemetryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Monolog\Level;
use Monolog\Logger;
use OpenTelemetry\API\Logs\LoggerProviderInterface;
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\API\Trace\TracerInterface;
use OpenTelemetry\API\Trace\TracerProviderInterface;
use OpenTelemetry\Contrib\Logs\Monolog\Handler;
Expand Down Expand Up @@ -46,4 +48,11 @@ public function tracer(): TracerInterface
{
return $this->tracerProvider->getTracer('io.opentelemetry.contrib.php');
}

public function registerException(\Throwable $exception, SpanInterface $span): void
{
$this->logger()->error($exception->getMessage(), ['exception' => $exception]);
$span->setStatus(StatusCode::STATUS_ERROR, $exception->getMessage());
$span->recordException($exception);
}
}

0 comments on commit e071ee0

Please sign in to comment.