From d67bfd901ede5fb89f3fb9ff5c23bec9e02ed82a Mon Sep 17 00:00:00 2001 From: Evgeny Smyshlyaev Date: Tue, 23 Jul 2019 21:55:19 +0300 Subject: [PATCH] Slack webhook returns 400 missing_text_or_fallback_or_attachments #708 (#719) --- server/src/services/alert_service.ts | 30 +++++++++------------ server/src/services/analytics_service.ts | 5 ++-- server/src/services/notification_service.ts | 10 +++---- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/server/src/services/alert_service.ts b/server/src/services/alert_service.ts index 99bb1ac..7eef6c4 100644 --- a/server/src/services/alert_service.ts +++ b/server/src/services/alert_service.ts @@ -1,4 +1,4 @@ -import { sendNotification, InfoMeta, AnalyticMeta, WebhookType, Notification } from './notification_service'; +import { sendNotification, MetaInfo, AnalyticMeta, WebhookType, Notification } from './notification_service'; import * as AnalyticUnit from '../models/analytic_units'; import { Segment } from '../models/segment_model'; import { availableReporter } from '../utils/reporter'; @@ -28,14 +28,14 @@ export class Alert { protected async makeNotification(segment: Segment): Promise { const meta = this.makeMeta(segment); - const message = this.makeMessage(meta); - let result: Notification = { meta, message }; + const text = this.makeMessage(meta); + let result: Notification = { meta, text }; if(HASTIC_WEBHOOK_IMAGE_ENABLED) { try { - const image = await this.loadImage(); - result.image = image; + const image = await this.loadImage(); + result.image = image; } catch(err) { - console.error(`Can't load alert image: ${err}. Check that API key has admin permissions`); + console.error(`Can't load alert image: ${err}. Check that API key has admin permissions`); } } @@ -172,8 +172,8 @@ export class AlertService { this._grafanaAvailableReporter = availableReporter( ['[OK] Grafana available', WebhookType.RECOVERY], ['[FAILURE] Grafana unavailable for pulling data', WebhookType.FAILURE], - this.sendMsg, - this.sendMsg + this.sendMessage, + this.sendMessage ); } @@ -191,19 +191,15 @@ export class AlertService { this._alerts[id].receive(segment); }; - public async sendMsg(message: string, type: WebhookType, optionalInfo = {}) { + public sendMessage(text: string, type: WebhookType, optionalInfo = {}) { const now = Date.now(); - const infoAlert: InfoMeta = { + const infoAlert: MetaInfo = { params: optionalInfo, type, from: now, to: now } - try { - await sendNotification({ message, meta: infoAlert }) - } catch(error) { - console.error(`can't send notification ${error}`); - }; + sendNotification({ text, meta: infoAlert }); } public sendGrafanaAvailableWebhook() { @@ -251,8 +247,8 @@ export class AlertService { this._datasourceAvailableReporters[url] = availableReporter( [`[OK] Datasource ${url} available`, WebhookType.RECOVERY], [`[FAILURE] Datasource ${url} unavailable`, WebhookType.FAILURE], - this.sendMsg, - this.sendMsg + this.sendMessage, + this.sendMessage ); } return this._datasourceAvailableReporters[url]; diff --git a/server/src/services/analytics_service.ts b/server/src/services/analytics_service.ts index 4c3e32e..81a1fa2 100644 --- a/server/src/services/analytics_service.ts +++ b/server/src/services/analytics_service.ts @@ -182,14 +182,13 @@ export class AnalyticsService { this.sendTask(this._queue.shift(), true); } console.log(msg); - this._alertService.sendMsg(msg, WebhookType.RECOVERY); + this._alertService.sendMessage(msg, WebhookType.RECOVERY); } private async _onAnalyticsDown() { const msg = 'Analytics is down'; console.log(msg); - // TODO: enable analytics down webhooks when it stops bouncing - this._alertService.sendMsg(msg, WebhookType.FAILURE); + this._alertService.sendMessage(msg, WebhookType.FAILURE); if(this._productionMode && !this._inDocker) { await AnalyticsService._runAnalyticsProcess(this._zmqConnectionString); } diff --git a/server/src/services/notification_service.ts b/server/src/services/notification_service.ts index 2490496..7fd369b 100644 --- a/server/src/services/notification_service.ts +++ b/server/src/services/notification_service.ts @@ -28,7 +28,7 @@ export declare type AnalyticMeta = { message?: any } -export declare type InfoMeta = { +export declare type MetaInfo = { type: WebhookType, from: number, to: number, @@ -36,18 +36,18 @@ export declare type InfoMeta = { } export declare type Notification = { - message: string, - meta: InfoMeta | AnalyticMeta, + text: string, + meta: MetaInfo | AnalyticMeta, image?: any } export async function sendNotification(notification: Notification) { if(HASTIC_WEBHOOK_URL === null) { - console.log(`HASTIC_WEBHOOK_URL is not set, skip sending notification: ${notification.message}`); + console.log(`HASTIC_WEBHOOK_URL is not set, skip sending notification: ${notification.text}`); return; } - notification.message += `\nInstance: ${HASTIC_INSTANCE_NAME}`; + notification.text += `\nInstance: ${HASTIC_INSTANCE_NAME}`; let data; if(HASTIC_WEBHOOK_TYPE === ContentType.JSON) {