c++c++17rpcthriftthrift-protocol

Dump apache thrift messages for debugging purposes?


Apache Thrift is one of the more popular choices for an opensource RPC frameworks (gRPC is also one that gets lot of tracktion since release into the open).

In my setup in Im using a TMultiplexedProcessor. I guess this could be any TProcessor for that matter since Im just interested in printing whatever is sent.

This has a method called:

  bool process(std::shared_ptr<protocol::TProtocol> in,
               std::shared_ptr<protocol::TProtocol> out,
               void* connectionContext) override {

My idea was to override this again, so that I could print the in argument - but how can I write TProtocol to output ? (at a glance, it does not seem straightforward to serialize into a string)

I get a feeling there maybe is some other or an easier method. So my question is how can I dump all messages recieved via thrift (for debugging purpose) ?


Solution

  • There's TProtocolTap and TDebugProtocol.

    Usage example can be found in thrift_dump.cpp:

      shared_ptr<TProtocol> iprot(new TBinaryProtocol(itrans));
      shared_ptr<TProtocol> oprot(
          new TDebugProtocol(
            shared_ptr<TTransport>(new TBufferedTransport(
              shared_ptr<TTransport>(new TFDTransport(STDOUT_FILENO))))));
    
      TProtocolTap tap(iprot, oprot);
    
     std::string name;
          TMessageType messageType;
          int32_t seqid;
          for (;;) {
            tap.readMessageBegin(name, messageType, seqid);
            tap.skip(T_STRUCT);
            tap.readMessageEnd();
          }