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

prometheus: sanitize label value for text protocol #2400

Closed
wants to merge 3 commits into from

Commits on Aug 27, 2024

  1. tests: unit test prometheus wire format

    Adds a test to verify that the metrics returned with the prometheus text
    format are formatted as expected.
    pgellert committed Aug 27, 2024
    Configuration menu
    Copy the full SHA
    d308aba View commit details
    Browse the repository at this point in the history
  2. prometheus: sanitize label value for text protocol

    The prometheus text protocol requires that the label value to be
    escaped. Note that this is only needed for the text protocol and not for
    the protobuf-based prometheus protocol.
    
    Without this sanitization, a single label with one of these special
    characters makes the whole metrics output unparseable by prometheus.
    
    > label_value can be any sequence of UTF-8 characters, but the backslash
    > (\), double-quote ("), and line feed (\n) characters have to be
    > escaped as \\, \", and \n, respectively.
    
    From https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md
    pgellert committed Aug 27, 2024
    Configuration menu
    Copy the full SHA
    6062086 View commit details
    Browse the repository at this point in the history

Commits on Sep 2, 2024

  1. prometheus: fmt::print to stringstream directly

    This avoids a string copy when writing the metric value and the metric
    type to the prometheus text-based response.
    
    Note that now `fmt::print` is used to format the values instead of the
    originally used `std::to_string`. This is because `std::to_string` is
    going to change its formatting behaviour in C++26, so we prefer to use a
    more deterministic formatter here.
    
    The formatting of the numbers matters because if the floating point
    numbers are converted to scientific notation, they can lose precision.
    
    Ref: https://en.cppreference.com/w/cpp/string/basic_string/to_string
    pgellert committed Sep 2, 2024
    Configuration menu
    Copy the full SHA
    6e2f629 View commit details
    Browse the repository at this point in the history