Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved error processing and service naming #7

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator;
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Contrib\Grpc\GrpcTransportFactory;
use OpenTelemetry\Contrib\Otlp\OtlpUtil;
Expand All @@ -33,7 +34,9 @@
use Spryker\Service\Opentelemetry\Instrumentation\SpanProcessor\PostFilterBatchSpanProcessor;
use Spryker\Service\Opentelemetry\Instrumentation\Tracer\TracerProvider;
use Spryker\Service\Opentelemetry\OpentelemetryInstrumentationConfig;
use Spryker\Service\Opentelemetry\Plugin\OpentelemetryMonitoringExtensionPlugin;
use Spryker\Service\Opentelemetry\Storage\CustomParameterStorage;
use Spryker\Service\Opentelemetry\Storage\ExceptionStorage;
use Spryker\Service\Opentelemetry\Storage\ResourceNameStorage;
use Spryker\Service\Opentelemetry\Storage\RootSpanNameStorage;
use Spryker\Shared\Opentelemetry\Instrumentation\CachedInstrumentation;
Expand Down Expand Up @@ -295,20 +298,32 @@ protected static function formatSpanName(Request $request): string
public static function shutdownHandler(): void
{
$resourceName = ResourceNameStorage::getInstance()->getName();
$customParamsStorage = CustomParameterStorage::getInstance();
annakotiuk marked this conversation as resolved.
Show resolved Hide resolved
if ($resourceName) {
if ($customParamsStorage->getAttribute(OpentelemetryMonitoringExtensionPlugin::ATTRIBUTE_IS_CONSOLE_COMMAND)) {
$resourceName = sprintf('CLI %s', $resourceName);
}
static::$resourceInfo->setServiceName($resourceName);
}
$scope = Context::storage()->scope();
if (!$scope) {
return;
}

$scope->detach();
$span = static::$rootSpan;
$name = RootSpanNameStorage::getInstance()->getName();
if ($name) {
$span->updateName($name);
}
$customParamsStorage = CustomParameterStorage::getInstance();

$exceptions = ExceptionStorage::getInstance()->getExceptions();
foreach ($exceptions as $exception) {
$span->recordException($exception);
}

$span->setStatus($exceptions ? StatusCode::STATUS_ERROR : StatusCode::STATUS_OK);

$span->setAttributes($customParamsStorage->getAttributes());
$span->end();
}
Expand Down
24 changes: 24 additions & 0 deletions src/Spryker/Service/Opentelemetry/OpentelemetryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Spryker\Service\Kernel\AbstractService;
use Spryker\Service\Opentelemetry\Storage\ResourceNameStorage;
use Throwable;

/**
* @method \Spryker\Service\Opentelemetry\OpentelemetryServiceFactory getFactory()
Expand All @@ -31,6 +32,10 @@ public function setCustomParameter(string $key, $value): void
}

/**
* {@inheritDoc}
*
* @api
*
* @param string $name
*
* @return void
Expand All @@ -41,6 +46,10 @@ public function setRootSpanName(string $name): void
}

/**
* {@inheritDoc}
*
* @api
*
* @param string $name
*
* @return void
Expand All @@ -49,4 +58,19 @@ public function setResourceName(string $name): void
{
$this->getFactory()->createResourceNameStorage()->setName($name);
}

/**
* {@inheritDoc}
*
* @api
*
* @param string $message
* @param \Throwable $exception
*
* @return void
*/
public function setError(string $message, Throwable $exception): void
{
$this->getFactory()->createExceptionStorage()->addException($exception);
}
}
10 changes: 10 additions & 0 deletions src/Spryker/Service/Opentelemetry/OpentelemetryServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Spryker\Service\Kernel\AbstractServiceFactory;
use Spryker\Service\Opentelemetry\Storage\CustomParameterStorage;
use Spryker\Service\Opentelemetry\Storage\CustomParameterStorageInterface;
use Spryker\Service\Opentelemetry\Storage\ExceptionStorage;
use Spryker\Service\Opentelemetry\Storage\ExceptionStorageInterface;
use Spryker\Service\Opentelemetry\Storage\ResourceNameStorage;
use Spryker\Service\Opentelemetry\Storage\RootSpanNameStorage;
use Spryker\Service\Opentelemetry\Storage\RootSpanNameStorageInterface;
Expand Down Expand Up @@ -39,4 +41,12 @@ public function createResourceNameStorage(): ResourceNameStorage
{
return ResourceNameStorage::getInstance();
}

/**
* @return \Spryker\Service\Opentelemetry\Storage\ExceptionStorageInterface
*/
public function createExceptionStorage(): ExceptionStorageInterface
{
return ExceptionStorage::getInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Spryker\Service\Opentelemetry;

use Throwable;

interface OpentelemetryServiceInterface
{
/**
Expand Down Expand Up @@ -34,4 +36,29 @@ public function setCustomParameter(string $key, $value): void;
* @return void
*/
public function setRootSpanName(string $name): void;

/**
* Specification:
* - Sets service name to the root span.
*
* @api
*
* @param string $name
*
* @return void
*/
public function setResourceName(string $name): void;

/**
* Specification:
* - Adds error event to the root span.
*
* @api
*
* @param string $message
* @param \Throwable $exception
*
* @return void
*/
public function setError(string $message, Throwable $exception): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@

namespace Spryker\Service\Opentelemetry\Plugin;

use OpenTelemetry\Context\Context;
use Spryker\Service\Kernel\AbstractPlugin;
use Spryker\Service\MonitoringExtension\Dependency\Plugin\MonitoringExtensionPluginInterface;
use Spryker\Service\Opentelemetry\Instrumentation\Span\Span;

/**
* @method \Spryker\Service\Opentelemetry\OpentelemetryService getService()
* @method \Spryker\Service\Opentelemetry\OpentelemetryServiceInterface getService()
*/
class OpentelemetryMonitoringExtensionPlugin extends AbstractPlugin implements MonitoringExtensionPluginInterface
{
/**
* @var string
*/
public const ATTRIBUTE_IS_CONSOLE_COMMAND = 'is_console_command';

/**
* Specification:
* - Adds error to the current active span.
Expand All @@ -30,13 +33,7 @@ class OpentelemetryMonitoringExtensionPlugin extends AbstractPlugin implements M
*/
public function setError(string $message, $exception): void
{
$scope = Context::storage()->scope();
if (!$scope) {
return;
}

$span = Span::fromContext($scope->context());
$span->recordException($exception);
$this->getService()->setError($message, $exception);
}

/**
Expand All @@ -53,7 +50,8 @@ public function setError(string $message, $exception): void
*/
public function setApplicationName(?string $application = null, ?string $store = null, ?string $environment = null): void
{
$this->getService()->setResourceName(sprintf('%s-%s (%s)', $application ?? '', $store ?? '', $environment ?? ''));
$storeRegion = $store ?? (defined('APPLICATION_REGION') ? APPLICATION_REGION : '');
$this->getService()->setResourceName(sprintf('%s-%s (%s)', $application ?? '', $storeRegion, $environment ?? ''));
}

/**
Expand Down Expand Up @@ -112,15 +110,16 @@ public function markIgnoreTransaction(): void

/**
* Specification:
* - Opentelemetry has no attributes to specify background jobs. Service name already defined for CLI commands.
* - Adds custom param into the root span that can be used to show only console commands.
* - Trace service name will be prepended with `CLI` prefix if this param is true.
*
* @api
*
* @return void
*/
public function markAsConsoleCommand(): void
{
return;
$this->getService()->setCustomParameter(static::ATTRIBUTE_IS_CONSOLE_COMMAND, true);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Service\Opentelemetry\Storage;

class CustomParameterStorage implements CustomParameterStorageInterface
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Service\Opentelemetry\Storage;

interface CustomParameterStorageInterface
{
/**
* @return \Spryker\Service\Opentelemetry\Storage\CustomParameterStorage
*/
public static function getInstance(): CustomParameterStorage;

/**
Expand Down
48 changes: 48 additions & 0 deletions src/Spryker/Service/Opentelemetry/Storage/ExceptionStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Spryker\Service\Opentelemetry\Storage;

use Throwable;

class ExceptionStorage implements ExceptionStorageInterface
{
/**
* @var \Spryker\Service\Opentelemetry\Storage\ExceptionStorageInterface|null
*/
private static ?ExceptionStorageInterface $instance = null;

/**
* @var array<\Throwable>
*/
protected array $exceptions = [];

/**
* @return \Spryker\Service\Opentelemetry\Storage\ExceptionStorageInterface
*/
public static function getInstance(): ExceptionStorageInterface
{
if (static::$instance === null) {
static::$instance = new static();
}

return static::$instance;
}

/**
* @return array<\Throwable>
*/
public function getExceptions(): array
{
return $this->exceptions;
}

/**
* @param \Throwable $exception
*
* @return void
*/
public function addException(Throwable $exception): void
{
$this->exceptions[] = $exception;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Spryker\Service\Opentelemetry\Storage;

use Throwable;

interface ExceptionStorageInterface
{
/**
* @return \Spryker\Service\Opentelemetry\Storage\ExceptionStorageInterface
*/
public static function getInstance(): ExceptionStorageInterface;

/**
* @return array<\Throwable>
*/
public function getExceptions(): array;

/**
* @param \Throwable $exception
*
* @return void
*/
public function addException(Throwable $exception): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ function: \'%s\',

$type = $params[1] ?? \\Symfony\\Component\\HttpKernel\\HttpKernelInterface::MAIN_REQUEST;

$request = \\Spryker\\Shared\\OpenTelemetry\\Request\\RequestProcessor::getRequest();

$span = \\Spryker\\Shared\\OpenTelemetry\\Instrumentation\\CachedInstrumentation::getCachedInstrumentation()
->tracer()
->spanBuilder(\'%s\')
Expand Down Expand Up @@ -110,8 +108,8 @@ function: \'%s\',

if ($exception !== null) {
$span->recordException($exception);
$span->setAttribute(\'error_message\', isset($exception) ? $exception->getMessage() : \'\');
$span->setAttribute(\'error_code\', isset($exception) ? $exception->getCode() : \'\');
$span->setAttribute(\'error_message\', $exception->getMessage());
$span->setAttribute(\'error_code\', $exception->getCode());
}

$span->setStatus($exception !== null ? \\OpenTelemetry\\API\\Trace\\StatusCode::STATUS_ERROR : \\OpenTelemetry\\API\\Trace\\StatusCode::STATUS_OK);
Expand Down
Loading