Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust metrics content-type #1361

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/metrics/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Server {

async fn handle_metrics(
reg: Arc<Mutex<Registry>>,
_req: Request<Incoming>,
req: Request<Incoming>,
) -> Response<Full<Bytes>> {
let mut buf = String::new();
let reg = reg.lock().expect("mutex");
Expand All @@ -73,12 +73,26 @@ async fn handle_metrics(
.expect("builder with known status code should not fail");
}

let response_content_type = match req
.headers()
.get(http::header::ACCEPT)
.unwrap_or(&http::HeaderValue::from_str("").expect("static str must parse"))
.to_str()
.unwrap_or_default()
.to_lowercase()
.split(";")
.collect::<Vec<_>>()
.first()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know that prometheus will always scrape with openmetrics as the first 'accept'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, that is likely not a safe invariant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally got back around to this. Got it reworked to support multiple Accept headers and added testing.

{
Some(&"application/openmetrics-text") => {
"application/openmetrics-text;charset=utf-8;version=1.0.0"
}
_ => "text/plain; charset=utf-8",
};

Response::builder()
.status(hyper::StatusCode::OK)
.header(
hyper::header::CONTENT_TYPE,
"application/openmetrics-text;charset=utf-8;version=1.0.0",
)
.header(hyper::header::CONTENT_TYPE, response_content_type)
.body(buf.into())
.expect("builder with known status code should not fail")
}