-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement TracerFactory, begin migration to recommended library layout, use docker-compose and make to simplify development * add tests for all TracerFactory method, move TraceFactory configuration in constructor * run phan in travis-ci build * rename namespace Tracing into Trace
- Loading branch information
1 parent
cf85f7d
commit 0991f72
Showing
24 changed files
with
254 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<ruleset name="PSR2 extended"> | ||
<rule ref="PSR2" /> | ||
<rule ref="Squiz.Strings.DoubleQuoteUsage.NotRequired" /> | ||
<rule ref="Generic.Arrays.DisallowLongArraySyntax" /> | ||
<rule ref="PEAR.ControlStructures.MultiLineCondition" /> | ||
</ruleset> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
DC_RUN_PHP = docker-compose run php | ||
|
||
install: | ||
$(DC_RUN_PHP) composer install | ||
test: | ||
$(DC_RUN_PHP) php ./vendor/bin/phpunit --colors=always | ||
phan: | ||
$(DC_RUN_PHP) php ./vendor/bin/phan | ||
examples: FORCE | ||
$(DC_RUN_PHP) php ./examples/AlwaysOnTraceExample.php | ||
bash: | ||
$(DC_RUN_PHP) bash | ||
FORCE: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
version: '3.7' | ||
services: | ||
php: | ||
build: | ||
context: . | ||
dockerfile: docker/Dockerfile | ||
volumes: | ||
- ./:/usr/src/myapp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM php:7.1-buster | ||
|
||
RUN apt-get -y update && apt-get -y install git zip && \ | ||
curl -sS https://getcomposer.org/installer | php && \ | ||
mv composer.phar /usr/local/bin/composer && \ | ||
chmod +x /usr/local/bin/composer && \ | ||
|
||
pecl install ast-1.0.4 && \ | ||
echo "extension=ast.so" >> /usr/local/etc/php/conf.d/docker-php-ext-ast.ini | ||
|
||
WORKDIR /usr/src/myapp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,29 @@ | ||
<?php | ||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use OpenTelemetry\Tracing\SpanContext; | ||
use OpenTelemetry\Tracing\Sampler\AlwaysOnSampler; | ||
use OpenTelemetry\Tracing\Tracer; | ||
use OpenTelemetry\Trace\TracerFactory; | ||
use OpenTelemetry\Trace\Sampler\AlwaysOnSampler; | ||
|
||
$sampler = AlwaysOnSampler::shouldSample(); | ||
if ($sampler) { | ||
$spanContext = SpanContext::generate(); // or extract from headers | ||
$tracer = new Tracer($spanContext); | ||
$tracer = (TracerFactory::getInstance()) | ||
->getTracer('io.opentelemetry.contrib.php'); | ||
|
||
// start a span, register some events | ||
$span = $tracer->createSpan('session.generate'); | ||
$span->setAttributes(['remote_ip' => '1.2.3.4']); | ||
$span->setAttribute('country', 'USA'); | ||
// start a span, register some events | ||
$span = $tracer->createSpan('session.generate'); | ||
$span->setAttributes(['remote_ip' => '1.2.3.4']); | ||
$span->setAttribute('country', 'USA'); | ||
|
||
$span->addEvent('found_login', [ | ||
$span->addEvent('found_login', [ | ||
'id' => 12345, | ||
'username' => 'otuser', | ||
]); | ||
$span->addEvent('generated_session', [ | ||
]); | ||
$span->addEvent('generated_session', [ | ||
'id' => md5(microtime(true)) | ||
]); | ||
]); | ||
|
||
$span->end(); // pass status as an optional argument | ||
print_r($span); // print the span as a resulting output | ||
$span->end(); // pass status as an optional argument | ||
print_r($span); // print the span as a resulting output | ||
} else { | ||
echo "Sampling is not enabled"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Tracing/Sampler/AlwaysOffSampler.php → src/Trace/Sampler/AlwaysOffSampler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Tracing/Sampler/AlwaysOnSampler.php → src/Trace/Sampler/AlwaysOnSampler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Tracing/Sampler/SamplerInterface.php → src/Trace/Sampler/SamplerInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/SpanProcessorInterface.php → src/Trace/SpanProcessorInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace OpenTelemetry\Trace; | ||
|
||
use InvalidArgumentException; | ||
use OpenTelemetry\Context\SpanContext; | ||
use OpenTelemetry\Trace\SpanProcessorInterface; | ||
|
||
class TracerFactory | ||
{ | ||
/** | ||
* @var self | ||
*/ | ||
protected static $instance; | ||
|
||
/** | ||
* @var Tracer[] | ||
*/ | ||
protected $tracers; | ||
|
||
/** | ||
* @var SpanProcessorInterface[] | ||
*/ | ||
protected $spanProcessors; | ||
|
||
/** | ||
* TracerFactory constructor. | ||
* | ||
* @param SpanProcessorInterface[] $spanProcessors | ||
*/ | ||
final private function __construct(array $spanProcessors = []) | ||
{ | ||
foreach ($spanProcessors as $spanProcessor) { | ||
if (!$spanProcessor instanceof SpanProcessorInterface) { | ||
throw new InvalidArgumentException( | ||
sprintf( | ||
"Span Processors should be of type %s, but object of type %s provided", | ||
SpanProcessorInterface::class, | ||
gettype($spanProcessor) == "object" ? get_class($spanProcessor) : gettype($spanProcessor) | ||
) | ||
); | ||
} | ||
} | ||
|
||
$this->spanProcessors = $spanProcessors; | ||
} | ||
|
||
/** | ||
* @param SpanProcessorInterface[] $spanProcessors | ||
* | ||
* @return static | ||
*/ | ||
public static function getInstance(array $spanProcessors = []): self | ||
{ | ||
if (self::$instance instanceof TracerFactory) { | ||
return self::$instance; | ||
} | ||
|
||
$instance = new TracerFactory($spanProcessors); | ||
|
||
return self::$instance = $instance; | ||
} | ||
|
||
public function getTracer(string $name, string $version = ""): Tracer | ||
{ | ||
|
||
if ($this->tracers[$name] instanceof Tracer) { | ||
return $this->tracers[$name]; | ||
} | ||
|
||
$spanContext = SpanContext::generate(); | ||
return $this->tracers[$name] = new Tracer($spanContext); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.