Browse Source

Slack webhook returns 400 missing_text_or_fallback_or_attachments #708 (#719)

pull/1/head
Evgeny Smyshlyaev 5 years ago committed by rozetko
parent
commit
d67bfd901e
  1. 30
      server/src/services/alert_service.ts
  2. 5
      server/src/services/analytics_service.ts
  3. 10
      server/src/services/notification_service.ts

30
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 * as AnalyticUnit from '../models/analytic_units';
import { Segment } from '../models/segment_model'; import { Segment } from '../models/segment_model';
import { availableReporter } from '../utils/reporter'; import { availableReporter } from '../utils/reporter';
@ -28,14 +28,14 @@ export class Alert {
protected async makeNotification(segment: Segment): Promise<Notification> { protected async makeNotification(segment: Segment): Promise<Notification> {
const meta = this.makeMeta(segment); const meta = this.makeMeta(segment);
const message = this.makeMessage(meta); const text = this.makeMessage(meta);
let result: Notification = { meta, message }; let result: Notification = { meta, text };
if(HASTIC_WEBHOOK_IMAGE_ENABLED) { if(HASTIC_WEBHOOK_IMAGE_ENABLED) {
try { try {
const image = await this.loadImage(); const image = await this.loadImage();
result.image = image; result.image = image;
} catch(err) { } 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( this._grafanaAvailableReporter = availableReporter(
['[OK] Grafana available', WebhookType.RECOVERY], ['[OK] Grafana available', WebhookType.RECOVERY],
['[FAILURE] Grafana unavailable for pulling data', WebhookType.FAILURE], ['[FAILURE] Grafana unavailable for pulling data', WebhookType.FAILURE],
this.sendMsg, this.sendMessage,
this.sendMsg this.sendMessage
); );
} }
@ -191,19 +191,15 @@ export class AlertService {
this._alerts[id].receive(segment); 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 now = Date.now();
const infoAlert: InfoMeta = { const infoAlert: MetaInfo = {
params: optionalInfo, params: optionalInfo,
type, type,
from: now, from: now,
to: now to: now
} }
try { sendNotification({ text, meta: infoAlert });
await sendNotification({ message, meta: infoAlert })
} catch(error) {
console.error(`can't send notification ${error}`);
};
} }
public sendGrafanaAvailableWebhook() { public sendGrafanaAvailableWebhook() {
@ -251,8 +247,8 @@ export class AlertService {
this._datasourceAvailableReporters[url] = availableReporter( this._datasourceAvailableReporters[url] = availableReporter(
[`[OK] Datasource ${url} available`, WebhookType.RECOVERY], [`[OK] Datasource ${url} available`, WebhookType.RECOVERY],
[`[FAILURE] Datasource ${url} unavailable`, WebhookType.FAILURE], [`[FAILURE] Datasource ${url} unavailable`, WebhookType.FAILURE],
this.sendMsg, this.sendMessage,
this.sendMsg this.sendMessage
); );
} }
return this._datasourceAvailableReporters[url]; return this._datasourceAvailableReporters[url];

5
server/src/services/analytics_service.ts

@ -182,14 +182,13 @@ export class AnalyticsService {
this.sendTask(this._queue.shift(), true); this.sendTask(this._queue.shift(), true);
} }
console.log(msg); console.log(msg);
this._alertService.sendMsg(msg, WebhookType.RECOVERY); this._alertService.sendMessage(msg, WebhookType.RECOVERY);
} }
private async _onAnalyticsDown() { private async _onAnalyticsDown() {
const msg = 'Analytics is down'; const msg = 'Analytics is down';
console.log(msg); console.log(msg);
// TODO: enable analytics down webhooks when it stops bouncing this._alertService.sendMessage(msg, WebhookType.FAILURE);
this._alertService.sendMsg(msg, WebhookType.FAILURE);
if(this._productionMode && !this._inDocker) { if(this._productionMode && !this._inDocker) {
await AnalyticsService._runAnalyticsProcess(this._zmqConnectionString); await AnalyticsService._runAnalyticsProcess(this._zmqConnectionString);
} }

10
server/src/services/notification_service.ts

@ -28,7 +28,7 @@ export declare type AnalyticMeta = {
message?: any message?: any
} }
export declare type InfoMeta = { export declare type MetaInfo = {
type: WebhookType, type: WebhookType,
from: number, from: number,
to: number, to: number,
@ -36,18 +36,18 @@ export declare type InfoMeta = {
} }
export declare type Notification = { export declare type Notification = {
message: string, text: string,
meta: InfoMeta | AnalyticMeta, meta: MetaInfo | AnalyticMeta,
image?: any image?: any
} }
export async function sendNotification(notification: Notification) { export async function sendNotification(notification: Notification) {
if(HASTIC_WEBHOOK_URL === null) { 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; return;
} }
notification.message += `\nInstance: ${HASTIC_INSTANCE_NAME}`; notification.text += `\nInstance: ${HASTIC_INSTANCE_NAME}`;
let data; let data;
if(HASTIC_WEBHOOK_TYPE === ContentType.JSON) { if(HASTIC_WEBHOOK_TYPE === ContentType.JSON) {

Loading…
Cancel
Save