Skip to content

Commit 9da2a61

Browse files
committed
npm package readme created
1 parent 7f1c166 commit 9da2a61

File tree

1 file changed

+221
-3
lines changed

1 file changed

+221
-3
lines changed

chronos_npm_package/README.md

Lines changed: 221 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ The `database` property is required and takes in the following:
6868
- `URI` should be a connection string to the database where you intend Chronos to write and record data regarding health, HTTP route tracing, and container infomation.
6969
We reccommend using dotenv
7070

71-
Wherever you create an instance of your server (see example below),
71+
You will also need to add the following two lines of code to your express server:,
7272

7373
```js
7474
// Example for REST
@@ -77,7 +77,7 @@ const app = express();
7777

7878
```
7979

80-
you will also need to require in `chronos-tracker` and initialize Chronos, as well as the `./chronos-config` file. You will then need to invoke `chronos.propagate()` to initiate the route tracing, in addition to implementing `chronos.track()` for all endpoints.
80+
you will also need to require in `chronos-tracker-7` and initialize Chronos, as well as the `./chronos-config` file in addition to implementing `chronos.track()` for all endpoints.
8181

8282
```js
8383
const chronos = require('chronos-tracker');
@@ -88,6 +88,224 @@ require('./chronos-config'); // Bring in config file
8888
app.use('/', chronos.track());
8989
```
9090

91-
You should be good to go! The last step, **Docker Configuration**, is **only applicable** if you need to configure <a href="#"><img src="./app/assets/docker-logo-color.png" alt="Docker" title="Docker" align="center" height="20" /></a> for your application.
91+
You should be good to go! The steps below for **Docker Configuration** and **Kafka Configuration**, are **only applicable** if you need to configure <a href="#"><img src="./app/assets/docker-logo-color.png" alt="Docker" title="Docker" align="center" height="20" /></a> or Kafka for your application. See the **Notifications** section below for information on how to uses the notifications propery in `chronos-config.js`.
9292

9393
<br>
94+
95+
#### Initialize Chronos Tracker for gRPC
96+
97+
Wherever you create an instance of your server (see example below),
98+
99+
100+
```js
101+
// Example of gRPC server
102+
const server = new grpc.Server();
103+
104+
server.bindAsync("127.0.0.1:30044", grpc. ServerCredentials.createInsecure(), () => {
105+
server.start();
106+
console.log("Server running at http://127.0.0.1:30044");
107+
});
108+
```
109+
you will also need to require Chronos-tracker, Chronos-config, and dotenv.config(if this is used). For health data, simply use Chronos.track()
110+
111+
112+
113+
```js
114+
//track health data
115+
const chronos = require('chronos-tracker');
116+
require('./chronos-config');
117+
require('dotenv').config(); // set up environment variables in .env
118+
const BookModel = require('./BookModel');
119+
120+
chronos.track()
121+
```
122+
To trace requests, first wrap the gRPC client using Chronos
123+
```js
124+
const grpc = require('@grpc/grpc-js');
125+
const protoLoader = require('@grpc/proto-loader');
126+
const chronos = require('chronos');
127+
128+
const PROTO_PATH = './order.proto';
129+
130+
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
131+
keepCase: true,
132+
longs: String,
133+
enums: String,
134+
arrays: true,
135+
});
136+
const OrderToBookService = grpc.loadPackageDefinition(packageDefinition).OrderToBook;
137+
const bookClient = new OrderToBookService('localhost:30044', grpc.credentials.createInsecure());
138+
139+
const ClientWrapper = chronos.ClientWrapper(bookClient, OrderToBookService);
140+
```
141+
Next wrap the gRPC server using Chronos
142+
```js
143+
144+
const ServerWrapper = chronos.ServerWrapper(server, Proto.protoname.service, {
145+
AddBook: (call, callback) => {
146+
// console.log(call.metadata)
147+
// get the properties from the gRPC client call
148+
const { title, author, numberOfPages, publisher, bookID } = call.request;
149+
// create a book in our book collection
150+
BookModel.create({
151+
title,
152+
author,
153+
numberOfPages,
154+
publisher,
155+
bookID,
156+
});
157+
callback(null, {});
158+
},
159+
});
160+
161+
```
162+
For any request you wish to trace, require uuidv4 and write the following code where the initial gRPC request begins,
163+
```js
164+
const require { v4: uuidv4} = require('uuid')
165+
const createMeta = () => {
166+
const meta = new grpc.Metadata();
167+
meta.add('id', uuidvd());
168+
return meta
169+
}
170+
```
171+
and then, invoke createMeta as a third argument to any client method that is the beginning of the request path.
172+
173+
```js
174+
orderClient.AddOrder(
175+
order,
176+
(err, data) => {
177+
if (err !== null) {
178+
console.log(err);
179+
// could not add order because bookID does not exist
180+
return res.sendStatus(404);
181+
}
182+
console.log('addOrder response: ', data);
183+
return res.sendStatus(200);
184+
},
185+
createMeta()
186+
);
187+
188+
```
189+
Finally, on all servers that will be involved in the request path, invoke `chronos.link` with parameters of `client` and `ServerWrapper` in the server wrapper.
190+
191+
```js
192+
chronos.link(client, ServerWrapper);
193+
```
194+
195+
### Apache Monitoring (Via JMX to Prometheus Exporter)
196+
197+
Chronos now offers the ability to monitor an Apache Kafka cluster via JMX to Prometheus Exporter. In order for this feature to work you must be running [JMX to Prometheus
198+
Exporter](https://github.com/prometheus/jmx_exporter) either as a Java Agent with your cluster or as a standalone HTTP server. Then, use `chronos-config.js` to specifiy the port exposed for metrics scraping.
199+
200+
To start, add the property `jmxuri` to the object in `chronos-config.js`. Your file should look similar to this:
201+
202+
```js
203+
// A sample `chronos-config.js` file
204+
205+
const chronos = require('chronos-tracker-7');
206+
207+
chronos.use({
208+
microservice: 'payments',
209+
interval: 5000,
210+
dockerized: true,
211+
jmxuri: // your URI here
212+
database: {
213+
connection: 'REST',
214+
type: 'MongoDB',
215+
URI: process.env.URI,
216+
},
217+
notifications: [],
218+
});
219+
```
220+
The `jmxuri` property should be a string whose value is the port specified for scraping when starting the exporter.
221+
222+
Then, in ***ONE AND ONLY ONE** of your microservices, call
223+
224+
```js
225+
226+
chronos.kafka()
227+
228+
```
229+
230+
in your express server. When viewing your information in the Chronos Electron application the data will be available in the service "kafkametrics"
231+
232+
### Docker Configuration
233+
234+
Again, this step is **only applicable** if you are currently using <a href="#"><img src="./app/assets/docker-logo-color.png" alt="Docker" title="Docker" align="center" height="20" /></a> containers for your microservices.
235+
236+
<a href="#"><img src="./app/assets/important.png" alt="Important" title="Important" align="center" height="20" /></a> Give your containers the same names you pass in as arguments for microservice names.
237+
238+
<a href="#"><img src="./app/assets/important.png" alt="Important" title="Important" align="center" height="20" /></a> In order to have container statistics saved to your database along with other health info, bind volumes to this path when starting up the containers:
239+
```
240+
/var/run/docker.sock
241+
```
242+
243+
For example, you can type the following when starting up a container:
244+
```
245+
docker run -v /var/run/docker.sock:/var/run/docker.sock [your-image-tag]
246+
```
247+
248+
If you're using `docker-compose` to start up multiple containers, you can add a `volumes` key for each of your services in the `docker-compose.yml` file:
249+
250+
```
251+
volumes:
252+
- "/var/run/docker.sock:/var/run/docker.sock"
253+
```
254+
255+
## Notifications
256+
257+
The `notifications` property is optional and allows developers to be alerted when the server responds to requests with status codes >= 400. To set up notifications, set the value of the `notifications` property to an array of objects, each with a `type` and `settings` property.
258+
259+
Chronos only supports **Slack** and **email** notifications.
260+
<br>
261+
262+
### Slack
263+
264+
Chronos uses the <a href="#"><img src="./app/assets/slack-logo-color.png" alt="Slack" title="Slack" align="center" height="20" /></a> API to send messages to a Slack channel and only requires the **webhook url**. Learn how to set up [Slack webhooks](https://api.slack.com/messaging/webhooks) for your team.
265+
266+
An example of configured **slack** settings:
267+
268+
```js
269+
// ...
270+
notifications: [
271+
{
272+
type: 'email',
273+
settings: {
274+
slackurl: process.env.WEBHOOK
275+
}
276+
}
277+
]
278+
// ...
279+
```
280+
281+
### Email
282+
Chronos provides the option to send <a href="#"><img src="./app/assets/email-icon-black.png" alt="Slack" title="Slack" align="center" height="20" /></a> emails. The properties that should be provided are the following
283+
- `emails` - The recipient list (string) can be a single email address or multiple as comma seprated values.
284+
- `emailHost` - The smtp host (string) of your email server
285+
- `emailPort` - The email port (integer) is either **465** or **587** depending on the sender email security settings. Learn more about email ports by reading the [nodemailer docs](https://nodemailer.com/smtp/)
286+
- `user` - The email address (string) of the sender
287+
- `password` - The password (string) of the sender email
288+
289+
_NOTE: Email notification settings may require alternative security settings to work_
290+
291+
An example of configured **email** settings:
292+
293+
```js
294+
// ...
295+
notifications: [
296+
{
297+
type: 'email',
298+
settings: {
299+
300+
emailHost: '[email protected]',
301+
emailPort: 465,
302+
user: process.env.SENDER_EMAIL,
303+
password: process.env.SENDER_PASSWORD
304+
}
305+
}
306+
]
307+
// ...
308+
```
309+
#
310+
###### Return to [Top](#chronos)
311+
<br>

0 commit comments

Comments
 (0)