Logo

dev-resources.site

for different kinds of informations.

Datadog PHP APM filtering

Published at
2/21/2024
Categories
datadog
php
apm
Author
zarmin
Categories
3 categories in total
datadog
open
php
open
apm
open
Author
6 person written this
zarmin
open
Datadog PHP APM filtering

A client needed a solution to filter PHP APM traces before sending them to the Datadog server to reduce ingestion fees.

The filter rules were to exclude everything except requests exceeding 1 second and errors (HTTP code >= 400, exceptions, PHP errors).

They still wanted to utilize automatic instrumentation for several libraries (e.g., Laravel, Curl, PDO).

(The client is still using PHP 7.4, which is end-of-life; I know - they will upgrade to 8.x later.)

This auto-prepend script will filter out the specified request traces (returning from the function will retain the trace).

<?php

// dd-filter.php

register_shutdown_function(function() {
  // config
  $TIME_LIMIT_SEC = 1.0;

  // exit if Datadog (DDTrace) is NOT loaded
  if (!class_exists('\DDTrace\GlobalTracer') || !class_exists('\DDTrace\Tag')) {
    return;
  }

  // get HTTP status code
  $httpResponseCode = http_response_code();
  if ($httpResponseCode >= 400) {
    return; // keep DD trace
  }

  // is the script exited with an error?
  $lastError = error_get_last();
  if ($lastError) {
    return; // keep DD trace
  }

  // get current request time
  if (empty($_SERVER['REQUEST_TIME_FLOAT'])) {
    return; // keep DD trace 
  }
  $startTime = $_SERVER['REQUEST_TIME_FLOAT'];
  $currentTime = microtime(true);
  $elapsedTime = max($currentTime - $startTime, 0.0);

  if ($elapsedTime > $TIME_LIMIT_SEC) {
    return; // keep DD trace
  }

  // drop DD trace
  $tracer = \DDTrace\GlobalTracer::get();
  $span = $tracer->getActiveSpan();
  if (null !== $span) {
    $span->setTag(\DDTrace\Tag::MANUAL_DROP, true);
  }
});
Enter fullscreen mode Exit fullscreen mode

(Yes, I know it would be better if the configuration, for example, TIME_LIMIT_SEC, were kept in a separate file.)

This can be set up in the following way:

  • Place this script to /etc/php/7.4/datadog-filter/dd-filter.php
  • Set the script for auto-prepending in /etc/php/7.4/fpm/php.ini: auto_prepend_file = /etc/php/7.4/datadog-filter/dd-filter.php
  • Restart PHP-FPM: systemctl restart php7.4-fpm

(The client is using their app on a Debian 12 Linux VM.)

datadog Article's
30 articles in total
Favicon
Distributed Tracing in Microservices: AWS X-Ray vs DataDog
Favicon
Understanding Datadog: Monitoring and Observability for Modern Applications
Favicon
Golang: Como a observabilidade e profiling revelaram um throttling quase indetectável
Favicon
Datadog : how to filter metrics on tag "team"
Favicon
Self-Healing and Monitoring: A comprehensive guide to revolutionizing System Resilience Through Automation
Favicon
Setting Up Datadog Agent for Nginx Log Collection on AWS EC2
Favicon
How to Monitor your AWS EC2/Workspace with Datadog
Favicon
Getting Started with DataDog's APM: A Developer's Guide
Favicon
Integrating Keycloak with Datadog: Enabling Keycloak Traces in Kubernetes using Datadog APM
Favicon
Monitor EC2 instance metrics with Datadog (step-by-step)
Favicon
Datadog PHP APM filtering
Favicon
Datadog Resource Inventory
Favicon
The Saga of the Replica Lag
Favicon
Send the logs of your Shuttle-powered backend to Datadog
Favicon
Monitoring SQS with Datadog
Favicon
Forecast log costs pre-production
Favicon
Datadog-Cloudformation Integration using Serverless Framework
Favicon
Datadog Alternative: When to Move Out?
Favicon
New dog is ready to rock
Favicon
Datadog vs New Relic: A Duel for Dominance in LLM Observability Platforms
Favicon
Quick tip: Monitoring SingleStoreDB Cloud using Datadog
Favicon
Full Stack Observability: Connecting AWS with Datadog
Favicon
Jump into Datadog With AWS Serverless CDK App
Favicon
How to use DataDog to find utilisation of AWS EBS volume
Favicon
Monitorando RabbitMQ implantado no Kubernetes como Cluster Operator com Datadog Autodiscovery
Favicon
Find your application's hidden secrets using opentelemetry
Favicon
Datadog - export more than 5000 records.
Favicon
Send the logs of your Shuttle-powered backend to Datadog (outdated)
Favicon
CockroachDB: trace logging with Datadog
Favicon
CI/CD con Synthetic Monitoring de Datadog y Bitbucket pipelines

Featured ones: