Skip to content

chore: uses HTTP instrumentation from Zipkin PHP. #18

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
15 changes: 13 additions & 2 deletions backend.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Zipkin\Propagation\Map;
use Zipkin\Propagation\TraceContext;

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/functions.php';
Expand All @@ -9,6 +10,8 @@

$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();

usleep(1000 * mt_rand(1, 3));

$carrier = array_map(function ($header) {
return $header[0];
}, $request->headers->all());
Expand 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 */
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -18,4 +19,4 @@
"run-backend": "php -S 'localhost:9000' backend.php",
"run-zipkin": "docker run -p 9411:9411 -d openzipkin/zipkin"
}
}
}
30 changes: 11 additions & 19 deletions frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 */

Expand Down