Giter Club home page Giter Club logo

opencensus-php's Introduction

OpenCensus for PHP - A stats collection and distributed tracing framework

Census for PHP. Census provides a framework to measure a server's resource usage and collect performance stats. This repository contains PHP related utilities and supporting software needed by Census.

CircleCI Packagist PHP-Version

Installation & basic usage

  1. Install the opencensus/opencensus package using composer:

    $ composer require opencensus/opencensus:~0.2

    IMPORTANT: Please ensure your version is >= 0.2.0. There is a potential security vulnerability in < 0.2.0.

  2. [Optional]: Install the opencensus extension from PECL:

    $ pecl install opencensus-alpha

    Enable the extension in your php.ini:

    extension=opencensus.so
  3. Initialize a tracer for your application:

    use OpenCensus\Trace\Tracer;
    use OpenCensus\Trace\Exporter\EchoExporter;
    
    Tracer::start(new EchoExporter());

Usage

To add tracing to a block of code, you can use the closure/callable form or explicitly open and close spans yourself.

Closure/Callable (preferred)

$pi = Tracer::inSpan(['name' => 'expensive-operation'], function() {
    // some expensive operation
    return calculatePi(1000);
});

$pi = Tracer::inSpan(['name' => 'expensive-operation'], 'calculatePi', [1000]);

Explicit Span Management

// Creates a detached span
$span = Tracer::startSpan(['name' => 'expensive-operation']);

// Opens a scope that attaches the span to the current context
$scope = Tracer::withSpan($span);
try {
    $pi = calculatePi(1000);
} finally {
    // Closes the scope (ends the span)
    $scope->close();
}

Customization

Samplers

You can specify different samplers when initializing a tracer. The default sampler is the AlwaysSampleSampler which will attempt to trace all requests.

The provided samplers are:

Class Description
NeverSampleSampler Never trace any requests
AlwaysSampleSampler Trace all requests
MultiSampler Check multiple samplers
QpsSampler Trace X requests per second. Requires a PSR-6 cache implementation
ProbabilitySampler Trace X percent of requests.

If you would like to provide your own sampler, create a class that implements SamplerInterface.

Exporters

You can choose different exporters to send the collected traces to.

The provided exporters are:

Class Description Dependency
EchoExporter Output the collected spans to stdout
FileExporter Output JSON encoded spans to a file
JaegerExporter Report traces to Jaeger server via Thrift over UDP opencensus/opencensus-exporter-jaeger
LoggerExporter Exporter JSON encoded spans to a PSR-3 logger
NullExporter No-op
OneLineEchoExporter Output the collected spans to stdout with one-line
StackdriverExporter Report traces to Google Cloud Stackdriver Trace opencensus/opencensus-exporter-stackdriver
ZipkinExporter Report collected spans to a Zipkin server opencensus/opencensus-exporter-zipkin

If you would like to provide your own reporter, create a class that implements ExporterInterface.

Versioning

Packagist

This library follows Semantic Versioning.

Please note it is currently under active development. Any release versioned 0.x.y is subject to backwards incompatible changes at any time.

GA: Libraries defined at a GA quality level are stable, and will not introduce backwards-incompatible changes in any minor or patch releases. We will address issues and requests with the highest priority.

Beta: Libraries defined at a Beta quality level are expected to be mostly stable and we're working towards their release candidate. We will address issues and requests with a higher priority.

Alpha: Libraries defined at an Alpha quality level are still a work-in-progress and are more likely to get backwards-incompatible updates.

Current Status: Alpha

Contributing

Contributions to this library are always welcome and highly encouraged.

See CONTRIBUTING for more information on how to get started.

Releasing

See RELEASING for more information on releasing new versions.

License

Apache 2.0 - See LICENSE for more information.

Disclaimer

This is not an official Google product.

opencensus-php's People

Contributors

bpot avatar bshaffer avatar castaneai avatar chingor13 avatar draffensperger avatar hectorj avatar jcchavezs avatar klibbbs avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.