Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions collector/process_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package collector
import (
"log"
"time"
"unicode/utf8"

common "github.com/ncabatoff/process-exporter"
"github.com/ncabatoff/process-exporter/proc"
Expand Down Expand Up @@ -318,33 +319,34 @@ func (p *NamedProcessCollector) scrape(ch chan<- prometheus.Metric) {

if p.threads {
for _, thr := range gcounts.Threads {
threadName := filterNonutf8Characters(thr.Name)
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
threadName := filterNonutf8Characters(thr.Name)
threadName := strings.ToValidUTF8(thr.Name, "\uFFFD")

I believe this is the "replacement character" that's commonly used for invalid utf-8.

ch <- prometheus.MustNewConstMetric(threadCountDesc,
prometheus.GaugeValue, float64(thr.NumThreads),
gname, thr.Name)
gname, threadName)
ch <- prometheus.MustNewConstMetric(threadCpuSecsDesc,
prometheus.CounterValue, float64(thr.CPUUserTime),
gname, thr.Name, "user")
gname, threadName, "user")
ch <- prometheus.MustNewConstMetric(threadCpuSecsDesc,
prometheus.CounterValue, float64(thr.CPUSystemTime),
gname, thr.Name, "system")
gname, threadName, "system")
ch <- prometheus.MustNewConstMetric(threadIoBytesDesc,
prometheus.CounterValue, float64(thr.ReadBytes),
gname, thr.Name, "read")
gname, threadName, "read")
ch <- prometheus.MustNewConstMetric(threadIoBytesDesc,
prometheus.CounterValue, float64(thr.WriteBytes),
gname, thr.Name, "write")
gname, threadName, "write")
ch <- prometheus.MustNewConstMetric(threadMajorPageFaultsDesc,
prometheus.CounterValue, float64(thr.MajorPageFaults),
gname, thr.Name)
gname, threadName)
ch <- prometheus.MustNewConstMetric(threadMinorPageFaultsDesc,
prometheus.CounterValue, float64(thr.MinorPageFaults),
gname, thr.Name)
gname, threadName)
ch <- prometheus.MustNewConstMetric(threadContextSwitchesDesc,
prometheus.CounterValue, float64(thr.CtxSwitchVoluntary),
gname, thr.Name, "voluntary")
gname, threadName, "voluntary")
ch <- prometheus.MustNewConstMetric(threadContextSwitchesDesc,
prometheus.CounterValue, float64(thr.CtxSwitchNonvoluntary),
gname, thr.Name, "nonvoluntary")
gname, threadName, "nonvoluntary")
}
}
}
Expand All @@ -356,3 +358,20 @@ func (p *NamedProcessCollector) scrape(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(scrapePartialErrorsDesc,
prometheus.CounterValue, float64(p.scrapePartialErrors))
}

func filterNonutf8Characters(s string) string {
if !utf8.ValidString(s) {
v := make([]rune, 0, len(s))
for i, r := range s {
if r == utf8.RuneError {
_, size := utf8.DecodeRuneInString(s[i:])
if size == 1 {
continue
}
}
v = append(v, r)
}
s = string(v)
}
return s
}