otaodev/opentelemetrytracer

A basic use for the opentelemetry tracer in php applications

1.0.4 2024-03-20 19:03 UTC

This package is auto-updated.

Last update: 2024-04-20 19:19:29 UTC


README

A basic use for the opentelemetry tracer in php applications based in symfony framework

Instalation

composer require otaodev/opentelemetrytracer

Add the snippet to your app/config/services.yaml

services:
    Otaodev\Opentelemetrytracer\EventListener\TraceRouteListener:
        public: true
        autowire: true
        autoconfigure: true

Add the environments variables to your .env file

OTEL_PHP_AUTOLOAD_ENABLED=true
OTEL_SERVICE_NAME=YourServiceAppName
OTEL_TRACES_EXPORTER=console
OTEL_METRICS_EXPORTER=none
OTEL_LOGS_EXPORTER=none

OTEL_PROPAGATORS=baggage,tracecontext
OTEL_EXPORTER_OTLP_PROTOCOL=grpc
OTEL_EXPORTER_OTLP_ENDPOINT="http://the-collector-ip:4317"

Utilization

use the php8 attribute #[TraceRoute()] in your desired route, for example:

<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Otaodev\Opentelemetrytracer\Attribute\TraceRoute;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

#[Route('/', name: 'default_')]
class DefaultController extends AbstractController
{

    #[Route('/healthcheck', methods: ['GET'])]
    #[TraceRoute('any_route_name')]
    public function index(): Response
    {
        $return = $this->json('black tests is ON!');

        return $return;
    }
}

NOTE

If you not pass a name, the TraceRouter will assume the method name.