Giter VIP home page Giter VIP logo

datadog-laravel-metric's Introduction

datadog-laravel-metric

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Collect Laravel's request histogram data and custom metric for Datadog.

Requirement

  • PHP 8
  • Laravel 9

Feature

  • 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

Limitation

  • 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)

Installation

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"

(optional) add alias to your app

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,
];

Configs

These are the configuration (as included in config/datadog-laravel-metric.php)

'enabled'

Toggle the feature on/off

'init_config'

These are configs to initialize DogstatsD object, the main class for sending metric to DataDog

'host'

Datadog Agent (or specifically DogstatsD) host address

'port'

Datadog Agent (or specifically DogstatsD) port. The default is 8125.

'socket_path'

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

'datadog_host'

The host of the DataDog you send metric data to. The default is 'https://app.datadoghq.com' .

'api_key'

API key you get on your DataDog account.

'app_key'

APP key you generate on your DataDog account.

'global_tags'

Tags that you want to include everywhere every time sending metric from your app. Formatted as array with key-value.

'metric_prefix'

Global prefix to each metric name you set.

'tags'

List of important tags. Mainly used for Middleware.

'app'

The name of the app.

'env'

The environment the app runs.

'middleware'

Specific config regarding Middleware.

'metric_name'

Specify the metric name for each request data. The default is request.

'exclude_tags'

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

'tag_transformers'

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.

ENV value

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

Usage

Laravel Middleware SendRequestDatadogMetric

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

Custom Metric ( measure )

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);

Custom Metric Function ( measureFunc )

use Mamitech\DatadogLaravelMetric\DatadogLaravelMetric;

$func = function () {
    return 'hello i am measureFunc';
};

$result = app(DatadogLaravelMetric::class)->measureFunc('my.metric', ['tag1' => 'value1', 'tag2' => 'value2'], $func);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

datadog-laravel-metric's People

Contributors

mwnnn avatar dependabot[bot] avatar vynhart avatar github-actions[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.