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

Middleware #20

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
88a3683
Reformatted README.md and added more detail to example, changed the w…
nmczormick Oct 6, 2019
8326266
Added comments to chronos.js, added queryFreq parameter to allow user…
nmczormick Oct 6, 2019
24443aa
delted middleware.js as it became redundant after chronos.js was impl…
nmczormick Oct 6, 2019
5ee736f
updated version number, updated description to better highlight npm p…
nmczormick Oct 6, 2019
53a4540
In the process of adding comments to SQL middleware file. Created que…
nmczormick Oct 6, 2019
90d8687
1.0.2
nmczormick Oct 6, 2019
1c8d774
Updated list styling for queryFreq
nmczormick Oct 6, 2019
78813f3
1.0.3
nmczormick Oct 6, 2019
a813586
Update README.md
mesherrera Oct 7, 2019
090907e
Finished commenting mwSQL.js
nmczormick Oct 8, 2019
3ada918
updated the files to fix bugs and to add a correlatingid to db
jenaepen Dec 21, 2019
356c252
remove const from path
jenaepen Dec 21, 2019
e1ede81
Merge pull request #7 from Chronos2-0/jenae/middleware
tim-atapagra Dec 21, 2019
ebacac6
removed the second connection, allowed microHealth to be invoked with…
jenaepen Dec 23, 2019
41687ed
updated Readme to have the right package name
jenaepen Dec 23, 2019
3d72f85
updated package
jenaepen Dec 24, 2019
d1935cc
Update README.md
jenaepen Dec 31, 2019
ea33c9b
Update README.md
jenaepen Dec 31, 2019
e31bf08
removed extra array example
jenaepen Dec 31, 2019
19f5b0b
Update README.md
jenaepen Dec 31, 2019
72875e6
Ben changes to ReadMe
jenaepen Jan 11, 2020
8984955
Merge pull request #31 from Chronos2-0/jenae/middleware
tim-atapagra Jan 13, 2020
51a5290
Changed Chronos link to OSLabs
ben-mizel Jan 14, 2020
189f7e8
Updated package version, description, and readme link before npm publish
Jan 14, 2020
ccc055f
updated readme, version, description before npm publish
Jan 14, 2020
28806be
Update README.md
ben-mizel Jan 15, 2020
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
5 changes: 4 additions & 1 deletion Communication.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ mongoose.model('Communication', {
},
timeSent: {
type: Date
}
},
correlatingId:{
type: String,
},
})
120 changes: 98 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,110 @@
##Chronos Microservice Debugger
Chronos Microservice Debugger consists of an npm package with an optional Electron front end to visualize and monitor your microservices.
![Chronos logo](https://raw.githubusercontent.com/Chronos2-0/Chronos/master/app/assets/logo2.png)
## Chronos
Microservice communication and health visualizer.

##Install
[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]

```js
const cmd = require('chronos-microservice-debugger3')
cmd.propagate()

app.use('/', cmd.microCom('microserviceName', 'databaseType', 'databaseURL', 'wantMicroHealth', 'queryFrequency'))
```

## Features

* HTTP request tracing
* Speed and latency tracking
* Process monitoring
* Memory usage

## Installation

Chronos consists of a [Node](https://nodejs.org/en/) module available through the
[npm registry](https://www.npmjs.com/) and a lightweight [Electron](https://electronjs.org/) desktop application.

#### Node module

To begin, install the [Chronos](https://www.npmjs.com/package/chronos-microservice-debugger3) node module within each microservice of your application using the
[`npm install`](https://docs.npmjs.com/getting-started/installing-npm-packages-locally)command:

```
npm install chronos-microservice-debugger3
```

Once installed, write the following two lines at the top of each microservice's server file:
```javascript
npm install chronos-microservice-debugger
const cmd = require('chronos-microservice-debugger3');
cmd.propagate();
```

##Usage
There are two main aspects to Chronos-Microservice-Debugger
*Communication Monitor: Listens in on all microservice-microservice and microservice-client communication and monitors the response statuses and messages to ensure communications are making it to the correct destination successfully.
*Health Monitor: The health monitor checks the status of your microservice every second and sends this health information to an optional electron frontend where it is visualized for easier use.
Then add a route handler for all incoming requests:
```js
app.use('/', cmd.microCom('microserviceName', 'databaseType', 'databaseURL', 'wantMicroHealth', 'queryFrequency'))
```

The cmd.microCom handler function logs communication and health data to a user-provided database. This is to ensure that your private data stays private. We currently support MongoDB and SQL/PostgreSQL databases.

cmd.microCom takes four parameters and an optional fifth parameter. You can enter the arguments as individual strings or as an array.

The parameters are:
1. microserviceName: To identify the microservice (i.e. "payments")
2. databaseType: Enter either "mongo" or "sql"
3. databaseURL: Enter the URL of your database
4. wantMicroHealth: Do you want to monitor the health of this microservice? Enter "yes" or "no"
5. queryFrequency (optional): How frequently do you want to log the health of this microservice? It defaults to every minute, but you can choose:
* "s" : every second
* "m" : every minute (default)
* "h" : every hour
* "d" : once per day
* "w" : once per week

String parameter example:
```javascript
app.use('/', chronos-microservice-debgugger.microCom('microserviceName', 'databaseType', 'databaseURL'))
chronos-microservice-debugger.microHealth('microserviceName', databaseType, databaseURL))
app.use('/', cmd.microCom('payments', 'mongo', 'mongodb+srv://user:[email protected]/','yes','h'))
```

Array parameter example:
```javascript
let values = [
'payments',
'mongo',
'mongodb+srv://user:[email protected]/',
'yes',
'h'
]

app.use('/', cmd.microCom(values)
```
Chronos uses a user-owned and provided database to ensure that your private data stays private. We currently support MongoDB and SQL/PostgresQL databases.

##Things in the Works
*gRPC support
*Ability to determine how often your microservice health is monitored (currently every second)
*'Time Travel' to see how your microservices have changed over time
*Docker health information for containerized microservices
*Implement additional unit testing
#### Electron desktop application

After installing the node module in each microservice, download the Electron desktop application from the public [Chronos](https://github.com/oslabs-beta/Chronos) repo.

Inside the downloaded directory, install all dependencies using the `npm install` command followed by the `npm start` command to start the Electron desktop application.

## Contributing

Chronos hopes to inspire an active community of both users and developers. For questions, comments, or contributions, please submit a pull request.

## People

[Tim Atapagra](https://github.com/timpagra),
[Mohtasim Chowdhury](https://github.com/mohtasim317),
[Ousman Diallo](https://github.com/Dialloousman),
[Michelle Herrera](https://github.com/mesherrera),
[Duane McFarlane](https://github.com/Duane11003),
[Ben Mizel](https://github.com/ben-mizel),
[Jenae Pennie](https://github.com/jenaepen),
[Chris Romano](https://github.com/robicano22),
[Natalie Umanzor](https://github.com/nmczormick)

##Links
*Chronos Website: http://chronos.ninja
*Gitub Page: http://https://github.com/oslabs-beta/Chronos
## License

##Contact Us
For questions, requests, or more information, please contact us at [email protected]
[MIT](LICENSE)

[npm-image]: https://img.shields.io/npm/v/chronos-microservice-debugger3.svg
[npm-url]: https://www.npmjs.com/package/chronos-microservice-debugger3
[downloads-image]: https://img.shields.io/npm/dm/chronos-microservice-debugger3.svg
[downloads-url]: https://npmjs.org/package/chronos-microservice-debugger3
120 changes: 59 additions & 61 deletions chronos.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,69 @@
const mongoMiddleware = require('./mwMongo.js')
const sqlMiddleware = require('./mwSQL.js')
const hpropagate = require('hpropagate');
const mongoMiddleware = require('./mwMongo.js');
const sqlMiddleware = require('./mwSQL.js');

const chronos = {}
chronos.microHealth = (microserviceName, databaseType, userOwnedDB) => {
microserviceName.toLowerCase();
databaseType.toLowerCase();

if (!microserviceName || !databaseType || !userOwnedDB) {
throw new Error(
"Please verify that you have provided all three required parameters"
);
}
const chronos = {};

if (
typeof microserviceName !== "string" ||
typeof databaseType !== "string" ||
typeof userOwnedDB !== "string"
) {
throw new Error(
"Please verify that the parameters you entered are all strings"
);
}

if (databaseType === "mongo" || databaseType === "mongodb") {
return mongoMiddleware.microHealth(userOwnedDB, microserviceName);
} else if (databaseType === "sql" || databaseType === "postgresql") {
return sqlMiddleware.microHealth(userOwnedDB, microserviceName);
} else {
throw new Error (
'Chronos currently only supports Mongo and SQL/PostgresQL databases. Please enter "mongo" or "sql'
);
}
},
chronos.microCom = (microserviceName, databaseType, userOwnedDB, req, res, next) => {
microserviceName.toLowerCase();
databaseType.toLowerCase();

if (!microserviceName || !databaseType || !userOwnedDB) {
throw new Error(
"Please verify that you have provided all three required parameters"
);
}

if (
typeof microserviceName !== "string" ||
typeof databaseType !== "string" ||
typeof userOwnedDB !== "string"
) {
throw new Error(
"Please verify that the parameters you entered are all strings"
);
}

if (databaseType === "mongo" || databaseType === "mongodb") {
return mongoMiddleware.microCom(userOwnedDB, microserviceName);
} else if (databaseType === "sql" || databaseType === "postgresql") {
return sqlMiddleware.microCom(userOwnedDB, microserviceName);
} else {
throw new Error(
'Chronos currently only supports Mongo and SQL/PostgresQL databases. Please enter "mongo" or "sql'
);
}
/*propagate places an unique x-correlating-id into the headers of
each request/response. The same id persist thru the whole life cycle of the
request.
*/
chronos.propagate = () => {
hpropagate({ propagateInResponses: true });
};

// microCom logs all microservice-microservice and microservice-client communication into the user-owned database.

module.exports = chronos;
/*
* microserviceName: what the user wants current microservice to be called
* databaseType: type of database user is providing: Mongo or PostgreSQL
* userOwnedDB: URL to user database
* queryFreq: Not necessary for microCom as microCom monitors only when an endpoint is hit
*/
chronos.microCom = (microserviceName, databaseType, userOwnedDB, wantMicroHealth, queryFreq = 'm', req, res, next) => {
// Handles if user inputs an array. Grabs information and assigns to correct parameters
if (Array.isArray(microserviceName) === true && microserviceName.length >= 4) {
userOwnedDB = microserviceName[2];
databaseType = microserviceName[1];
wantMicroHealth = microserviceName[3];
queryFreq = microserviceName[4] || 'm';
microserviceName = microserviceName[0];
}
// Changes user inputs to lowercase to account for any capitalization errors
databaseType = databaseType.toLowerCase();
wantMicroHealth = wantMicroHealth.toLowerCase();
queryFreq = queryFreq.toLowerCase();

// Ensures that the required parameters are entered, errors out otherwise
if (!microserviceName || !databaseType || !userOwnedDB || !wantMicroHealth) {
throw new Error(
'Please verify that you have provided all four required parameters',
);
}

// Verifies that the user has enteres strings, throws error otherwise
if (
typeof microserviceName !== 'string'
|| typeof databaseType !== 'string'
|| typeof userOwnedDB !== 'string'
|| typeof wantMicroHealth !== 'string'
|| typeof queryFreq !== 'string'
) {
throw new Error(
'Please verify that the parameters you entered are all strings',
);
}

// Checks the type of database provided by the user and uses appropriate middleware files. Throws error if input db type is not supported
if (databaseType === 'mongo' || databaseType === 'mongodb') {
return mongoMiddleware.microCom(userOwnedDB, microserviceName, wantMicroHealth,queryFreq);
} if (databaseType === 'sql' || databaseType === 'postgresql') {
return sqlMiddleware.microCom(userOwnedDB, microserviceName, wantMicroHealth,queryFreq);
}
throw new Error(
'Chronos currently only supports Mongo and PostgreSQL databases. Please enter "mongo" or "sql"',
);
};

module.exports = chronos;
Loading