Odigos will automatically instrument your services and record semantic spans from popular modules. Many users find the automatic instrumentation data sufficient for their needs.

However, if there is anything specific to your application that you want to record, you can enrich the data by adding custom spans to your code.

This is sometimes referred to as manual instrumentation.

Use Cases

Examples of custom spans you might want to add to your code include:

  • Spans for the execution of some potentially heavy or slow computation in you service.
  • Tracing for internal or third party libraries for which you don’t have automatic or integrated instrumentation.
  • Spans to describe some logical operations in your business logic that are meaningful in your domain.

Required Dependencies

Add the following dependencies to your project by running:

composer require open-telemetry/api

Creating a Span

To create a new span, use the TracerProvider.

<?php

use OpenTelemetry\API\Globals;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

require __DIR__ . '/vendor/autoload.php';

$tracer = Globals::tracerProvider()->getTracer('demo');

$app = AppFactory::create();
$app->get('/rolldice', function (Request $request, Response $response) use ($tracer) {
    $span = $tracer
        ->spanBuilder('manual-span')
        ->startSpan();

    $result = random_int(1,6);
    $response->getBody()->write(strval($result));

    $span
        ->addEvent('rolled dice', ['result' => $result])
        ->end();
    return $response;
});

$app->run();

Additional Information

For more use cases, see the OpenTelemetry PHP API documentation.