Skip to content

Commit

Permalink
fix(plugin/prometheus): fix an error of ngx.re.gsub (#11115)
Browse files Browse the repository at this point in the history
Summary
The second param of ngx.re.gsub() is a regex pattern,
so \\ is actually \, which is an invalid regex.

Note: ngx.re.gsub() is different from string.gsub()!

This PR gives the correct regex to ngx.re.gsub().

* fix ngx.re.gsub

* style lint
  • Loading branch information
chronolaw authored Jun 27, 2023
1 parent 9a95a32 commit f005013
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions kong/plugins/prometheus/prometheus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ local function full_metric_name(name, label_names, label_values)
return name
end

local slash, double_slash, reg_slash = [[\]], [[\\]], [[\\]]
local quote, slash_quote, reg_quote = [["]], [[\"]], [["]]

local buf = buffer.new(NAME_BUFFER_SIZE_HINT)

-- format "name{k1=v1,k2=v2}"
Expand All @@ -200,12 +203,12 @@ local function full_metric_name(name, label_names, label_values)
label_value = string.sub(label_value, 1, pos - 1)
end

if string.find(label_value, "\\", 1, true) then
label_value = ngx_re_gsub(label_value, "\\", "\\\\", "jo")
if string.find(label_value, slash, 1, true) then
label_value = ngx_re_gsub(label_value, reg_slash, double_slash, "jo")
end

if string.find(label_value, '"', 1, true) then
label_value = ngx_re_gsub(label_value, '"', '\\"', "jo")
if string.find(label_value, quote, 1, true) then
label_value = ngx_re_gsub(label_value, reg_quote, slash_quote, "jo")
end
end

Expand Down

1 comment on commit f005013

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

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

Bazel Build

Docker image available kong/kong:f0050135c204d9cc4f716a103de2bbbeb9539dc2
Artifacts available https://github.com/Kong/kong/actions/runs/5385332601

Please sign in to comment.