Skip to content

Commit

Permalink
[Fixes #24] Correctly set tags for RPC calls
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahgeorge committed May 17, 2018
1 parent af73083 commit 39c1132
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
42 changes: 37 additions & 5 deletions src/Jaeger/LocalAgentSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
use Jaeger\ThriftGen\BinaryAnnotation;
use Jaeger\ThriftGen\Endpoint;
use Jaeger\ThriftGen\Span;
use const OpenTracing\Ext\Tags\COMPONENT;
use Thrift\Protocol\TCompactProtocol;
use Thrift\Transport\TBufferedTransport;
use Thrift\Transport\TSocket;

class LocalAgentSender
{
const CLIENT_ADDR = "ca";
const SERVER_ADDR = "sa";

/** @var Span[] */
private $spans = [];

Expand Down Expand Up @@ -131,12 +135,28 @@ private function makeZipkinBatch(array $spans): array

private function addZipkinAnnotations(\Jaeger\Span $span, $endpoint)
{
$tag = $this->makeLocalComponentTag(
$span->getComponent() ?? $span->getTracer()->getServiceName(),
$endpoint
);
if ($span->isRpc()) {
$isClient = $span->isRpcClient();

if ($span->peer) {
$host = $this->makeEndpoint(
$span->peer['ipv4'] ?? 0,
$span->peer['port'] ?? 0,
$span->peer['service_name'] ?? '');

$key = ($isClient) ? self::SERVER_ADDR : self::CLIENT_ADDR;

$peer = $this->makePeerAddressTag($key, $host);
$span->tags[$key] = $peer;
}
} else {
$tag = $this->makeLocalComponentTag(
$span->getComponent() ?? $span->getTracer()->getServiceName(),
$endpoint
);

$span->tags[] = $tag;
$span->tags[COMPONENT] = $tag;
}
}

private function makeLocalComponentTag(string $componentName, $endpoint): BinaryAnnotation
Expand Down Expand Up @@ -170,4 +190,16 @@ private function ipv4ToInt($ipv4): int

return ip2long($ipv4);
}

// Used for Zipkin binary annotations like CA/SA (client/server address).
// They are modeled as Boolean type with '0x01' as the value.
private function makePeerAddressTag($key, $host)
{
return new BinaryAnnotation([
"key" => $key,
"value" => '0x01',
"annotation_type" => AnnotationType::BOOL,
"host" => $host
]);
}
}
20 changes: 17 additions & 3 deletions src/Jaeger/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ class Span implements OpenTracing\Span
/** @var float */
private $endTime;

/**
* SPAN_RPC_CLIENT
* @var null|string
*/
private $kind;

/** @var array|null */
private $peer;
public $peer;

private $component;

Expand Down Expand Up @@ -182,9 +186,9 @@ public function setTag($key, $value): Span

if ($this->isSampled()) {
$special = self::SPECIAL_TAGS[$key] ?? null;
$handled = False;
$handled = false;

if ($special !== null && is_callable($special)) {
if ($special !== null && is_callable([$this, $special])) {
$handled = $this->$special($value);
}

Expand Down Expand Up @@ -250,6 +254,16 @@ private function setPeerService($value): bool
return true;
}

public function isRpc(): bool
{
return $this->kind == SPAN_KIND_RPC_CLIENT || $this->kind == SPAN_KIND_RPC_SERVER;
}

public function isRpcClient(): bool
{
return $this->kind == SPAN_KIND_RPC_CLIENT;
}

/**
* Adds a log record to the span
*
Expand Down
4 changes: 2 additions & 2 deletions src/Jaeger/SpanContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class SpanContext implements OpenTracing\SpanContext
private $baggage;
private $debugId;

public function __construct($traceId, $spanId, $parentId, $flags, $baggage = null)
public function __construct($traceId, $spanId, $parentId, $flags, $baggage = [])
{
$this->traceId = $traceId;
$this->spanId = $spanId;
$this->parentId = $parentId;
$this->flags = $flags;
$this->baggage = $baggage ?? [];
$this->baggage = $baggage;
$this->debugId = null;
}

Expand Down

0 comments on commit 39c1132

Please sign in to comment.