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

Content-Type negotiation #29

Closed
deadtrickster opened this issue Sep 2, 2016 · 6 comments
Closed

Content-Type negotiation #29

deadtrickster opened this issue Sep 2, 2016 · 6 comments
Milestone

Comments

@deadtrickster
Copy link
Owner

This is how Accept header looks currently:

application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited;q=0.7,text/plain;version=0.0.4;q=0.3,application/json;schema=\"prometheus/telemetry\";version=0.0.2;q=0.2,*/*;q=0.1.

prometheus.erl only supported delimited protobuf and text version 0.0.4

@deadtrickster
Copy link
Owner Author

Looks like it's too http dependent and should be handler by exporters

@deadtrickster
Copy link
Owner Author

Looks like there is no standard way to parse accept header yet (will be in next cowlib, what about elli?). I'll write poor's man parser then.

@deadtrickster deadtrickster mentioned this issue Sep 5, 2016
@deadtrickster deadtrickster modified the milestones: Version 4, Version 3 Sep 5, 2016
@aweiker
Copy link

aweiker commented Oct 18, 2016

This is how I did it. It's not the most performant, but it may give you some ideas.

  def index(conn, _params) do
    accept_headers = get_req_header(conn, "accept")
    format = case Enum.any?(accept_headers, &protobuf?/1) do
      true ->
        Prometheus.Format.Protobuf
      _ ->
        Prometheus.Format.Text
    end

    conn
    |> put_resp_header("Content-Type", format.content_type)
    |> send_resp(200, format.format)
  end

  defp protobuf?(header) do
    String.contains?(header, "protobuf")
  end
end

@deadtrickster
Copy link
Owner Author

While this definitely works it's not entirely correct since nothing mandates only two formats. For example, I'm playing with text/html renderer - really helpful for debugging.

But! At least I know now that someone else cares about content type negotiation too, so thank you a lot!

@aweiker
Copy link

aweiker commented Oct 18, 2016

I just use the text for debugging as I'm lazy and just CTRL+F my way through finding the counter I'm looking for. If you have something that does HTML, then I can see how you would want a real accept header parser.

I'm also very curious what kind of UI you have for the html display. I'm not sure if I would see the value in shipping it inside the current libraries, but it would be interesting.

@deadtrickster
Copy link
Owner Author

prometheus-plugs now supports content type negotiation using https://github.com/deadtrickster/accept

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants