Skip to content

Commit

Permalink
feat(listener): instrument discord and slack more
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Oct 13, 2019
1 parent a5fafd3 commit ba82c7c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
24 changes: 19 additions & 5 deletions src/listener/DiscordListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Message } from '../entity/Message';
import { InvalidArgumentError } from '../error/InvalidArgumentError';
import { NotFoundError } from '../error/NotFoundError';
import { doesExist, mustExist } from '../utils';
import { createServiceCounter } from '../utils/metrics';
import { createServiceCounter, incrementServiceCounter } from '../utils/metrics';
import { TYPE_TEXT } from '../utils/Mime';
import { SessionListener } from './SessionListener';

Expand All @@ -42,6 +42,7 @@ export class DiscordListener extends SessionListener<DiscordListenerData> implem
protected readonly threads: Map<string, DiscordMessage>;

protected readonly onCounter: Counter;
protected readonly sendCounter: Counter;

constructor(options: BotServiceOptions<DiscordListenerData>) {
super(options, 'isolex#/definitions/service-listener-discord');
Expand All @@ -55,6 +56,11 @@ export class DiscordListener extends SessionListener<DiscordListenerData> implem
labelNames: ['eventKind'],
name: 'discord_event',
});
this.sendCounter = createServiceCounter(metrics, {
help: 'events send from discord client',
labelNames: ['sendType'],
name: 'discord_send',
});
}

public async start() {
Expand Down Expand Up @@ -142,11 +148,8 @@ export class DiscordListener extends SessionListener<DiscordListenerData> implem
}

protected countEvent(eventKind: string) {
this.onCounter.inc({
incrementServiceCounter(this, this.onCounter, {
eventKind,
serviceId: this.id,
serviceKind: this.kind,
serviceName: this.name,
});
}

Expand All @@ -158,11 +161,18 @@ export class DiscordListener extends SessionListener<DiscordListenerData> implem
}

if (msg.body.length > 0) {
incrementServiceCounter(this, this.sendCounter, {
sendType: 'thread',
});
await thread.reply(escape(msg.body));
}

const reactions = this.filterEmoji(msg.reactions);
for (const reaction of reactions) {
incrementServiceCounter(this, this.sendCounter, {
sendType: 'reaction',
});

this.logger.debug({ reaction }, 'adding reaction to thread');
await thread.react(reaction);
}
Expand All @@ -182,6 +192,10 @@ export class DiscordListener extends SessionListener<DiscordListenerData> implem
return;
}

incrementServiceCounter(this, this.sendCounter, {
sendType: 'channel',
});

await channel.send(escape(msg.body));
return;
}
Expand Down
40 changes: 36 additions & 4 deletions src/listener/SlackListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import escape from 'escape-html';
import { isNil } from 'lodash';
import { find as findEmoji } from 'node-emoji';
import { BaseError, Container, Inject } from 'noicejs';
import { Counter } from 'prom-client';

import { FetchOptions, Listener, ListenerData } from '.';
import { INJECT_CLOCK } from '../BaseService';
import { INJECT_CLOCK, INJECT_METRICS } from '../BaseService';
import { BotServiceOptions } from '../BotService';
import { Message } from '../entity/Message';
import { NotFoundError } from '../error/NotFoundError';
import { doesExist, mustExist } from '../utils';
import { SlackLogger } from '../utils/logger/SlackLogger';
import { createServiceCounter, incrementServiceCounter } from '../utils/metrics';
import { TYPE_TEXT } from '../utils/Mime';
import { SessionListener } from './SessionListener';

Expand Down Expand Up @@ -48,16 +50,29 @@ export interface SlackSearchResults extends WebAPICallResult {
messages: Array<SlackMessage>;
}

@Inject(INJECT_CLOCK)
@Inject(INJECT_CLOCK, INJECT_METRICS)
export class SlackListener extends SessionListener<SlackListenerData> implements Listener {
protected container: Container;
protected readonly container: Container;
protected readonly onCounter: Counter;
protected readonly sendCounter: Counter;
protected rtmClient?: RTMClient;
protected webClient?: WebClient;

constructor(options: BotServiceOptions<SlackListenerData>) {
super(options, 'isolex#/definitions/service-listener-slack');

this.container = options.container;
const metrics = mustExist(options[INJECT_METRICS]);
this.onCounter = createServiceCounter(metrics, {
help: 'events received from slack client',
labelNames: ['eventKind'],
name: 'slack_event',
});
this.sendCounter = createServiceCounter(metrics, {
help: 'sends through slack client',
labelNames: ['sendType'],
name: 'slack_send',
});
}

public async send(msg: Message): Promise<void> {
Expand Down Expand Up @@ -100,12 +115,14 @@ export class SlackListener extends SessionListener<SlackListenerData> implements
this.webClient = new WebClient(this.data.token.web, { logger });

this.rtmClient.on('message', (msg) => {
this.countEvent('message');
this.convertMessage(msg).then((it) => this.bot.receive(it)).catch((err) => {
this.logger.error(err, 'error receiving message');
});
});

this.rtmClient.on('reaction_added', (reaction) => {
this.countEvent('reaction_added');
this.convertReaction(reaction).then((msg) => this.bot.receive(msg)).catch((err) => {
this.logger.error(err, 'error adding reaction');
});
Expand All @@ -122,6 +139,12 @@ export class SlackListener extends SessionListener<SlackListenerData> implements
}
}

protected countEvent(eventKind: string) {
incrementServiceCounter(this, this.onCounter, {
eventKind,
});
}

protected async sendReactions(msg: Message): Promise<void> {
const ctx = mustExist(msg.context);
const web = mustExist(this.webClient);
Expand All @@ -130,6 +153,11 @@ export class SlackListener extends SessionListener<SlackListenerData> implements
const result = findEmoji(reaction);

if (doesExist(result)) {
this.logger.debug({ channel: ctx.channel, reaction: result }, 'sending reaction to channel');
incrementServiceCounter(this, this.sendCounter, {
sendType: 'reaction',
});

await web.reactions.add({
channel: ctx.channel.id,
name: result.key,
Expand All @@ -146,7 +174,11 @@ export class SlackListener extends SessionListener<SlackListenerData> implements
const rtm = mustExist(this.rtmClient);

if (ctx.channel.id !== '') {
this.logger.debug({ channel: ctx.channel }, 'sending message to channel');
this.logger.debug({ channel: ctx.channel, text: msg.body }, 'sending message to channel');
incrementServiceCounter(this, this.sendCounter, {
sendType: 'text',
});

const result = await rtm.sendMessage(escape(msg.body), ctx.channel.id);
if (doesExist(result.error)) {
const err = new BaseError(result.error.msg);
Expand Down

0 comments on commit ba82c7c

Please sign in to comment.