Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/basis-company/telemetry.php

telemetry framework for php
https://github.com/basis-company/telemetry.php

metrics opentelemetry php prometheus tracing zipkin

Last synced: about 2 months ago
JSON representation

telemetry framework for php

Awesome Lists containing this project

README

        

# Service telemetry for php
[![License](https://poser.pugx.org/basis-company/telemetry/license.png)](https://packagist.org/packages/basis-company/telemetry)
[![Testing](https://github.com/basis-company/telemetry.php/actions/workflows/tests.yml/badge.svg)](https://github.com/basis-company/telemetry.php/actions/workflows/tests.yml)
[![Latest Version](https://img.shields.io/github/release/basis-company/telemetry.php.svg)](https://github.com/basis-company/telemetry.php/releases)
[![Total Downloads](https://img.shields.io/packagist/dt/basis-company/telemetry.svg)](https://packagist.org/packages/basis-company/telemetry)

- [Installation](#installation)
- [Metrics](#metrics)
- [Tracing](#tracing)

## Installation
The recommended way to install the library is through [Composer](http://getcomposer.org):
```bash
$ composer require basis-company/telemetry
```

## Metrics
```php
increment('request_counter', 1, [ 'user' => 1 ]);
$registry->increment('request_counter', 2, [ 'user' => 2 ]);
$registry->set('memory_usage', memory_get_usage(true));
$registry->set('uptime', 30);

// describe additional info for metrics
$info = new Info();
$info->set('memory_usage', 'Memory usage');
$info->set('request_counter', 'Request Counter', Type::COUNTER);
$info->set('uptime', 'Uptime in seconds', Type::COUNTER);

$exporter = new PrometheusExporter($registry, $info);
// render prometheus metrics with service prefix and optional extra labels
echo $exporter->toString('mailer_', ['env' => 'test']);

// or write result to a file
$exporter->toFile('public/metrics', 'mailer_');

// # HELP mailer_request_counter Request Counter
// # TYPE mailer_request_counter counter
// mailer_request_counter{env="test",user="1"} 1
// mailer_request_counter{env="test",user="2"} 2
// # HELP mailer_memory_usage Memory usage
// # TYPE mailer_memory_usage gauge
// mailer_memory_usage{env="test"} 4194304
// # HELP mailer_uptime Uptime
// # TYPE mailer_uptime counter
// mailer_uptime{env="test"} 30

$remoteRegistry = new Registry();
$remoteInfo = new Info();
$importer = new PrometheusImporter($remoteRegistry, $remoteInfo);

// cut prefix and import registry data and info configuration
$importer->fromFile('public/metrics', 'mailer_');

// prometheus metrics were parsed and prefix removed
echo $remoteRegistry->get('uptime'); // 30
echo $remoteInfo->get('uptime')['help']; // Uptime in seconds
```

## Tracing
```php
setSpanContext($spanContext)->getTracer();

// start a span, register some events
$span = $tracer->createSpan('session.generate');

// set attributes as array
$span->setAttributes([ 'remote_ip' => '5.23.99.245' ]);
// set attribute one by one
$span->setAttribute('country', 'Russia');

$span->addEvent('found_login', [
'id' => 67235,
'username' => 'nekufa',
]);
$span->addEvent('generated_session', [
'id' => md5(microtime(true))
]);

$span->end(); // pass status as an optional argument

// add additional endpoint information
$exporter = new ZipkinExporter([ 'serviceName' => 'tester' ]);

// configure transport using symfony http client instance
$client = new CurlHttpClient();
$transport = new ZipkinTransport($client, 'zipkin-hostname'); // set zipkin hostname, override port or url

$exporter->flush($tracer, $transport);

```