Update dependencies

This commit is contained in:
Manuel de Brito Fontes 2017-10-06 17:33:32 -03:00
parent bf5616c65b
commit d6d374b28d
13962 changed files with 48226 additions and 3618880 deletions

View file

@ -0,0 +1,75 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
branch = "master"
name = "github.com/beorn7/perks"
packages = ["quantile"]
revision = "4c0e84591b9aa9e6dcfdf3e020114cd81f89d5f9"
[[projects]]
branch = "master"
name = "github.com/golang/protobuf"
packages = ["proto"]
revision = "17ce1425424ab154092bbb43af630bd647f3bb0d"
[[projects]]
branch = "master"
name = "github.com/kylelemons/godebug"
packages = ["diff","pretty"]
revision = "d65d576e9348f5982d7f6d83682b694e731a45c6"
[[projects]]
name = "github.com/matttproud/golang_protobuf_extensions"
packages = ["pbutil"]
revision = "3247c84500bff8d9fb6d579d800f20b3e091582c"
version = "v1.0.0"
[[projects]]
branch = "master"
name = "github.com/ncabatoff/fakescraper"
packages = ["."]
revision = "15938421d91a82d197de7fc59aebcac65c43407d"
[[projects]]
name = "github.com/prometheus/client_golang"
packages = ["prometheus"]
revision = "c5b7fccd204277076155f10851dad72b76a49317"
version = "v0.8.0"
[[projects]]
branch = "master"
name = "github.com/prometheus/client_model"
packages = ["go"]
revision = "6f3806018612930941127f2a7c6c453ba2c527d2"
[[projects]]
branch = "master"
name = "github.com/prometheus/common"
packages = ["expfmt","internal/bitbucket.org/ww/goautoneg","model"]
revision = "2f17f4a9d485bf34b4bfaccc273805040e4f86c8"
[[projects]]
branch = "master"
name = "github.com/prometheus/procfs"
packages = [".","xfs"]
revision = "e645f4e5aaa8506fc71d6edbc5c4ff02c04c46f2"
[[projects]]
branch = "v1"
name = "gopkg.in/check.v1"
packages = ["."]
revision = "20d25e2804050c1cd24a7eea1e7a6447dd0e74ec"
[[projects]]
branch = "v2"
name = "gopkg.in/yaml.v2"
packages = ["."]
revision = "eb3733d160e74a9c7e442f435eb3bea458e1d19f"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "abd920f891c3e5fe2ee27ce40acbdde66e0799704d160b01f22530df003adfe1"
solver-name = "gps-cdcl"
solver-version = 1

View file

@ -0,0 +1,46 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
[[constraint]]
branch = "master"
name = "github.com/kylelemons/godebug"
[[constraint]]
branch = "master"
name = "github.com/ncabatoff/fakescraper"
[[constraint]]
name = "github.com/prometheus/client_golang"
version = "0.8.0"
[[constraint]]
branch = "master"
name = "github.com/prometheus/procfs"
[[constraint]]
branch = "v1"
name = "gopkg.in/check.v1"
[[constraint]]
branch = "v2"
name = "gopkg.in/yaml.v2"

View file

@ -12,7 +12,7 @@
# limitations under the License.
GO := GO15VENDOREXPERIMENT=1 go
FIRST_GOPATH := $(firstword $(subst :, ,$(GOPATH)))
FIRST_GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH)))
PROMU := $(FIRST_GOPATH)/bin/promu
pkgs = $(shell $(GO) list ./... | grep -v /vendor/)
@ -26,7 +26,7 @@ ifdef DEBUG
endif
all: format build test
all: format vet build test
style:
@echo ">> checking code style"
@ -64,7 +64,7 @@ docker:
promu:
@echo ">> fetching promu"
@GOOS=$(shell uname -s | tr A-Z a-z) \
GOARCH=$(subst x86_64,amd64,$(patsubst i%86,386,$(shell uname -m))) \
GOARCH=$(subst x86_64,amd64,$(patsubst i%86,386,$(patsubst arm%,arm,$(shell uname -m)))) \
$(GO) get -u github.com/prometheus/promu

View file

@ -1 +1 @@
0.0.2
0.1.0

View file

@ -1,13 +0,0 @@
package config
import (
. "gopkg.in/check.v1"
"testing"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})

View file

@ -1,268 +0,0 @@
package config
import (
"bytes"
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"strings"
"text/template"
common "github.com/ncabatoff/process-exporter"
"gopkg.in/yaml.v2"
)
type (
Matcher interface {
// Match returns empty string for no match, or the group name on success.
Match(common.NameAndCmdline) bool
}
FirstMatcher []common.MatchNamer
Config struct {
MatchNamers FirstMatcher
}
commMatcher struct {
comms map[string]struct{}
}
exeMatcher struct {
exes map[string]string
}
cmdlineMatcher struct {
regexes []*regexp.Regexp
captures map[string]string
}
andMatcher []Matcher
templateNamer struct {
template *template.Template
}
matchNamer struct {
andMatcher
templateNamer
}
templateParams struct {
Comm string
ExeBase string
ExeFull string
Matches map[string]string
}
)
func (f FirstMatcher) MatchAndName(nacl common.NameAndCmdline) (bool, string) {
for _, m := range f {
if matched, name := m.MatchAndName(nacl); matched {
return true, name
}
}
return false, ""
}
func (m *matchNamer) MatchAndName(nacl common.NameAndCmdline) (bool, string) {
if !m.Match(nacl) {
return false, ""
}
matches := make(map[string]string)
for _, m := range m.andMatcher {
if mc, ok := m.(*cmdlineMatcher); ok {
for k, v := range mc.captures {
matches[k] = v
}
}
}
exebase, exefull := nacl.Name, nacl.Name
if len(nacl.Cmdline) > 0 {
exefull = nacl.Cmdline[0]
exebase = filepath.Base(exefull)
}
var buf bytes.Buffer
m.template.Execute(&buf, &templateParams{
Comm: nacl.Name,
ExeBase: exebase,
ExeFull: exefull,
Matches: matches,
})
return true, buf.String()
}
func (m *commMatcher) Match(nacl common.NameAndCmdline) bool {
_, found := m.comms[nacl.Name]
return found
}
func (m *exeMatcher) Match(nacl common.NameAndCmdline) bool {
if len(nacl.Cmdline) == 0 {
return false
}
thisbase := filepath.Base(nacl.Cmdline[0])
fqpath, found := m.exes[thisbase]
if !found {
return false
}
if fqpath == "" {
return true
}
return fqpath == nacl.Cmdline[0]
}
func (m *cmdlineMatcher) Match(nacl common.NameAndCmdline) bool {
for _, regex := range m.regexes {
captures := regex.FindStringSubmatch(strings.Join(nacl.Cmdline, " "))
if m.captures == nil {
return false
}
subexpNames := regex.SubexpNames()
if len(subexpNames) != len(captures) {
return false
}
for i, name := range subexpNames {
m.captures[name] = captures[i]
}
}
return true
}
func (m andMatcher) Match(nacl common.NameAndCmdline) bool {
for _, matcher := range m {
if !matcher.Match(nacl) {
return false
}
}
return true
}
// ReadRecipesFile opens the named file and extracts recipes from it.
func ReadFile(cfgpath string) (*Config, error) {
content, err := ioutil.ReadFile(cfgpath)
if err != nil {
return nil, err
}
return GetConfig(string(content))
}
// GetConfig extracts Config from content by parsing it as YAML.
func GetConfig(content string) (*Config, error) {
var yamldata map[string]interface{}
err := yaml.Unmarshal([]byte(content), &yamldata)
if err != nil {
return nil, err
}
yamlProcnames, ok := yamldata["process_names"]
if !ok {
return nil, fmt.Errorf("error parsing YAML config: no top-level 'process_names' key")
}
procnames, ok := yamlProcnames.([]interface{})
if !ok {
return nil, fmt.Errorf("error parsing YAML config: 'process_names' is not a list")
}
var cfg Config
for i, procname := range procnames {
mn, err := getMatchNamer(procname)
if err != nil {
return nil, fmt.Errorf("unable to parse process_name entry %d: %v", i, err)
}
cfg.MatchNamers = append(cfg.MatchNamers, mn)
}
return &cfg, nil
}
func getMatchNamer(yamlmn interface{}) (common.MatchNamer, error) {
nm, ok := yamlmn.(map[interface{}]interface{})
if !ok {
return nil, fmt.Errorf("not a map")
}
var smap = make(map[string][]string)
var nametmpl string
for k, v := range nm {
key, ok := k.(string)
if !ok {
return nil, fmt.Errorf("non-string key %v", k)
}
if key == "name" {
value, ok := v.(string)
if !ok {
return nil, fmt.Errorf("non-string value %v for key %q", v, key)
}
nametmpl = value
} else {
vals, ok := v.([]interface{})
if !ok {
return nil, fmt.Errorf("non-string array value %v for key %q", v, key)
}
var strs []string
for i, si := range vals {
s, ok := si.(string)
if !ok {
return nil, fmt.Errorf("non-string value %v in list[%d] for key %q", v, i, key)
}
strs = append(strs, s)
}
smap[key] = strs
}
}
var matchers andMatcher
if comm, ok := smap["comm"]; ok {
comms := make(map[string]struct{})
for _, c := range comm {
comms[c] = struct{}{}
}
matchers = append(matchers, &commMatcher{comms})
}
if exe, ok := smap["exe"]; ok {
exes := make(map[string]string)
for _, e := range exe {
if strings.Contains(e, "/") {
exes[filepath.Base(e)] = e
} else {
exes[e] = ""
}
}
matchers = append(matchers, &exeMatcher{exes})
}
if cmdline, ok := smap["cmdline"]; ok {
var rs []*regexp.Regexp
for _, c := range cmdline {
r, err := regexp.Compile(c)
if err != nil {
return nil, fmt.Errorf("bad cmdline regex %q: %v", c, err)
}
rs = append(rs, r)
}
matchers = append(matchers, &cmdlineMatcher{
regexes: rs,
captures: make(map[string]string),
})
}
if len(matchers) == 0 {
return nil, fmt.Errorf("no matchers provided")
}
if nametmpl == "" {
nametmpl = "{{.ExeBase}}"
}
tmpl := template.New("cmdname")
tmpl, err := tmpl.Parse(nametmpl)
if err != nil {
return nil, fmt.Errorf("bad name template %q: %v", nametmpl, err)
}
return &matchNamer{matchers, templateNamer{tmpl}}, nil
}

View file

@ -1,77 +0,0 @@
package config
import (
// "github.com/kylelemons/godebug/pretty"
common "github.com/ncabatoff/process-exporter"
. "gopkg.in/check.v1"
)
func (s MySuite) TestConfigBasic(c *C) {
yml := `
process_names:
- exe:
- bash
- exe:
- sh
- exe:
- /bin/ksh
`
cfg, err := GetConfig(yml)
c.Assert(err, IsNil)
c.Check(cfg.MatchNamers, HasLen, 3)
bash := common.NameAndCmdline{"bash", []string{"/bin/bash"}}
sh := common.NameAndCmdline{"sh", []string{"sh"}}
ksh := common.NameAndCmdline{"ksh", []string{"/bin/ksh"}}
found, name := cfg.MatchNamers[0].MatchAndName(bash)
c.Check(found, Equals, true)
c.Check(name, Equals, "bash")
found, name = cfg.MatchNamers[0].MatchAndName(sh)
c.Check(found, Equals, false)
found, name = cfg.MatchNamers[0].MatchAndName(ksh)
c.Check(found, Equals, false)
found, name = cfg.MatchNamers[1].MatchAndName(bash)
c.Check(found, Equals, false)
found, name = cfg.MatchNamers[1].MatchAndName(sh)
c.Check(found, Equals, true)
c.Check(name, Equals, "sh")
found, name = cfg.MatchNamers[1].MatchAndName(ksh)
c.Check(found, Equals, false)
found, name = cfg.MatchNamers[2].MatchAndName(bash)
c.Check(found, Equals, false)
found, name = cfg.MatchNamers[2].MatchAndName(sh)
c.Check(found, Equals, false)
found, name = cfg.MatchNamers[2].MatchAndName(ksh)
c.Check(found, Equals, true)
c.Check(name, Equals, "ksh")
}
func (s MySuite) TestConfigTemplates(c *C) {
yml := `
process_names:
- exe:
- postmaster
cmdline:
- "-D\\s+.+?(?P<Path>[^/]+)(?:$|\\s)"
name: "{{.ExeBase}}:{{.Matches.Path}}"
- exe:
- prometheus
name: "{{.ExeFull}}"
`
cfg, err := GetConfig(yml)
c.Assert(err, IsNil)
c.Check(cfg.MatchNamers, HasLen, 2)
postgres := common.NameAndCmdline{"postmaster", []string{"/usr/bin/postmaster", "-D", "/data/pg"}}
found, name := cfg.MatchNamers[0].MatchAndName(postgres)
c.Check(found, Equals, true)
c.Check(name, Equals, "postmaster:pg")
pm := common.NameAndCmdline{"prometheus", []string{"/usr/local/bin/prometheus"}}
found, name = cfg.MatchNamers[1].MatchAndName(pm)
c.Check(found, Equals, true)
c.Check(name, Equals, "/usr/local/bin/prometheus")
}

View file

@ -22,6 +22,8 @@ type (
Memresident uint64
Memvirtual uint64
OldestStartTime time.Time
OpenFDs uint64
WorstFDratio float64
}
)
@ -84,7 +86,7 @@ func (g *Grouper) Update(iter ProcIter) (int, error) {
// Step 1: track any new proc that should be tracked based on its name and cmdline.
untracked := make(map[ProcId]ProcIdInfo)
for _, idinfo := range newProcs {
wanted, gname := g.namer.MatchAndName(common.NameAndCmdline{idinfo.Name, idinfo.Cmdline})
wanted, gname := g.namer.MatchAndName(common.NameAndCmdline{Name: idinfo.Name, Cmdline: idinfo.Cmdline})
if !wanted {
untracked[idinfo.ProcId] = idinfo
continue
@ -121,14 +123,19 @@ func (g *Grouper) groups() GroupCountMap {
}
cur := gcounts[tinfo.GroupName]
cur.Procs++
_, counts, mem, start := tinfo.GetStats()
cur.Memresident += mem.Resident
cur.Memvirtual += mem.Virtual
cur.Counts.Cpu += counts.Cpu
cur.Counts.ReadBytes += counts.ReadBytes
cur.Counts.WriteBytes += counts.WriteBytes
if cur.OldestStartTime == zeroTime || start.Before(cur.OldestStartTime) {
cur.OldestStartTime = start
tstats := tinfo.GetStats()
cur.Memresident += tstats.Memory.Resident
cur.Memvirtual += tstats.Memory.Virtual
cur.OpenFDs += tstats.Filedesc.Open
openratio := float64(tstats.Filedesc.Open) / float64(tstats.Filedesc.Limit)
if cur.WorstFDratio < openratio {
cur.WorstFDratio = openratio
}
cur.Counts.Cpu += tstats.latest.Cpu
cur.Counts.ReadBytes += tstats.latest.ReadBytes
cur.Counts.WriteBytes += tstats.latest.WriteBytes
if cur.OldestStartTime == zeroTime || tstats.start.Before(cur.OldestStartTime) {
cur.OldestStartTime = tstats.start
}
gcounts[tinfo.GroupName] = cur
}

View file

@ -36,8 +36,8 @@ func (s MySuite) TestGrouperBasic(c *C) {
}
}
gr := NewGrouper(false, newNamer("g1", "g2"))
p1 := newProc(1, "g1", ProcMetrics{1, 2, 3, 4, 5})
p2 := newProc(2, "g2", ProcMetrics{2, 3, 4, 5, 6})
p1 := newProc(1, "g1", ProcMetrics{1, 2, 3, 4, 5, 4, 400})
p2 := newProc(2, "g2", ProcMetrics{2, 3, 4, 5, 6, 40, 400})
p3 := newProc(3, "g3", ProcMetrics{})
_, err := gr.Update(procInfoIter(p1, p2, p3))
@ -45,52 +45,53 @@ func (s MySuite) TestGrouperBasic(c *C) {
got1 := gr.groups()
want1 := GroupCountMap{
"g1": GroupCounts{Counts{0, 0, 0}, 1, 4, 5, time.Time{}},
"g2": GroupCounts{Counts{0, 0, 0}, 1, 5, 6, time.Time{}},
"g1": GroupCounts{Counts{0, 0, 0}, 1, 4, 5, time.Time{}, 4, 0.01},
"g2": GroupCounts{Counts{0, 0, 0}, 1, 5, 6, time.Time{}, 40, 0.1},
}
c.Check(got1, DeepEquals, want1)
c.Check(got1, DeepEquals, want1, Commentf("diff %s", pretty.Compare(got1, want1)))
// Now increment counts and memory and make sure group counts updated.
p1.ProcMetrics = ProcMetrics{2, 3, 4, 5, 6}
p2.ProcMetrics = ProcMetrics{4, 5, 6, 7, 8}
p1.ProcMetrics = ProcMetrics{2, 3, 4, 5, 6, 4, 400}
p2.ProcMetrics = ProcMetrics{4, 5, 6, 7, 8, 40, 400}
_, err = gr.Update(procInfoIter(p1, p2, p3))
c.Assert(err, IsNil)
got2 := gr.groups()
want2 := GroupCountMap{
"g1": GroupCounts{Counts{1, 1, 1}, 1, 5, 6, time.Time{}},
"g2": GroupCounts{Counts{2, 2, 2}, 1, 7, 8, time.Time{}},
"g1": GroupCounts{Counts{1, 1, 1}, 1, 5, 6, time.Time{}, 4, 0.01},
"g2": GroupCounts{Counts{2, 2, 2}, 1, 7, 8, time.Time{}, 40, 0.1},
}
c.Check(got2, DeepEquals, want2)
c.Check(got2, DeepEquals, want2, Commentf("diff %s", pretty.Compare(got2, want2)))
// Now add a new proc and update p2's metrics. The
// counts for p4 won't be factored into the total yet
// because we only add to counts starting with the
// second time we see a proc. Memory is affected though.
p4 := newProc(4, "g2", ProcMetrics{1, 1, 1, 1, 1})
p2.ProcMetrics = ProcMetrics{5, 6, 7, 8, 9}
// second time we see a proc. Memory and FDs are affected
// though.
p4 := newProc(4, "g2", ProcMetrics{1, 1, 1, 1, 1, 80, 400})
p2.ProcMetrics = ProcMetrics{5, 6, 7, 8, 9, 40, 400}
_, err = gr.Update(procInfoIter(p1, p2, p3, p4))
c.Assert(err, IsNil)
got3 := gr.groups()
want3 := GroupCountMap{
"g1": GroupCounts{Counts{0, 0, 0}, 1, 5, 6, time.Time{}},
"g2": GroupCounts{Counts{1, 1, 1}, 2, 9, 10, time.Time{}},
"g1": GroupCounts{Counts{0, 0, 0}, 1, 5, 6, time.Time{}, 4, 0.01},
"g2": GroupCounts{Counts{1, 1, 1}, 2, 9, 10, time.Time{}, 120, 0.2},
}
c.Check(got3, DeepEquals, want3, Commentf("diff %s", pretty.Compare(got3, want3)))
p4.ProcMetrics = ProcMetrics{2, 2, 2, 2, 2}
p2.ProcMetrics = ProcMetrics{6, 7, 8, 8, 9}
p4.ProcMetrics = ProcMetrics{2, 2, 2, 2, 2, 100, 400}
p2.ProcMetrics = ProcMetrics{6, 7, 8, 8, 9, 40, 400}
_, err = gr.Update(procInfoIter(p1, p2, p3, p4))
c.Assert(err, IsNil)
got4 := gr.groups()
want4 := GroupCountMap{
"g1": GroupCounts{Counts{0, 0, 0}, 1, 5, 6, time.Time{}},
"g2": GroupCounts{Counts{2, 2, 2}, 2, 10, 11, time.Time{}},
"g1": GroupCounts{Counts{0, 0, 0}, 1, 5, 6, time.Time{}, 4, 0.01},
"g2": GroupCounts{Counts{2, 2, 2}, 2, 10, 11, time.Time{}, 140, 0.25},
}
c.Check(got4, DeepEquals, want4, Commentf("diff %s", pretty.Compare(got4, want4)))
@ -118,8 +119,8 @@ func (s MySuite) TestGrouperParents(c *C) {
got1 := gr.groups()
want1 := GroupCountMap{
"g1": GroupCounts{Counts{}, 1, 0, 0, time.Time{}},
"g2": GroupCounts{Counts{}, 1, 0, 0, time.Time{}},
"g1": GroupCounts{Counts{}, 1, 0, 0, time.Time{}, 0, 0},
"g2": GroupCounts{Counts{}, 1, 0, 0, time.Time{}, 0, 0},
}
c.Check(got1, DeepEquals, want1, Commentf("diff %s", pretty.Compare(got1, want1)))
@ -135,8 +136,8 @@ func (s MySuite) TestGrouperParents(c *C) {
got2 := gr.groups()
want2 := GroupCountMap{
"g1": GroupCounts{Counts{}, 2, 0, 0, time.Time{}},
"g2": GroupCounts{Counts{}, 2, 0, 0, time.Time{}},
"g1": GroupCounts{Counts{}, 2, 0, 0, time.Time{}, 0, 0},
"g2": GroupCounts{Counts{}, 2, 0, 0, time.Time{}, 0, 0},
}
c.Check(got2, DeepEquals, want2, Commentf("diff %s", pretty.Compare(got2, want2)))
@ -151,8 +152,8 @@ func (s MySuite) TestGrouperParents(c *C) {
got3 := gr.groups()
want3 := GroupCountMap{
"g1": GroupCounts{Counts{}, 1, 0, 0, time.Time{}},
"g2": GroupCounts{Counts{}, 5, 0, 0, time.Time{}},
"g1": GroupCounts{Counts{}, 1, 0, 0, time.Time{}, 0, 0},
"g2": GroupCounts{Counts{}, 5, 0, 0, time.Time{}, 0, 0},
}
c.Check(got3, DeepEquals, want3, Commentf("diff %s", pretty.Compare(got3, want3)))
}
@ -171,23 +172,23 @@ func (s MySuite) TestGrouperGroup(c *C) {
gr := NewGrouper(false, newNamer("g1"))
// First call should return zero CPU/IO.
p1 := newProc(1, "g1", ProcMetrics{1, 2, 3, 4, 5})
p1 := newProc(1, "g1", ProcMetrics{1, 2, 3, 4, 5, 8, 400})
_, err := gr.Update(procInfoIter(p1))
c.Assert(err, IsNil)
got1 := gr.Groups()
want1 := GroupCountMap{
"g1": GroupCounts{Counts{0, 0, 0}, 1, 4, 5, time.Time{}},
"g1": GroupCounts{Counts{0, 0, 0}, 1, 4, 5, time.Time{}, 8, 0.02},
}
c.Check(got1, DeepEquals, want1)
// Second call should return the delta CPU/IO from first observance,
// as well as latest memory/proccount.
p1.ProcMetrics = ProcMetrics{2, 3, 4, 5, 6}
p1.ProcMetrics = ProcMetrics{2, 3, 4, 5, 6, 12, 400}
_, err = gr.Update(procInfoIter(p1))
c.Assert(err, IsNil)
got2 := gr.Groups()
want2 := GroupCountMap{
"g1": GroupCounts{Counts{1, 1, 1}, 1, 5, 6, time.Time{}},
"g1": GroupCounts{Counts{1, 1, 1}, 1, 5, 6, time.Time{}, 12, 0.03},
}
c.Check(got2, DeepEquals, want2)
@ -196,7 +197,7 @@ func (s MySuite) TestGrouperGroup(c *C) {
c.Assert(err, IsNil)
got3 := gr.Groups()
want3 := GroupCountMap{
"g1": GroupCounts{Counts{1, 1, 1}, 1, 5, 6, time.Time{}},
"g1": GroupCounts{Counts{1, 1, 1}, 1, 5, 6, time.Time{}, 12, 0.03},
}
c.Check(got3, DeepEquals, want3, Commentf("diff %s", pretty.Compare(got3, want3)))
}
@ -213,54 +214,54 @@ func (s MySuite) TestGrouperNonDecreasing(c *C) {
}
}
gr := NewGrouper(false, newNamer("g1", "g2"))
p1 := newProc(1, "g1", ProcMetrics{1, 2, 3, 4, 5})
p2 := newProc(2, "g2", ProcMetrics{2, 3, 4, 5, 6})
p1 := newProc(1, "g1", ProcMetrics{1, 2, 3, 4, 5, 4, 400})
p2 := newProc(2, "g2", ProcMetrics{2, 3, 4, 5, 6, 40, 400})
_, err := gr.Update(procInfoIter(p1, p2))
c.Assert(err, IsNil)
got1 := gr.Groups()
want1 := GroupCountMap{
"g1": GroupCounts{Counts{0, 0, 0}, 1, 4, 5, time.Time{}},
"g2": GroupCounts{Counts{0, 0, 0}, 1, 5, 6, time.Time{}},
"g1": GroupCounts{Counts{0, 0, 0}, 1, 4, 5, time.Time{}, 4, 0.01},
"g2": GroupCounts{Counts{0, 0, 0}, 1, 5, 6, time.Time{}, 40, 0.1},
}
c.Check(got1, DeepEquals, want1)
c.Check(got1, DeepEquals, want1, Commentf("diff %s", pretty.Compare(got1, want1)))
// Now add a new proc p3 to g2, and increment p1/p2's metrics.
p1.ProcMetrics = ProcMetrics{2, 3, 4, 5, 6}
p2.ProcMetrics = ProcMetrics{4, 5, 6, 7, 8}
p3 := newProc(3, "g2", ProcMetrics{1, 1, 1, 1, 1})
p1.ProcMetrics = ProcMetrics{2, 3, 4, 5, 6, 8, 400}
p2.ProcMetrics = ProcMetrics{4, 5, 6, 7, 8, 80, 400}
p3 := newProc(3, "g2", ProcMetrics{1, 1, 1, 1, 1, 8, 400})
_, err = gr.Update(procInfoIter(p1, p2, p3))
c.Assert(err, IsNil)
got2 := gr.Groups()
want2 := GroupCountMap{
"g1": GroupCounts{Counts{1, 1, 1}, 1, 5, 6, time.Time{}},
"g2": GroupCounts{Counts{2, 2, 2}, 2, 8, 9, time.Time{}},
"g1": GroupCounts{Counts{1, 1, 1}, 1, 5, 6, time.Time{}, 8, 0.02},
"g2": GroupCounts{Counts{2, 2, 2}, 2, 8, 9, time.Time{}, 88, 0.2},
}
c.Check(got2, DeepEquals, want2)
c.Check(got2, DeepEquals, want2, Commentf("diff %s", pretty.Compare(got2, want2)))
// Now update p3's metrics and kill p2.
p3.ProcMetrics = ProcMetrics{2, 3, 4, 5, 6}
p3.ProcMetrics = ProcMetrics{2, 3, 4, 5, 6, 8, 400}
_, err = gr.Update(procInfoIter(p1, p3))
c.Assert(err, IsNil)
got3 := gr.Groups()
want3 := GroupCountMap{
"g1": GroupCounts{Counts{1, 1, 1}, 1, 5, 6, time.Time{}},
"g2": GroupCounts{Counts{3, 4, 5}, 1, 5, 6, time.Time{}},
"g1": GroupCounts{Counts{1, 1, 1}, 1, 5, 6, time.Time{}, 8, 0.02},
"g2": GroupCounts{Counts{3, 4, 5}, 1, 5, 6, time.Time{}, 8, 0.02},
}
c.Check(got3, DeepEquals, want3, Commentf("diff %s", pretty.Compare(got3, want3)))
// Now update p3's metrics and kill p1.
p3.ProcMetrics = ProcMetrics{4, 4, 4, 2, 1}
p3.ProcMetrics = ProcMetrics{4, 4, 4, 2, 1, 4, 400}
_, err = gr.Update(procInfoIter(p3))
c.Assert(err, IsNil)
got4 := gr.Groups()
want4 := GroupCountMap{
"g1": GroupCounts{Counts{1, 1, 1}, 0, 0, 0, time.Time{}},
"g2": GroupCounts{Counts{5, 5, 5}, 1, 2, 1, time.Time{}},
"g1": GroupCounts{Counts{1, 1, 1}, 0, 0, 0, time.Time{}, 0, 0},
"g2": GroupCounts{Counts{5, 5, 5}, 1, 2, 1, time.Time{}, 4, 0.01},
}
c.Check(got4, DeepEquals, want4, Commentf("diff %s\n%s", pretty.Compare(got4, want4), pretty.Sprint(gr)))

View file

@ -2,8 +2,9 @@ package proc
import (
"fmt"
"github.com/prometheus/procfs"
"time"
"github.com/prometheus/procfs"
)
func newProcIdStatic(pid, ppid int, startTime uint64, name string, cmdline []string) ProcIdStatic {
@ -35,6 +36,8 @@ type (
WriteBytes uint64
ResidentBytes uint64
VirtualBytes uint64
OpenFDs uint64
MaxFDs uint64
}
ProcIdStatic struct {
@ -79,7 +82,7 @@ type (
stat *procfs.ProcStat
cmdline []string
io *procfs.ProcIO
bootTime int64
bootTime uint64
}
procs interface {
@ -89,7 +92,7 @@ type (
procfsprocs struct {
Procs []procfs.Proc
bootTime int64
bootTime uint64
}
// ProcIter is an iterator over a sequence of procs.
@ -230,7 +233,7 @@ func (p proc) GetStatic() (ProcStatic, error) {
if err != nil {
return ProcStatic{}, err
}
startTime := time.Unix(p.bootTime, 0)
startTime := time.Unix(int64(p.bootTime), 0)
startTime = startTime.Add(time.Second / userHZ * time.Duration(stat.Starttime))
return ProcStatic{
Name: stat.Comm,
@ -249,18 +252,28 @@ func (p proc) GetMetrics() (ProcMetrics, error) {
if err != nil {
return ProcMetrics{}, err
}
numfds, err := p.Proc.FileDescriptorsLen()
if err != nil {
return ProcMetrics{}, err
}
limits, err := p.NewLimits()
if err != nil {
return ProcMetrics{}, err
}
return ProcMetrics{
CpuTime: stat.CPUTime(),
ReadBytes: io.ReadBytes,
WriteBytes: io.WriteBytes,
ResidentBytes: uint64(stat.ResidentMemory()),
VirtualBytes: uint64(stat.VirtualMemory()),
OpenFDs: uint64(numfds),
MaxFDs: uint64(limits.OpenFiles),
}, nil
}
type FS struct {
procfs.FS
BootTime int64
BootTime uint64
}
// See https://github.com/prometheus/procfs/blob/master/proc_stat.go for details on userHZ.

View file

@ -18,6 +18,11 @@ type (
Virtual uint64
}
Filedesc struct {
Open uint64
Limit uint64
}
// Tracker tracks processes and records metrics.
Tracker struct {
// Tracked holds the processes are being monitored. Processes
@ -43,6 +48,13 @@ type (
// GroupName is an optional tag for this proc.
GroupName string
}
trackedStats struct {
aggregate, latest Counts
Memory
Filedesc
start time.Time
}
)
func (tp *TrackedProc) GetName() string {
@ -53,8 +65,16 @@ func (tp *TrackedProc) GetCmdLine() []string {
return tp.info.Cmdline
}
func (tp *TrackedProc) GetStats() (aggregate, latest Counts, mem Memory, start time.Time) {
return tp.accum, tp.lastaccum, Memory{Resident: tp.info.ResidentBytes, Virtual: tp.info.VirtualBytes}, tp.info.StartTime
func (tp *TrackedProc) GetStats() trackedStats {
mem := Memory{Resident: tp.info.ResidentBytes, Virtual: tp.info.VirtualBytes}
fd := Filedesc{Open: tp.info.OpenFDs, Limit: tp.info.MaxFDs}
return trackedStats{
aggregate: tp.accum,
latest: tp.lastaccum,
Memory: mem,
Filedesc: fd,
start: tp.info.StartTime,
}
}
func NewTracker() *Tracker {

View file

@ -60,7 +60,7 @@ func (s MySuite) TestTrackerCounts(c *C) {
tr := NewTracker()
// Test that p1 is seen as new
p1 := newProc(1, 1, "p1", ProcMetrics{1, 2, 3, 4, 5})
p1 := newProc(1, 1, "p1", ProcMetrics{1, 2, 3, 4, 5, 6, 4096})
want1 := []ProcIdInfo{p1}
got1, _, err := tr.Update(procInfoIter(p1))
c.Assert(err, IsNil)
@ -73,7 +73,7 @@ func (s MySuite) TestTrackerCounts(c *C) {
c.Check(got2, DeepEquals, []ProcIdInfo(nil))
// Now update p1's metrics
p1.ProcMetrics = ProcMetrics{2, 3, 4, 5, 6}
p1.ProcMetrics = ProcMetrics{2, 3, 4, 5, 6, 7, 4096}
got3, _, err := tr.Update(procInfoIter(p1))
c.Assert(err, IsNil)
c.Check(got3, DeepEquals, []ProcIdInfo(nil))
@ -83,7 +83,7 @@ func (s MySuite) TestTrackerCounts(c *C) {
c.Check(tr.Tracked[p1.ProcId].info, DeepEquals, ProcInfo{p1.ProcStatic, p1.ProcMetrics})
// Now update p1's metrics again
p1.ProcMetrics = ProcMetrics{4, 6, 8, 9, 10}
p1.ProcMetrics = ProcMetrics{4, 6, 8, 9, 10, 11, 4096}
got4, _, err := tr.Update(procInfoIter(p1))
c.Assert(err, IsNil)
c.Check(got4, DeepEquals, []ProcIdInfo(nil))