auxmoney/opentracing-bundle-php-http-httplug-bundle
Prerequisites: Ensure you have auxmoney/opentracing-bundle-core and php-http/httplug-bundle installed.
composer require auxmoney/opentracing-bundle-core php-http/httplug-bundle
Install the bundle:
composer require auxmoney/opentracing-bundle-php-http-httplug-bundle
// config/bundles.php
Auxmoney\OpentracingHttplugBundle\OpentracingHttplugBundle::class => ['all' => true],
First Use Case: Send an HTTP request via Httplug (e.g., Guzzle, Symfony HTTP client) and verify tracing headers are injected automatically. Example:
use Http\Client\Common\Plugin\PluginClient;
use Http\Client\Common\PluginClientFactory;
$client = $container->get('httplug.client'); // Auto-injected with tracing plugin
$response = $client->request('GET', 'https://api.example.com');
config/packages/auxmoney_opentracing.yaml (if extended).src/DependencyInjection/Compiler/OpentracingHttplugPass.php (how plugins are injected).src/Plugin/OpentracingPlugin.php (core header injection logic).Automatic Plugin Injection:
The bundle decorates PluginClientFactory during container compilation, injecting OpentracingPlugin into all Httplug clients. No manual client configuration is needed.
Tracing Headers:
Outgoing requests automatically include OpenTracing headers (uber-trace-id, x-request-id, etc.). Example:
// Headers are injected transparently; no manual setup required.
$response = $client->request('GET', 'https://api.example.com');
Custom Clients: For non-container-managed clients (e.g., created in services), manually add the plugin:
use Auxmoney\OpentracingHttplugBundle\Plugin\OpentracingPlugin;
$client = new PluginClient(
$baseClient,
[new OpentracingPlugin($tracer)] // Inject tracer from OpentracingBundle
);
Symfony HTTP Client: If using Symfony’s HTTP client (built on Httplug), the bundle works out-of-the-box. Example:
$response = $this->httpClient->request('GET', 'https://api.example.com');
Testing:
Mock the OpentracingPlugin to verify header injection:
$plugin = new OpentracingPlugin($mockTracer);
$this->assertArrayHasKey('uber-trace-id', $plugin->getHeaders($request));
Conditional Tracing: Disable tracing for specific clients by excluding the plugin:
# config/packages/httplug.yaml
httplug.client.my_client:
factory: ['@httplug.client_factory', 'createClient']
plugins: [] # Exclude OpentracingPlugin
Custom Headers:
Extend OpentracingPlugin to add/override headers:
class CustomOpentracingPlugin extends OpentracingPlugin
{
public function getHeaders(RequestInterface $request): array
{
$headers = parent::getHeaders($request);
$headers['x-custom-header'] = 'value';
return $headers;
}
}
Missing Dependencies:
auxmoney/opentracing-bundle-core or php-http/httplug-bundle will cause failures. Verify with:
composer why auxmoney/opentracing-bundle-core
Symfony Version Mismatch:
v0.1.1.composer.json for supported versions.Plugin Order:
OpentracingPlugin runs last to avoid header corruption. Reorder in PluginClientFactory:
$client = new PluginClient($baseClient, [
new AuthPlugin(),
new OpentracingPlugin($tracer), // Last
]);
Tracer Unavailability:
if (!$this->tracer->isActive()) {
return [];
}
Verify Headers: Log outgoing requests to confirm headers:
$client->getEmitter()->attach(
function (RequestInterface $request) {
error_log('Headers: ' . print_r($request->getHeaders(), true));
}
);
Check Tracer: Ensure the tracer is active:
$this->tracer->inject(
$spanContext,
'http_headers',
$carrier = []
);
$this->assertNotEmpty($carrier);
Compiler Pass Issues: If the plugin isn’t injected, clear the cache and check for errors:
php bin/console cache:clear
php bin/console debug:container httplug.client | grep OpentracingPlugin
Custom Plugin:
Override OpentracingPlugin to modify behavior (e.g., header naming):
class CustomPlugin extends OpentracingPlugin
{
protected function getTraceIdHeader(): string
{
return 'x-custom-trace-id'; // Override default
}
}
Dynamic Header Injection: Conditionally inject headers based on request attributes:
public function getHeaders(RequestInterface $request): array
{
if (!$request->hasAttribute('skip_tracing')) {
return parent::getHeaders($request);
}
return [];
}
Span Naming: Customize span names for specific endpoints:
$this->tracer->startSpan('custom-span-name')->withTag('http.url', $request->getUri());
No YAML Config: The bundle requires zero configuration, but you can disable it by removing the compiler pass (not recommended).
Flex Auto-Enable:
If using Symfony Flex, ensure auto-config and auto-register are enabled in config/bundles.php.
PHP 8 Attributes:
The bundle uses PHP 8 features (e.g., constructor property promotion). Downgrade to v0.1.1 for PHP 7.4.
```markdown
### Pro Tips
1. **Performance**:
Header injection adds minimal overhead (~1ms per request). Benchmark with:
```bash
php bin/console debug:autowiring Http\Client\Common\PluginClient
Distributed Tracing: Pair with Jaeger or Zipkin for visualization. Example Jaeger setup:
# config/packages/auxmoney_opentracing.yaml
auxmoney_opentracing:
exporter: jaeger
jaeger:
agent_host: localhost
agent_port: 6831
Testing:
Use OpentracingBundle's test utilities to mock tracers:
$mockTracer = $this->createMock(TracerInterface::class);
$mockTracer->method('inject')->willReturn([]);
$plugin = new OpentracingPlugin($mockTracer);
How can I help you explore Laravel packages today?