Skip to content

Commit

Permalink
metrics (#29)
Browse files Browse the repository at this point in the history
* add barebones metrics and bump `dwn-sdk-js` to `0.0.32`

* update `CODEOWNERS`

* Fix tests failing due to upstream dwn-sdk-js error code change

Signed-off-by: Frank Hinek <[email protected]>

* Consist catch block

Signed-off-by: Frank Hinek <[email protected]>

---------

Signed-off-by: Frank Hinek <[email protected]>
Co-authored-by: Frank Hinek <[email protected]>
  • Loading branch information
mistermoe and frankhinek authored May 16, 2023
1 parent 977278c commit 5178a0c
Show file tree
Hide file tree
Showing 7 changed files with 559 additions and 514 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# These owners will be the default owners for everything in the repo.
* @michaelneale
* @mistermoe
* @frankhinek


# -----------------------------------------------
Expand Down
1,029 changes: 522 additions & 507 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
"type": "module",
"version": "0.0.2",
"dependencies": {
"@tbd54566975/dwn-sdk-js": "0.0.30",
"@tbd54566975/dwn-sdk-js": "0.0.32",
"bytes": "3.1.2",
"node-fetch": "3.3.1",
"cors": "2.8.5",
"express": "4.18.2",
"multiformats": "11.0.2",
"prom-client": "14.2.0",
"readable-stream": "4.3.0",
"response-time": "2.3.2",
"uuid": "9.0.0",
"ws": "8.12.0"
},
Expand Down Expand Up @@ -38,4 +40,4 @@
"test": "npm run compile && cp -R tests/fixtures dist/tests && mocha dist/tests/*.spec.js",
"server": "npm run compile && node dist/src/main.js"
}
}
}
31 changes: 29 additions & 2 deletions src/http-api.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { Express, Request } from 'express';
import type { Express, Request, Response } from 'express';
import type { Dwn } from '@tbd54566975/dwn-sdk-js';
import type { RequestContext } from './lib/json-rpc-router.js';
import responseTime from 'response-time';

import cors from 'cors';
import express from 'express';
import { register, Histogram } from 'prom-client';

import { v4 as uuidv4 } from 'uuid';

Expand All @@ -18,13 +20,38 @@ export class HttpApi {
this.api = express();
this.dwn = dwn;

const responseHistogram = new Histogram({
name : 'http_response',
help : 'response histogram',
buckets : [50, 250, 500, 750, 1000],
labelNames : ['route', 'code'],
});

this.api.use(cors({ exposedHeaders: 'dwn-response' }));
this.api.use(responseTime((req: Request, res: Response, time) => {
const url = req.url === '/' ? '/jsonrpc' : req.url;
const route = (req.method + url).toLowerCase()
.replace(/[:.]/g, '')
.replace(/\//g, '_');

const statusCode = res.statusCode.toString();
responseHistogram.labels(route, statusCode).observe(time);
}));

this.api.get('/health', (_req, res) => {
// return 200 ok
return res.json({ ok: true });
});

this.api.get('/metrics', async (req, res) => {
try {
res.set('Content-Type', register.contentType);
res.end(await register.metrics());
} catch (e) {
res.status(500).end(e);
}
});

this.api.get('/', (_req, res) => {
// return a plain text string
res.setHeader('content-type', 'text/plain');
Expand All @@ -43,7 +70,7 @@ export class HttpApi {

try {
dwnRequest = JSON.parse(dwnRequest);
} catch(e) {
} catch (e) {
const reply = createJsonRpcErrorResponse(uuidv4(), JsonRpcErrorCodes.BadRequest, e.message);

return res.status(400).json(reply);
Expand Down
2 changes: 1 addition & 1 deletion src/json-rpc-handlers/dwn/process-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const handleDwnProcessMessage: JsonRpcHandler = async (dwnRequest, contex
}

return responsePayload;
} catch(e) {
} catch (e) {
const jsonRpcResponse = createJsonRpcErrorResponse(
requestId, JsonRpcErrorCodes.InternalError, e.message);

Expand Down
2 changes: 1 addition & 1 deletion src/ws-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class WsApi {
}

dwnRequest = JSON.parse(dwnRequest);
} catch(e) {
} catch (e) {
const jsonRpcResponse = createJsonRpcErrorResponse(
uuidv4(), JsonRpcErrorCodes.BadRequest, e.message);

Expand Down
2 changes: 1 addition & 1 deletion tests/http-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('http api', function() {

const { reply } = body.result;
expect(reply.status.code).to.equal(400);
expect(reply.status.detail).to.include('MessageStoreDataNotFound');
expect(reply.status.detail).to.include('RecordsWriteMissingDataStream');
});

it('exposes dwn-response header', async function() {
Expand Down

0 comments on commit 5178a0c

Please sign in to comment.