matthiasbayer/datadog-client

此包已被弃用,不再维护。未建议替代包。

独立 DataDog PHP 客户端,无需 datadog 代理即可工作

v1.1.2 2019-10-20 12:33 UTC

This package is not auto-updated.

Last update: 2022-11-07 20:16:45 UTC


README

轻量级仅 PHP 的 datadog 客户端

这是一个简单的 datadog php 客户端,无需设置 DataDog 代理。此库目前支持发送指标数据或事件,因为这些都是最常用的用例。请随意扩展以满足您的需求或发送拉取请求。

安装

可以使用 composer 安装此库

composer require matthiasbayer/datadog-client

入门

要开始向 Datadog 发送请求,您首先需要提供您的个人 API 密钥。您可以在 Datadog API 设置 中找到此密钥。

$client = new Client('mysecretapikey');

发送事件

可以使用提供的 Event 类直接将事件发送到 Datadog

$myEvent = new Event('This is my test event', 'Optional event title');
$client->sendEvent($myEvent);

可以使用常规方式设置其他属性

$myEvent->setAlertType(Event::TYPE_ERROR);

此外,还有一个快捷方法可以为您处理所有这些

// Create and send event in one call
$client->event('My test event', 'Optional event title', array(
    'alert_type' => Event::TYPE_ERROR
));

事件属性

dateHappened

类型: integer

事件的时间戳。默认为当前时间戳。

priority

类型: Event::PRIORITY_

事件优先级。Datadog 支持 LOW 和 NORMAL

alertType

类型: Event::TYPE_

事件 alert_type。Datadog 支持 INFO,WARNING,ERROR 和 SUCCESS

aggregationKey

类型: string

用于对事件进行分组的任意字符串

发送指标

Datadog 要求将指标数据封装到一系列中。一个系列包含一个或多个指标对象,每个对象包含一个或多个测量点。

// Create Series
$mySeries = new Series();

// Create a new metric with multiple points
$firstMetric = new Metric('my.metric.name', array(
    array(20),            // Dummy points
    array(13456789, 30),  // Point with timestamp set
    array(40),            // If not set, timestamp default to current time
));

// Create a new metric with one point
$secondMetric = new Metric('my.second.metric', array(20));

$mySeries->addMetrics(array(
    $firstMetric,
    $secondMetric
));

// Send data
$client->sendSeries($mySeries);

如果您想一次发送一个指标,您可以使用 Client::sendMetric 方法

// Create a new metric with multiple points
$myMetric = new Metric('my.metric.name', array(
    array(20),            // Dummy points
    array(13456789, 30),  // Point with timestamp set
    array(40),            // If not set, timestamp default to current time
));

$client->sendMetric($myMetric);

同样,还有一个快捷方法可以为您处理这些

$client->metric('my.test.metric', array(
    array(20),
    array(13456789, 30),
    array(40),
));

指标属性

name

类型: string

指标名称。可以包含下划线或点以方便分组,例如 web.exception.404

type

类型: Metric::TYPE_

指标类型。Datadog 支持 gauge 或 counter

host

类型: string

源机器的主机名

分数

类型: 数组

数据点的数组。

一个点由可选的时间戳和数值组成。如果没有指定时间戳,将使用当前时间戳。顺序很重要。如果指定了时间戳,它应该是第一个值。

示例

  // Timestamp will be set to time()
  $simplePoint = array(20);
  
  // Set timestamp to explicit value
  $pointWithTimestamp = array(1234567, 20);