hs: Double quote the metrics label value

Fixes #40552

Signed-off-by: David Goulet <dgoulet@torproject.org>
This commit is contained in:
David Goulet 2022-02-02 13:51:22 -05:00
parent 1b58399b07
commit 3c73622f27
3 changed files with 10 additions and 3 deletions

3
changes/ticket40552 Normal file
View File

@ -0,0 +1,3 @@
o Minor bugfixes (MetricsPort, Prometheus):
- Add double quotes to the label values of the onion service metrics. Fixes
bug 40552; bugfix on 0.4.5.1-alpha.

View File

@ -32,12 +32,16 @@ port_to_str(const uint16_t port)
/** Return a static buffer pointer that contains a formatted label on the form /** Return a static buffer pointer that contains a formatted label on the form
* of key=value. * of key=value.
* *
* NOTE: Important, label values MUST NOT contain double quotes else, in the
* case of Prometheus, it will fail with a malformed line because we force the
* label value to be enclosed in double quotes.
*
* Subsequent call to this function invalidates the previous buffer. */ * Subsequent call to this function invalidates the previous buffer. */
static const char * static const char *
format_label(const char *key, const char *value) format_label(const char *key, const char *value)
{ {
static char buf[128]; static char buf[128];
tor_snprintf(buf, sizeof(buf), "%s=%s", key, value); tor_snprintf(buf, sizeof(buf), "%s=\"%s\"", key, value);
return buf; return buf;
} }

View File

@ -30,8 +30,8 @@
#define TEST_METRICS_ENTRY_NAME "entryA" #define TEST_METRICS_ENTRY_NAME "entryA"
#define TEST_METRICS_ENTRY_HELP "Description of entryA" #define TEST_METRICS_ENTRY_HELP "Description of entryA"
#define TEST_METRICS_ENTRY_LABEL_1 "label=farfadet" #define TEST_METRICS_ENTRY_LABEL_1 "label=\"farfadet\""
#define TEST_METRICS_ENTRY_LABEL_2 "label=ponki" #define TEST_METRICS_ENTRY_LABEL_2 "label=\"ponki\""
static void static void
set_metrics_port(or_options_t *options) set_metrics_port(or_options_t *options)