Skip to content

Commit

Permalink
Merge pull request #22 from jukylin/add_zipkin_header
Browse files Browse the repository at this point in the history
Add zipkin header
  • Loading branch information
jky-yy authored Sep 18, 2018
2 parents ff801e9 + 98c2feb commit 6e455e6
Show file tree
Hide file tree
Showing 15 changed files with 522 additions and 43 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ $span->log(['error' => "HTTP request timeout"]);
$config->setDisabled(true);
```

## Zipkin B3 Propagation

> not support ```128bit``` and ```Distributed context propagation ```
```
$config::$propagator = \Jaeger\Constants\PROPAGATOR_ZIPKIN;
```


## finish span and flush Trace

```
Expand All @@ -88,6 +97,8 @@ $config->flush();

- [HTTP](https://github.com/jukylin/jaeger-php/blob/master/example/HTTP.php)
- [Hprose](https://github.com/jukylin/blog/blob/master/Uber%E5%88%86%E5%B8%83%E5%BC%8F%E8%BF%BD%E8%B8%AA%E7%B3%BB%E7%BB%9FJaeger%E4%BD%BF%E7%94%A8%E4%BB%8B%E7%BB%8D%E5%92%8C%E6%A1%88%E4%BE%8B%E3%80%90PHP%20%20%20Hprose%20%20%20Go%E3%80%91.md#跨语言调用案例)
- [Istio](https://github.com/jukylin/jaeger-php/blob/master/example/README.md)

## Features

- Transports
Expand Down
2 changes: 1 addition & 1 deletion example/HTTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_once dirname(dirname(dirname(dirname(__FILE__)))).'/autoload.php';

use Jaeger\Config;
//use GuzzleHttp\Client;
use GuzzleHttp\Client;
use OpenTracing\Formats;
use OpenTracing\Reference;

Expand Down
44 changes: 44 additions & 0 deletions example/Istio1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

require_once dirname(dirname(dirname(dirname(__FILE__)))).'/autoload.php';

use Jaeger\Config;
use OpenTracing\Formats;
use GuzzleHttp\Client;

$http = new swoole_http_server("0.0.0.0", 8000);
$http->on('request', function ($request, $response) {
unset($_SERVER['argv']);
$config = Config::getInstance();
$config::$propagator = \Jaeger\Constants\PROPAGATOR_ZIPKIN;

//init server span start
$tracer = $config->initTrace('Istio', 'jaeger-agent.istio-system:6831');
$spanContext = $tracer->extract(Formats\TEXT_MAP, $request->header);

$serverSpan = $tracer->startSpan('Istio1', ['child_of' => $spanContext]);
$tracer->inject($serverSpan->getContext(), Formats\TEXT_MAP, $_SERVER);
print_r($_SERVER);
//client span1 start
$clientTrace = $config->initTrace('Istio1 HTTP');
$injectTarget = [];
$spanContext = $clientTrace->extract(Formats\TEXT_MAP, $_SERVER);
$clientSapn = $clientTrace->startSpan('Istio1', ['child_of' => $spanContext]);
$clientTrace->inject($clientSapn->spanContext, Formats\TEXT_MAP, $injectTarget);

$client = new Client();
$clientSapn->setTags(["http.url" => "Istio2:8001"]);
$res = $client->request('GET', 'Istio2:8001' ,['headers' => $injectTarget]);
$clientSapn->setTags(["http.status_code" => $res->getStatusCode()]);
//client span1 end

//server span end
$serverSpan->finish();
//trace flush
$config->flush();

$response->end("Hello Istio1");
});
$http->start();

?>
42 changes: 42 additions & 0 deletions example/Istio2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

require_once dirname(dirname(dirname(dirname(__FILE__)))).'/autoload.php';

use Jaeger\Config;
use OpenTracing\Formats;

$http = new swoole_http_server("0.0.0.0", 8001);
$http->on('request', function ($request, $response) {
unset($_SERVER['argv']);
$config = Config::getInstance();
$config::$propagator = \Jaeger\Constants\PROPAGATOR_ZIPKIN;

//init server span start
$tracer = $config->initTrace('Istio', 'jaeger-agent.istio-system:6831');
$spanContext = $tracer->extract(Formats\TEXT_MAP, $request->header);
$serverSpan = $tracer->startSpan('Istio2', ['child_of' => $spanContext]);
$tracer->inject($serverSpan->getContext(), Formats\TEXT_MAP, $_SERVER);

//client span1 start
$clientTrace = $config->initTrace('Istio2 HTTP');
$injectTarget = [];
$spanContext = $clientTrace->extract(Formats\TEXT_MAP, $_SERVER);
$clientSapn = $clientTrace->startSpan('Istio2', ['child_of' => $spanContext]);
$clientTrace->inject($clientSapn->spanContext, Formats\TEXT_MAP, $injectTarget);

$client = new \GuzzleHttp\Client();
$clientSapn->setTags(["http.url" => "Istio3:8002"]);
$res = $client->request('GET', 'Istio3:8002', ['headers' => $injectTarget]);
$clientSapn->setTags(["http.status_code" => $res->getStatusCode()]);
//client span1 end

//server span end
$serverSpan->finish();
//trace flush
$config->flush();

$response->end("Hello Istio2");
});
$http->start();

?>
45 changes: 45 additions & 0 deletions example/Istio3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

require_once dirname(dirname(dirname(dirname(__FILE__)))).'/autoload.php';

use Jaeger\Config;
use OpenTracing\Formats;

$http = new swoole_http_server("0.0.0.0", 8002);
$http->on('request', function ($request, $response) {
unset($_SERVER['argv']);
$config = Config::getInstance();
$config::$propagator = \Jaeger\Constants\PROPAGATOR_ZIPKIN;

//init server span start
$tracer = $config->initTrace('Istio', 'jaeger-agent.istio-system:6831');

$spanContext = $tracer->extract(Formats\TEXT_MAP, $request->header);

$serverSpan = $tracer->startSpan('Istio3', ['child_of' => $spanContext]);
$tracer->inject($serverSpan->getContext(), Formats\TEXT_MAP, $_SERVER);

//client span1 start
$clientTrace = $config->initTrace('Istio3 Bus');
$spanContext = $clientTrace->extract(Formats\TEXT_MAP, $_SERVER);
$clientSapn = $clientTrace->startSpan('Istio3', ['child_of' => $spanContext]);

$sum = 0;
for($i = 0; $i < 10; $i++){
$sum += $i;
}
$clientSapn->log(['message' => 'result:'.$sum]);
$clientSapn->finish();

//client span1 end

//server span end
$serverSpan->finish();
//trace flush
$config->flush();

$response->end("Hello Istio3");
});
$http->start();

?>
30 changes: 30 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

# Just for testing

## Bring up the application containers
```
kubectl apply -f <(istioctl kube-inject -f istioctl kube-inject -f ./istio.yaml > istio-inject.yaml)
```

## Define the ingress gateway for the application

```
kubectl apply -f istio-gateway.yaml
```

## Access the istio service using curl

```
curl -I http://${GATEWAY_URL}/istio1
```

## Check the result in Browser

```
kubectl port-forward -n istio-system $(kubectl get pod -n istio-system -l app=jaeger -o jsonpath='{.items[0].metadata.name}') 16686:16686 &
http://localhost:16686
```



33 changes: 33 additions & 0 deletions example/istio-gateway.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: istio-gateway
spec:
selector:
istio: ingressgateway # use istio default controller
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: istio1
spec:
hosts:
- "*"
gateways:
- istio-gateway
http:
- match:
- uri:
exact: /istio1
route:
- destination:
host: istio1
port:
number: 8000
109 changes: 109 additions & 0 deletions example/istio.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
##################################################################################################
# Details service
##################################################################################################
apiVersion: v1
kind: Service
metadata:
name: istio1
labels:
app: istio1
spec:
ports:
- port: 8000
name: http
selector:
app: istio1
# type : NodePort
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: istio1
spec:
replicas: 1
template:
metadata:
labels:
app: istio1
version: v1
spec:
containers:
- name: istio1
image: registry.cn-shenzhen.aliyuncs.com/jukylin/php_swoole:v10
imagePullPolicy: IfNotPresent
command: ["php"]
args: ["/jukylin/jaeger-php/example/Istio1.php"]
ports:
- containerPort: 8000
---
apiVersion: v1
kind: Service
metadata:
name: istio2
labels:
app: istio2
spec:
ports:
- port: 8001
name: http
selector:
app: istio2
# type : NodePort
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: istio2
spec:
replicas: 1
template:
metadata:
labels:
app: istio2
version: v1
spec:
containers:
- name: istio2
image: registry.cn-shenzhen.aliyuncs.com/jukylin/php_swoole:v10
imagePullPolicy: IfNotPresent
command: ["php"]
args: ["/jukylin/jaeger-php/example/Istio2.php"]
ports:
- containerPort: 8001
---
apiVersion: v1
kind: Service
metadata:
name: istio3
labels:
app: istio3
spec:
ports:
- port: 8002
name: http
selector:
app: istio3
# type : NodePort
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: istio3
spec:
replicas: 1
template:
metadata:
labels:
app: istio3
version: v1
spec:
containers:
- name: istio3
image: registry.cn-shenzhen.aliyuncs.com/jukylin/php_swoole:v10
imagePullPolicy: IfNotPresent
command: ["php"]
args: ["/jukylin/jaeger-php/example/Istio3.php"]
ports:
- containerPort: 8002
---

13 changes: 13 additions & 0 deletions src/Jaeger/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use OpenTracing\NoopTracer;
use Jaeger\Sampler\Sampler;
use Jaeger\Sampler\ConstSampler;
use Jaeger\Propagator\JaegerPropagator;
use Jaeger\Propagator\ZipkinPropagator;

class Config {

Expand All @@ -29,6 +31,9 @@ class Config {

public static $disabled = false;

public static $propagator = \Jaeger\Constants\PROPAGATOR_JAEGER;


private function __construct(){

}
Expand Down Expand Up @@ -83,12 +88,20 @@ public function initTrace($serverName, $agentHostPort = ''){
$this->sampler = new ConstSampler(true);
}


$tracer = new Jaeger($serverName, $this->reporter, $this->sampler);

if($this->gen128bit == true){
$tracer->gen128bit();
}

if(self::$propagator == \Jaeger\Constants\PROPAGATOR_ZIPKIN){
$tracer->setPropagator(new ZipkinPropagator());
}else{
$tracer->setPropagator(new JaegerPropagator());
}


self::$tracer[$serverName] = $tracer;


Expand Down
Loading

0 comments on commit 6e455e6

Please sign in to comment.