diff --git a/examples/otel_metrics.php b/examples/otel_metrics.php index f7bda0be..4e7dd055 100644 --- a/examples/otel_metrics.php +++ b/examples/otel_metrics.php @@ -29,11 +29,14 @@ use OpenTelemetry\SDK\Metrics\MeterProvider; use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader; use OpenTelemetry\SDK\Resource\ResourceInfo; use Predis\Client; +use Predis\Connection\Parameters; use Predis\Observability\MetricGroup; use Predis\Observability\MetricsConfig; use Predis\Observability\ObservabilityInstance; use Predis\Observability\OTelConfig; use Predis\Response\ServerException; +use Predis\Retry\Retry; +use Predis\Retry\Strategy\ExponentialBackoff; require __DIR__ . '/../vendor/autoload.php'; @@ -56,7 +59,8 @@ ObservabilityInstance::getInstance()->init(new OTelConfig( $provider )); -$client = new Client($redis); +$client = new Client(Parameters::parse($redis) + ['retry' => new Retry(new ExponentialBackoff(1000, 10000), 3)]); +$admin = new Client($redis); echo "Exporting to $collector for $seconds seconds as service 'predis-example'", PHP_EOL; @@ -82,6 +86,11 @@ do { $transaction->incr('otel:counter'); }); + // Closes the connection server-side: the next command fails once, then succeeds on retry + // and is recorded with redis.client.operation.retry_attempts = 1. + $admin->executeRaw(['CLIENT', 'KILL', 'ID', $client->executeRaw(['CLIENT', 'ID'])]); + $client->get('otel:string'); + $client->del('otel:string', 'otel:counter'); // PHP has no background exporter: nothing leaves the process until the provider is flushed. diff --git a/tests/Predis/Observability/OTelIntegrationTest.php b/tests/Predis/Observability/OTelIntegrationTest.php index eb2bbfad..4eadd159 100644 --- a/tests/Predis/Observability/OTelIntegrationTest.php +++ b/tests/Predis/Observability/OTelIntegrationTest.php @@ -19,6 +19,8 @@ use OpenTelemetry\SDK\Metrics\MeterProvider; use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader; use OpenTelemetry\SDK\Resource\ResourceInfo; use Predis\Response\ServerException; +use Predis\Retry\Retry; +use Predis\Retry\Strategy\NoBackoff; use PredisTestCase; /** @@ -110,6 +112,29 @@ class OTelIntegrationTest extends PredisTestCase $this->assertSeries($series, $name . '_bucket', ['db_operation_name' => 'SET', 'le' => '2.5']); } + /** + * @group connected + * @group relay-incompatible + */ + public function testExportsRetryAttemptsThroughCollector(): void + { + $client = $this->createClient(['retry' => new Retry(new NoBackoff(), 3)]); + + // Closes the connection server-side: the next command fails once, then succeeds on retry. + $this->createClient()->executeRaw(['CLIENT', 'KILL', 'ID', $client->executeRaw(['CLIENT', 'ID'])]); + + $this->initObservability([MetricGroup::COMMAND]); + + $this->assertNull($client->get('otel:missing')); + $this->assertTrue(ObservabilityInstance::getInstance()->forceFlush()); + + $name = 'db_client_operation_duration_seconds_count'; + $series = $this->scrape($name, 1); + + $this->assertSeries($series, $name, ['db_operation_name' => 'GET', 'redis_client_operation_retry_attempts' => '1']); + $this->assertStringNotContainsString('error_type', $series[0]); + } + /** * Enables the given metric groups with a provider exporting to the collector. */