Getting Started
This page will show you how to get started with OpenTelemetry in PHP.
You will learn how you can instrument a simple PHP application automatically, in such a way that traces, metrics and logs are emitted to the console.
Prerequisites¶
Ensure that you have the following installed locally: open source
Before you get started make sure that you have both available in your shell:
Example Application¶
The following example uses a basic Slim application. If you are not using Slim, that's ok — you can use OpenTelemetry PHP with other web frameworks as well, such as Wordpress, Symfony and Laravel. For a complete list of libraries for supported frameworks, see the registry.
Dependencies¶
In an empty directory create a minimal composer.json
file in your directory:
In an empty directory create a minimal composer.json
file in your directory:
Create and launch an HTTP Server¶
In that same directory, create a file called index.php
Export to Console¶
In your directory create a file called GettingStarted.php
with the following
content:
Run the application with the following command and open http://localhost:8080/rolldice in your web browser to ensure it is working.
Instrumentation¶
Next, you’ll use the OpenTelemetry PHP extension to automatically instrument the application at launch time.
- Since the extension is built from source, you need to setup some build tools
{{< tabpane lang=shell persistLang=false >}}
{{< tab "Linux (apt)" >}}sudo apt-get install gcc make autoconf{{< /tab >}}
{{< tab "MacOS (homebrew)" >}}brew install gcc make autoconf{{< /tab >}}
{{< /tabpane >}}
- Build the extension with
PECL
:
{{% alert title="Note" color="warning" %}}If you want to pickle or the docker-specific use php-extension-installer to build/install the extension, read the instructions here. {{% /alert %}}
- Add the extension to your
php.ini
file:
- Verify that the extension is installed and enabled:
- Add additional dependencies to your application, which are required for the automatic instrumentation of your code:
With the OpenTelemetry PHP extension set up you can now run your application and automatically instrument it at launch time:
Open http://localhost:8080/rolldice in your web browser and reload the page a few times. After a while you should see the spans printed to your console:
View example output
What's next?¶
For more:
- Run this example with another exporter for telemetry data.
- Try automatic instrumentation on one of your own apps.
- Learn about manual instrumentation and try out more examples.