--- title: Prometheus and Aegis date: 2021-04-05 tags: - prometheus - o11y --- # Prometheus and Aegis [*Last time on the christine dot website cinematic universe:*](https://christine.website/blog/unix-domain-sockets-2021-04-01) *Unix sockets started to be used to grace the cluster. Things were at peace. Then, a realization came through:* [What about Prometheus? Doesn't it need a direct line of fire to the service to scrape metrics?](conversation://Mara/hmm?smol) *This could not do! Without observability the people of the Discord wouldn't have a livefeed of the infrastructure falling over! This cannot stand! Look, our hero takes action!* [It will soon!](conversation://Cadey/percussive-maintenance?smol) In order to help keep an eye on all of the services I run, I use [Prometheus](https://prometheus.io/) for collecting metrics. For an example of the kind of metrics I collect, see [here (1)](/metrics). In the configuration that I have, Prometheus runs on a server in my apartment and reaches out to my other machines to scrape metrics over the network. This worked great when I had my major services listen over TCP, I could just point Prometheus at the backend port over my tunnel. When I started using Unix sockets for hosting my services, this stopped working. It became very clear very quickly that I needed some kind of shim. This shim needed to do the following things: - Listen over the network as a HTTP server - Connect to the unix sockets for relevant services based on the path (eg. `/xesite` should get the metrics from `/srv/within/run/xesite.sock`) - Do nothing else The Go standard library has a tool for doing reverse proxying in the standard library: [`net/http/httputil#ReverseProxy`](https://pkg.go.dev/net/http/httputil#ReverseProxy). Maybe we could build something with this? [The documentation seems to imply it will use the network by default. Wait, what's this `Transport` field?](conversation://Mara/hmm?smol) ```go type ReverseProxy struct { // ... // The transport used to perform proxy requests. // If nil, http.DefaultTransport is used. Transport http.RoundTripper // ... } ``` [So a transport is a `RoundTripper`, which is a function that takes a request and returns a response somehow. It uses `http.DefaultTransport` by default, which reads from the network. So at a minimum we're gonna need: