Skip to content

Commit

Permalink
Merge pull request #274 from adri/add-distributed-tracing-methods
Browse files Browse the repository at this point in the history
Adds distributed tracing methods
  • Loading branch information
jderusse authored Feb 8, 2022
2 parents 2af8555 + 24e7439 commit 106f948
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
45 changes: 45 additions & 0 deletions NewRelic/AdaptiveInteractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
23 changes: 23 additions & 0 deletions NewRelic/BlackholeInteractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
}
44 changes: 44 additions & 0 deletions NewRelic/LoggingInteractorDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
45 changes: 45 additions & 0 deletions NewRelic/NewRelicInteractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
6 changes: 6 additions & 0 deletions NewRelic/NewRelicInteractorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 106f948

Please sign in to comment.