Skip to content

Commit

Permalink
fixed: remove non-utf8 characters from thread name
Browse files Browse the repository at this point in the history
  • Loading branch information
张恕征 authored and fcecagno committed Oct 28, 2024
1 parent 7ef0b73 commit d3154a4
Showing 1 changed file with 28 additions and 9 deletions.
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)
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
}

0 comments on commit d3154a4

Please sign in to comment.