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

Logging Prism's decisions. #323

Merged
merged 117 commits into from Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
117 commits
Select commit Hold shift + click to select a range
a70cf3a
feat: add pino and its types
XVincentX May 27, 2019
746ffb8
feat: cluster prism-http and forward logs to the cli
XVincentX May 27, 2019
e278478
refactor: do not export what's not used
XVincentX May 27, 2019
46dfe0f
feat: add main logging logic to core
XVincentX May 27, 2019
c11e69a
chore: remove settings file copied who knows from where
XVincentX May 27, 2019
72a3ce8
refactor: move away logger construction logic from cli
XVincentX May 27, 2019
e81117d
chore: lock adjustments
XVincentX May 27, 2019
6f47f4b
chore: better launch.json
XVincentX May 27, 2019
a5e7c27
feat: export single logger instance
XVincentX May 27, 2019
cc7406d
chore: add success log level
XVincentX May 27, 2019
054925b
feat: log negotiator invocations
XVincentX May 27, 2019
b98ec9d
test: remove
XVincentX May 27, 2019
a03cea9
refactor: close the worker when something goes wrong
XVincentX May 27, 2019
32bc99f
chore: add chalk and colorise
XVincentX May 27, 2019
208558a
feat: parentesis
XVincentX May 27, 2019
9dc481c
feat: log negotiation for invalid responses
XVincentX May 27, 2019
345d143
feat: create logger at server level
XVincentX May 27, 2019
c3c6122
feat: send request id with each line if possible
XVincentX May 28, 2019
e6dcef0
feat: print request details when provided
XVincentX May 28, 2019
c584c43
fix: use types for http methods
XVincentX May 28, 2019
fa4dc58
refactor: logger can be provided as a component
XVincentX May 28, 2019
a094c84
feat: add colour map
XVincentX May 29, 2019
23a7a55
refactor: move map to options
XVincentX May 29, 2019
2d494ec
feat: serialise only what's necessary
XVincentX May 29, 2019
0ffcb1b
test: get rid
XVincentX May 29, 2019
7089b0e
refactor: let the logger grab the data from the input gave to prism
XVincentX May 31, 2019
48c8d56
feat: change colours
XVincentX May 31, 2019
daf1c30
feat: inject logging in mocker
XVincentX May 31, 2019
e185f81
refactor: use Logger instead of BaseLogger
XVincentX May 31, 2019
086ac22
fix: type error
XVincentX May 31, 2019
59a3db7
feat: log successful responses
XVincentX May 31, 2019
111d03c
fix: make sure to handle payload serialisation errors
XVincentX May 31, 2019
0e52f09
fix: remove the cast
XVincentX May 31, 2019
b45d746
feat: add logging for the content negotiation part
XVincentX Jun 3, 2019
665e2bd
test: start refactoring tests
XVincentX Jun 3, 2019
5f031f9
refactor: use either to handle negotiation errors
XVincentX Jun 6, 2019
98c1aeb
refactor: mocker shuold use either as well
XVincentX Jun 6, 2019
5a77690
test: fix tests
XVincentX Jun 6, 2019
2e14e82
Merge branch 'master' into feat/logs
XVincentX Jun 11, 2019
37584fa
chore: post merge fixes
XVincentX Jun 11, 2019
8684b37
test: fix
XVincentX Jun 11, 2019
0539921
chore: use official json schema faker
XVincentX Jun 11, 2019
aa34a6b
fix: use correct types
XVincentX Jun 11, 2019
a8a134e
feat: restore original dynamic flag
XVincentX Jun 12, 2019
f6a29f2
refactor: rename and fix
XVincentX Jun 12, 2019
cd9e1bc
chore: use sampler
XVincentX Jun 12, 2019
9fabb94
refactor: get rid of the async process
XVincentX Jun 12, 2019
8c3b685
test: fix
XVincentX Jun 12, 2019
71bd181
test: fix
XVincentX Jun 12, 2019
4227ee9
test: better print out
XVincentX Jun 12, 2019
158dc9c
test: add the test we need to test all the scenarios
XVincentX Jun 12, 2019
01fd67e
feat: support single example for fallbacks
XVincentX Jun 12, 2019
ad85dcf
use jsonschema bundled version
XVincentX Jun 12, 2019
d803f22
chore: download prism2 after regular tests
XVincentX Jun 12, 2019
5aed8b8
refactor: one line checks
XVincentX Jun 12, 2019
02731c9
refactor: better test messages
XVincentX Jun 12, 2019
613a9c3
test: better assertion sentences
XVincentX Jun 12, 2019
d777295
test: add enum test case
XVincentX Jun 12, 2019
9872b0a
test: add additional cases
XVincentX Jun 12, 2019
f3f7678
Merge branch 'master' into kill-jsf-fork
XVincentX Jun 13, 2019
49e62c1
feat: better jsonesque transformation
XVincentX Jun 13, 2019
1d0d222
feat: better jsonesque transformation
XVincentX Jun 13, 2019
9d0e730
Merge branch 'master' into feat/logs
XVincentX Jun 13, 2019
1212b6f
chore: restore our sad fork
XVincentX Jun 13, 2019
10272c1
chore: fix the version
XVincentX Jun 13, 2019
67a4b88
chore: better assertions text
XVincentX Jun 13, 2019
88169fa
Merge branch 'master' into feat/logs
XVincentX Jun 13, 2019
0f89fc9
chore: add launch interactive launch command
XVincentX Jun 13, 2019
694b96b
Merge branch 'master' into kill-jsf-fork
XVincentX Jun 13, 2019
175088c
fix: check that we've got an array
XVincentX Jun 13, 2019
60eeb50
chore: use the cloned object
XVincentX Jun 13, 2019
c1764e3
refactor: simply example selection
XVincentX Jun 13, 2019
b393c87
Merge branch 'master' into feat/logs
Jun 13, 2019
0bf0051
Merge branch 'master' into kill-jsf-fork
XVincentX Jun 14, 2019
7123262
test: divide tests
XVincentX Jun 14, 2019
c7896e8
test: add empty examples and multiple examples
XVincentX Jun 14, 2019
fd8fdb1
Update packages/http/src/mocker/__tests__/HttpMocker.spec.ts
XVincentX Jun 14, 2019
34c8076
test: try to refactor
XVincentX Jun 14, 2019
1693c75
Merge branch 'kill-jsf-fork' into feat/logs
XVincentX Jun 14, 2019
053d8ef
chore: post merge fixes
XVincentX Jun 14, 2019
676ba12
Merge branch 'master' into feat/logs
XVincentX Jun 14, 2019
bc964f2
feat: add fork option
XVincentX Jun 14, 2019
0ef6db5
feat: pino can take a destination stream
XVincentX Jun 14, 2019
7db547c
feat: fork the process according to the flag
XVincentX Jun 14, 2019
a5efe83
test: restore
XVincentX Jun 14, 2019
ebabaf5
chore: put green on enabled stuff
XVincentX Jun 14, 2019
da7ea3d
refactor: tests
XVincentX Jun 14, 2019
2eef08a
Merge branch 'master' into feat/logs
XVincentX Jun 14, 2019
55db38c
refactor: use fold
XVincentX Jun 14, 2019
4bcd68b
Merge branch 'master' into feat/logs
XVincentX Jun 14, 2019
bd57f8c
feat: update fastify so I can remove the workaround
XVincentX Jun 14, 2019
d21b07d
refactor: rename fork to multiprocess
XVincentX Jun 14, 2019
52d838f
docs: content header is done
XVincentX Jun 14, 2019
59e63aa
docs: add a note on running the CLI in production
XVincentX Jun 14, 2019
6dfd664
refactor: define helpers once
XVincentX Jun 16, 2019
8ef5934
refactor: do not create another variable
XVincentX Jun 17, 2019
5c3ca48
refactor: simplify mock file
XVincentX Jun 17, 2019
59d1c51
feat: pipe logger from CLI to server
XVincentX Jun 17, 2019
d4c188d
refactor: put some constraints to avoid null loggers
XVincentX Jun 17, 2019
8763003
chore: remove unused file
XVincentX Jun 17, 2019
fbbf451
refactor: use withLgger function
XVincentX Jun 17, 2019
1ff2d5c
chore: prettier on the readme
XVincentX Jun 17, 2019
ffffb9d
refactor: prefer const statement
XVincentX Jun 17, 2019
8d9f6d2
refactor: externalize prefix creation
XVincentX Jun 17, 2019
b95eef9
refactor: put assembleResponse out
XVincentX Jun 17, 2019
34638c0
refactor: move negotiateResponse out
XVincentX Jun 17, 2019
5ddfe73
Merge branch 'master' into feat/logs
XVincentX Jun 17, 2019
d1d00ec
refactor: use the type checker instead of the run time checks
XVincentX Jun 17, 2019
e47598f
fix: remove vscode files
XVincentX Jun 17, 2019
4b486e3
refactor: extract logEnabledFeature
XVincentX Jun 18, 2019
94fb452
chore: better log message
XVincentX Jun 18, 2019
f0c348c
refactor: logCLIMessage
XVincentX Jun 18, 2019
2199db6
docs: fix
XVincentX Jun 18, 2019
6eb7174
refactor: avoid the interactive instance, we do not need it
XVincentX Jun 18, 2019
72336ba
refactor: put process helpers outside
XVincentX Jun 18, 2019
85aa60a
test: refactor and add
XVincentX Jun 18, 2019
5923c75
Merge branch 'master' into feat/logs
XVincentX Jun 18, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 0 additions & 48 deletions .vscode/launch.json

This file was deleted.

36 changes: 0 additions & 36 deletions .vscode/settings.json

This file was deleted.

1 change: 0 additions & 1 deletion CHANGELOG.md
Expand Up @@ -18,7 +18,6 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
### Features

- Prism now has got a static example fallback in case the `dynamic` flag is not enabled #347
- Prism can now validate servers #351
XVincentX marked this conversation as resolved.
Show resolved Hide resolved

# 3.0.0-alpha.14 (2019-06-11)

Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -43,6 +43,8 @@
"@types/lodash": "^4.14.132",
"@types/nock": "^10.0.3",
"@types/node": "^12.0.2",
"@types/pino": "^5.8.7",
"@types/split2": "^2.1.6",
"@types/signale": "^1.2.1",
"@types/type-is": "^1.6.2",
"chance": "^1.0.18",
Expand Down
13 changes: 11 additions & 2 deletions packages/cli/README.md
Expand Up @@ -14,6 +14,7 @@
# Usage

<!-- usage -->

```sh-session
$ npm install -g @stoplight/prism-cli
$ prism COMMAND
Expand All @@ -25,13 +26,15 @@ USAGE
$ prism COMMAND
...
```

<!-- usagestop -->

# Commands

<!-- commands -->
* [`prism help [COMMAND]`](#prism-help-command)
* [`prism mock SPEC`](#prism-mock-spec)

- [`prism help [COMMAND]`](#prism-help-command)
- [`prism mock SPEC`](#prism-mock-spec)

## `prism help [COMMAND]`

Expand Down Expand Up @@ -70,6 +73,12 @@ OPTIONS
_See code: [src/commands/mock.ts](https://github.com/stoplightio/prism/blob/v3.0.0-alpha.16/src/commands/mock.ts)_
<!-- commandsstop -->

## Running in production

When running in development mode (which happens when the `NODE_ENV` environment variable is not set to `production`) or the `-m` flag is set to false, both the HTTP Server and the CLI (which is responsible of parsing and showing the received logs on the screen) will run within the same process.

Processing logs slows down the process significantly. If you're planning to use the CLI in production (for example in a Docker Container) we recommend to run the CLI with the `-m` flag or set the `NODE_ENV` variable to `production`. In this way, the CLI and the HTTP server will run on two different processes, so that logs processing, parsing and printing does not slow down the http requests processing.
XVincentX marked this conversation as resolved.
Show resolved Hide resolved

## Development

### Debugging
Expand Down
1 change: 1 addition & 0 deletions packages/cli/package.json
Expand Up @@ -13,6 +13,7 @@
"@stoplight/prism-core": "^3.0.0-alpha.16",
"@stoplight/prism-http-server": "^3.0.0-alpha.16",
"signale": "^1.4.0",
"split2": "^3.1.1",
"tslib": "^1.0.0",
"urijs": "^1.19.1"
},
Expand Down
60 changes: 43 additions & 17 deletions packages/cli/src/commands/__tests__/mock.spec.ts
@@ -1,38 +1,64 @@
import { createServer } from '../../util/createServer';
import { createMultiProcessPrism, createSingleProcessPrism } from '../../util/createServer';
import Mock from '../mock';

const listenMock = jest.fn().mockReturnValue('http://localhost:1000');

jest.mock('../../util/createServer', () => ({
createServer: jest.fn(() => ({ listen: listenMock, prism: { resources: [{ method: 'get', path: '/test' }] } })),
}));
jest.mock('../../util/createServer');

describe('mock command', () => {
beforeEach(() => {
(createServer as jest.Mock).mockClear();
(createSingleProcessPrism as jest.Mock).mockClear();
(createMultiProcessPrism as jest.Mock).mockClear();
});

test('starts mock server', async () => {
await Mock.run(['/path/to']);
expect(createServer).toHaveBeenLastCalledWith('/path/to', { mock: { dynamic: false } });
expect(listenMock).toHaveBeenLastCalledWith(4010, '127.0.0.1');
expect(createMultiProcessPrism).not.toHaveBeenCalled();
expect(createSingleProcessPrism).toHaveBeenLastCalledWith({
spec: '/path/to',
dynamic: false,
host: '127.0.0.1',
port: 4010,
});
});

test('starts mock server on custom port', async () => {
await Mock.run(['-p', '666', '/path/to']);
expect(createServer).toHaveBeenLastCalledWith('/path/to', { mock: { dynamic: false } });
expect(listenMock).toHaveBeenLastCalledWith(666, '127.0.0.1');
expect(createMultiProcessPrism).not.toHaveBeenCalled();
expect(createSingleProcessPrism).toHaveBeenLastCalledWith({
spec: '/path/to',
dynamic: false,
host: '127.0.0.1',
port: 666,
});
});

test('starts mock server on custom host', async () => {
await Mock.run(['-h', '0.0.0.0', '/path/to']);
expect(createServer).toHaveBeenLastCalledWith('/path/to', { mock: { dynamic: false } });
expect(listenMock).toHaveBeenLastCalledWith(4010, '0.0.0.0');
expect(createMultiProcessPrism).not.toHaveBeenCalled();
expect(createSingleProcessPrism).toHaveBeenLastCalledWith({
spec: '/path/to',
dynamic: false,
host: '0.0.0.0',
port: 4010,
});
});

test('starts mock server on custom host and port', async () => {
await Mock.run(['-p', '666', '-h', '0.0.0.0', '/path/to']);
expect(createServer).toHaveBeenLastCalledWith('/path/to', { mock: { dynamic: false } });
expect(listenMock).toHaveBeenLastCalledWith(666, '0.0.0.0');
expect(createMultiProcessPrism).not.toHaveBeenCalled();
expect(createSingleProcessPrism).toHaveBeenLastCalledWith({
spec: '/path/to',
dynamic: false,
host: '0.0.0.0',
port: 666,
});
});

test('starts mock server with multiprocess option ', async () => {
await Mock.run(['-p', '666', '-m', '-h', '0.0.0.0', '/path/to']);
expect(createSingleProcessPrism).not.toHaveBeenCalled();
expect(createMultiProcessPrism).toHaveBeenLastCalledWith({
spec: '/path/to',
dynamic: false,
host: '0.0.0.0',
port: 666,
});
});
});
35 changes: 7 additions & 28 deletions packages/cli/src/commands/mock.ts
@@ -1,43 +1,22 @@
import { Command } from '@oclif/command';
import * as signale from 'signale';
import { ARGS, FLAGS } from '../const/options';
import { createServer } from '../util/createServer';
import { createMultiProcessPrism, createSingleProcessPrism } from '../util/createServer';

export default class Server extends Command {
public static description = 'Start a mock server with the given spec file';
public static flags = { port: FLAGS.port, host: FLAGS.host, dynamic: FLAGS.dynamic };
public static flags = FLAGS;
public static args = [ARGS.spec];

public async run() {
const signaleInteractiveInstance = new signale.Signale({ interactive: true });

public run() {
const {
flags: { port, dynamic, host },
flags: { port, dynamic, host, multiprocess },
args: { spec },
} = this.parse(Server);

signaleInteractiveInstance.await('Starting Prism…');

if (dynamic) {
signale.star('Dynamic example generation enabled.');
if (multiprocess) {
return createMultiProcessPrism({ dynamic, port, host, spec });
}

const server = createServer(spec, { mock: { dynamic } });
try {
const address = await server.listen(port, host);

if (server.prism.resources.length === 0) {
signaleInteractiveInstance.fatal('No operations found in the current file.');
this.exit(1);
}

signaleInteractiveInstance.success(`Prism is listening on ${address}`);

server.prism.resources.forEach(resource => {
signale.note(`${resource.method.toUpperCase().padEnd(10)} ${address}${resource.path}`);
});
} catch (e) {
signaleInteractiveInstance.fatal(e.message);
}
return createSingleProcessPrism({ dynamic, port, host, spec });
}
}
16 changes: 15 additions & 1 deletion packages/cli/src/const/options.ts
@@ -1,9 +1,10 @@
import { flags as oflags } from '@oclif/command';
import chalk from 'chalk';

export const ARGS = {
spec: {
name: 'spec',
description: 'Path to a spec file',
description: 'Path to a spec file. Can be both a file or a fetchable resource on the web',
required: true,
},
};
Expand All @@ -27,4 +28,17 @@ export const FLAGS = {
description: 'Dynamically generate examples.',
default: false,
}),

multiprocess: oflags.boolean({
char: 'm',
description: 'Fork the http server from the CLI',
default: process.env.NODE_ENV === 'production',
}),
};

export const LOG_COLOR_MAP = {
CLI: chalk.bgWhiteBright,
'HTTP SERVER': chalk.bgYellowBright,
MOCKER: chalk.bgBlueBright,
HTTP: chalk.bgGreenBright,
};
29 changes: 0 additions & 29 deletions packages/cli/src/util/__tests__/createServer.unit.ts

This file was deleted.