deeplay-io / nice-grpc

A TypeScript gRPC library that is nice to you
MIT License
414 stars 32 forks source link

Middlewares not working #447

Open iseekyouu opened 11 months ago

iseekyouu commented 11 months ago

trying to implement prometheus-middlewares and got nothing

//server

import { createServer } from 'nice-grpc';
import { prometheusServerMiddleware } from 'nice-grpc-prometheus';
...
    this.grpcServer = createServer(options);
    this.grpcServer.use(prometheusServerMiddleware());

  start(opts: { port: number } = { port: 50501 }) {
    this.grpcServer.listen(`0.0.0.0:${opts.port}`);
  }

// metrics endpoint

import { register as globalRegistry, Registry } from 'prom-client';
import { registry as niceGrpcRegistry } from 'nice-grpc-prometheus';
...
  async function promMw(app: FastifyInstance) {
    app.get(
      '/',
      async function (req, res) {
        const mergedRegistry = Registry.merge([globalRegistry, niceGrpcRegistry]);
        res.code(200).send(await mergedRegistry.metrics());
      });
  }

  fastify.register(promMw, { prefix: 'api/v1/prom' });

after that i called my rpc with BloomRPC and got nothing in metrics

# HELP grpc_server_started_total Total number of RPCs started on the server.
# TYPE grpc_server_started_total counter

# HELP grpc_server_handled_total Total number of RPCs completed on the server, regardless of success or failure.
# TYPE grpc_server_handled_total counter

# HELP grpc_server_msg_received_total Total number of RPC stream messages received by the server.
# TYPE grpc_server_msg_received_total counter

# HELP grpc_server_msg_sent_total Total number of gRPC stream messages sent by the server.
# TYPE grpc_server_msg_sent_total counter

# HELP grpc_server_handling_seconds Histogram of response latency (seconds) of gRPC that had been application-level handled by the server.
# TYPE grpc_server_handling_seconds histogram

# HELP grpc_client_started_total Total number of RPCs started on the client.
# TYPE grpc_client_started_total counter

# HELP grpc_client_handled_total Total number of RPCs completed on the client, regardless of success or failure.
# TYPE grpc_client_handled_total counter

# HELP grpc_client_msg_received_total Total number of RPC stream messages received by the client.
# TYPE grpc_client_msg_received_total counter

# HELP grpc_client_msg_sent_total Total number of gRPC stream messages sent by the client.
# TYPE grpc_client_msg_sent_total counter

# HELP grpc_client_handling_seconds Histogram of response latency (seconds) of the gRPC until it is finished by the application.
# TYPE grpc_client_handling_seconds histogram
iseekyouu commented 11 months ago

Also i tried get yours test middleware

export function createTestServerMiddleware<Ext>(
  contextExt: Ext,
  actions: any[],
  actionTypePrefix = '',
): ServerMiddleware<Ext> {

and put it into my server

    this.grpcServer.use(createTestServerMiddleware(
      { test1: 'test-value-1' },
      actions,
      'middleware1-',
    ));

after that done some calls throught BloomRPC and actions array still empty

aikoven commented 11 months ago

The server.use method does not mutate the server instance, but creates a new instance with the middleware attached.

Try to change this:

this.grpcServer.use(prometheusServerMiddleware());

to this:

this.grpcServer = this.grpcServer.use(prometheusServerMiddleware());