Skip to content

nyris/prometheus-grpc-bridge

Repository files navigation

Prometheus gRPC Bridge

A Prometheus /metrics endpoint bridging data from a gRPC service, for when you cannot host gRPC and HTTP at the same time.

HTTP /metrics endpoint

When running the service using Docker, provide the GRPC_SERVER_CONNECT_ADDRESS environment variable with the host and port combination of the gRPC server to addess:

docker run --rm \
  --env GRPC_SERVER_CONNECT_ADDRESS="example:11000" \
  --publish 80:80 \
  nyris/prometheus-grpc-bridge:0.1.0

You can optionally provide GRPC_SERVER_CONNECT_SCHEME as well, this defaults to http.

The server supports both HTTP/1.1 and HTTP/2 transparently without requiring a secure connection to perform ALPN.

For a complete example, see docker-compose.yml. You can start both the bridge and an example server by running:

docker compose up

Testing locally

To host the service locally, run e.g.

cargo run -- --serve 0.0.0.0:8080

Afterwards, you can fetch metrics using e.g. curl (HTTP/1.1) or nghttp (HTTP/2 prior knowledge):

curl http://localhost:8080/metrics
nghttp http://localhost:8080/metrics

The server's log output can be controlled using the RUST_LOG environment variable. To switch between plain and JSON log output, provide the --log simple or --log json argument respectively:

cargo run -- --serve 0.0.0.0:8080 --log json

Note that logging of debug level and below is disabled in release builds.

Protocol

The gRPC protocol is kept simple and assumes the metrics can already be provided in Prometheus text format as described in the Exposition formats section. See the prometheus.proto file for the protocol:

syntax = "proto3";

package prometheus;

service PrometheusMetrics {
  rpc Metrics(PrometheusMetricsRequest) returns (PrometheusMetricsResponse) {}
}

message PrometheusMetricsRequest {
}

message PrometheusMetricsResponse {
  // The metrics as a string in Prometheus text format.
  string text = 1;
}

Example server

This project comes with a Rust example server based on Tonic. To run the server defined in examples/grpc_server.rs, execute

cargo run --example grpc_server

It provides a simple instance metric and a gauge that increments with every request. To quickly poll the server using the default settings, run:

cargo run

Similarly, you can inspect the gRPC call using grpcurl:

grpcurl -plaintext localhost:11000 prometheus.PrometheusMetrics/Metrics

License

If not mentioned otherwise in a file, the content provided is licensed under the EUPL v1.2, a copy of which can be found in LICENSE.md.