Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 32468e5

Browse files
authored
Update README and generated code docs (#71)
* Simplifying and updating the README * Fixing code blocks * Update sami config to skip documentation for methods marked internal * Fix documentation code blocks in integration code * Fix links in docblocks * Sami doesn't support 'see' annotations. Replace with links * Add class examples for the samplers * Fix phpcs * Add badge images for latest released packagist version, add current status * Need to indent code blocks in the ordered list
1 parent 60f31a4 commit 32468e5

37 files changed

Lines changed: 315 additions & 196 deletions

README.md

Lines changed: 89 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,67 @@
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\Trace\Tracer;
36+
use OpenCensus\Trace\Exporter\EchoExporter;
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);
13265
try {
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
164116
Please note it is currently under active development. Any release versioned
165117
0.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
176128
work-in-progress and are more likely to get backwards-incompatible updates.
177129
130+
**Current Status:** Pre-Alpha
131+
132+
178133
## Contributing
179134
180135
Contributions 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
194149
This 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/

config/sami.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
use Sami\Sami;
1919
// use Sami\Version\GitVersionCollection;
2020
use Symfony\Component\Finder\Finder;
21+
use Sami\Parser\Filter\FilterInterface;
22+
use Sami\Reflection\ClassReflection;
23+
use Sami\Reflection\MethodReflection;
24+
use Sami\Reflection\PropertyReflection;
2125

2226
$root_dir = __DIR__ . '/../';
2327

@@ -30,7 +34,31 @@
3034
// ->addFromTags('v0.*')
3135
// ->add('master', 'master branch');
3236

33-
return new Sami($iterator, [
37+
$sami = new Sami($iterator, [
3438
// 'versions' => $versions,
35-
'build_dir' => __DIR__ . '/../docs'
39+
'build_dir' => __DIR__ . '/../docs',
40+
'title' => 'OpenCensus PHP API'
3641
]);
42+
43+
class VisibleFilter implements FilterInterface
44+
{
45+
public function acceptClass(ClassReflection $class)
46+
{
47+
return true;
48+
}
49+
50+
public function acceptMethod(MethodReflection $method)
51+
{
52+
return !$method->isPrivate() && empty($method->getTags('internal'));
53+
}
54+
public function acceptProperty(PropertyReflection $property)
55+
{
56+
return !$property->isPrivate();
57+
}
58+
}
59+
60+
$sami['filter'] = function () {
61+
return new \VisibleFilter();
62+
};
63+
64+
return $sami;

src/Trace/Exporter/EchoExporter.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@
2222
/**
2323
* This implementation of the ExporterInterface uses `print_r` to output
2424
* the trace's representation to stdout.
25+
*
26+
* Example:
27+
* ```
28+
* use OpenCensus\Trace\Exporter\EchoExporter;
29+
* use OpenCensus\Trace\Tracer;
30+
*
31+
* $exporter = new EchoExporter();
32+
* Tracer::begin($exporter);
33+
* ```
2534
*/
2635
class EchoExporter implements ExporterInterface
2736
{

src/Trace/Exporter/FileExporter.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,17 @@
2020
use OpenCensus\Trace\Tracer\TracerInterface;
2121

2222
/**
23-
* This implementation of the ExporterInterface appends a json
23+
* This implementation of the ExporterInterface appends a JSON
2424
* representation of the trace to a file.
25+
*
26+
* Example:
27+
* ```
28+
* use OpenCensus\Trace\Exporter\FileExporter;
29+
* use OpenCensus\Trace\Tracer;
30+
*
31+
* $exporter = new FileExporter('/path/to/file.txt');
32+
* Tracer::begin($exporter);
33+
* ```
2534
*/
2635
class FileExporter implements ExporterInterface
2736
{

src/Trace/Exporter/LoggerExporter.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,20 @@
2121
use Psr\Log\LoggerInterface;
2222

2323
/**
24-
* This implementation of the ExporterInterface appends a json
25-
* representation of the trace to a file.
24+
* This implementation of the ExporterInterface sends log messages to a
25+
* configurable PSR-3 logger.
26+
*
27+
* Example:
28+
* ```
29+
* use OpenCensus\Trace\Exporter\LoggerExporter;
30+
* use OpenCensus\Trace\Tracer;
31+
* use Monolog\Logger;
32+
*
33+
* // Example PSR-3 logger
34+
* $logger = new Logger('traces');
35+
* $exporter = new LoggerExporter($logger);
36+
* Tracer::begin($exporter);
37+
* ```
2638
*/
2739
class LoggerExporter implements ExporterInterface
2840
{

src/Trace/Exporter/NullExporter.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121

2222
/**
2323
* This implementation of the ExporterInterface does nothing.
24+
*
25+
* Example:
26+
* ```
27+
* use OpenCensus\Trace\Exporter\NullExporter;
28+
* use OpenCensus\Trace\Tracer;
29+
*
30+
* $exporter = new NullExporter();
31+
* Tracer::begin($exporter);
32+
* ```
2433
*/
2534
class NullExporter implements ExporterInterface
2635
{

0 commit comments

Comments
 (0)