-
Notifications
You must be signed in to change notification settings - Fork 5
PHP: Add OTEL support #72
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
ff90f49
807b7da
0f76aa7
0a105f6
290db09
f854b3e
dc74a75
0b5628e
a42f56c
4115930
f3fe8ad
27cb998
a7b757b
94e597e
441398d
4f16dfa
cab3d84
e7b9418
e8baa8a
6a3abc8
b6d87b2
4848433
e518a6c
1d1593e
b7c13b8
716f3b0
3e30be6
c589fac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <?php | ||
| /** | ||
| * Example demonstrating OpenTelemetry integration with Valkey GLIDE PHP | ||
| * Following Java/Go patterns with proper defaults | ||
| */ | ||
|
|
||
| require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
|
||
| try { | ||
| // OTEL configuration for traces and metrics | ||
| // Following Java/Go defaults: sample_percentage=1%, flush_interval_ms=5000 | ||
| $otelConfig = [ | ||
| 'traces' => [ | ||
| 'endpoint' => 'grpc://localhost:4317', // OTEL collector endpoint | ||
| 'sample_percentage' => 10 // Sample 10% of requests (default is 1%) | ||
prateek-kumar-improving marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ], | ||
| 'metrics' => [ | ||
| 'endpoint' => 'grpc://localhost:4317' // OTEL collector endpoint | ||
| ], | ||
| 'flush_interval_ms' => 5000 // Flush every 5 seconds (default) | ||
| ]; | ||
|
|
||
| // Create ValkeyGlide client with OTEL configuration | ||
| $client = new ValkeyGlide( | ||
| addresses: [ | ||
| ['host' => 'localhost', 'port' => 6379] | ||
| ], | ||
| use_tls: false, | ||
| credentials: null, | ||
| read_from: ValkeyGlide::READ_FROM_PRIMARY, | ||
| request_timeout: null, | ||
| reconnect_strategy: null, | ||
| database_id: 0, | ||
| client_name: 'otel-example-client', | ||
| client_az: null, | ||
| advanced_config: [ | ||
| 'connection_timeout' => 5000, | ||
| 'otel' => $otelConfig // Add OTEL configuration | ||
| ] | ||
| ); | ||
|
|
||
| echo "ValkeyGlide client created with OpenTelemetry support\n"; | ||
| echo "- Sample percentage: 10% (higher than default 1% for demo)\n"; | ||
| echo "- Flush interval: 5000ms (default)\n"; | ||
| echo "- Traces endpoint: grpc://localhost:4317\n"; | ||
| echo "- Metrics endpoint: grpc://localhost:4317\n\n"; | ||
|
|
||
| // Perform some operations that will be traced | ||
| $client->set('otel:test:key1', 'value1'); | ||
| echo "SET operation completed\n"; | ||
|
|
||
| $value = $client->get('otel:test:key1'); | ||
| echo "GET operation completed: $value\n"; | ||
|
|
||
| $client->set('otel:test:key2', 'value2'); | ||
| $client->set('otel:test:key3', 'value3'); | ||
|
|
||
| // Batch operations will also be traced | ||
| $results = $client->mget(['otel:test:key1', 'otel:test:key2', 'otel:test:key3']); | ||
| echo "MGET operation completed: " . json_encode($results) . "\n"; | ||
|
|
||
| // Cleanup | ||
| $client->del(['otel:test:key1', 'otel:test:key2', 'otel:test:key3']); | ||
| echo "Cleanup completed\n"; | ||
|
|
||
| $client->close(); | ||
| echo "Client closed\n"; | ||
|
|
||
| } catch (Exception $e) { | ||
| echo "Error: " . $e->getMessage() . "\n"; | ||
| exit(1); | ||
| } | ||
|
|
||
| echo "\nOpenTelemetry example completed successfully!\n"; | ||
| echo "Check your OTEL collector for traces and metrics.\n"; | ||
| echo "\nNote: OTEL can only be initialized once per process (like Java/Go).\n"; | ||
prateek-kumar-improving marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| echo "If you need to change configuration, restart the process.\n"; | ||
| ?> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| <?php | ||
prateek-kumar-improving marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| require_once 'TestSuite.php'; | ||
|
|
||
| class ValkeyGlideOtelTest extends TestSuite | ||
| { | ||
| public function testOtelConfigurationParsing() | ||
| { | ||
| // Test that OTEL configuration is accepted without errors | ||
| $otelConfig = [ | ||
| 'traces' => [ | ||
|
||
| 'endpoint' => 'grpc://localhost:4317', | ||
| 'sample_percentage' => 50 | ||
| ], | ||
| 'metrics' => [ | ||
| 'endpoint' => 'grpc://localhost:4317' | ||
| ], | ||
| 'flush_interval_ms' => 2000 | ||
| ]; | ||
|
|
||
| try { | ||
| $client = new ValkeyGlide( | ||
| addresses: [['host' => 'localhost', 'port' => 6379]], | ||
| use_tls: false, | ||
| credentials: null, | ||
| read_from: ValkeyGlide::READ_FROM_PRIMARY, | ||
| request_timeout: null, | ||
| reconnect_strategy: null, | ||
| database_id: 0, | ||
| client_name: 'otel-test-client', | ||
| client_az: null, | ||
| advanced_config: [ | ||
| 'connection_timeout' => 5000, | ||
| 'otel' => $otelConfig | ||
| ] | ||
| ); | ||
|
|
||
| // If we get here, OTEL config was accepted | ||
| $this->assertTrue(true); | ||
| $client->close(); | ||
| } catch (Exception $e) { | ||
| // OTEL config should not cause construction to fail | ||
| $this->fail("OTEL configuration caused client construction to fail: " . $e->getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public function testOtelWithTracesOnly() | ||
| { | ||
| $otelConfig = [ | ||
| 'traces' => [ | ||
| 'endpoint' => 'file:///tmp/valkey-traces.json', | ||
| 'sample_percentage' => 100 | ||
| ] | ||
| ]; | ||
|
|
||
| try { | ||
| $client = new ValkeyGlide( | ||
| addresses: [['host' => 'localhost', 'port' => 6379]], | ||
| use_tls: false, | ||
| credentials: null, | ||
| read_from: ValkeyGlide::READ_FROM_PRIMARY, | ||
| request_timeout: null, | ||
| reconnect_strategy: null, | ||
| database_id: 0, | ||
| client_name: 'otel-traces-test', | ||
| client_az: null, | ||
| advanced_config: [ | ||
| 'otel' => $otelConfig | ||
| ] | ||
| ); | ||
|
|
||
| // Perform some operations to generate traces | ||
| $client->set('otel:trace:test', 'value'); | ||
| $value = $client->get('otel:trace:test'); | ||
| $this->assertEquals('value', $value); | ||
| $client->del('otel:trace:test'); | ||
|
|
||
| $client->close(); | ||
| $this->assertTrue(true); | ||
prateek-kumar-improving marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } catch (Exception $e) { | ||
| $this->fail("Traces-only OTEL config failed: " . $e->getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public function testOtelWithMetricsOnly() | ||
| { | ||
| $otelConfig = [ | ||
| 'metrics' => [ | ||
| 'endpoint' => 'file:///tmp/valkey-metrics.json' | ||
| ] | ||
| ]; | ||
|
|
||
| try { | ||
| $client = new ValkeyGlide( | ||
| addresses: [['host' => 'localhost', 'port' => 6379]], | ||
| use_tls: false, | ||
| credentials: null, | ||
| read_from: ValkeyGlide::READ_FROM_PRIMARY, | ||
| request_timeout: null, | ||
| reconnect_strategy: null, | ||
| database_id: 0, | ||
| client_name: 'otel-metrics-test', | ||
| client_az: null, | ||
| advanced_config: [ | ||
| 'otel' => $otelConfig | ||
| ] | ||
| ); | ||
|
|
||
| // Perform some operations to generate metrics | ||
| $client->set('otel:metric:test', 'value'); | ||
| $client->get('otel:metric:test'); | ||
| $client->del('otel:metric:test'); | ||
|
|
||
| $client->close(); | ||
| $this->assertTrue(true); | ||
| } catch (Exception $e) { | ||
| $this->fail("Metrics-only OTEL config failed: " . $e->getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public function testOtelClusterSupport() | ||
| { | ||
| $otelConfig = [ | ||
| 'traces' => [ | ||
| 'endpoint' => 'file:///tmp/valkey-cluster-traces.json', | ||
| 'sample_percentage' => 100 | ||
| ] | ||
| ]; | ||
|
|
||
| try { | ||
| $client = new ValkeyGlideCluster( | ||
| addresses: [['host' => 'localhost', 'port' => 7001]], | ||
| use_tls: false, | ||
| credentials: null, | ||
| read_from: ValkeyGlide::READ_FROM_PRIMARY, | ||
| request_timeout: null, | ||
| reconnect_strategy: null, | ||
| client_name: 'otel-cluster-test', | ||
| periodic_checks: ValkeyGlideCluster::PERIODIC_CHECK_ENABLED_DEFAULT_CONFIGS, | ||
| client_az: null, | ||
| advanced_config: [ | ||
| 'otel' => $otelConfig | ||
| ] | ||
| ); | ||
|
|
||
| // Perform cluster operations to generate traces | ||
| $client->set('otel:cluster:test', 'value'); | ||
| $value = $client->get('otel:cluster:test'); | ||
| $this->assertEquals('value', $value); | ||
| $client->del('otel:cluster:test'); | ||
|
|
||
| $client->close(); | ||
| $this->assertTrue(true); | ||
| } catch (Exception $e) { | ||
| $this->fail("Cluster OTEL config failed: " . $e->getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public function testOtelWithoutConfiguration() | ||
| { | ||
| // Test that client works normally without OTEL config | ||
| try { | ||
| $client = new ValkeyGlide( | ||
| addresses: [['host' => 'localhost', 'port' => 6379]], | ||
| use_tls: false, | ||
| credentials: null, | ||
| read_from: ValkeyGlide::READ_FROM_PRIMARY, | ||
| request_timeout: null, | ||
| reconnect_strategy: null, | ||
| database_id: 0, | ||
| client_name: 'no-otel-test', | ||
| client_az: null, | ||
| advanced_config: [ | ||
| 'connection_timeout' => 5000 | ||
| // No OTEL config | ||
| ] | ||
| ); | ||
|
|
||
| // Operations should work normally | ||
| $client->set('no:otel:test', 'value'); | ||
| $value = $client->get('no:otel:test'); | ||
| $this->assertEquals('value', $value); | ||
| $client->del('no:otel:test'); | ||
|
|
||
| $client->close(); | ||
| $this->assertTrue(true); | ||
| } catch (Exception $e) { | ||
| $this->fail("Client without OTEL should work normally: " . $e->getMessage()); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.