Circular reference detected with Database state tracement enabled.

Created on 24 June 2025, 8 days ago

Problem/Motivation

When the opentelemetry module is installed and the "Database Statement" trace plugin is enabled, clearing the Drupal cache from UI results in a fatal error:

Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException: Circular reference detected for service "Drupal\opentelemetry\EventSubscriber\DatabaseStatementTraceEventSubscriber"

This occurs because the DatabaseStatementTraceEventSubscriber has a direct dependency on the opentelemetry service. During service container compilation, instantiating this subscriber triggers a dependency chain that eventually requires the config.factory service, which in turn depends on the event_dispatcher. Since the event_dispatcher needs all subscribers to be instantiated first, this creates a circular reference that breaks site builds and cache clears.

Steps to reproduce

  • Install and enable the opentelemetry module.
  • Navigate to the OpenTelemetry configuration page (/admin/config/development/opentelemetry).
  • Enable the "Database Statement" trace plugin and save the configuration.
  • Clear the Drupal cache via the UI
  • Observe the ServiceCircularReferenceException fatal error.
The website encountered an unexpected error. Try again later.
Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException: Circular reference detected for service "Drupal\opentelemetry\EventSubscriber\DatabaseStatementTraceEventSubscriber", path: "Drupal\opentelemetry\EventSubscriber\DatabaseStatementTraceEventSubscriber -> opentelemetry -> opentelemetry.tracer_provider -> opentelemetry.span_processor -> opentelemetry.span.exporter -> opentelemetry.span.exporter.factory -> opentelemetry.traces.transport.factory -> opentelemetry.transport.factory.provider". in Drupal\Component\DependencyInjection\Container->get() (line 147 of core/lib/Drupal/Component/DependencyInjection/Container.php).
Drupal\Component\DependencyInjection\Container->get() (Line: 454)
Drupal\Component\DependencyInjection\Container->Drupal\Component\DependencyInjection\{closure}() (Line: 185)
Drupal\tracer\EventDispatcher\TraceableEventDispatcher->addCalledListener() (Line: 158)
Drupal\tracer\EventDispatcher\TraceableEventDispatcher->callListeners() (Line: 77)
Drupal\tracer\EventDispatcher\TraceableEventDispatcher->dispatch() (Line: 1527)
Drupal\Core\Database\Connection->dispatchEvent() (Line: 135)
...
Drupal\Core\DrupalKernel->handle() (Line: 19)

Proposed resolution

The circular dependency will be resolved by removing the direct service injection of opentelemetry from the DatabaseStatementTraceEventSubscriber's constructor and dynamically retrieving the opentelemetry service within its event methods, but only if the service has already been initialized by the container. Same as https://www.drupal.org/project/opentelemetry/issues/3392836 📌 Make a soft dependency for the ExceptionTraceEventSubscriber Fixed

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇮🇹Italy pix_

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024