Merge pull request #130 from lsy1990/jstat-730

support jvm
This commit is contained in:
ulricqin 2022-08-02 10:27:43 +08:00 committed by GitHub
commit 45ff0476cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 1 deletions

View File

@ -26,6 +26,7 @@ gather_total = true
# will append pid as tag
gather_per_pid = false
# gather jvm metrics only when jstat is ready
# gather_more_metrics = [
# "threads",
# "fd",
@ -33,5 +34,6 @@ gather_per_pid = false
# "uptime",
# "cpu",
# "mem",
# "limit"
# "limit",
# "jvm"
# ]

View File

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"os/exec"
"runtime"
"strings"
"time"
@ -130,6 +131,8 @@ func (ins *Instance) Gather(slist *types.SampleList) {
ins.gatherMem(slist, ins.procs, tags)
case "limit":
ins.gatherLimit(slist, ins.procs, tags)
case "jvm":
ins.gatherJvm(slist, ins.procs, tags)
default:
log.Println("unknown choice in gather_more_metrics:", field)
}
@ -327,6 +330,35 @@ func (ins *Instance) gatherLimit(slist *types.SampleList, procs map[PID]Process,
}
}
func (ins *Instance) gatherJvm(slist *types.SampleList, procs map[PID]Process, tags map[string]string) {
for pid := range procs {
jvmStat, err := execJstat(pid)
if err == nil {
for k, v := range jvmStat {
slist.PushFront(types.NewSample(inputName, "jvm_"+k, v, map[string]string{"pid": fmt.Sprint(pid)}, tags))
}
}
}
}
func execJstat(pid PID) (map[string]string, error) {
bin, err := exec.LookPath("jstat")
if err != nil {
return nil, err
}
out, err := exec.Command(bin, "-gc", fmt.Sprint(pid)).Output()
if err != nil {
return nil, err
}
jvmMetrics := make(map[string]string)
jvm:=strings.Fields(string(out))
half := len(jvm) / 2
for i := 0; i < half; i++ {
jvmMetrics[jvm[i]] = jvm[i+half]
}
return jvmMetrics, err
}
func (ins *Instance) winServicePIDs() ([]PID, error) {
var pids []PID