java-grpc-prometheus icon indicating copy to clipboard operation
java-grpc-prometheus copied to clipboard

Collecting metrics

Open igorvpcleao opened this issue 8 years ago • 5 comments

Hey everyone,

I'm having difficulties to get the library working. Right below I set a register and bind it to a pushgateway. In addition, I define a gauge and set it to the previous defined register, as well as pass this register as a parameter to MonitoringServerInterceptor . Whenever e check the metrics on pushgateway I only see the gauge I defined. No Grpc metrics are found there.

Am I doing anything wrong?

Thanks in advance!

public class UserApiServer {
  private static final int APP_DEFAULT_PORT = 50020;
  private static final Logger logger = Logger.getLogger(UserApiServer.class.getName());
  private Server server;
  static final CollectorRegistry pushRegistry = new CollectorRegistry();
  static final Gauge g = (Gauge) Gauge.build().name("userapi").help("blah").register(pushRegistry);

  void start(final int port) throws IOException {
    final MonitoringServerInterceptor monitoringInterceptor =
        MonitoringServerInterceptor.create(Configuration.cheapMetricsOnly().withCollectorRegistry(pushRegistry));
    server = ServerBuilder.forPort(port)
        .addService(ServerInterceptors.intercept(new UserApiService().bindService(), monitoringInterceptor))
        .build()
        .start();

        
    logger.info("Server started, listening on " + port);
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
      logger.warning("*** shutting down gRPC server since JVM is shutting down");
      try {
        UserApiServer.this.stop();
      } catch (IOException ignore) {}
      logger.warning("*** server shut down");
    }));
  }

  ...

  public static void main(String[] args) throws IOException, InterruptedException {
    Thread.setDefaultUncaughtExceptionHandler(RaygunExceptionHandler.getInstance());
    PushGateway pg = new PushGateway("localhost:9091" );
    g.set(42);
    pg.push(pushRegistry, "grpc");

    final UserApiServer server = new UserApiServer();
    server.start(APP_DEFAULT_PORT);
    server.blockUntilShutdown();
  }
}

igorvpcleao avatar May 08 '17 13:05 igorvpcleao

I'm having similar issues. It would be great to know if there's anything wrong with this approach.

racevedoo avatar May 08 '17 14:05 racevedoo

i got this problems: io.netty.handler.codec.http2.Http2Exception: HTTP/2 client preface string missing or corrupt. Hex dump for received bytes: 16030100850100008103031a44d4a944f84807b840c9d654

shanghai-Jerry avatar Jun 07 '17 08:06 shanghai-Jerry

@igorvpcleao your code snippet looks sane to me. One thing worth noting is that you will only get metrics once the first rpc is intercepted. Could it be that you just haven't triggered any rpcs yet?

dinowernli avatar Jun 13 '17 16:06 dinowernli

Where does it shows the logs of the intercepted requests from the client on server? Is there a method we need to call to print out the logs?

wingoku avatar Nov 08 '17 10:11 wingoku

Did someone able to figure this out. I wanted to capture metrics for my own service.Any help is appreciated.

wandermonk avatar Oct 21 '21 07:10 wandermonk