nathanielks / cronitor-io-php
Cronitor PHP 库
Requires
- php: >=5.6
- symfony/yaml: ^4.4|^5.2|^6.0|^7.0
Requires (Dev)
- ext-curl: *
- codeception/aspect-mock: ^3.1
- friendsofphp/php-cs-fixer: ^2.18
- phpunit/phpunit: ^6.0
- dev-master
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.7
- 1.0.6
- 1.0.5.x-dev
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.3.1
- 0.3.0
- 0.2.0
- 0.1.5
- 0.1.4
- 0.1.2
- 0.1.1
- 0.1.0
- dev-dynamic-headers
- dev-generate-config
- dev-github-actions
- dev-monitor-tests
- dev-tests
- dev-unit-tests
- dev-fix-exception
- dev-fix/readme
- dev-fix/autoloading
- dev-fix/feedback
- dev-modernize
- dev-rebuild
- dev-feature/new-sdk
- dev-aflanagan-patch-2
- dev-aflanagan-patch-1
- dev-feature/update-names
- dev-develop
This package is auto-updated.
Last update: 2024-08-29 04:45:48 UTC
README
Cronitor 提供了背景任务、网站、API 以及任何可以发送或接收 HTTP 请求的服务的端到端监控。此库提供了从 PHP 编写的应用程序中方便地访问 Cronitor API 的方式。请参阅我们的 API 文档,了解配置监控和发送遥测ping的详细参考。
本指南
安装
composer require cronitor/cronitor-php
要手动使用,您可以包含源中的 init.php
文件。
require_once('/path/to/cronitor-php/init.php');
监控后台任务
$cronitor->job
函数将在调用您的函数之前和退出之后发送遥测事件。如果您的函数抛出异常,将发送 fail
事件(并且重新抛出异常)。
$cronitor = new Cronitor\Client('api_key_123'); $closureVar = time(); $cronitor->job('weekly-report-job', function() use ($closureVar){ new WeeklyReportJob($closureVar)->run(); });
发送遥测事件
如果您想发送心跳事件或希望对作业的遥测事件发送进行更精细的控制,您可以创建一个 Monitor
实例并调用 .ping
方法。
$cronitor = new Cronitor\Client('api_key_123'); $monitor = $cronitor->monitor('heartbeat-monitor'); $monitor->ping(); # a basic heartbeat event # optional params can be passed as kwargs # complete list - https://cronitor.io/docs/telemetry-api#parameters $monitor->ping(['state' => 'run']); # a job/process has started # a job/process has completed (include metrics for Cronitor to record) $monitor->ping(['state' => 'complete', 'metrics' => ['count' => 1000, 'error_count' => 17]]);
配置监控
您可以使用单个 YAML 文件配置所有监控。这可以作为部署或构建过程的一部分进行版本控制并同步到 Cronitor。有关可以设置的属性的所有详细信息,请参阅 监控 API 文档。
# read config file and set credentials (if included). $cronitor->readConfig('./cronitor.yaml'); # sync config file's monitors to Cronitor. $cronitor->applyConfig(); # send config file's monitors to Cronitor to validate correctness. # monitors will not be saved. $cronitor->validateConfig(); # save config to local YAML file (defaults to cronitor.yaml) $cronitor->generateConfig();
cronitor.yaml
文件包含三个顶级键 jobs
、checks
、heartbeats
。您可以通过声明监控 key
并定义 监控属性 在每个键下配置监控。
jobs: nightly-database-backup: schedule: 0 0 * * * notify: - devops-alert-pagerduty assertions: - metric.duration < 5 minutes send-welcome-email: schedule: every 10 minutes assertions: - metric.count > 0 - metric.duration < 30 seconds checks: cronitor-homepage: request: url: https://cronitor.io regions: - us-east-1 - eu-central-1 - ap-northeast-1 assertions: - response.code = 200 - response.time < 2s cronitor-telemetry-api: request: url: https://cronitor.link/ping assertions: - response.body contains ok - response.time < .25s heartbeats: production-deploy: notify: alerts: ["deploys-slack"] events: true # send alert when the event occurs
您还可以通过调用 $cronitor->monitors->put
创建和更新监控。有关可以设置的属性的所有详细信息,请参阅 Monitor API [文档](https://cronitor.io/docs/monitor-api#attributes).
$cronitor->monitors->put([ [ 'type' => 'job', 'key' => 'send-customer-invoices', 'schedule' => '0 0 * * *', 'assertions' => [ 'metric.duration < 5 min' ], 'notify' => ['devops-alerts-slack'] ], [ 'type' => 'check', 'key' => 'Cronitor Homepage', 'schedule' => 'every 45 seconds', 'request' => [ 'url' => 'https://cronitor.io' ] 'assertions' => [ 'response.code = 200', 'response.time < 1.5s', 'response.json "open_orders" < 2000' ] ] ])
暂停、重置、删除
require 'cronitor' $monitor = $cronitor->monitor('heartbeat-monitor'); $monitor->pause(24) # pause alerting for 24 hours $monitor->unpause() # alias for ->pause(0) $monitor->ok() # manually reset to a passing state alias for $monitor->ping({state: ok}) $monitor->delete() # destroy the monitor
包配置
需要使用您的账户的 API 密钥
配置此包,该密钥可在 账户设置 页面上找到。您还可以可选地指定 api_version
和 environment
。如果没有提供,将使用账户默认值。这些也可以使用环境变量 CRONITOR_API_KEY
、CRONITOR_API_VERSION
、CRONITOR_ENVIRONMENT
提供。
$apiKey = 'apiKey123'; $apiVersion = '2020-10-01'; $environment = 'staging'; $cronitor = new Cronitor\Client($apiKey, $apiVersion, $environment);
贡献
我们欢迎拉取请求和功能!通过参与此项目,您同意遵守 行为准则。
要贡献
分支,然后克隆仓库
git clone git@github.com:your-username/cronitor-php.git
推送到您的分支并 提交拉取请求