Help Docs

Instrument PHP application for OpenTelemetry

To monitor your PHP application with Site24x7 OpenTelemetry, follow the steps below.

Prerequisites

When instrumenting PHP applications, the most straightforward approach requires two prerequisites.

  1. Execute the following commands to see if PHP and Composer are available in your shell.
    php -v
    composer -v
  2. Verify if there is a composer.json file in your directory; if not, create one.
    {
    "require": {}
    }

Step 1:
Install the following packages.

composer require "php-http/guzzle7-adapter"
composer require open-telemetry/opentelemetry

Step 2:
Create a PHP file. For example, say GettingStarted.php in your project directory for OpenTelemetry configuration, as given below.

<?phpdeclare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use OpenTelemetry\SDK\Trace\SpanExporter\ConsoleSpanExporter;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\Contrib\OtlpHttp\Exporter as OtlpHttpExporter;
use OpenTelemetry\SDK\Trace\TracerProvider;echo 'Starting OtlpHttpExporter' . PHP_EOL;$tracerProvider = new TracerProvider(
new SimpleSpanProcessor(
new OtlpHttpExporter(new Client(), new HttpFactory(), new HttpFactory())
)
);$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');$rootSpan = $tracer->spanBuilder('root')->startSpan();
$rootSpan->activate();try {
$span1 = $tracer->spanBuilder('foo')->startSpan();
$span1->activate();
try {
$span2 = $tracer->spanBuilder('bar')->startSpan();
echo 'OpenTelemetry welcomes PHP' . PHP_EOL;
} finally {
$span2->end();
}
} finally {
$span1->end();
}
$rootSpan->end();


Step 3:
Set the endpoint in the environment and run the application.

env OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://otel.site24x7rum.com:4318/v1/trace
php GettingStarted.php
Note
Replace the endpoint and the application name.

 

Next Step

You are all set. Now the agent will start sending the performance metrics to your Site24x7 web client portal.

 

 

Was this document helpful?

Would you like to help us improve our documents? Tell us what you think we could do better.


We're sorry to hear that you're not satisfied with the document. We'd love to learn what we could do to improve the experience.


Thanks for taking the time to share your feedback. We'll use your feedback to improve our online help resources.

Shortlink has been copied!