diff --git a/CHANGELOG.md b/CHANGELOG.md index 33a2c44..4a9a624 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## v2.4.0 + +### Added + +- Support for distributed tracing functions provided by the NewRelic PHP extension are now supported in the `NewRelicInteractorInterface` + ## v2.3.0 ### Added diff --git a/NewRelic/AdaptiveInteractor.php b/NewRelic/AdaptiveInteractor.php index f4804d4..a9606d9 100644 --- a/NewRelic/AdaptiveInteractor.php +++ b/NewRelic/AdaptiveInteractor.php @@ -139,4 +139,49 @@ public function setUserAttributes(string $userValue, string $accountValue, strin { return $this->interactor->setUserAttributes($userValue, $accountValue, $productValue); } + + public function getTraceMetadata(): array + { + if (!method_exists($this->interactor, 'getTraceMetadata')) { + throw new \BadMethodCallException('The decorated interaction does not implement this method'); + } + + return $this->interactor->getTraceMetadata(); + } + + public function getLinkingMetadata(): array + { + if (!method_exists($this->interactor, 'getLinkingMetadata')) { + throw new \BadMethodCallException('The decorated interaction does not implement this method'); + } + + return $this->interactor->getLinkingMetadata(); + } + + public function isSampled(): bool + { + if (!method_exists($this->interactor, 'isSampled')) { + throw new \BadMethodCallException('The decorated interaction does not implement this method'); + } + + return $this->interactor->isSampled(); + } + + public function insertDistributedTracingHeaders(array $headers): void + { + if (!method_exists($this->interactor, 'insertDistributedTracingHeaders')) { + throw new \BadMethodCallException('The decorated interaction does not implement this method'); + } + + $this->interactor->insertDistributedTracingHeaders($headers); + } + + public function acceptDistributedTraceHeaders(array $headers, string $transportType = 'HTTP'): void + { + if (!method_exists($this->interactor, 'acceptDistributedTraceHeaders')) { + throw new \BadMethodCallException('The decorated interaction does not implement this method'); + } + + $this->interactor->acceptDistributedTraceHeaders($headers, $transportType); + } } diff --git a/NewRelic/BlackholeInteractor.php b/NewRelic/BlackholeInteractor.php index b0ecfee..0f91653 100644 --- a/NewRelic/BlackholeInteractor.php +++ b/NewRelic/BlackholeInteractor.php @@ -120,4 +120,27 @@ public function setUserAttributes(string $userValue, string $accountValue, strin { return true; } + + public function getTraceMetadata(): array + { + return []; + } + + public function getLinkingMetadata(): array + { + return []; + } + + public function isSampled(): bool + { + return true; + } + + public function insertDistributedTracingHeaders(array $headers): void + { + } + + public function acceptDistributedTraceHeaders(array $headers, string $transportType = 'HTTP'): void + { + } } diff --git a/NewRelic/LoggingInteractorDecorator.php b/NewRelic/LoggingInteractorDecorator.php index 1dd0a29..0e08b09 100644 --- a/NewRelic/LoggingInteractorDecorator.php +++ b/NewRelic/LoggingInteractorDecorator.php @@ -184,4 +184,48 @@ public function setUserAttributes(string $userValue, string $accountValue, strin return $this->interactor->setUserAttributes($userValue, $accountValue, $productValue); } + + public function getTraceMetadata(): array + { + $traceMetadata = $this->interactor->getTraceMetadata(); + + $this->logger->debug('Getting New Relic trace metadata', $traceMetadata); + + return $traceMetadata; + } + + public function getLinkingMetadata(): array + { + $linkingMetadata = $this->interactor->getLinkingMetadata(); + + $this->logger->debug('Getting New Relic linking metadata', $linkingMetadata); + + return $linkingMetadata; + } + + public function isSampled(): bool + { + $isSampled = $this->interactor->isSampled(); + + $this->logger->debug('Getting New Relic sampled status', ['sampled' => $isSampled]); + + return $isSampled; + } + + public function insertDistributedTracingHeaders(array $headers): void + { + $this->logger->debug('Setting New Relic distributed tracing headers', ['headers' => $headers]); + + $this->interactor->insertDistributedTracingHeaders($headers); + } + + public function acceptDistributedTraceHeaders(array $headers, string $transportType = 'HTTP'): void + { + $this->logger->debug('Accepting New Relic distributed tracing headers', [ + 'headers' => $headers, + 'transport_type' => $transportType, + ]); + + $this->interactor->acceptDistributedTraceHeaders($headers, $transportType); + } } diff --git a/NewRelic/NewRelicInteractor.php b/NewRelic/NewRelicInteractor.php index a2a5ada..4669c03 100644 --- a/NewRelic/NewRelicInteractor.php +++ b/NewRelic/NewRelicInteractor.php @@ -127,4 +127,49 @@ public function setUserAttributes(string $userValue, string $accountValue, strin { return newrelic_set_user_attributes($userValue, $accountValue, $productValue); } + + public function getTraceMetadata(): array + { + if (!function_exists('newrelic_get_trace_metadata')) { + throw new \BadMethodCallException('You need the "newrelic" extension version 9.3 or higher to use this method'); + } + + return newrelic_get_trace_metadata(); + } + + public function getLinkingMetadata(): array + { + if (!function_exists('newrelic_get_linking_metadata')) { + throw new \BadMethodCallException('You need the "newrelic" extension version 9.3 or higher to use this method'); + } + + return newrelic_get_linking_metadata(); + } + + public function isSampled(): bool + { + if (!function_exists('newrelic_is_sampled')) { + throw new \BadMethodCallException('You need the "newrelic" extension version 9.3 or higher to use this method'); + } + + return newrelic_is_sampled(); + } + + public function insertDistributedTracingHeaders(array $headers): void + { + if (!function_exists('newrelic_insert_distributed_trace_headers')) { + throw new \BadMethodCallException('You need the "newrelic" extension version 9.8 or higher to use this method'); + } + + newrelic_insert_distributed_trace_headers($headers); + } + + public function acceptDistributedTraceHeaders(array $headers, string $transportType = 'HTTP'): void + { + if (!function_exists('newrelic_accept_distributed_trace_headers')) { + throw new \BadMethodCallException('You need the "newrelic" extension version 9.8 or higher to use this method'); + } + + newrelic_accept_distributed_trace_headers($headers, $transportType); + } } diff --git a/NewRelic/NewRelicInteractorInterface.php b/NewRelic/NewRelicInteractorInterface.php index 63bf286..c6ecc86 100644 --- a/NewRelic/NewRelicInteractorInterface.php +++ b/NewRelic/NewRelicInteractorInterface.php @@ -15,6 +15,12 @@ /** * This is the service that talks to NewRelic. + * + * @method array getTraceMetadata() + * @method array getLinkingMetadata() + * @method bool isSampled() + * @method void insertDistributedTracingHeaders(array $headers) + * @method void acceptDistributedTraceHeaders(array $headers, string $transportType = 'HTTP') */ interface NewRelicInteractorInterface {