Skip to content
Mehrdad Arshad Rad edited this page Dec 17, 2020 · 5 revisions

gRPC integration

Protobuf

There are three RPCs to add new target, delete target and get metrics for specific target (stream). you can find the tcpprobe.proto here.

service TCPProbe {
    rpc Add(Target) returns (Response){}
    rpc Delete(Target) returns (Response){}
    rpc Get(Target) returns (stream Stats) {}
}

message Target {
    string addr = 1;
    string interval = 2;
    map<string, string> labels = 3;
}

message Response {
    int32 code =1;
    string message = 2;
}

message Stats {
   google.protobuf.Struct metrics = 1;
}

Run TCPProbe in gRPC mode

binary

tcpprobe --grpc

docker container

docker run -p 8082:8082 mehrdadrad/tcpprobe:latest --grpc

Python

python3 -m grpc_tools.protoc -I../proto --python_out=. --grpc_python_out=. ../proto/tcpprobe.proto
import signal
import sys
import grpc
import tcpprobe_pb2
import tcpprobe_pb2_grpc

from google.protobuf import json_format


def tcpprobe():
    addr = "https://www.google.com"
    with grpc.insecure_channel('127.0.0.1:8082') as channel:
        stub = tcpprobe_pb2_grpc.TCPProbeStub(channel)
        stub.Add(tcpprobe_pb2.Target(
            addr=addr, interval="10s"))

        stream = stub.Get(tcpprobe_pb2.Target(
            addr=addr))

        def cancel_request(unused_signum, unused_frame):
            stub.Delete(tcpprobe_pb2.Target(addr=addr))
            stream.cancel()
            sys.exit(0)

        signal.signal(signal.SIGINT, cancel_request)
        for m in stream:
            print(json_format.MessageToDict(
                m.metrics))


if __name__ == '__main__':
    tcpprobe()