grafana -> notification-provider -> mattermost | see https://gitlab.fedy95.com/dev/notification-provider
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

117 lines
3.9 KiB

<?php
declare(strict_types=1);
namespace App\Controller\Api\v1;
use App\Model\Alertmanager\AlertmanagerMessage;
use App\Model\Grafana\GrafanaMessage;
use App\Service\Mattermost\MattermostServiceInterface;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Annotations as OA;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
use Throwable;
class MattermostController extends AbstractController
{
protected LoggerInterface $logger;
protected SerializerInterface $serializer;
protected MattermostServiceInterface $mattermostService;
public function __construct(
LoggerInterface $logger,
SerializerInterface $serializer,
MattermostServiceInterface $mattermostService,
) {
$this->logger = $logger;
$this->serializer = $serializer;
$this->mattermostService = $mattermostService;
}
/**
* Send message to a mattermost chat from alertmanager.
*
* Docs <br>
* - <a href="https://grafana.com/docs/grafana/latest/alerting/old-alerting/notifications/#webhook" target="_blank">alertmanager</a> <br>
* - <a href="https://docs.mattermost.com/developer/webhooks-incoming.html" target="_blank">mattermost</a> <br>
*
* @Route("/api/v1/send_message/alertmanager", methods={"POST"})
* @OA\Tag(name="mattermost")
* @OA\RequestBody(
* @OA\JsonContent(
* ref=@Model(type=AlertmanagerMessage::class)
* )
* )
* @OA\Response(
* response=200,
* description="Success delivery"
* )
*/
public function sendMessageFromAlertmanager(Request $request): Response
{
try {
$this->logger->notice($request->getContent());
$message = $this->serializer->deserialize(
$request->getContent(),
AlertmanagerMessage::class,
'json'
);
$response = $this->mattermostService->sendAlertmanagerMessage($message);
return new Response($response->getBody()->getContents());
} catch (Throwable $e) {
$this->logger->critical($e->getMessage());
return new Response(
$e->getMessage(),
0 === $e->getCode() ? Response::HTTP_INTERNAL_SERVER_ERROR : $e->getCode()
);
}
}
/**
* Send message to a mattermost chat from grafana.
*
* Docs <br>
* - <a href="https://grafana.com/docs/grafana/latest/alerting/old-alerting/notifications/#webhook" target="_blank">grafana</a> <br>
* - <a href="https://docs.mattermost.com/developer/webhooks-incoming.html" target="_blank">mattermost</a> <br>
*
* @Route("/api/v1/send_message/grafana", methods={"POST"})
* @OA\Tag(name="mattermost")
* @OA\RequestBody(
* @OA\JsonContent(
* ref=@Model(type=GrafanaMessage::class)
* )
* )
* @OA\Response(
* response=200,
* description="Success delivery"
* )
*/
public function sendMessageFromGrafana(Request $request): Response
{
try {
$this->logger->notice($request->getContent());
$message = $this->serializer->deserialize(
$request->getContent(),
GrafanaMessage::class,
'json'
);
$response = $this->mattermostService->sendGrafanaMessage($message);
return new Response($response->getBody()->getContents());
} catch (Throwable $e) {
$this->logger->critical($e->getMessage());
return new Response(
$e->getMessage(),
0 === $e->getCode() ? Response::HTTP_INTERNAL_SERVER_ERROR : $e->getCode()
);
}
}
}