Skip to content
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

Add php-amqplib integration #1996

Merged
merged 32 commits into from
Apr 21, 2023
Merged

Add php-amqplib integration #1996

merged 32 commits into from
Apr 21, 2023

Conversation

PROFeNoM
Copy link
Contributor

Description

Add a php-amqplib integration (#1840).

Methods Traced

AMQPChannel

  • PhpAmqpLib\Channel\AMQPChannel::basic_get
  • PhpAmqpLib\Channel\AMQPChannel::basic_deliver
  • PhpAmqpLib\Channel\AMQPChannel::basic_publish
  • PhpAmqpLib\Channel\AMQPChannel::basic_cancel
  • PhpAmqpLib\Channel\AMQPChannel::basic_cancel_ok
  • PhpAmqpLib\Channel\AMQPChannel::basic_consume
  • PhpAmqpLib\Channel\AMQPChannel::basic_consume_ok
  • PhpAmqpLib\Channel\AMQPChannel::basic_ack
  • PhpAmqpLib\Channel\AMQPChannel::basic_nack
  • PhpAmqpLib\Channel\AMQPChannel::exchange_declare
  • PhpAmqpLib\Channel\AMQPChannel::queue_declare
  • PhpAmqpLib\Channel\AMQPChannel::queue_bind

AbstractConnection

  • PhpAmqpLib\Connection\AbstractConnection::connect
  • PhpAmqpLib\Connection\AbstractConnection::reconnect

Readiness checklist

  • (only for Members) Changelog has been added to the release document.
  • Tests added for this feature/bug.

Reviewer checklist

  • Appropriate labels assigned.
  • Milestone is set.
  • Changelog has been added to the release document. For community contributors the reviewer is in charge of this task.

@PROFeNoM PROFeNoM requested a review from a team as a code owner March 31, 2023 15:10
@PROFeNoM PROFeNoM self-assigned this Mar 31, 2023
@PROFeNoM PROFeNoM added the 🎉 new-integration A new integration label Mar 31, 2023
Copy link
Collaborator

@pierotibou pierotibou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking great, thanks. A quick review from my phone. I'll have another look on Monday

src/Integrations/Integrations/AMQP/AMQPIntegration.php Outdated Show resolved Hide resolved
$integration->setOptionalMessageTags($span, $message);

// Try to extract propagated context values from headers
$integration->extract($message);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extractContext may be a better name

'prehook' => function (SpanData $span, $args) use ($integration) {
/** @var AMQPMessage $message */
$message = $args[0];
$integration->inject($message);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

injectContext

/** @var string $routing_key */
$routingKey = $args[2] ?? '';

$exchangeDisplayName = empty($exchange) ? '<default>' : $exchange;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we factorize duplicated code?

@PROFeNoM
Copy link
Contributor Author

PROFeNoM commented Apr 3, 2023

Ok, applied these changes. What's more, I also unmarked as 'skipped' the distributed tracing test so it can run on the CI (where it passes).

@PROFeNoM PROFeNoM added this to the 0.87.0 milestone Apr 11, 2023
Comment on lines 385 to 406
// If present, set the traceid of the current span to x-datadog-trace-id
if (isset($headers['x-datadog-trace-id'])) {
$traceId = $headers['x-datadog-trace-id'];
} else {
return;
}

// If present, set the spanid of the current span to x-datadog-parent-id
if (isset($headers['x-datadog-parent-id'])) {
$parentId = $headers['x-datadog-parent-id'];
} else {
return;
}

// If present, set the sampling priority of the current span to x-datadog-sampling-priority
$priority = $headers['x-datadog-sampling-priority'] ?? null;
\DDTrace\set_priority_sampling($priority);

// If present, set the origin of the current span to x-datadog-origin
$origin = $headers['x-datadog-origin'] ?? null;

\DDTrace\set_distributed_tracing_context($traceId, $parentId, $origin);
Copy link
Collaborator

@bwoebi bwoebi Apr 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// If present, set the traceid of the current span to x-datadog-trace-id
if (isset($headers['x-datadog-trace-id'])) {
$traceId = $headers['x-datadog-trace-id'];
} else {
return;
}
// If present, set the spanid of the current span to x-datadog-parent-id
if (isset($headers['x-datadog-parent-id'])) {
$parentId = $headers['x-datadog-parent-id'];
} else {
return;
}
// If present, set the sampling priority of the current span to x-datadog-sampling-priority
$priority = $headers['x-datadog-sampling-priority'] ?? null;
\DDTrace\set_priority_sampling($priority);
// If present, set the origin of the current span to x-datadog-origin
$origin = $headers['x-datadog-origin'] ?? null;
\DDTrace\set_distributed_tracing_context($traceId, $parentId, $origin);
\DDTrace\consume_distributed_tracing_headers($headers);

(Merge master into this first.)

}
}

public function injectContext(AMQPMessage $message)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a check for DD_DISTRIBUTED_TRACING config first.

Comment on lines 332 to 334
public function setGenericTags(SpanData $span, string $name, string $spanKind, $exception = null)
{
$span->name = $name;
Copy link
Collaborator

@bwoebi bwoebi Apr 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function setGenericTags(SpanData $span, string $name, string $spanKind, $exception = null)
{
$span->name = $name;
public function setGenericTags(SpanData $span, string $name, string $spanKind, string $resourceDetail = null, $exception = null)
{
$span->name = "amqp.$name";
$span->resource = "$name" . ($resourceDetail === null ? "" : " $resourceDetail");

Wouldn't that reduce redundancy? Given that name and resource always share that part.

Comment on lines 288 to 293
$exchangeDisplayName = empty($exchange) ? '<default>' : $exchange;
$routingKeyDisplayName = empty($routingKey)
? '<all>'
: (str_starts_with($routingKey, 'amq.gen-')
? '<generated>'
: $routingKey);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$exchangeDisplayName = empty($exchange) ? '<default>' : $exchange;
$routingKeyDisplayName = empty($routingKey)
? '<all>'
: (str_starts_with($routingKey, 'amq.gen-')
? '<generated>'
: $routingKey);
$exchangeDisplayName = $this->formatExchangeName($exchange);
$routingKeyDisplayName = $this->formatRoutingKey($routingKey);

Comment on lines 327 to 329
: (str_starts_with($routingKey, 'amq.gen-')
? '<generated>'
: $routingKey);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
: (str_starts_with($routingKey, 'amq.gen-')
? '<generated>'
: $routingKey);
: $this->formatQueueName($routingKey);

# Conflicts:
#	ext/ddtrace.stub.php
public function testDistributedTracing()
{
// Note: This test is extremely flaky, locally at least. It will eventually pass with some tries...
// Reason: We may parse the traces from dumped data BEFORE the traces are flushed.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate? Also \DDTrace\Tests\Common\TracerTestTrait::resetRequestDumper() is a thing, if needed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me it was never flaky, so, let's ship it.

@PROFeNoM PROFeNoM merged commit 4504bbf into master Apr 21, 2023
@PROFeNoM PROFeNoM deleted the alex/integration/rabbitmq branch April 21, 2023 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants