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 { Segment } from '../models/segment_model';
import { availableReporter } from '../utils/reporter';
@ -28,14 +28,14 @@ export class Alert {
protected async makeNotification(segment: Segment): Promise<Notification> {
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];

5
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);
}

10
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) {

Loading…
Cancel
Save