1- # OpenCensus - A stats collection and distributed tracing framework
1+ # OpenCensus for PHP - A stats collection and distributed tracing framework
22
3- This is the open-source release of Census for PHP. Census provides a
4- framework to measure a server's resource usage and collect performance stats.
5- This repository contains PHP related utilities and supporting software needed by
6- Census.
3+ > [ Census] [ census-org ] for PHP. Census provides a framework to measure a
4+ server's resource usage and collect performance stats. This repository contains
5+ PHP related utilities and supporting software needed by Census.
76
8- ## Installation
7+ [ ![ CircleCI] ( https://circleci.com/gh/census-instrumentation/opencensus-php.svg?style=svg )] ( https://circleci.com/gh/census-instrumentation/opencensus-php )
8+ [ ![ Packagist] ( https://img.shields.io/packagist/v/opencensus/opencensus.svg )] ( https://packagist.org/packages/opencensus/opencensus )
9+ ![ PHP-Version] ( https://img.shields.io/packagist/php-v/opencensus/opencensus.svg )
910
10- ### PHP library
11+ * [ API Documentation ] [ api-docs ]
1112
12- 1 . Install with ` composer ` or add to your ` composer.json ` .
13+ ## Installation & basic usage
1314
14- ```
15- $ composer require opencensus/opencensus
16- ```
17-
18- 2 . Include and start the library as the first action in your application:
19-
20- ``` php
21- use OpenCensus\Trace\RequestTracer;
22- use OpenCensus\Trace\Exporter\EchoExporter;
23-
24- RequestTracer::start(new EchoExporter());
25- ```
15+ 1 . Install the ` opencensus/opencensus ` package using [ composer] [ composer ] :
2616
27- ### PHP Extension
17+ ``` bash
18+ $ composer require opencensus/opencensus
19+ ```
2820
29- 1 . Install the ` opencensus ` extension from PECL.
21+ 1. [Optional]: Install the ` opencensus` extension from [ PECL][pecl]:
3022
31- ```
32- $ pecl install opencensus
33- ```
23+ ` ` ` bash
24+ $ pecl install opencensus-devel
25+ ` ` `
26+ Enable the extension in your ` php.ini` :
3427
35- 2 . Enable the extension in your ` php.ini ` .
28+ ` ` ` ini
29+ extension=opencensus.so
30+ ` ` `
3631
37- ```
38- extension=opencensus.so
39- ```
32+ 1. Initialize a tracer for your application:
4033
41- ## Customizing
34+ ` ` ` php
35+ use OpenCensus\T race\T racer;
36+ use OpenCensus\T race\E xporter\E choExporter;
4237
43- ### Reporting Traces
38+ Tracer::start(new EchoExporter ());
39+ ` ` `
4440
45- The above sample uses the ` EchoExporter ` to dump trace results to the
46- bottom of the webpage.
41+ # # Usage
4742
48- If you would like to provide your own reporter, create a class that implements ` ExporterInterface ` .
49-
50- #### Currently implemented reporters
51-
52- | Class | Description |
53- | ----- | ----------- |
54- | [ EchoExporter] ( src/Trace/Exporter/EchoExporter.php ) | Output the collected spans to stdout |
55- | [ FileExporter] ( src/Trace/Exporter/FileExporter.php ) | Output JSON encoded spans to a file |
56- | [ StackdriverExporter] ( src/Trace/Exporter/StackdriverExporter.php ) | Report traces to Google Cloud Stackdriver Trace |
57- | [ LoggerExporter] ( src/Trace/Exporter/LoggerExporter.php ) | Exporter JSON encoded spans to a PSR-3 logger |
58- | [ NullExporter] ( scr/Trace/Exporter/NullExporter.php ) | No-op |
59- | [ ZipkinExporter] ( src/Trace/Exporter/ZipkinExporter.php ) | Report collected spans to a Zipkin server |
60-
61- ### Sampling Rate
62-
63- By default we attempt to trace all requests. This is not ideal as it adds a little bit of
64- latency and require more memory for requests that are traced. To trace only a sampling
65- of requests, configure a sampler.
66-
67- The preferred sampler is the ` QpsSampler ` (Queries Per Second). This sampler implementation
68- requires a PSR-6 cache implementation to function.
69-
70- ``` php
71- use OpenCensus\Trace\Exporter\EchoExporter;
72- use OpenCensus\Trace\Sampler\QpsSampler;
73-
74- $cache = new SomeCacheImplementation();
75- $sampler = new QpsSampler($cache, ['rate' => 0.1]); // sample 0.1 requests per second
76- RequestTracer::start(new EchoExporter(), ['sampler' => $sampler]);
77- ```
78-
79- Please note: While required for the ` QpsSampler ` , a PSR-6 implementation is
80- not included in this library. It will be necessary to include a separate
81- dependency to fulfill this requirement. For PSR-6 implementations, please see the
82- [ Packagist PHP Package Repository] ( https://packagist.org/providers/psr/cache-implementation ) .
83- If the APCu extension is available (available on Google AppEngine Flexible Environment)
84- and you include the cache/apcu-adapter composer package, we will set up the cache for you.
85-
86- You can also choose to use the ` ProbabilitySampler ` which simply samples a flat
87- percentage of requests.
88-
89- #### Currently implemented samplers
90-
91- | Class | Description |
92- | ----- | ----------- |
93- | [ NeverSampleSampler] ( src/Trace/Sampler/NeverSampleSampler.php ) | Never trace any requests |
94- | [ AlwaysSampleSampler] ( src/Trace/Sampler/AlwaysSampleSampler.php ) | Trace all requests |
95- | [ QpsSampler] ( src/Trace/Sampler/QpsSampler.php ) | Trace X requests per second. Requires a PSR-6 cache implementation |
96- | [ ProbabilitySampler] ( src/Trace/Sampler/ProbabilitySampler.php ) | Trace X percent of requests. |
97-
98- ``` php
99- use OpenCensus\Trace\Exporter\EchoExporter;
100- use OpenCensus\Trace\Sampler\ProbabilitySampler;
101-
102- $sampler = new ProbabilitySampler(0.1); // sample 10% of requests
103- RequestTracer::start(new EchoExporter(), ['sampler' => $sampler]);
104- ```
105-
106- If you would like to provide your own sampler, create a class that implements ` SamplerInterface ` .
107-
108- ## Tracing Code Blocks
109-
110- To add tracing to a block of code, you can use the closure/callable form or explicitly open
111- and close spans yourself.
43+ To add tracing to a block of code, you can use the closure/callable form or
44+ explicitly open and close spans yourself.
11245
11346# ## Closure/Callable (preferred)
11447
11548` ` ` php
116- $pi = RequestTracer ::inSpan(['name' => 'expensive-operation'], function() {
49+ $pi = Tracer ::inSpan([' name' => ' expensive-operation' ], function () {
11750 // some expensive operation
11851 return calculatePi(1000);
11952});
12053
121- $pi = RequestTracer ::inSpan(['name' => 'expensive-operation'], 'calculatePi', [1000]);
54+ $pi = Tracer ::inSpan([' name' => ' expensive-operation' ], ' calculatePi' , [1000]);
12255` ` `
12356
12457# ## Explicit Span Management
12558
12659` ` ` php
12760// Creates a detached span
128- $span = RequestTracer ::startSpan(['name' => 'expensive-operation']);
61+ $span = Tracer ::startSpan([' name' => ' expensive-operation' ]);
12962
13063// Opens a scope that attaches the span to the current context
131- $scope = RequestTracer ::withSpan($span);
64+ $scope = Tracer ::withSpan($span );
13265try {
13366 $pi = calculatePi(1000);
13467} finally {
@@ -137,29 +70,48 @@ try {
13770}
13871` ` `
13972
140- ### OpenCensus extension
73+ # # Customization
14174
142- The ` opencensus ` extension collects nested span data throughout the course of your application's
143- execution. You can collect spans in 2 ways, by watching for function/method invocations or by manually
144- starting and stopping spans. In both cases, the spans will be collected together and can be retrieved
145- at the end of the request.
75+ # ## Samplers
14676
147- See [ extension README] ( ext/README.md ) for more information.
77+ You can specify different samplers when initializing a tracer. The default
78+ sampler is the ` AlwaysSampleSampler` which will attempt to trace all requests.
14879
149- ## Versioning
80+ The provided samplers are:
15081
151- You can retrieve the version of this extension at runtime.
82+ | Class | Description |
83+ | ----- | ----------- |
84+ | [NeverSampleSampler][never-sampler] | Never trace any requests |
85+ | [AlwaysSampleSampler][always-sampler] | Trace all requests |
86+ | [QpsSampler][qps-sampler] | Trace X requests per second. Requires a PSR-6 cache implementation |
87+ | [ProbabilitySampler][probability-sampler] | Trace X percent of requests. |
15288
153- ``` php
154- /**
155- * Return the current version of the opencensus_trace extension
156- *
157- * @return string
158- */
159- function opencensus_trace_version();
160- ```
89+ If you would like to provide your own sampler, create a class that implements
90+ ` SamplerInterface` .
16191
162- This library follows [ Semantic Versioning] ( http://semver.org/ ) .
92+ # ## Exporters
93+
94+ You can choose different exporters to send the collected traces to.
95+
96+ The provided exporters are:
97+
98+ | Class | Description |
99+ | ----- | ----------- |
100+ | [EchoExporter][echo-exporter] | Output the collected spans to stdout |
101+ | [FileExporter][file-exporter] | Output JSON encoded spans to a file |
102+ | [StackdriverExporter][stackdriver-exporter] | Report traces to Google Cloud Stackdriver Trace |
103+ | [LoggerExporter][logger-exporter] | Exporter JSON encoded spans to a PSR-3 logger |
104+ | [NullExporter][null-exporter] | No-op |
105+ | [ZipkinExporter][zipkin-exporter] | Report collected spans to a Zipkin server |
106+
107+ If you would like to provide your own reporter, create a class that implements
108+ ` ExporterInterface` .
109+
110+ # # Versioning
111+
112+ [! [Packagist](https://img.shields.io/packagist/v/opencensus/opencensus.svg)](https://packagist.org/packages/opencensus/opencensus)
113+
114+ This library follows [Semantic Versioning][semver].
163115
164116Please note it is currently under active development. Any release versioned
1651170.x.y is subject to backwards incompatible changes at any time.
@@ -175,6 +127,9 @@ and requests with a higher priority.
175127**Alpha**: Libraries defined at an Alpha quality level are still a
176128work-in-progress and are more likely to get backwards-incompatible updates.
177129
130+ **Current Status:** Pre-Alpha
131+
132+
178133## Contributing
179134
180135Contributions to this library are always welcome and highly encouraged.
@@ -192,3 +147,19 @@ Apache 2.0 - See [LICENSE](LICENSE) for more information.
192147## Disclaimer
193148
194149This is not an official Google product.
150+
151+ [census-org]: https://github.com/census-instrumentation
152+ [api-docs]: http://opencensus.io/opencensus-php/
153+ [composer]: https://getcomposer.org/
154+ [pecl]: https://pecl.php.net/
155+ [never-sampler]: http://opencensus.io/opencensus-php/OpenCensus/Trace/Sampler/NeverSampleSampler.html
156+ [always-sampler]: http://opencensus.io/opencensus-php/OpenCensus/Trace/Sampler/NeverSampleSampler.html
157+ [qps-sampler]: http://opencensus.io/opencensus-php/OpenCensus/Trace/Sampler/NeverSampleSampler.html
158+ [probability-sampler]: http://opencensus.io/opencensus-php/OpenCensus/Trace/Sampler/NeverSampleSampler.html
159+ [echo-exporter]: http://opencensus.io/opencensus-php/OpenCensus/Trace/Exporter/EchoExporter.html
160+ [file-exporter]: http://opencensus.io/opencensus-php/OpenCensus/Trace/Exporter/FileExporter.html
161+ [stackdriver-exporter]: http://opencensus.io/opencensus-php/OpenCensus/Trace/Exporter/StackdriverExporter.html
162+ [logger-exporter]: http://opencensus.io/opencensus-php/OpenCensus/Trace/Exporter/LoggerExporter.html
163+ [null-exporter]: http://opencensus.io/opencensus-php/OpenCensus/Trace/Exporter/NullExporter.html
164+ [zipkin-exporter]: http://opencensus.io/opencensus-php/OpenCensus/Trace/Exporter/ZipkinExporter.html
165+ [semver]: http://semver.org/
0 commit comments