Skip to content

Commit 4a00482

Browse files
hodeszaDerekQuoc
andcommitted
Update README
Add gRPC setup instructions Modify config instructions Update current features Co-authored-by: Derek <[email protected]>
1 parent e19371d commit 4a00482

File tree

1 file changed

+105
-5
lines changed

1 file changed

+105
-5
lines changed

README.md

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ Chronos is a comprehensive developer tool that monitors the health and web traff
5656
- Distributed tracing across network request
5757
- Currently chronos gRPC tracing for Mongodb and PostgresQL
5858

59-
## Ver 5.0 Features
59+
## Ver 6.0 Features
6060

6161
- Distributed tracing enabled across microservices applications
62-
- Supports <a href="#"><img src="./app/assets/http-logo-color.png" alt="HTTP" title="HTTP" align="center" height="20" /></a> protocol, with <img src="./app/assets/grpc-logo-color.png" alt="gRPC" title="gRPC" align="center" height="20" /></a> _coming soon_
6362
- Compatible with <img src="./app/assets/graphql-logo-color.png" alt="GraphQL" title="GraphQL" align="center" height="20" /></a>
6463
- Provides <a href="#"><img src="./app/assets/docker-logo-color.png" alt="Docker" title="Docker" align="center" height="20" /></a> container stats (e.g. ID, memory usage %, CPU usage %, running processes, etc.)
6564
- Supports <a href="#"><img src="./app/assets/postgres-logo-color.png" alt="PostgreSQL" title="PostgreSQL" align="center" height="20" /></a> and <img src="./app/assets/mongo-logo-color.png" alt="MongoDB" title="MongoDB" align="center" height="20" /></a> databases
@@ -101,7 +100,7 @@ To use Chronos in your existing application, download and install the following
101100
npm install chronos-tracker
102101
```
103102

104-
### Configure Chronos for REST
103+
### Configure Chronos for REST or gRPC
105104

106105
Similarly, in the **root directory** of _each of your microservice applications_, create a `chronos-config.js` file with properties listed below:
107106

@@ -147,9 +146,10 @@ The `notifications` property is optional. Jump to the section below, [Notificati
147146
Wherever you create an instance of your server (see example below),
148147

149148
```js
150-
// Example
149+
// Example for REST
151150
const express = require('express');
152151
const app = express());
152+
153153
```
154154

155155
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.
@@ -165,10 +165,110 @@ app.use('/', chronos.track());
165165
```
166166

167167
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.
168+
168169
<br>
169170

171+
170172
### Initialize Chronos for gRPC
171173

174+
Wherever you create an instance of your server (see example below),
175+
176+
177+
```js
178+
// Example of gRPC server
179+
const server = new grpc.Server();
180+
181+
server.bindAsync("127.0.0.1:30044", grpc. ServerCredentials.createInsecure(), () => {
182+
server.start();
183+
console.log("Server running at http://127.0.0.1:30044");
184+
});
185+
```
186+
you will also need to require Chronos-tracker, Chronos-config, and dotenv.config(if this is used). For health data simply use Chronos.track()
187+
188+
189+
190+
```js
191+
//track health data
192+
const chronos = require('chronos-tracker');
193+
require('./chronos-config');
194+
require('dotenv').config(); // set up environment variables in .env
195+
const BookModel = require('./BookModel');
196+
197+
chronos.track()
198+
```
199+
To trace requests, first wrap the gRPC client using Chronos
200+
```js
201+
const grpc = require('@grpc/grpc-js');
202+
const protoLoader = require('@grpc/proto-loader');
203+
const chronos = require('chronos');
204+
205+
const PROTO_PATH = './order.proto';
206+
207+
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
208+
keepCase: true,
209+
longs: String,
210+
enums: String,
211+
arrays: true,
212+
});
213+
const OrderToBookService = grpc.loadPackageDefinition(packageDefinition).OrderToBook;
214+
const bookClient = new OrderToBookService('localhost:30044', grpc.credentials.createInsecure());
215+
216+
const ClientWrapper = chronos.ClientWrapper(bookClient, OrderToBookService);
217+
```
218+
Next wrap the gRPC server using Chronos
219+
```js
220+
221+
const ServerWrapper = chronos.ServerWrapper(server, Proto.protoname.service, {
222+
AddBook: (call, callback) => {
223+
// console.log(call.metadata)
224+
// get the properties from the gRPC client call
225+
const { title, author, numberOfPages, publisher, bookID } = call.request;
226+
// create a book in our book collection
227+
BookModel.create({
228+
title,
229+
author,
230+
numberOfPages,
231+
publisher,
232+
bookID,
233+
});
234+
callback(null, {});
235+
},
236+
});
237+
})
238+
```
239+
For any request you wish to trace, require uuidv4 and write the following code where the initial gRPC request begins,
240+
```js
241+
const require { v4: uuidv4} = require('uuid')
242+
const createMeta = () => {
243+
const meta = new grpc.Metadata();
244+
meta.add('id', uuidvd());
245+
return meta
246+
}
247+
```
248+
and then invoke createMeta as a third argument to any client method that is the beginning of the request path.
249+
250+
```js
251+
orderClient.AddOrder(
252+
order,
253+
(err, data) => {
254+
if (err !== null) {
255+
console.log(err);
256+
// could not add order because bookID does not exist
257+
return res.sendStatus(404);
258+
}
259+
console.log('addOrder response: ', data);
260+
return res.sendStatus(200);
261+
},
262+
createMeta()
263+
);
264+
265+
```
266+
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.
267+
268+
```js
269+
chronos.link(client, ServerWrapper);
270+
```
271+
172272
### Docker Configuration
173273

174274
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.
@@ -304,7 +404,7 @@ The **'gRPC'** branch is the current codebase for the <a href="#"><img src="./ap
304404
- <a href="#"><img src="./app/assets/node-logo-color.png" alt="Node" title="Node" align="center" height="30" /></a>
305405
- <a href="#"><img src="./app/assets/express-logo-color.png" alt="Express" title="Express" align="center" height="30" /></a>
306406
- <a href="#"><img src="./app/assets/http-logo-color.png" alt="HTTP" title="HTTP" align="center" height="30" /></a>
307-
- <a href="#"><img src="./app/assets/grpc-logo-color.png" alt="gRPC" title="gRPC" align="center" height="30" /></a> _coming soon_
407+
- <a href="#"><img src="./app/assets/grpc-logo-color.png" alt="gRPC" title="gRPC" align="center" height="30" /></a>
308408
- <a href="#"><img src="./app/assets/graphql-logo-color.png" alt="GraphQL" title="GraphQL" align="center" height="30" /></a>
309409
- <a href="#"><img src="./app/assets/docker-logo-color.png" alt="Docker" title="Docker" align="center" height="30" /></a>
310410
- <a href="#"><img src="./app/assets/aws-logo-color.png" alt="AWS" title="AWS" align="center" height="30" /></a>

0 commit comments

Comments
 (0)