-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VACMS-7673: Enable metrics collection on new content types when they …
…are created. (#16436)
- Loading branch information
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...les/custom/va_gov_content_types/src/EventSubscriber/EntityBundleCreateEventSubscriber.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,84 @@ | ||
<?php | ||
|
||
namespace Drupal\va_gov_content_types\EventSubscriber; | ||
|
||
use Drupal\Core\Config\ConfigFactoryInterface; | ||
use Drupal\core_event_dispatcher\EntityHookEvents; | ||
use Drupal\core_event_dispatcher\Event\Entity\EntityBundleCreateEvent; | ||
use Drupal\prometheus_exporter\MetricsCollectorManager; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* Subscribes to EntityBundleCreateEvent events. | ||
* | ||
* We use this event to ensure that prometheus_exporter exports the node count | ||
* for newly-created bundles. | ||
*/ | ||
class EntityBundleCreateEventSubscriber implements EventSubscriberInterface { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() { | ||
$events = []; | ||
$events[EntityHookEvents::ENTITY_BUNDLE_CREATE][] = ['onEntityBundleCreate']; | ||
return $events; | ||
} | ||
|
||
/** | ||
* The config factory. | ||
* | ||
* @var \Drupal\Core\Config\ConfigFactoryInterface | ||
*/ | ||
private ConfigFactoryInterface $configFactory; | ||
|
||
/** | ||
* The Prometheus Exporter metrics collector manager. | ||
* | ||
* @var \Drupal\prometheus_exporter\MetricsCollectorManager | ||
*/ | ||
private MetricsCollectorManager $metricsCollectorManager; | ||
|
||
/** | ||
* Constructor for EntityBundleEventSubscriber objects. | ||
* | ||
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory | ||
* The config factory. | ||
* @param \Drupal\prometheus_exporter\MetricsCollectorManager $metricsCollectorManager | ||
* The Prometheus Exporter metrics collector manager. | ||
*/ | ||
public function __construct(ConfigFactoryInterface $configFactory, MetricsCollectorManager $metricsCollectorManager) { | ||
$this->configFactory = $configFactory; | ||
$this->metricsCollectorManager = $metricsCollectorManager; | ||
} | ||
|
||
/** | ||
* Respond to the entity_bundle_create event. | ||
* | ||
* @param \Drupal\core_event_dispatcher\Event\Entity\EntityBundleCreateEvent $event | ||
* The event to process. | ||
*/ | ||
public function onEntityBundleCreate(EntityBundleCreateEvent $event): void { | ||
$entityTypeId = $event->getEntityTypeId(); | ||
if ($entityTypeId !== 'node') { | ||
return; | ||
} | ||
$this->enableNodeCountOnContentType($event->getBundle()); | ||
} | ||
|
||
/** | ||
* Enable node_count for the specified content type. | ||
* | ||
* @param string $contentType | ||
* The content type to enable. | ||
*/ | ||
public function enableNodeCountOnContentType(string $contentType): void { | ||
$config = $this->configFactory->getEditable('prometheus_exporter.settings'); | ||
$bundles = $config->get('collectors.node_count.settings.bundles') ?? []; | ||
$bundles[$contentType] = $contentType; | ||
$config->set('collectors.node_count.settings.bundles', $bundles); | ||
$config->save(); | ||
$this->metricsCollectorManager->syncPluginConfig(); | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
docroot/modules/custom/va_gov_content_types/va_gov_content_types.services.yml
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,6 @@ | ||
services: | ||
va_gov_content_types.entity_bundle_create_event_subscriber: | ||
class: Drupal\va_gov_content_types\EventSubscriber\EntityBundleCreateEventSubscriber | ||
arguments: ['@config.factory', '@prometheus_exporter.metrics_collector_manager'] | ||
tags: | ||
- { name: event_subscriber } |
48 changes: 48 additions & 0 deletions
48
...va_gov_content_types/functional/EventSubscriber/EntityBundleCreateEventSubscriberTest.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,48 @@ | ||
<?php | ||
|
||
namespace tests\phpunit\va_gov_content_types\functional\EventSubscriber; | ||
|
||
use Drupal\Tests\node\Traits\ContentTypeCreationTrait; | ||
use Drupal\va_gov_content_types\EventSubscriber\EntityBundleCreateEventSubscriber; | ||
use Tests\Support\Classes\VaGovExistingSiteBase; | ||
|
||
/** | ||
* Functional test of the EntityBundleCreateEventSubscriber class. | ||
* | ||
* @group functional | ||
* @group all | ||
* | ||
* @coversDefaultClass \Drupal\va_gov_content_types\EventSubscriber\EntityBundleCreateEventSubscriber | ||
*/ | ||
class EntityBundleCreateEventSubscriberTest extends VaGovExistingSiteBase { | ||
|
||
use ContentTypeCreationTrait; | ||
|
||
/** | ||
* Verify that the service is registered and can be instantiated. | ||
*/ | ||
public function testServiceRegistered(): void { | ||
$this->assertInstanceOf(EntityBundleCreateEventSubscriber::class, $this->container->get('va_gov_content_types.entity_bundle_create_event_subscriber')); | ||
} | ||
|
||
/** | ||
* Verify the prometheus_exporter config is updated. | ||
*/ | ||
public function testOnEntityBundleCreate(): void { | ||
$config = $this->container->get('config.factory')->get('prometheus_exporter.settings'); | ||
$bundles = $config->get('collectors.node_count.settings.bundles') ?? []; | ||
// Generate a random machine name that is unlikely to exist. | ||
$typeName = strtolower($this->randomMachineName(8)); | ||
$this->assertArrayNotHasKey($typeName, $bundles); | ||
|
||
$this->createContentType([ | ||
'type' => $typeName, | ||
'name' => 'My Test Content Type ' . $typeName, | ||
]); | ||
|
||
$config = $this->container->get('config.factory')->get('prometheus_exporter.settings'); | ||
$bundles = $config->get('collectors.node_count.settings.bundles') ?? []; | ||
$this->assertArrayHasKey($typeName, $bundles); | ||
} | ||
|
||
} |