Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
with:
php-version: ${{ matrix.php-versions }}
coverage: xdebug #optional
extensions: memcached
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
Expand Down Expand Up @@ -51,7 +52,9 @@ jobs:
- name: Run tests
run: composer test -- --coverage-clover=build/logs/clover.xml
- name: Upload coverage report to codecov service
if: matrix.operating-system == 'ubuntu-latest'
if: |
matrix.operating-system == 'ubuntu-latest' ||
env.GITHUB_REPOSITORY == 'openzipkin/zipkin-php'
run: |
php php-coveralls.phar -v
bash <(curl -s https://codecov.io/bash)
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ parameters:
# SpanCustomizer is definitively not null
message: '#Parameter \#3 \$ of callable callable\(.*, Zipkin\\Propagation\\TraceContext, Zipkin\\SpanCustomizer\): void expects Zipkin\\SpanCustomizer, Zipkin\\SpanCustomizerShield\|null given.#'
path: src/Zipkin/Tracer
excludePaths:
analyse:
- src/Zipkin/Reporters/Aggregation/MemcachedClient.php
- src/Zipkin/Reporters/Memcached.php
131 changes: 131 additions & 0 deletions src/Zipkin/Reporters/Aggregation/MemcachedClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

declare(strict_types=1);

namespace Zipkin\Reporters\Aggregation;

use Exception;

class MemcachedClient
{
const GET_EXTENDED = \Memcached::GET_EXTENDED;

const DEFAULT_OPTIONS = [
\Memcached::OPT_COMPRESSION => true,
];

/**
* @var \Memcached
*/
private $client;

/**
* @var string
*/
private $server;

/**
* @var int
*/
private $port;

/**
* @var int
*/
private $timeout;

/**
* @param string $server
* @param int $port
* @param int $timeout
* @param array $options
*/
public function __construct(
string $server = '127.0.0.1',
int $port = 11211,
int $timeout = 30,
array $options = []
) {
$this->server = $server;
$this->port = $port;
$this->timeout = $timeout;

if (!class_exists('\Memcached')) {
throw new Exception("PHP ext-memcached is required");
}

$this->client = new \Memcached();
$this->client->addServer($this->server, $this->port);

$options = \array_merge(self::DEFAULT_OPTIONS, $options);
foreach ($options as $key => $value) {
$this->client->setOption($key, $value);
}
}

/**
* Check connection
*
* @return bool
*/
public function ping(): bool
{
if (false === @fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout)) {
throw new Exception(sprintf(
"Unable to connect to memcached server {$this->server}:{$this->port}: %s",
$errstr
));
}

return true;
}

/**
* Set an item
*
* @param string $key
* @param mixed $value
* @param int $expiration
*/
public function set($key, $value, $expiration = 0): bool
{
return $this->client->set($key, $value, $expiration);
}

/**
* Get item by key.
*
* @param string $key
* @param mixed $cacheCallback
* @param int $flags
*
* @return mixed
*/
public function get($key, $cacheCallback = null, $flags = 0)
{
return $this->client->get($key, $cacheCallback, $flags);
}

/**
* Compare and swap an item.
*
* @param float $casToken
* @param string $key
* @param mixed $value
* @param int $expiration
*/
public function compareAndSwap($casToken, $key, $value, $expiration = 0): bool
{
return $this->client->cas($casToken, $key, $value, $expiration);
}

/**
* Quit all connections.
*
* @return bool
*/
public function quit(): bool
{
return $this->client->quit();
}
}
4 changes: 3 additions & 1 deletion src/Zipkin/Reporters/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __construct(
}

/**
* @param ReadbackSpan[] $spans
* @param ReadbackSpan[]|array $spans
* @return void
*/
public function report(array $spans): void
Expand All @@ -83,6 +83,7 @@ public function report(array $spans): void
}

$payload = $this->serializer->serialize($spans);

if ($payload === false) {
$this->logger->error(
\sprintf('failed to encode spans with code %d', \json_last_error())
Expand All @@ -91,6 +92,7 @@ public function report(array $spans): void
}

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

try {
$client($payload);
} catch (RuntimeException $e) {
Expand Down
Loading