OpenTelemetry PHP Configuration

Ship traces from PHP to OpenSearch with OpenTelemetry

Use OpenTelemetry to easily send PHP traces to your Logit.io Stack.

This sample app was created and tested with

PHP 8.3.11 (cli) (built: Aug 27 2024 21:28:59) (ZTS Visual C++ 2019 x64)

Composer version 2.7.9 2024-09-04 14:43:28

Install

Create a new directory for your project and name it PHPAPMTestApp.

Open a Terminal window or Command Prompt and navigate into the new PHPAPMTestApp folder.

Now we need to install the required OpenTelemetry dependencies, do this by pasting the following into the terminal.

composer require open-telemetry/exporter-otlp

If running the above command displays a message asking whether you trust the Composer because it is not in your allow-plugins config then press 'y' and then press enter.

Then paste the following.

composer require php-http/guzzle7-adapter

You will now have a folder called vendor and two files called called composer.json and composer.lock in your project folder.

Create two additional files in your project folder and call them index.php and config.php. Open the index.php file using your choice of text editor.

Copy and Paste the code below into the index.php file and then save.

index.php
<?php
 
require __DIR__ . '/vendor/autoload.php';
 
// Load config from external file
$config = require 'config.php';
 
use OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory;
use OpenTelemetry\Contrib\Otlp\SpanExporter;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\Sampler\ParentBased;
use OpenTelemetry\SemConv\ResourceAttributes;
 
$resource = ResourceInfoFactory::emptyResource()->merge(ResourceInfo::create(Attributes::create([
    ResourceAttributes::SERVICE_NAMESPACE => 'demo',
    ResourceAttributes::SERVICE_NAME => 'LogitPHPTestApp',
    ResourceAttributes::SERVICE_VERSION => '0.1',
    ResourceAttributes::DEPLOYMENT_ENVIRONMENT => 'development',
])));
 
$credentials = base64_encode("{$config['username']}:{$config['password']}"); 
$headers = [
    'Authorization' => 'Basic ' . $credentials,
    'Content-Type' => 'application/x-protobuf'
];
 
$transport = (new OtlpHttpTransportFactory())->create("{$config['endpoint']}:{$config['port']}/v1/traces", 'application/x-protobuf', $headers);
$exporter = new SpanExporter($transport);
 
$tracerProvider = TracerProvider::builder()
    ->addSpanProcessor(
        new SimpleSpanProcessor($exporter)
    )
    ->setResource($resource)
    ->setSampler(new ParentBased(new AlwaysOnSampler()))
    ->build();
 
echo "Logit.io APM Integration Test App Running";
 
$tracer = $tracerProvider->getTracer('php-tracer');
 
// Start a new trace and a span
$span = $tracer->spanBuilder('first-span')->startSpan();
$span->setAttribute('example-attribute', 'example value');
$span->addEvent('Creating first span');
 
// Simulate some work (like an API call, DB query, etc.)
sleep(1);
 
// End the span
$span->end();

Configuring the App

Open the config.php file with your text editor and paste the following and then save.

⚠️

You will need to update any placeholder variables with your OpenTelemetry details.

config.php
<?php
// config.php
 
return [
    'endpoint' => 'https://@opentelemetry.endpointAddress',
    'port' => '@opentelemetry.httpsPort',
    'username' => '@opentelemetry.username',
    'password' => '@opentelemetry.password',
];

Run the PHP App

Copy and Paste the code below into the terminal to run the app.

php -S localhost:8000

You will see feedback from the app so that you know that is running http://localhost:8000 (opens in a new tab).

Open your browser and enter the url, http://localhost:8000 (opens in a new tab). A message will display in the browser saying "Logit.io APM Integration Test App Running".

Traces will now have been sent to your stack.

Launch Logit.io to view your traces

Launch APM

How to diagnose no data in stack

If you don't see data appearing in your stack after following this integration, take a look at the troubleshooting guide for steps to diagnose and resolve the problem or contact our support team and we'll be happy to assist.