Skip to content

Commit

Permalink
feat: more detailed event metrics from bot
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Dec 9, 2018
1 parent 3307b4f commit 3bca055
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 deletions.
12 changes: 9 additions & 3 deletions deploy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,27 @@ kubectl create ns isolex
Prepare your config file and create a secret in the cluster with the contents:

```shell
kubectl create secret generic isolex-config --from-file=.isolex.yml --namespace=isolex
kubectl describe secret isolex-config --namespace=isolex -o yaml
kubectl create configmap isolex-config --from-file=.isolex.yml --namespace=isolex
kubectl describe configmap isolex-config --namespace=isolex -o yaml
```

should print:

```yaml
apiVersion: v1
kind: Secret
kind: ConfigMap
metadata:
name: isolex-config
data:
".isolex.yml": "base64encoded==="
```
### Secrets
**Secrets should never be put in a ConfigMap**.
Secrets needed by the bot can be referenced from the container's environment using `!env`.

## Application

Create the application deployment:
Expand Down
15 changes: 15 additions & 0 deletions docs/style.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ This document covers Typescript and YAML style, explains some lint rules, and ma
- [Messages](#messages)
- [Paths](#paths)
- [Typescript](#typescript)
- [Arrays](#arrays)
- [Destructuring](#destructuring)
- [Entities](#entities)
- [Exports](#exports)
- [Generics](#generics)
- [Imports](#imports)
- [Order](#order)
- [Properties](#properties)
Expand Down Expand Up @@ -65,6 +67,12 @@ Messages are sent.

Dictionary objects (`{...}`) must always be treated as immutable.

### Arrays

Always use generic array types (`Array<Foo>`).

Declare empty arrays with `[]`.

### Destructuring

Destructuring is great, use it! Groups should be `{ spaced, out }` like imports (lint will warn about this, code can
Expand All @@ -84,6 +92,13 @@ Never use default exports.

Do not ever `export default` anything ever.

### Generics

Always use generic arrays (see [arrays](#arrays)).

Generic type names should start with `T` and have some meaningful name, like any other variable. For example: `TData`,
`TConfig`, `TKey` and `TValue`.

### Imports

Never use `../` imports. Use `src/` or `test/` instead. Local `./` imports _are_ allowed.
Expand Down
28 changes: 20 additions & 8 deletions src/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { StorageLogger, StorageLoggerOptions } from 'src/utils/StorageLogger';

import { BaseService } from './BaseService';
import { ServiceModule } from './module/ServiceModule';
import { incrementServiceCounter } from './utils/metrics/Service';

export interface BotData {
filters: Array<ServiceDefinition>;
Expand Down Expand Up @@ -144,14 +145,14 @@ export class Bot extends BaseService<BotData> implements Service {
return;
}

let commands: Array<Command> = [];
let matched = false;
for (const parser of this.parsers) {
try {
if (await parser.match(msg)) {
matched = true;
this.logger.debug({ msg, parser: parser.name }, 'parsing message');
const commands = await parser.parse(msg);
this.emitCommand(...commands);
commands.push(...await parser.parse(msg));
}
} catch (err) {
this.logger.error(err, 'error running parser');
Expand All @@ -161,6 +162,12 @@ export class Bot extends BaseService<BotData> implements Service {
if (!matched) {
this.logger.debug({ msg }, 'incoming message was not matched by any parsers');
}

if (!commands.length) {
this.logger.debug({ msg }, 'incoming message did not produce any commands');
}

return this.emitCommand(...commands);
}

/**
Expand Down Expand Up @@ -211,8 +218,11 @@ export class Bot extends BaseService<BotData> implements Service {
* Handle a command using the appropriate controller.
*/
protected async receiveCommand(cmd: Command): Promise<void> {
this.logger.debug({ cmd }, 'handling command');
this.cmdCounter.labels(this.id, this.kind, this.name).inc();
this.logger.debug({ cmd }, 'receiving command');
incrementServiceCounter(this, this.cmdCounter, {
commandNoun: cmd.noun,
commandVerb: cmd.verb,
});

if (!await this.checkFilters(cmd)) {
this.logger.warn({ cmd }, 'dropped command due to filters');
Expand All @@ -233,8 +243,10 @@ export class Bot extends BaseService<BotData> implements Service {
* Dispatch a message to the appropriate listeners (based on the context).
*/
protected async receiveMessage(msg: Message): Promise<void> {
this.logger.debug({ msg }, 'dispatching outgoing message');
this.msgCounter.labels(this.id, this.kind, this.name).inc();
this.logger.debug({ msg }, 'receiving outgoing message');
incrementServiceCounter(this, this.msgCounter, {
messageType: msg.type,
});

if (!await this.checkFilters(msg)) {
this.logger.warn({ msg }, 'dropped outgoing message due to filters');
Expand Down Expand Up @@ -263,14 +275,14 @@ export class Bot extends BaseService<BotData> implements Service {

this.cmdCounter = new Counter({
help: 'commands received by the bot',
labelNames: ['service_id', 'service_kind', 'service_name'],
labelNames: ['commandNoun', 'commandVerb', 'serviceId', 'serviceKind', 'serviceName'],
name: 'bot_command',
registers: [this.metrics],
});

this.msgCounter = new Counter({
help: 'messages received by the bot',
labelNames: ['service_id', 'service_kind', 'service_name'],
labelNames: ['messageType', 'serviceId', 'serviceKind', 'serviceName'],
name: 'bot_message',
registers: [this.metrics],
});
Expand Down
2 changes: 1 addition & 1 deletion src/listener/Listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ export interface Listener extends Service {
/**
* Callback from the auth controller to get a session from a uid.
*/
getSession(uid: string): Promise<Session | undefined>
getSession(uid: string): Promise<Session | undefined>;
}
23 changes: 23 additions & 0 deletions src/utils/metrics/Service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Counter, CounterConfiguration } from 'prom-client';

import { Service } from 'src/Service';

export function createServiceCounter(config: Partial<CounterConfiguration>): Counter {
const { labelNames = [] } = config;
labelNames.unshift('serviceId', 'serviceKind', 'serviceName');
return new Counter({
help: 'default service counter',
name: 'change_me',
...config,
labelNames,
});
}

export function incrementServiceCounter(svc: Service, counter: Counter, data: any) {
counter.inc({
...data,
serviceId: svc.id,
serviceKind: svc.kind,
serviceName: svc.name,
});
}

0 comments on commit 3bca055

Please sign in to comment.