Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add browser console plugin + update CONTRIBUTING.md #23

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ Once we've looked over the issue, we'll label your PR as one of the following:

## Local development

### 1. Install dependencies
### 1. Fork the Repository

Start by forking the [@capitalone/Stratum-Observability repository](https://github.com/capitalone/Stratum-Observability/), and cloning that forked repository onto your local machine.

### 2. Install dependencies

```
nvm use && npm ci
```

### 2. Build
### 3. Build
This command will build out the compiled code within the repository's `dist/` folder.

```
Expand Down Expand Up @@ -71,7 +75,7 @@ For continuous building on changes, use the `build:watch` script instead.
npm run build:watch
```

### 3. Code quality & maintenance
### 4. Code quality & maintenance
#### lint
Run code quality checks and attempt to fix any violations. This script is automatically run as part of a pre-commit hook. You will not likely need to run this manually.

Expand All @@ -86,7 +90,7 @@ Update code to conform to repository code style rules. This script is automatica
npm run prettier
```

### 4. Unit tests
### 5. Unit tests
Unit tests are run via Jest.

#### test
Expand All @@ -103,7 +107,7 @@ npm run test:coverage

## Pull requests

Once you are ready to merge your changes, open PR(s) in the repository and fill out the provided template.
Once you are ready to merge your changes, open a pull request (PR) from your forked repository to the main project repository. Complete the provided template to help reviewers understand your changes.

The template will prompt you for the following information:
* A link to the connected Stratum intake request GitHub issue (include the "Fixes" prefix so that your issue will auto-close on PR merge)
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
"types": "./dist/types/plugins/new-relic-plus/index.d.ts",
"import": "./dist/esm/plugins/new-relic-plus/index.js",
"default": "./dist/cjs/plugins/new-relic-plus/index.js"
},
"./plugins/browser-console": {
"types": "./dist/types/plugins/browser-console/index.d.ts",
"import": "./dist/esm/plugins/browser-console/index.js",
"default": "./dist/cjs/plugins/browser-console/index.js"
}
},
"scripts": {
Expand Down Expand Up @@ -78,6 +83,9 @@
],
"plugins/new-relic-plus": [
"./dist/types/plugins/new-relic-plus/index.d.ts"
],
"plugins/browser-console": [
"./dist/types/plugins/browser-console/index.d.ts"
]
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/plugins/browser-console/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { BasePlugin } from '../../base';
import type { PluginFactory } from '../../types';
import { BrowserConsolePublisher } from './publisher';

/**
* Browser Console plugin
*/
export class BrowserConsolePlugin extends BasePlugin<never, never> {
name = 'browserConsole';
publishers = [new BrowserConsolePublisher()];
}

/**
* Browser Console plugin factory function
*
* Use this function to instantiate the BrowserConsolePlugin when registering
* this plugin within Stratum.
*/
export const BrowserConsolePluginFactory: PluginFactory<BrowserConsolePlugin> = () => new BrowserConsolePlugin();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add browser-console to the package.json file so that it can be directly imported by consuming apps.


export * from './publisher';
50 changes: 50 additions & 0 deletions src/plugins/browser-console/publisher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { BaseEventModel, BasePublisher } from '../../base';
import { StratumSnapshot } from '../../types';

/**
* Browser Console Publisher
*/
export class BrowserConsolePublisher extends BasePublisher {
name = 'browserConsole';

/**
* Handle all event types generically, even those provided
* by a separate plugin.
*/
shouldPublishEvent(): boolean {
return true;
}

/**
* Required
* Returns data from underlying events to utilize
* in the publish step.
*
* In this case, we send the stringified event data to be logged in the console.
*/
getEventOutput(_event: BaseEventModel, snapshot: StratumSnapshot): string {
return JSON.stringify(snapshot.event);
}

/**
* Required
* Check if your publisher source is available (aka scripts installed, environment
* is set up, etc.)
*
* In this case, we make sure that console.log() is accessible.
*/
async isAvailable(): Promise<boolean> {
return typeof console !== 'undefined';
}

/**
* Required
* Send your content to the external publisher
*
* In this, case we publish the stringified event data to the console.
*/
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
async publish(content: string): Promise<void> {
console.log(`BrowserConsolePlugin: ${content}`);
}
}
66 changes: 66 additions & 0 deletions tests/plugins/browser-console.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { BasePublisher, StratumService } from '../../src';
import {
BrowserConsolePlugin,
BrowserConsolePluginFactory,
BrowserConsolePublisher
} from '../../src/plugins/browser-console';
import { CATALOG_METADATA, PRODUCT_NAME, PRODUCT_VERSION } from '../utils/constants';
import { getPublishers, restoreStratumMocks } from '../utils/helpers';
import { BASE_CATALOG } from '../utils/catalog';

describe('browser console plugin', () => {
let stratum: StratumService;
let plugin: BrowserConsolePlugin;
let publisher: BasePublisher;

beforeEach(() => {
plugin = BrowserConsolePluginFactory();
stratum = new StratumService({
catalog: { items: BASE_CATALOG, ...CATALOG_METADATA },
plugins: [plugin],
productName: PRODUCT_NAME,
productVersion: PRODUCT_VERSION
});
publisher = stratum.publishers[0];
});

afterEach(() => {
restoreStratumMocks();
jest.restoreAllMocks();
});

it('should successfully publish from catalog', async () => {
const result = await stratum.publish(1);
expect(result).toBe(true);
});

it('should initialize the BrowserConsole publisher', () => {
expect(getPublishers(stratum)[0]).toBeInstanceOf(BrowserConsolePublisher);
});

it('should publish and log events to the console', async () => {
expect(publisher).toBeInstanceOf(BrowserConsolePublisher);

const publishSpy = jest.spyOn(publisher, 'publish');
const consoleSpy = jest.spyOn(console, 'log');

let result = await stratum.publish(1);
let expectedContent = `{"eventType":"${BASE_CATALOG[1].eventType}","id":${BASE_CATALOG[1].id}}`;
let expectedLoggedMessage = `BrowserConsolePlugin: ${expectedContent}`;

expect(result).toBe(true);
expect(publishSpy).toHaveBeenCalledWith(expectedContent, expect.anything());
expect(consoleSpy).toHaveBeenCalledWith(expectedLoggedMessage);

expectedContent = `{"eventType":"${BASE_CATALOG[2].eventType}","id":${BASE_CATALOG[2].id}}`;
expectedLoggedMessage = `BrowserConsolePlugin: ${expectedContent}`;
result = await stratum.publish(2);

expect(result).toBe(true);
expect(publishSpy).toHaveBeenCalledWith(expectedContent, expect.anything());
expect(consoleSpy).toHaveBeenCalledWith(expectedLoggedMessage);

expect(publishSpy).toHaveBeenCalledTimes(2);
expect(consoleSpy).toHaveBeenCalledTimes(2);
});
});