Skip to content

[fix][monitor] fix metrics string encoding - #18138

Merged
codelipenghui merged 8 commits into
apache:masterfrom
tjiuming:dev/metrics_encoding
Oct 24, 2022
Merged

codelipenghui merged 8 commits into
apache:masterfrom
tjiuming:dev/metrics_encoding

Conversation

@tjiuming

@tjiuming tjiuming commented Oct 20, 2022 •

Copy link
Copy Markdown
Contributor

Motivation

For the method SimpleTextOutputStream#write(String), it's implementation as below:

    public SimpleTextOutputStream write(String s) {
        if (s == null) {
            return this;
        }
        int len = s.length();
        for (int i = 0; i < len; i++) {
            buffer.writeByte((byte) s.charAt(i));
        }

        return this;
    }

Generally, it works fine. But if there is a character has different length in different encoding, it would lead to an issue, say ¬.

In latin1, it's bytes is [-84], by calling "¬".getBytes(StandardCharset.ISO_8859_1)
but in UTF8, it's bytes is [-62, -84], by calling "¬".getBytes(StandardCharset.UTF_8)

if we replace str.getBytes(charset) with (byte) s.charAt(i), ¬ will display as �.

Documentation

  • doc
  • doc-required
  • doc-not-needed
  • doc-complete

Matching PR in forked repository

PR in forked repository: tjiuming#6

@coderzc coderzc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@asafm

asafm commented Oct 20, 2022

Copy link
Copy Markdown
Contributor

Thanks, @tjiuming for that prompt fix!
I'll just expand a bit on the motivation:

SimpleTextOutputFormat is the class used when writing Prometheus Text Format lines - we print those lines in this class, which inside writes as bytes to a ByteBuf (Netty's). That buffer is then flushed to the HTTP response upon /metrics endpoint (when Prometheus scrapes metrics from Pulsar).

The Prometheus Text Format requires label values to be UTF-8 encoded, as written here:

label_value can be any sequence of UTF-8 characters

Today, as @tjiuming mentioned, the current implementation iterates over the String and each character is written in an undefined encoding - i.e. manipulating the char and then casting it to byte

buffer.writeByte((byte) s.charAt(i));

This causes Prometheus to fail the scraping as it tries to validate that the label value byte array in the response is a valid UTF-8 encoded string (which is not).

The fix as @tjiuming mentioned is encoding the char using UTF-8 as provided by the ByteBuf class.

@github-actions github-actions Bot added the doc-not-needed Your PR changes do not impact docs label Oct 20, 2022
lhotari
lhotari previously approved these changes Oct 20, 2022
@lhotari

lhotari commented Oct 20, 2022

Copy link
Copy Markdown
Member

@lhotari

lhotari commented Oct 20, 2022

Copy link
Copy Markdown
Member

I wonder if it would also make sense to fix this method:

public SimpleTextOutputStream write(char c) {
buffer.writeByte((byte) c);
return this;
}

@lhotari
lhotari dismissed their stale review October 20, 2022 18:19

additional changes required

@tjiuming tjiuming changed the title [fix][monitoring] fix metrics string encoding [fix][monitor] fix metrics string encoding Oct 21, 2022
@tjiuming

Copy link
Copy Markdown
Contributor Author

I wonder if it would also make sense to fix this method:

public SimpleTextOutputStream write(char c) {
buffer.writeByte((byte) c);
return this;
}

write(char) is used to add some symbols like , \n ", generally, it won't lead to an issue. But, sure, fix it is better.
@lhotari I've fixed it, PTAL


public SimpleTextOutputStream write(char c) {
buffer.writeByte((byte) c);
write(String.valueOf(c));

@tjiuming tjiuming Oct 21, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've tried to use buffer.writeChar(), but it doesn't work

@tjiuming
tjiuming requested a review from lhotari October 21, 2022 13:23
@codecov-commenter

codecov-commenter commented Oct 24, 2022 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 45.82%. Comparing base (6c65ca0) to head (7cb9d17).
⚠️ Report is 3529 commits behind head on master.

Files with missing lines Patch % Lines
...che/pulsar/common/util/SimpleTextOutputStream.java 0.00% 2 Missing ⚠️
...ker/stats/prometheus/PrometheusMetricsServlet.java 0.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##             master   #18138       +/-   ##
=============================================
+ Coverage     34.91%   45.82%   +10.90%     
- Complexity     5707    17600    +11893     
=============================================
  Files           607     1574      +967     
  Lines         53396   128637    +75241     
  Branches       5712    14150     +8438     
=============================================
+ Hits          18644    58943    +40299     
- Misses        32119    63630    +31511     
- Partials       2633     6064     +3431     
Flag Coverage Δ
unittests 45.82% <0.00%> (+10.90%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ker/stats/prometheus/PrometheusMetricsServlet.java 0.00% <0.00%> (ø)
...che/pulsar/common/util/SimpleTextOutputStream.java 0.00% <0.00%> (ø)

... and 1114 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@codelipenghui
codelipenghui merged commit 031e37c into apache:master Oct 24, 2022
codelipenghui pushed a commit that referenced this pull request Oct 27, 2022
codelipenghui pushed a commit that referenced this pull request Oct 27, 2022
codelipenghui pushed a commit that referenced this pull request Oct 27, 2022
@codelipenghui codelipenghui modified the milestones: 2.12.0, 2.11.0 Oct 31, 2022
@tjiuming
tjiuming deleted the dev/metrics_encoding branch November 1, 2022 17:38
nicoloboschi pushed a commit to datastax/pulsar that referenced this pull request Nov 9, 2022
(cherry picked from commit 031e37c)
(cherry picked from commit b7def56)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants