Skip to content

Commit

Permalink
Merge pull request #67 from jcchavezs/adds_support_for_reporter_metrics
Browse files Browse the repository at this point in the history
Adds support for reporter metrics.
  • Loading branch information
jcchavezs authored Mar 7, 2018
2 parents 17caf54 + ac215e7 commit c5fed92
Show file tree
Hide file tree
Showing 10 changed files with 457 additions and 9 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ php:
- '5.6'
- '7.0'
- '7.1'
- '7.2'

install:
- composer install
Expand Down
28 changes: 23 additions & 5 deletions src/Zipkin/Reporters/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Zipkin\Reporters;

use Exception;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use RuntimeException;
use Zipkin\Recording\Span;
use Zipkin\Reporter;
use Zipkin\Reporters\Http\ClientFactory;
Expand All @@ -26,12 +24,19 @@ final class Http implements Reporter
*/
private $options;

/**
* @var Metrics
*/
private $reportMetrics;

public function __construct(
ClientFactory $requesterFactory = null,
array $options = []
array $options = [],
Metrics $reporterMetrics = null
) {
$this->clientFactory = $requesterFactory ?: CurlFactory::create();
$this->options = array_merge(self::DEFAULT_OPTIONS, $options);
$this->reportMetrics = $reporterMetrics;
}

/**
Expand All @@ -44,7 +49,20 @@ public function report(array $spans)
return $span->toArray();
}, $spans));

$this->reportMetrics->incrementSpans(count($spans));
$this->reportMetrics->incrementMessages();

$payloadLength = strlen($payload);
$this->reportMetrics->incrementSpanBytes($payloadLength);
$this->reportMetrics->incrementMessageBytes($payloadLength);

$client = $this->clientFactory->build($this->options);
$client($payload);

try {
$client($payload);
} catch (RuntimeException $e) {
$this->reportMetrics->incrementSpansDropped(count($spans));
$this->reportMetrics->incrementMessagesDropped($e);
}
}
}
150 changes: 150 additions & 0 deletions src/Zipkin/Reporters/InMemoryMetrics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

namespace Zipkin\Reporters;

final class InMemoryMetrics implements Metrics
{
/**
* @var int
*/
private $spansCount = 0;

/**
* @var int
*/
private $spanBytesCount = 0;

/**
* @var int
*/
private $spansDroppedCount = 0;

/**
* @var int
*/
private $messagesCount = 0;

/**
* @var int
*/
private $messagesDroppedCount = 0;

/**
* @var int
*/
private $messageBytes = 0;

/**
* @var int
*/
private $queuedSpans = 0;

/**
* @var int
*/
private $queuedBytes = 0;

/**
* {@inheritdoc}
*/
public function incrementSpans($quantity)
{
$this->spansCount += $quantity;
}

public function getSpans()
{
return $this->spansCount;
}

/**
* {@inheritdoc}
*/
public function incrementSpansDropped($quantity)
{
$this->spansDroppedCount += $quantity;
}

public function getSpansDropped()
{
return $this->spansDroppedCount;
}

/**
* {@inheritdoc}
*/
public function incrementSpanBytes($quantity)
{
$this->spanBytesCount += $quantity;
}

public function getSpanBytes()
{
return $this->spanBytesCount;
}

/**
* {@inheritdoc}
*/
public function incrementMessages()
{
$this->messagesCount++;
}

public function getMessages()
{
return $this->messagesCount;
}

/**
* {@inheritdoc}
*/
public function incrementMessagesDropped($cause)
{
$this->messagesDroppedCount++;
}

public function getMessagesDropped()
{
return $this->messagesDroppedCount;
}

/**
* {@inheritdoc}
*/
public function incrementMessageBytes($quantity)
{
$this->messageBytes += $quantity;
}

public function getMessageBytes()
{
return $this->messageBytes;
}

/**
* {@inheritdoc}
*/
public function updateQueuedSpans($update)
{
$this->queuedSpans = $update;
}

public function getQueuedSpans()
{
return $this->queuedSpans;
}

/**
* {@inheritdoc}
*/
public function updateQueuedBytes($update)
{
$this->queuedBytes = $update;
}

public function getQueuedBytes()
{
return $this->queuedBytes;
}
}
99 changes: 99 additions & 0 deletions src/Zipkin/Reporters/Metrics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Zipkin\Reporters;

/**
* Instrumented applications report spans over a transport such as Kafka to Zipkin Collectors.
*
* <p>Callbacks on this type are invoked by zipkin reporters to improve the visibility of the
* system. A typical implementation will report metrics to a telemetry system for analysis and
* reporting.</p>
*
* <h3>Spans Reported vs Queryable Spans</h3>
*
* <p>A span in the context of reporting is <= span in the context of query. Instrumentation should
* report a span only once except, but certain types of spans cross the network. For example, RPC
* spans are reported at the client and the server separately.</p>
*
* <h3>Key Relationships</h3>
*
* <p>The following relationships can be used to consider health of the tracing system.
* <pre>
* <ul>
* <li>{@link #updateQueuedSpans Pending spans}. Alert when this increases over time as it could
* lead to dropped spans.
* <li>Dropped spans = Alert when this increases as it could indicate a queue backup.
* <li>Successful Messages = {@link #incrementMessages() Accepted messages} -
* {@link #incrementMessagesDropped Dropped messages}. Alert when this is more than amount of
* messages received from collectors.</li>
* </li>
* </ul>
* </pre>
*/
interface Metrics
{
/**
* Increments count of message attempts, which contain 1 or more spans. Ex POST requests or Kafka
* messages sent.
*/
public function incrementMessages();

/**
* Increments count of messages that could not be sent. Ex host unavailable, or peer disconnect.
* @param \Throwable|\Exception $cause
* @return void
*/
public function incrementMessagesDropped($cause);

/**
* Increments the count of spans reported. When {@link AsyncReporter} is used, reported spans will
* usually be a larger number than messages.
*
* @param int $quantity
* @return void
*/
public function incrementSpans($quantity);

/**
* Increments the number of encoded span bytes reported.
* @param int $quantity
* @return void
*/
public function incrementSpanBytes($quantity);

/**
* Increments the number of bytes containing encoded spans in a message.
*
* <p>This is a function of span bytes per message and overhead</p>
*
* @see Sender#messageSizeInBytes
* @param $quantity
* @return void
*/
public function incrementMessageBytes($quantity);

/**
* Increments the count of spans dropped for any reason. For example, failure queueing or
* sending.
*
* @param int $quantity
* @return void
*/
public function incrementSpansDropped($quantity);

/**
* Updates the count of spans pending, following a flush activity.
*
* @param int $update
* @return void
*/
public function updateQueuedSpans($update);

/**
* Updates the count of encoded span bytes pending, following a flush activity.
*
* @param int $update
* @return void
*/
public function updateQueuedBytes($update);
}
62 changes: 62 additions & 0 deletions src/Zipkin/Reporters/NoopMetrics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Zipkin\Reporters;

final class NoopMetrics implements Metrics
{
/**
* {@inheritdoc}
*/
public function incrementSpans($quantity)
{
}

/**
* {@inheritdoc}
*/
public function incrementSpansDropped($quantity)
{
}

/**
* {@inheritdoc}
*/
public function incrementMessages()
{
}

/**
* {@inheritdoc}
*/
public function incrementMessagesDropped($cause)
{
}

/**
* {@inheritdoc}
*/
public function incrementSpanBytes($quantity)
{
}

/**
* {@inheritdoc}
*/
public function incrementMessageBytes($quantity)
{
}

/**
* {@inheritdoc}
*/
public function updateQueuedSpans($update)
{
}

/**
* {@inheritdoc}
*/
public function updateQueuedBytes($update)
{
}
}
Loading

0 comments on commit c5fed92

Please sign in to comment.