Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/uptrace/uptrace-php

OpenTelemetry PHP distribution for Uptrace
https://github.com/uptrace/uptrace-php

distributed-tracing opentelemetry php tracing uptrace

Last synced: 9 days ago
JSON representation

OpenTelemetry PHP distribution for Uptrace

Awesome Lists containing this project

README

        

# Uptrace for PHP

[![Documentation](https://img.shields.io/badge/uptrace-documentation-informational)](https://uptrace.dev/get/opentelemetry-php.html)
[![Chat](https://img.shields.io/badge/-telegram-red?color=white&logo=telegram&logoColor=black)](https://t.me/uptrace)



## Introduction

uptrace-php is a thing wrapper over OpenTelemery PHP that configures Otel SDK to export
[traces](https://uptrace.dev/opentelemetry/distributed-tracing.html) and
[metrics](https://uptrace.dev/opentelemetry/metrics.html) to Uptrace.

## Quickstart

First, install Composer using the
[installation instructions](https://getcomposer.org/doc/00-intro.md#installation-linux-unix-macos)
and add the following line to your project's `composer.json` file, as this library has not reached a
stable release status yet:

```json
"minimum-stability": "dev"
```

Then, you can install uptrace-php:

```bash
composer require uptrace/uptrace
```

Run the [basic example](example/basic) below using the DSN from the Uptrace project settings page.

```php
setDsn('https://@uptrace.dev/')
->setServiceName('myservice')
->setServiceVersion('1.0.0')
->buildAndRegisterGlobal();

// Create a tracer. Usually, tracer is a global variable.
$tracer = \OpenTelemetry\API\Globals::tracerProvider()->getTracer('app_or_package_name');

// Create a root span (a trace) to measure some operation.
$main = $tracer->spanBuilder('main-operation')->startSpan();
// Future spans will be parented to the currently active span.
$mainScope = $main->activate();

$child1 = $tracer->spanBuilder('GET /posts/:id')
->setSpanKind(SpanKind::KIND_SERVER)
->startSpan();
$child1Scope = $child1->activate();
$child1->setAttribute('http.method"', 'GET');
$child1->setAttribute('http.route"', '/posts/:id');
$child1->setAttribute('http.url', 'http://localhost:8080/posts/123');
$child1->setAttribute('http.status_code', 200);
try {
throw new \Exception('Some error message');
} catch (\Exception $exc) {
$child1->setStatus('error', $exc->getMessage());
$child1->recordException($exc);
}
$child1Scope->detach();
$child1->end();

$child2 = $tracer->spanBuilder('child2-of-main')->startSpan();
$child2Scope = $child1->activate();
$child2->setAttributes([
'db.system' => 'mysql',
'db.statement' => 'SELECT * FROM posts LIMIT 100',
]);
$child2Scope->detach();
$child2->end();

// End the span and detached context when the operation we are measuring is done.
$mainScope->detach();
$main->end();

echo $uptrace->traceUrl($main) . PHP_EOL;
```

## Links

- [Examples](example)
- [Get started with OpenTelemetry PHP](https://uptrace.dev/get/opentelemetry-php.html)