@coozzy/prometheus
Advanced tools
Comparing version
@@ -6,2 +6,5 @@ /*~ If this module is a UMD module that exposes a global variable 'myLib' when | ||
import {Request, Response, NextFunction} from "express"; | ||
import {ServerInterceptingCall} from "@grpc/grpc-js"; | ||
import {ServerInterceptingCallInterface} from "@grpc/grpc-js/src/server-interceptors"; | ||
import {ServerMethodDefinition} from "@grpc/grpc-js/build/src/make-client"; | ||
@@ -21,3 +24,3 @@ export as namespace metrics; | ||
function grpcMiddleware(ctx: object, next: () => void): void; | ||
function grpcMiddleware(methodDescriptor: ServerMethodDefinition<any, any>, call: ServerInterceptingCallInterface): ServerInterceptingCall; | ||
@@ -24,0 +27,0 @@ function incGrpcError(clientName: string, methodName: string, errorCode: number): void; |
41
index.js
const Prometheus = require('prom-client'); | ||
const http = require('http'); | ||
const grpc = require('@grpc/grpc-js'); | ||
@@ -84,19 +85,31 @@ const logger = require('@coozzy/logger'); | ||
module.exports.grpcMiddleware = async function (ctx, next) { | ||
const startTime = Date.now(); | ||
module.exports.grpcMiddleware = function (methodDescriptor, call) { | ||
let startTime; | ||
await next(); | ||
const listener = new grpc.ServerListenerBuilder() | ||
.withOnReceiveMessage((message, next) => { | ||
startTime = Date.now(); | ||
next(message); | ||
}) | ||
.build(); | ||
const responseTime = Date.now() - startTime; | ||
const responder = new grpc.ResponderBuilder() | ||
.withStart(next => next(listener)) | ||
.withSendStatus((status, next) => { | ||
const responseTime = Date.now() - startTime; | ||
if (grpcRequestDurationMilliseconds) { | ||
grpcRequestDurationMilliseconds | ||
.observe( | ||
{ | ||
method: ctx.call.metadata.get('method'), | ||
status_code: ctx.status.details && ctx.status.details.code ? ctx.status.details.code : ctx.status.code | ||
}, | ||
responseTime | ||
); | ||
} | ||
if (grpcRequestDurationMilliseconds) { | ||
grpcRequestDurationMilliseconds | ||
.observe( | ||
{ | ||
method: methodDescriptor.path ? methodDescriptor.path.substring(methodDescriptor.path.lastIndexOf('/') + 1): 'unknown-path', | ||
status_code: status.code | ||
}, | ||
responseTime | ||
); | ||
} | ||
next(status); | ||
}) | ||
.build() | ||
return new grpc.ServerInterceptingCall(call, responder); | ||
}; | ||
@@ -103,0 +116,0 @@ |
{ | ||
"name": "@coozzy/prometheus", | ||
"version": "3.2.3", | ||
"version": "3.2.4", | ||
"description": "Helps collecting and exporting prometheus metrics", | ||
@@ -18,3 +18,4 @@ "main": "index.js", | ||
"@coozzy/logger": "1.3.2", | ||
"prom-client": "15.1.2" | ||
"prom-client": "15.1.2", | ||
"@grpc/grpc-js": "1.10.6" | ||
}, | ||
@@ -21,0 +22,0 @@ "devDependencies": { |
9536
10.41%224
7.69%4
33.33%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added