Skip to content

Commit

Permalink
Release v3.1.0
Browse files Browse the repository at this point in the history
Changed the HTML rendering to use sections, and definition lists for
labels, to make the output more accessible and easier to navigate for
screenreader users.

If you notice accessiblility issues or have ideas for improvement,
please let me know!
  • Loading branch information
fhemberger committed Sep 11, 2023
1 parent cfc82a4 commit ed3e390
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 34 deletions.
63 changes: 40 additions & 23 deletions extension/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,51 @@ const defaultPaths = [
'^/actuator/prometheus'
]

const formatPrometheusMetrics = (body) => body
.split(/\r?\n/)
.map(line => {
// line is a comment
if (/^#/.test(line)) {
return `<span class="comment">${line}</span>`
}
// https://adrianroselli.com/2022/12/brief-note-on-description-list-support.html
const formatPrometheusMetrics = (body) => {
let previousMetricName = ''
return body
.split(/\r?\n/)
.map(line => {
let tmp

// line is a comment
tmp = line.match(/^# (?:HELP|TYPE) ([^ ]+)/)
if (tmp && tmp.length > 1) {
let metricName = tmp[1]

// First comment, don't render closing </section>
if (previousMetricName == '') {
previousMetricName = metricName
return `<section aria-label="${metricName}">\n<span class="comment">${line}</span>`
}

// line is a metric
// Named RegExp groups not supported by Firefox:
// https://bugzilla.mozilla.org/show_bug.cgi?id=1362154
// const tmp = line.match(/^(?<metric>[\w_]+)(?:\{(?<tags>.*)\})?\x20(?<value>.+)/)
const tmp = line.match(/^([\w_]+)(?:\{(.*)\})?\x20(.+)/)
if (metricName != previousMetricName) {
previousMetricName = metricName
return `</section>\n<section aria-label="${metricName}">\n<span class="comment">${line}</span>`
}

if (tmp && tmp.length > 1) {
let [_, metric, tags, value] = tmp // eslint-disable-line no-unused-vars
if (tags) {
tags = tags.replace(/([^,]+?)="(.*?)"/g, '<span class="label-key">$1</span>="<span class="label-value">$2</span>"')
tags = `{${tags}}`
return `<span class="comment">${line}</span>`
}

return `<span class="metric">${metric}</span>${tags || ''} <span class="value">${value}</span>`
}
// line is a metric
tmp = line.match(/^([\w_]+)(?:\{(.*)\})?\x20(.+)/)
if (tmp && tmp.length > 1) {
let [_, metricName, labels, value] = tmp // eslint-disable-line no-unused-vars

// line is something else, do nothing
return line
})
.join('<br>')
if (labels) {
labels = labels.replace(/([^,]+?)="(.*?)",?/g, '<dt class="label-key" role="associationlistitemkey">$1</dt><dd class="label-value" role="associationlistitemvalue">$2</dd>')
labels = `<dl class="labels" role="associationlist">${labels}</dl>`
}

return `<span class="metric-name">${metricName}</span>${labels || ''} <span class="value">${value}</span>`
}

// line is something else, do nothing
return line
})
.join('<br>') + '</section>'
}

// Listen for requests from content pages wanting to set up a port
chrome.runtime.onConnect.addListener(port => {
Expand Down
22 changes: 16 additions & 6 deletions extension/js/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,31 @@ const renderFormattedHTML = (html) => {
#promformat {
font-family: SFMono-Regular,Consolas,Liberation Mono,Menlo,monospace;
word-wrap: break-word;
white-space: pre-wrap;
}
.comment {
display: inline-block;
}
br + .comment {
padding-top: 1em;
section {
padding-block-end: 1.5em;
}
.comment + br + .comment {
padding-top: 0;
dl,dt,dd {
display: initial;
padding: initial;
margin: initial;
}
.labels::before { color: var(--fg); content: '{' }
.labels::after { color: var(--fg); content: '}' }
.label-key::after { color: var(--fg); content: '="' }
.label-value::after { color: var(--fg); content: '",' }
.label-value:last-child::after { content: '"' }
html { color-scheme: light dark }
body { background-color: var(--bg); color: var(--fg) }
.metric { color: var(--red-1) }
.metric-name { color: var(--red-1) }
.value { color: var(--purple) }
.label-key { color: var(--blue) }
.label-value { color: var(--green) }
Expand All @@ -132,6 +140,8 @@ const renderFormattedHTML = (html) => {

// Insert HTML content
const doc = d.parseFromString(html, 'text/html')
document.documentElement.lang = 'en'
document.title = 'Prometheus metrics'
document.body = doc.body
document.body.id = 'promformat'
}
Expand Down
2 changes: 1 addition & 1 deletion extension/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Prometheus Formatter",
"version": "3.0.1",
"version": "3.1.0",
"manifest_version": 3,
"description": "Makes plain Prometheus/OpenMetrics endpoints easier to read.",
"homepage_url": "https://github.com/fhemberger/prometheus-formatter",
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prometheus-formatter",
"version": "3.0.1",
"version": "3.1.0",
"description": "Browser extension which makes plain Prometheus/OpenMetrics endpoints easier to read.",
"scripts": {
"test": "standard --fix",
Expand Down

0 comments on commit ed3e390

Please sign in to comment.