Collect Laravel's request histogram data and custom metric for Datadog.
- PHP 8
- Laravel 9
- Toggleable via env value
- Can use the same API as DogstatsD
- Wrapped with toggle
- Error handled with
Log::warning
- Laravel middleware integration. By default, it contains these tags
- app
- environment
- action
- host
- status_code
- Any default Tags above can be disabled via config
- Error handled with
Log::warning
, maybe this should be changed into some configurable Error Handler Interface (e.g.: You want to send the error to your Error Tracker)
You can install the package via composer:
composer require mamitech/datadog-laravel-metric
You can publish the config file with:
php artisan vendor:publish --provider="Mamitech\DatadogLaravelMetric\DatadogLaravelMetricServiceProvider"
You can add alias to simplify call the DatadogLaravelMetric API
Add this into your Laravel config/app.php
inside the array of 'aliases'
'aliases' => [
// ommitted
'DatadogMetric' => Mamitech\DatadogLaravelMetric\Facades\DatadogLaravelMetric::class,
];
These are the configuration (as included in config/datadog-laravel-metric.php
)
Toggle the feature on/off
These are configs to initialize DogstatsD object, the main class for sending metric to DataDog
Datadog Agent (or specifically DogstatsD) host address
Datadog Agent (or specifically DogstatsD) port. The default is 8125.
from DogstatsD docs: The path to the DogStatsD Unix domain socket (overrides host and port). This is only supported with Agent v6+. https://docs.datadoghq.com/developers/dogstatsd/?code-lang=php&tab=hostagent#client-instantiation-parameters
The host of the DataDog you send metric data to. The default is 'https://app.datadoghq.com' .
API key you get on your DataDog account.
APP key you generate on your DataDog account.
Tags that you want to include everywhere every time sending metric from your app. Formatted as array with key-value.
Global prefix to each metric name you set.
List of important tags. Mainly used for Middleware.
The name of the app.
The environment the app runs.
Specific config regarding Middleware.
Specify the metric name for each request data. The default is request
.
On the Middleware, you can exclude certain tags from being sent to datadog. Put them in a comma separated string.
List of possible tags (by default those tags are sent as metric data):
- app
- environment
- action
- host
- status_code
On the Middleware, you can transform (modify) array of tags before it's sent to datadog.
To do that, you have to write your own class in your Laravel app and
implements \Mamitech\DatadogLaravelMetric\TagTransformer
.
Then, put the class names inside the array. For example, please refer to class TransformerForTest
inside tests/Middleware/SendRequestDatadogMetricTest.php
.
As mentioned in config file, these are the ENV values that can be set for configuration
DATADOG_METRIC_ENABLED
DATADOG_STATSD_SERVER
DATADOG_STATSD_PORT
DATADOG_SOCKET_PATH
DATADOG_HOST
DATADOG_API_KEY
DATADOG_APP_KEY
DATADOG_METRIC_PREFIX
DATADOG_TAGS_APP
DATADOG_TAGS_ENV
DATADOG_MIDDLEWARE_METRIC_NAME
DATADOG_MIDDLEWARE_EXCLUDE_TAGS
This Middleware should be auto-added (prepended) to your Laravel app's middleware via Service Provider
Functions from DogstatsD
This Library wraps DogstatsD so this can use functions from them. This also adds global toggle and error handle (it will try to call Log::warning
). The main functions are:
count
gauge
set
histogram
timer
distribution
use Mamitech\DatadogLaravelMetric\DatadogLaravelMetric;
$func = function () {
return 'hello this is testing for measure';
};
$startTime = microtime(true);
$result = $func();
$duration = microtime(true) - $startTime;
app(DatadogLaravelMetric::class)->measure('my.metric', ['tag1' => 'value1', 'tag2' => 'value2'], $duration);
use Mamitech\DatadogLaravelMetric\DatadogLaravelMetric;
$func = function () {
return 'hello i am measureFunc';
};
$result = app(DatadogLaravelMetric::class)->measureFunc('my.metric', ['tag1' => 'value1', 'tag2' => 'value2'], $func);
composer test
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.