-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Pull `Admin` module out of `Metrics` This pulls the `hyper` server out of the `Metrics` module and move it into its own `Admin` module that handles metrics, and in the future, the health/liveness endpoint as well. Closes #101
- Loading branch information
1 parent
35975f1
commit b5e088f
Showing
20 changed files
with
270 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Administration Interface | ||
|
||
Quilkin exposes an HTTP interface to query different aspects of the server. | ||
|
||
> It is assumed that the administration interface will only ever be able to be accessible on `localhost`. | ||
By default, the administration interface is bound to `[::]:9091`, but it can be configured through the | ||
[proxy configuration file](./proxy-configuration.md), like so: | ||
|
||
```yaml | ||
admin: | ||
address: [::]:9095 | ||
``` | ||
The admin interface provides the following endpoints: | ||
## /metrics | ||
Outputs [Prometheus](https://prometheus.io/) formatted metrics for this proxy. | ||
See the [Proxy Metrics](./proxy.md#metrics) documentation for what metrics are available. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2021 Google LLC All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
use std::convert::Infallible; | ||
use std::net::SocketAddr; | ||
use std::sync::Arc; | ||
|
||
use hyper::service::{make_service_fn, service_fn}; | ||
use hyper::{Body, Method, Request, Response, Server as HyperServer, StatusCode}; | ||
use slog::{error, info, o, Logger}; | ||
use tokio::sync::watch; | ||
|
||
use crate::proxy::Metrics; | ||
|
||
pub struct Admin { | ||
log: Logger, | ||
/// The address that the Admin server starts on | ||
addr: SocketAddr, | ||
metrics: Arc<Metrics>, | ||
} | ||
|
||
impl Admin { | ||
pub fn new(base: &Logger, addr: SocketAddr, metrics: Arc<Metrics>) -> Self { | ||
Admin { | ||
log: base.new(o!("source" => "proxy::Admin")), | ||
addr, | ||
metrics, | ||
} | ||
} | ||
|
||
pub fn run(&self, mut shutdown_rx: watch::Receiver<()>) { | ||
info!(self.log, "Starting admin endpoint"; "address" => self.addr.to_string()); | ||
|
||
let metrics = self.metrics.clone(); | ||
let make_svc = make_service_fn(move |_conn| { | ||
let metrics = metrics.clone(); | ||
async move { | ||
let metrics = metrics.clone(); | ||
Ok::<_, Infallible>(service_fn(move |req| { | ||
let metrics = metrics.clone(); | ||
async move { Ok::<_, Infallible>(handle_request(req, metrics)) } | ||
})) | ||
} | ||
}); | ||
|
||
let server = HyperServer::bind(&self.addr) | ||
.serve(make_svc) | ||
.with_graceful_shutdown(async move { | ||
shutdown_rx.changed().await.ok(); | ||
}); | ||
|
||
let log = self.log.clone(); | ||
tokio::spawn(async move { | ||
if let Err(err) = server.await { | ||
error!(log, "Admin server exited with an error"; "error" => %err); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
fn handle_request(request: Request<Body>, metrics: Arc<Metrics>) -> Response<Body> { | ||
match (request.method(), request.uri().path()) { | ||
(&Method::GET, "/metrics") => metrics.collect_metrics(), | ||
(_, _) => { | ||
let mut response = Response::new(Body::empty()); | ||
*response.status_mut() = StatusCode::NOT_FOUND; | ||
response | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.