Skip to content

Commit 6d3f096

Browse files
committed
syslog-ng-ctl: workaround invalid escaping in syslog-ng/AxoSyslog metrics
Signed-off-by: László Várady <laszlo.varady@axoflow.com>
1 parent fd3bfdd commit 6d3f096

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

pkg/syslog-ng-ctl/stats_prometheus.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,39 @@ func transformEventDelayMetric(delayMetric *io_prometheus_client.MetricFamily, d
154154
delayMetric.Type = io_prometheus_client.MetricType_GAUGE.Enum()
155155
}
156156

157+
// Workaround for a bug in older syslog-ng/AxoSyslog versions where the output of STATS PROMETHEUS was overescaped.
158+
// Escapes \ as \\ everywhere except for the allowed sequences: \\, \n, \"
159+
func sanitizeBuggyFormat(output string) string {
160+
var fixedOutput strings.Builder
161+
162+
length := len(output)
163+
for i := 0; i < length; i++ {
164+
c := output[i]
165+
166+
if c != '\\' {
167+
fixedOutput.WriteByte(c)
168+
continue
169+
}
170+
171+
if i+1 >= length {
172+
fixedOutput.WriteString("\\\\")
173+
break
174+
}
175+
176+
next := output[i+1]
177+
if next == '\\' || next == 'n' || next == '"' {
178+
fixedOutput.WriteByte(c)
179+
fixedOutput.WriteByte(next)
180+
i++
181+
continue
182+
}
183+
184+
fixedOutput.WriteString("\\\\")
185+
}
186+
187+
return fixedOutput.String()
188+
}
189+
157190
func StatsPrometheus(ctx context.Context, cc ControlChannel, lastMetricQueryTime *time.Time) ([]*io_prometheus_client.MetricFamily, error) {
158191
rsp, err := cc.SendCommand(ctx, "STATS PROMETHEUS")
159192
if err != nil {
@@ -169,6 +202,7 @@ func StatsPrometheus(ctx context.Context, cc ControlChannel, lastMetricQueryTime
169202
return maps.Values(mfs), err
170203
}
171204

205+
rsp = sanitizeBuggyFormat(rsp)
172206
mfs, err = new(expfmt.TextParser).TextToMetricFamilies(strings.NewReader(rsp))
173207

174208
var delayMetric *io_prometheus_client.MetricFamily

0 commit comments

Comments
 (0)