Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP 5.3 support #20

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true

[*.php]
indent_style = space
indent_size = 4

[buddy.yml]
indent_style = space
indent_size = 2
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/tests export-ignore
/example export-ignore
phpunit.xml.dist export-ignore
phpunit.xml export-ignore
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![Latest Stable Version](https://img.shields.io/packagist/v/whitemerry/phpkin.svg?style=flat-square&label=stable "Latest Stable Version")](https://packagist.org/packages/whitemerry/phpkin) [![OpenTracing Badge](https://img.shields.io/badge/OpenTracing-enabled-blue.svg)](http://opentracing.io)
[![Maintainability](https://api.codeclimate.com/v1/badges/a785fa78ec069394b21d/maintainability)](https://codeclimate.com/github/whitemerry/phpkin/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/a785fa78ec069394b21d/test_coverage)](https://codeclimate.com/github/whitemerry/phpkin/test_coverage)
[![buddy pipeline](https://app.buddy.works/whitemerry/phpkin/pipelines/pipeline/204054/badge.svg?token=22731a0e88035e9b44c8ffbe93dfbff5f4dd2a91e4c5d0aada37eb55c43c461b "buddy pipeline")](https://app.buddy.works/whitemerry/phpkin/pipelines/pipeline/204054)

First ***production ready***, simple and full Zipkin implementation without dependencies.

Expand Down Expand Up @@ -161,12 +162,6 @@ For SimpleHttpLogger, short answer, ***yes***

For FileLogger, bit logner answer, you need to upload logs from *zipkin.log* to Zipkin by your own, for example by cron working in background making POST's to the [Zipkin (API)](http://zipkin.io/zipkin-api/#/paths/%252Fspans/post)

## Unit tests
Code Coverage (Generated by PHPUnit):
- Lines: 70.35% (140 / 199)
- Functions and Methods: 52.08% (25 / 48)
- Classes and Traits: 58.33% (7 / 12)

## TODO
- AsyncHttpLogger (Based on CURL)
- *Upload to zipkin* cron for FileLogger
Expand Down
4 changes: 2 additions & 2 deletions example/backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ Just start application using built-in php server (in this directory)
``` bash
php -S 127.0.0.1:1234
```
Now your aplication is available at [http://127.0.0.1:1234/](http://127.0.0.1:1234/)
Now your application is available at [http://127.0.0.1:1234/](http://127.0.0.1:1234/)

Make sure your Zipkin's application is up on `127.0.0.1:9411`.

If you have other ip/port just modify index.php at line 27.

### Are you getting error?
- Fatal error: Uncaught whitemerry\phpkin\Logger\LoggerException: Trace upload failed
* Zipkin's URL is invalid at line 27 in index.php
* Zipkin's URL is invalid at line 27 in index.php
10 changes: 5 additions & 5 deletions example/backend/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* Make sure host is available with http:// and port because SimpleHttpLogger does not throw error on failure
* For debug purposes you can disable muteErrors
*/
$logger = new SimpleHttpLogger(['host' => 'http://127.0.0.1:9411', 'muteErrors' => false]);
$logger = new SimpleHttpLogger(array('host' => 'http://127.0.0.1:9411', 'muteErrors' => false));

/**
* Read headers
Expand Down Expand Up @@ -60,16 +60,16 @@
$spanId = new SpanIdentifier();

// Context for file_get_contents for passing headers (B3 propagation)
$context = stream_context_create([
'http' => [
$context = stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' =>
'X-B3-TraceId: ' . TracerInfo::getTraceId() . "\r\n" .
'X-B3-SpanId: ' . ((string) $spanId) . "\r\n" .
'X-B3-ParentSpanId: ' . TracerInfo::getTraceSpanId() . "\r\n" .
'X-B3-Sampled: ' . ((int) TracerInfo::isSampled()) . "\r\n"
]
]);
)
));

$request = file_get_contents($url, false, $context);

Expand Down
4 changes: 2 additions & 2 deletions example/frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ Just start application using built-in php server (in this directory)
``` bash
php -S 127.0.0.1:1234
```
Now your aplication is available at [http://127.0.0.1:1234/](http://127.0.0.1:1234/)
Now your application is available at [http://127.0.0.1:1234/](http://127.0.0.1:1234/)

Make sure your Zipkin's application is up on `127.0.0.1:9411`.

If you have other ip/port just modify index.php at line 27.

### Are you getting error?
- Fatal error: Uncaught whitemerry\phpkin\Logger\LoggerException: Trace upload failed
* Zipkin's URL is invalid at line 27 in index.php
* Zipkin's URL is invalid at line 27 in index.php
10 changes: 5 additions & 5 deletions example/frontend/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* Make sure host is available with http:// and port because SimpleHttpLogger does not throw error on failure
* For debug purposes you can disable muteErrors
*/
$logger = new SimpleHttpLogger(['host' => 'http://127.0.0.1:9411', 'muteErrors' => false]);
$logger = new SimpleHttpLogger(array('host' => 'http://127.0.0.1:9411', 'muteErrors' => false));

/**
* And create tracer object, if you want to have statically access just initialize TracerProxy
Expand All @@ -40,16 +40,16 @@
$spanId = new SpanIdentifier();

// Context for file_get_contents for passing headers (B3 propagation)
$context = stream_context_create([
'http' => [
$context = stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' =>
'X-B3-TraceId: ' . TracerInfo::getTraceId() . "\r\n" .
'X-B3-SpanId: ' . ((string) $spanId) . "\r\n" .
'X-B3-ParentSpanId: ' . TracerInfo::getTraceSpanId() . "\r\n" .
'X-B3-Sampled: ' . ((int) TracerInfo::isSampled()) . "\r\n"
]
]);
)
));

$request = file_get_contents($url, false, $context);

Expand Down
16 changes: 8 additions & 8 deletions src/AnnotationBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ public function getStartTimestamp()
*/
public function toArray()
{
return [
[
return array(
array(
'endpoint' => $this->endpoint->toArray(),
'timestamp' => $this->startTimestamp,
'value' => $this->values[0]
],
[
),
array(
'endpoint' => $this->endpoint->toArray(),
'timestamp' => $this->endTimestamp,
'value' => $this->values[1]
]
];
)
);
}

/**
Expand All @@ -101,10 +101,10 @@ protected function setValues($type)
{
switch ($type) {
case static::CLIENT:
$this->values = ['cs', 'cr'];
$this->values = array('cs', 'cr');
break;
case static::SERVER:
$this->values = ['sr', 'ss'];
$this->values = array('sr', 'ss');
break;
default:
throw new \InvalidArgumentException('$type must be TYPE_CLIENT or TYPE_SERVER');
Expand Down
4 changes: 2 additions & 2 deletions src/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public function __construct($serviceName, $ip, $port = '80')
*/
public function toArray()
{
return [
return array(
'serviceName' => $this->serviceName,
'ipv4' => $this->ip,
'port' => $this->port
];
);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Logger/FileLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ class FileLogger implements Logger
* @param $options array See above
*
* @throws \BadMethodCallException
* @throws LoggerException
*/
public function __construct($options = [])
{
$defaults = [
$defaults = array(
'path' => 'tmp' . DIRECTORY_SEPARATOR,
'fileName' => 'zipkin.log'
];
);

$this->options = array_merge($defaults, $options);

Expand All @@ -50,4 +51,4 @@ public function trace($spans)
FILE_APPEND | LOCK_EX
);
}
}
}
4 changes: 2 additions & 2 deletions src/Logger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ interface Logger
*
* @param $options
*/
public function __construct($options = []);
public function __construct($options = array());

/**
* @param $spans array
*/
public function trace($spans);
}
}
18 changes: 11 additions & 7 deletions src/Logger/SimpleHttpLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,31 @@ class SimpleHttpLogger implements Logger
*/
public function __construct($options = [])
{
$defaults = [
$defaults = array(
'host' => 'http://127.0.0.1:9144',
'endpoint' => '/api/v1/spans',
'muteErrors' => true,
'contextOptions' => []
];
'contextOptions' => array()
);

$this->options = array_merge($defaults, $options);
}

/**
* @inheritdoc
*
* @throws LoggerException
*/
public function trace($spans)
{
$contextOptions = [
'http' => [
$contextOptions = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/json',
'content' => json_encode($spans),
'ignore_errors' => true
]
];
)
);
$context = stream_context_create(array_merge_recursive($contextOptions, $this->options['contextOptions']));
@file_get_contents($this->options['host'] . $this->options['endpoint'], false, $context);

Expand All @@ -66,6 +68,8 @@ public function trace($spans)
/**
* Search for 202 header
*
* @param $headers array
*
* @return bool
*/
protected function validResponse($headers)
Expand Down
6 changes: 3 additions & 3 deletions src/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Metadata
/**
* @var array[]
*/
protected $annotations = [];
protected $annotations = array();

/**
* Set meta annotation
Expand All @@ -88,10 +88,10 @@ public function set($key, $value)
throw new \InvalidArgumentException('$value must be string, int or bool');
}

$this->annotations[] = [
$this->annotations[] = array(
'key' => $key,
'value' => (string) $value
];
);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Sampler/PercentageSampler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class PercentageSampler implements Sampler
*
* @param $options array See above
*/
public function __construct($options = [])
public function __construct($options = array())
{
$defaults = [
$defaults = array(
'percents' => 50
];
);

$this->options = array_merge($defaults, $options);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Sampler/Sampler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ interface Sampler
*
* @param array $options
*/
public function __construct($options = []);
public function __construct($options = array());

/**
* Returns calculated flag
*
* @return bool
*/
public function isSampled();
}
}
12 changes: 7 additions & 5 deletions src/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Span
* @param $traceId Identifier Trace identifier (default from TraceInfo::getTraceId())
* @param $parentId Identifier Parent identifier (default from TraceInfo::getTraceSpanId())
*/
function __construct(
public function __construct(
$id,
$name,
$annotationBlock,
Expand All @@ -64,8 +64,10 @@ function __construct(
$this->setName($name);
$this->setAnnotationBlock($annotationBlock);
$this->setMetadata($metadata);
$this->setIdentifier('traceId', $traceId, [TracerInfo::class, 'getTraceId']);
$this->setIdentifier('parentId', $parentId, [TracerInfo::class, 'getTraceSpanId']);

# FIXME: Class name constant is available in PHP 5.5 only
$this->setIdentifier('traceId', $traceId, array(TracerInfo::class, 'getTraceId'));
$this->setIdentifier('parentId', $parentId, array(TracerInfo::class, 'getTraceSpanId'));
}

/**
Expand All @@ -75,14 +77,14 @@ function __construct(
*/
public function toArray()
{
$span = [
$span = array(
'id' => (string) $this->id,
'traceId' => (string) $this->traceId,
'name' => $this->name,
'timestamp' => $this->annotationBlock->getStartTimestamp(),
'duration' => $this->annotationBlock->getDuration(),
'annotations' => $this->annotationBlock->toArray()
];
);

if ($this->parentId !== null) {
$span['parentId'] = (string) $this->parentId;
Expand Down
2 changes: 1 addition & 1 deletion src/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Tracer
/**
* @var Span[]
*/
protected $spans = [];
protected $spans = array();

/**
* @var string
Expand Down
2 changes: 2 additions & 0 deletions src/TracerInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class TracerInfo
public static function init($sampler, $traceId, $traceSpanId)
{
static::setSampled($sampler);

# FIXME: Class name constant is available in PHP 5.5 only
static::setIdentifier('traceId', $traceId, TraceIdentifier::class);
static::setIdentifier('traceSpanId', $traceSpanId, SpanIdentifier::class);
}
Expand Down
2 changes: 2 additions & 0 deletions src/TracerProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public static function init($instance)

/**
* @see Tracer::addSpan($span)
*
* @param $span Span
*/
public static function addSpan($span)
{
Expand Down
Loading