Package Data | |
---|---|
Maintainer Username: | dipenduroy |
Maintainer Contact: | dipenduroy007@gmail.com (Dipendu) |
Package Create Date: | 2020-12-01 |
Package Last Update: | 2020-12-02 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-09 15:07:23 |
Package Statistics | |
---|---|
Total Downloads: | 20 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Distributed Tracing of Guzzle HTTP Client (Micro Services internal calls) and dynamic profiling in Lumen
composer require dipenduroy/lumenzipkin
To enable Lumen Zipkin library, add below in bootstrap/app.php
$app->register(DipenduRoy\LumenZipkin\LumenZipkinServiceProvider::class);
To automatically flush all the traces to zipkin server while script ends. This is required to push trace info to zipkin server.
Add DipenduRoy\LumenZipkin\ZipkinTraceMiddleware::class in middleware's list of bootstrap/app.php, If no middleware
$app->middleware([
DipenduRoy\LumenZipkin\ZipkinTraceMiddleware::class
]);
Use Docker to start Zipkin Server
composer run-zipkin
Below example traces all guzzle calls between microservices. Similarly refer zipkin-php for more customized tracing
//Set your application variables below
$baseUri='http://local.app/';
$method='POST';
$requestUrl='example/url';
$queryString='?filter=category';
$formParams=[];
//Get Zipkin Trace Object
$ZipkinTrace=app('Trace\ZipkinTrace');
/* Creates the span for getting the guzzle client call */
$childSpan = $ZipkinTrace->createChildClientSpan($baseUri);
//Tags can be configured as per your requirement for filtering the requests from zipkin dashboard
$childSpan->tag(\Zipkin\Tags\HTTP_HOST, $baseUri);
$childSpan->tag(\Zipkin\Tags\HTTP_URL, $baseUri.$requestUrl);
$childSpan->tag(\Zipkin\Tags\HTTP_PATH, $requestUrl);
$childSpan->tag(\Zipkin\Tags\HTTP_METHOD, strtoupper($method));
$childSpan->annotate('request_started', \Zipkin\Timestamp\now());
$client = new \GuzzleHttp\Client([
'base_uri' => $baseUri,
]);
$headers['accept'] = "application/json";
$headers=array_merge($headers,$ZipkinTrace->getHeaders());
$params=[
'form_params' => $formParams,
'headers' => $headers,
'query' => $queryString,
];
try {
$response = $client->request($method, $requestUrl, $params);
} catch (\GuzzleHttp\Exception\TransferException $e) {
$response = $e->getResponse();
if ($e->hasResponse()) {
$responseString = $e->getResponse()->getBody(true);
} else {
$childSpan->tag(\Zipkin\Tags\HTTP_STATUS_CODE, 503);
$childSpan->tag(\Zipkin\Tags\ERROR, 'Guzzle Transfer Exception');
$childSpan->annotate('request_failed', \Zipkin\Timestamp\now());
}
}
$childSpan->tag(\Zipkin\Tags\HTTP_STATUS_CODE, $response->getStatusCode());
$childSpan->annotate('request_finished', \Zipkin\Timestamp\now());
Set below environment variable as required
APP_NAME Environment variable is used for root trace name
LUMEN_ZIPKIN_ENABLE_SERVER_ADDRESS Environment variable is used to enable server address if available
If you need to pass the zipkin endpoint, just pass the reporter
url as HTTP_REPORTER_URL
env variable. Default zipkin location is - http://localhost:9411/api/v2/spans
HTTP_REPORTER_URL=http://myzipkin:9411/api/v2/span composer run-frontend
Thanks to the below contributors