From 435f13b3b584f5e7d1605e80978ea8ec7f57b889 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Ch=C3=A1vez?= Date: Sat, 20 Jun 2020 12:06:35 +0200 Subject: [PATCH] chore: uses HTTP instrumentation from Zipkin PHP. --- backend.php | 15 +++++++++++++-- composer.json | 9 +++++---- frontend.php | 30 +++++++++++------------------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/backend.php b/backend.php index 216d23b..9027d09 100644 --- a/backend.php +++ b/backend.php @@ -1,6 +1,7 @@ headers->all()); @@ -19,20 +22,28 @@ /* Get users from DB */ $tracer = $tracing->getTracer(); -$span = $tracer->nextSpan($extractedContext); + +$span = $extractedContext instanceof TraceContext + ? $tracer->joinSpan($extractedContext) + : $tracer->nextSpan($extractedContext); + $span->start(); $span->setKind(Zipkin\Kind\SERVER); $span->setName('parse_request'); +usleep(1000 * mt_rand(1, 3)); $childSpan = $tracer->newChild($span->getContext()); $childSpan->start(); $childSpan->setKind(Zipkin\Kind\CLIENT); $childSpan->setName('user:get_list:mysql_query'); +$childSpan->tag("sql.query", "SELECT * FROM users LIMIT 10"); -usleep(50000); +usleep(30000); $childSpan->finish(); +usleep(1000 * mt_rand(1, 3)); + $span->finish(); /* Sends the trace to zipkin once the response is served */ diff --git a/composer.json b/composer.json index 2ad7242..39be3ec 100644 --- a/composer.json +++ b/composer.json @@ -2,9 +2,10 @@ "name": "openzipkin/zipkin-php-example", "require": { "monolog/monolog": "^1.23", - "guzzlehttp/guzzle": "^6.3", - "openzipkin/zipkin": "^1.2.2", - "symfony/http-foundation": "3.4.x-dev" + "guzzlehttp/guzzle": "7.0.0-RC.1", + "openzipkin/zipkin": "dev-http_tracing", + "symfony/http-foundation": "3.4.x-dev", + "psr/http-client": "^1.0" }, "minimum-stability": "stable", "authors": [ @@ -18,4 +19,4 @@ "run-backend": "php -S 'localhost:9000' backend.php", "run-zipkin": "docker run -p 9411:9411 -d openzipkin/zipkin" } -} +} \ No newline at end of file diff --git a/frontend.php b/frontend.php index b59a053..c92a4cd 100644 --- a/frontend.php +++ b/frontend.php @@ -2,8 +2,8 @@ use GuzzleHttp\Client; use Zipkin\Propagation\DefaultSamplingFlags; -use Zipkin\Propagation\Map; use Zipkin\Timestamp; +use Zipkin\Instrumentation\Http\Client\Client as ZipkinClient; require_once __DIR__ . '/vendor/autoload.php'; require_once __DIR__ . '/functions.php'; @@ -21,30 +21,22 @@ $span->setName('parse_request'); $span->setKind(Zipkin\Kind\SERVER); -usleep(100 * mt_rand(1, 3)); - -/* Creates the span for getting the users list */ -$childSpan = $tracer->newChild($span->getContext()); -$childSpan->start(); -$childSpan->setKind(Zipkin\Kind\CLIENT); -$childSpan->setName('users:get_list'); +// We need to open a scope so the http client can retrieve the current +// context from it. +$scopeCloser = $tracer->openScope($span); -$headers = []; +usleep(100 * mt_rand(1, 3)); -/* Injects the context into the wire */ -$injector = $tracing->getPropagation()->getInjector(new Map()); -$injector($childSpan->getContext(), $headers); +$httpClient = new ZipkinClient(new Client, $tracing); +$request = new \GuzzleHttp\Psr7\Request('POST', 'localhost:9000'); +$response = $httpClient->sendRequest($request); -/* HTTP Request to the backend */ -$httpClient = new Client(); -$request = new \GuzzleHttp\Psr7\Request('POST', 'localhost:9000', $headers); -$childSpan->annotate('request_started', Timestamp\now()); -$response = $httpClient->send($request); -$childSpan->annotate('request_finished', Timestamp\now()); +echo $response->getBody(); -$childSpan->finish(); +usleep(100 * mt_rand(1, 3)); $span->finish(); +$scopeCloser(); /* Sends the trace to zipkin once the response is served */