forked from shopware/shopware
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEXT-36658 - Add system metrics structure
Co-authored-by: Andrii Havryliuk <[email protected]> Co-authored-by: Ghaith Olabi <[email protected]>
- Loading branch information
1 parent
e8f4d27
commit 4fbdffe
Showing
57 changed files
with
2,210 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
--- | ||
title: Telemetry abstraction layer | ||
date: 2024-07-30 | ||
area: core | ||
tags: [core, profile, performance, datadog, metrics, monitoring] | ||
--- | ||
## Context | ||
|
||
Observability is a key aspect of modern software development. It is essential to have the right tools in place to monitor and analyze runtime statistics of the application. | ||
|
||
Many tools and backends are available to enable telemetry and monitoring. The context of this ADR is to provide a streamlined and simple way to enable the integration of any observability tool into the Shopware platform. | ||
|
||
## Decision | ||
|
||
To address the need for a unified way to track metrics and performance data, we will introduce a telemetry abstraction layer. This layer will provide a common interface for integrating different monitoring tools into the Shopware platform. | ||
|
||
The telemetry abstraction layer will consist of the following components: | ||
|
||
### Shopware's abstraction layer | ||
|
||
The abstraction layer will provide a common interface for telemetry integration. It will define the methods and data structures required to send telemetry data to the monitoring backend. | ||
|
||
### Events subsystem attachment | ||
|
||
The telemetry abstraction layer will be integrated with the existing events subsystem. This integration will allow developers to hook into specific events and capture telemetry data related to those events. | ||
|
||
### Transport layer (integrations) | ||
|
||
Vendor specific implementation will not be part of the core. Those would be shipped as external libraries that implement the telemetry abstraction layer specification. The core will provide documentation on how to integrate these libraries into the Shopware platform. | ||
|
||
Each transport layer should at least be aware of the following metrics objects: | ||
- `Shopware\Core\Framework\Telemetry\Metrics\Metric\Counter` | ||
- `Shopware\Core\Framework\Telemetry\Metrics\Metric\Gauge` | ||
- `Shopware\Core\Framework\Telemetry\Metrics\Metric\Histogram` | ||
- `Shopware\Core\Framework\Telemetry\Metrics\Metric\UpDownCounter` | ||
|
||
Or more generally, should aim to cover all the metric types defined inside the `Shopware\Core\Framework\Telemetry\Metrics\Metric` namespace. | ||
|
||
### Implementation and Considerations | ||
|
||
Each transport should implement the `MetricTransportInterface`. This interface defines a method `emit` that takes a `MetricInterface` object as an argument. The `MetricInterface` object represents a single metric that needs to be sent to the monitoring backend. | ||
|
||
If an instance of an unsupported metric type is passed to the transport, it should throw a `MetricNotSupportedException`. This ensures that the transport layer is decoupled from the core and can be extended to support new metric types in the future. | ||
|
||
> `MetricNotSupportedException` is gracefully handled, and the application will skip over the unsupported metric type. | ||
```php | ||
interface MetricTransportInterface | ||
{ | ||
/** | ||
* @throws MetricNotSupportedException | ||
*/ | ||
public function emit(MetricInterface $metric): void;} | ||
``` | ||
|
||
The `MetricInterface` is a generic empty interface. This approach provides flexibility for different monitoring tools to define their own metric structures alongside the core ones. | ||
|
||
```php | ||
interface MetricInterface | ||
{ | ||
} | ||
``` | ||
|
||
|
||
## Consequences | ||
|
||
By implementing a telemetry abstraction layer, we provide a unified way to integrate monitoring tools into the Shopware platform. This approach simplifies the process of adding telemetry to the application and ensures consistency across different monitoring tools. | ||
|
||
|
||
## Usage | ||
|
||
See [README.md](../src/Core/Framework/Telemetry/README.md) for the implementation and usage details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
title: Metrics abstraction | ||
issue: NEXT-36658 | ||
flag: TELEMETRY_METRICS | ||
|
||
--- | ||
# Core | ||
* Added a new metrics abstraction layer `Shopware\Core\Framework\Telemetry` to the core. This layer allows collecting metrics from different sources and sending them to different targets. See documentation in the package folder for more details. | ||
* Added `TELEMETRY_METRICS` feature flag to enable/disable metrics collection. | ||
* Changed default system event dispatcher to the `MetricEventDispatcher` to listen on all events and this way support metrics collection for system events. | ||
* Changed attributes of `Shopware\Core\Framework\Adapter\Cache\InvalidateCacheEvent` to enable emitting of `cache.invalidate` metric. | ||
* Changed attributes of `Shopware\Core\Framework\App\Event\AppInstalledEvent` to enable emitting of `app.install` metric. | ||
* Changed attributes of `Shopware\Core\Framework\Plugin\Event\PluginPostInstallEvent` to enable emitting of `plugin.install` metric. | ||
* Changed `Shopware\Core\Framework\MessageQueue\Subscriber\MessageQueueSubscriber` to listen on `onMessageReceived` event and emit `messenger.message.size` metric. | ||
* Added `Shopware\Core\Framework\DataAbstractionLayer\Subscriber\EntityStatsSubscriber` to listen on `onEntitySearched` event and emit `dal.association.count` metric. | ||
* Changed `Shopware\Core\Framework\DataAbstractionLayer\Doctrine\RetryableQuery` and `Shopware\Core\Framework\DataAbstractionLayer\Doctrine\RetryableTransaction` to emit `database.locked` metric. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/Core/Framework/DataAbstractionLayer/Subscriber/EntityStatsSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Shopware\Core\Framework\DataAbstractionLayer\Subscriber; | ||
|
||
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchedEvent; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; | ||
use Shopware\Core\Framework\Log\Package; | ||
use Shopware\Core\Framework\Telemetry\Metrics\Meter; | ||
use Shopware\Core\Framework\Telemetry\Metrics\Metric\Histogram; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
#[Package('core')] | ||
class EntityStatsSubscriber implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @internal | ||
*/ | ||
public function __construct(private readonly Meter $meter) | ||
{ | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
EntitySearchedEvent::class => ['onEntitySearched', 99], | ||
]; | ||
} | ||
|
||
public function onEntitySearched(EntitySearchedEvent $event): void | ||
{ | ||
$criteria = $event->getCriteria(); | ||
$associationsCount = $this->getAssociationsCountFromCriteria($criteria); | ||
$this->meter->emit(new Histogram( | ||
name: 'dal.association.count', | ||
value: $associationsCount, | ||
description: 'Number of associations in request', | ||
)); | ||
} | ||
|
||
private function getAssociationsCountFromCriteria(Criteria $criteria): int | ||
{ | ||
return array_reduce( | ||
$criteria->getAssociations(), | ||
fn (int $carry, Criteria $association) => $carry + 1 + $this->getAssociationsCountFromCriteria($association), | ||
0 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="Shopware\Core\Framework\Telemetry\Metrics\Meter" public="true" lazy="true"> | ||
<argument type="tagged_iterator" tag="shopware.metric_transport"/> | ||
<argument type="service" id="logger"/> | ||
</service> | ||
|
||
<service id="Shopware\Core\Framework\Telemetry\Metrics\Extractor\MetricExtractor"> | ||
<argument type="service" id="logger"/> | ||
</service> | ||
|
||
<!-- should be the closest dispatcher to the main Symfony one, since it should catch the final state of ALL dispatched events --> | ||
<service id="Shopware\Core\Framework\Telemetry\Metrics\MetricEventDispatcher" decorates="event_dispatcher" decoration-priority="9999"> | ||
<argument type="service" id="Shopware\Core\Framework\Telemetry\Metrics\MetricEventDispatcher.inner"/> | ||
<argument type="service" id="Shopware\Core\Framework\Telemetry\Metrics\Extractor\MetricExtractor"/> | ||
<argument type="service" id="Shopware\Core\Framework\Telemetry\Metrics\Meter"/> | ||
</service> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.