Skip to content

Commit

Permalink
Create LevitateEvents class
Browse files Browse the repository at this point in the history
  • Loading branch information
chtushar committed Nov 10, 2023
1 parent 23ec9e3 commit 9fa1d15
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 11 deletions.
7 changes: 3 additions & 4 deletions playground/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ const openapm = new OpenAPM({
customPathsToMask: [/\b\d+(?:,\d+)*\b/gm],
excludeDefaultLabels: ['host', 'program']
});
openapm.on('application_started', () => {
console.log('out');
});
openapm.instrument('express');
openapm.instrument('mysql');

openapm.events.on('application_started', () => {
console.log('hello world');
});

const app = express();

const pool = mysql2.createPool(
Expand Down
12 changes: 7 additions & 5 deletions src/OpenAPM.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as os from 'os';
import http from 'http';
import EventEmitter from 'events';
import ResponseTime from 'response-time';
import promClient from 'prom-client';

Expand All @@ -23,6 +22,7 @@ import {
import { instrumentExpress } from './clients/express';
import { instrumentMySQL } from './clients/mysql2';
import { instrumentNestFactory } from './clients/nestjs';
import { LevitateConfig, LevitateEvents } from './levitate/events';

export type ExtractFromParams = {
from: 'params';
Expand Down Expand Up @@ -62,6 +62,8 @@ export interface OpenAPMOptions {
customPathsToMask?: Array<RegExp>;
/** Skip mentioned labels */
excludeDefaultLabels?: Array<DefaultLabels>;
/** Levitate Config */
levitateConfig?: LevitateConfig;
}

export type SupportedModules = 'express' | 'mysql' | 'nestjs';
Expand All @@ -74,7 +76,7 @@ const moduleNames = {

const packageJson = getPackageJson();

export class OpenAPM {
export class OpenAPM extends LevitateEvents {
private path: string;
private metricsServerPort: number;
private environment: string;
Expand All @@ -88,9 +90,9 @@ export class OpenAPM {
private excludeDefaultLabels?: Array<DefaultLabels>;

public metricsServer?: Server;
public events: EventEmitter;

constructor(options?: OpenAPMOptions) {
super();
// Initializing all the options
this.path = options?.path ?? '/metrics';
this.metricsServerPort = options?.metricsServerPort ?? 9097;
Expand Down Expand Up @@ -122,7 +124,7 @@ export class OpenAPM {
this.extractLabels = options?.extractLabels ?? {};
this.customPathsToMask = options?.customPathsToMask;
this.excludeDefaultLabels = options?.excludeDefaultLabels;
this.events = new EventEmitter();
this.levitateConfig = options?.levitateConfig;

this.initiateMetricsRoute();
this.initiatePromClient();
Expand Down Expand Up @@ -301,7 +303,7 @@ export class OpenAPM {
try {
if (moduleName === 'express') {
const express = require('express');
instrumentExpress(express, this._REDMiddleware, this.events);
instrumentExpress(express, this._REDMiddleware, this);
}
if (moduleName === 'mysql') {
const mysql2 = require('mysql2');
Expand Down
13 changes: 11 additions & 2 deletions src/clients/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import EventEmitter from 'events';
import type * as Express from 'express';
import type { RequestHandler } from 'express';
import { wrap } from '../shimmer';
import type OpenAPM from '../OpenAPM';

export const instrumentExpress = (
express: typeof Express,
redMiddleware: RequestHandler,
events: EventEmitter
openapm: OpenAPM
) => {
let redMiddlewareAdded = false;

Expand Down Expand Up @@ -35,7 +36,15 @@ export const instrumentExpress = (
this: typeof original,
...args: Parameters<typeof original>
) {
events.emit('application_started');
openapm.emit('application_started', {
timestamp: new Date().toISOString(),
event_name: 'express_app',
event_state: 'start',
entity_type: '',
workspace: '',
namespace: '',
data_source_name: ''
});
return original.apply(this, args);
};
}
Expand Down
91 changes: 91 additions & 0 deletions src/levitate/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import EventEmitter from 'events';

export interface LevitateConfig {
host?: string;
orgSlug: string;
refreshTokens: {
write: string;
};
}

export interface DomainEventsBody {
timestamp: string;
event_name: string;
event_state: 'start' | 'stop';
workspace?: string;
namespace?: string;
entity_type?: string;
data_source_name: string;
}

const defaultHost = 'https://app.last9.io';

export class LevitateEvents extends EventEmitter {
private eventsUrl: URL;
public levitateConfig?: LevitateConfig;
constructor() {
super();
this.eventsUrl = new URL(
`/api/v4/organizations/${this.levitateConfig?.orgSlug}/domain_events`,
this.levitateConfig?.host ?? defaultHost
);
this.initiateEventListeners();
}

// Making the emit and on methods type safe
public emit(
event: 'application_started',
...args: (DomainEventsBody | any)[]
): boolean;
public emit(
event: 'application_stopped',
...args: (DomainEventsBody | any)[]
): boolean;
public emit(event: any, ...args: any[]): any {
return super.emit(event, ...args);
}

public on(
event: 'application_started',
listener: (...args: (DomainEventsBody | any)[]) => void
): this;
public on(
event: 'application_stopped',
listener: (...args: (DomainEventsBody | any)[]) => void
): this;
public on(event: any, listener: (...args: any[]) => void): this {
return super.on(event, listener);
}

public once(
event: 'application_started',
listener: (...args: (DomainEventsBody | any)[]) => void
): this;
public once(
event: 'application_stopped',
listener: (...args: (DomainEventsBody | any)[]) => void
): this;
public once(event: any, listener: (...args: any[]) => void): this {
return super.on(event, listener);
}

private initiateEventListeners() {
if (typeof this.levitateConfig?.refreshTokens?.write === 'string') {
this.once('application_started', this.applicationStarted);
}
}

private applicationStarted(body: DomainEventsBody) {
// fetch(this.eventsUrl.toString(), {
// method: 'PUT',
// headers: {
// 'Content-Type': 'application/json',
// 'X-LAST9-API-TOKEN': `Bearer `
// },
// body: {

// }
// });
console.log(body);
}
}
Empty file added src/levitate/tokenHelpers.ts
Empty file.

0 comments on commit 9fa1d15

Please sign in to comment.