konekt/opsgenie-laravel

OpsGenie 通知通道用于 Laravel

1.3.0 2024-03-15 08:37 UTC

This package is auto-updated.

Last update: 2024-09-15 09:36:25 UTC


README

Tests Packagist Stable Version Packagist downloads StyleCI MIT Software License

本软件包使 Laravel 9 - 11 应用程序能够向 OpsGenie 发送通知。

安装

composer require konekt/opsgenie-laravel

配置

将您的身份验证令牌和端点配置添加到应用程序的 config/services.php 文件中

// config/services.php
...

'opsgenie' => [
    'auth_token' => env('OPSGENIE_AUTH_TOKEN'),
    'europe' => true, // OPTIONAL: if true, then the EU API endpoint will be used
    // 'endpoint' => 'https://some.custom.endpoint/', // VERY OPTIONAL: in case you use a non-official endpoint
],
...

关于 OpsGenie API 密钥的说明

要创建警报,您需要从 API 集成 获取 API 密钥,而不是 普通 API 密钥

❌ 普通API密钥可以在“设置”->“API密钥管理”中找到

normal_api_key.png

✔ 集成API密钥可以在“团队”->“{团队名称}”->“集成”中找到

integration_api_key.png

更多详细信息请参阅此 Atlassian 论坛线程

使用方法

截至编写本文时,仅实现了 2 个 OpsGenie 命令

独立模式

要向 OpsGenie 发送命令而不使用 Laravel 通知子系统,您需要获取客户端,创建命令并执行。

创建警报

use Konekt\OpsGenie\Client\OpsGenieClient;
use Konekt\OpsGenie\Commands\CreateAlert;

$genie = app(OpsGenieClient::class);
$genie->execute(CreateAlert::withMessage('I am an alert message'));

发送心跳

use Konekt\OpsGenie\Client\OpsGenieClient;
use Konekt\OpsGenie\Commands\PingHeartbeat;

$genie = app(OpsGenieClient::class);
$genie->execute(new PingHeartbeat('name of the heartbeat'));

Laravel 通知

您可以在通知类中的 via() 方法中使用 OpsGenie 通道。以下示例在 OpsGenie 中创建具有给定消息的警报

use Illuminate\Notifications\Notification;
use Konekt\OpsGenie\Commands\CreateAlert;
use Konekt\OpsGenie\Contracts\OpsGenieCommand;
use Konekt\OpsGenie\Contracts\OpsGenieNotification;
use Konekt\OpsGenie\Notification\OpsGenieChannel;

class SiteProblem extends Notification implements OpsGenieNotification
{
    private string $message;

    public function __construct(string $message)
    {
        $this->message = $message;
    }

    public function via($notifiable)
    {
        return [OpsGenieChannel::class];
    }

    public function toOpsGenie($notifiable): OpsGenieCommand
    {
        return CreateAlert::withMessage($this->message);
    }
}

要触发通知的发送,请使用

Notification::send(['*'], new SiteProblem('Hey, there is a problem here'));

除了触发警报外,您创建的 Laravel 通知还可以发送任何 OpsGenie 命令,例如发送心跳

use Illuminate\Notifications\Notification;
use Konekt\OpsGenie\Commands\PingHeartbeat;
use Konekt\OpsGenie\Contracts\OpsGenieCommand;
use Konekt\OpsGenie\Contracts\OpsGenieNotification;
use Konekt\OpsGenie\Notification\OpsGenieChannel;

class ERPSyncCompleted extends Notification implements OpsGenieNotification
{
    private string $heartbeat;

    public function __construct(string $heartbeat)
    {
        $this->heartbeat = $heartbeat;
    }

    public function via($notifiable)
    {
        return [OpsGenieChannel::class];
    }

    public function toOpsGenie($notifiable): OpsGenieCommand
    {
        return new PingHeartbeat($this->heartbeat);
    }
}

要发送此通知,请使用

Notification::send(['*'], new ERPSyncCompleted('erp-sync-heartbeat'));

自定义警报

可以设置创建的警报的进一步属性,例如 设置优先级 或添加描述等。

这可以通过在 toOpsGenie 方法中实例化 CreateAlert 命令等操作来实现

class CriticalConditionDetected extends Notification implements OpsGenieNotification
{
    private string $message;

    public function __construct(string $message)
    {
        $this->message = $message;
    }

    public function via($notifiable)
    {
        return [OpsGenieChannel::class];
    }

    public function toOpsGenie($notifiable): OpsGenieCommand
    {
        $alert = new Alert('Shit hit the fan', ['priority' => 'P1']);
        
        return new CreateAlert($alert);
    }
}