2014-12-11 02:01:46 +03:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import "github.com/prometheus/procfs"
|
|
|
|
|
|
|
|
type processCollector struct {
|
|
|
|
pid int
|
|
|
|
collectFn func(chan<- Metric)
|
2015-02-02 22:53:06 +03:00
|
|
|
pidFn func() (int, error)
|
2014-12-11 02:01:46 +03:00
|
|
|
cpuTotal Counter
|
|
|
|
openFDs, maxFDs Gauge
|
|
|
|
vsize, rss Gauge
|
|
|
|
startTime Gauge
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewProcessCollector returns a collector which exports the current state of
|
|
|
|
// process metrics including cpu, memory and file descriptor usage as well as
|
|
|
|
// the process start time for the given process id under the given namespace.
|
|
|
|
func NewProcessCollector(pid int, namespace string) *processCollector {
|
2015-02-02 22:53:06 +03:00
|
|
|
return NewProcessCollectorPIDFn(
|
|
|
|
func() (int, error) { return pid, nil },
|
|
|
|
namespace,
|
|
|
|
)
|
2014-12-11 02:01:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewProcessCollectorPIDFn returns a collector which exports the current state
|
|
|
|
// of process metrics including cpu, memory and file descriptor usage as well
|
|
|
|
// as the process start time under the given namespace. The given pidFn is
|
|
|
|
// called on each collect and is used to determine the process to export
|
|
|
|
// metrics for.
|
|
|
|
func NewProcessCollectorPIDFn(
|
2015-02-02 22:53:06 +03:00
|
|
|
pidFn func() (int, error),
|
2014-12-11 02:01:46 +03:00
|
|
|
namespace string,
|
|
|
|
) *processCollector {
|
|
|
|
c := processCollector{
|
|
|
|
pidFn: pidFn,
|
|
|
|
collectFn: noopCollect,
|
|
|
|
|
|
|
|
cpuTotal: NewCounter(CounterOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Name: "process_cpu_seconds_total",
|
|
|
|
Help: "Total user and system CPU time spent in seconds.",
|
|
|
|
}),
|
|
|
|
openFDs: NewGauge(GaugeOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Name: "process_open_fds",
|
|
|
|
Help: "Number of open file descriptors.",
|
|
|
|
}),
|
|
|
|
maxFDs: NewGauge(GaugeOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Name: "process_max_fds",
|
|
|
|
Help: "Maximum number of open file descriptors.",
|
|
|
|
}),
|
|
|
|
vsize: NewGauge(GaugeOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Name: "process_virtual_memory_bytes",
|
|
|
|
Help: "Virtual memory size in bytes.",
|
|
|
|
}),
|
|
|
|
rss: NewGauge(GaugeOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Name: "process_resident_memory_bytes",
|
|
|
|
Help: "Resident memory size in bytes.",
|
|
|
|
}),
|
|
|
|
startTime: NewGauge(GaugeOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Name: "process_start_time_seconds",
|
|
|
|
Help: "Start time of the process since unix epoch in seconds.",
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use procfs to export metrics if available.
|
|
|
|
if _, err := procfs.NewStat(); err == nil {
|
|
|
|
c.collectFn = c.procfsCollect
|
|
|
|
}
|
|
|
|
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Describe returns all descriptions of the collector.
|
|
|
|
func (c *processCollector) Describe(ch chan<- *Desc) {
|
|
|
|
ch <- c.cpuTotal.Desc()
|
|
|
|
ch <- c.openFDs.Desc()
|
|
|
|
ch <- c.maxFDs.Desc()
|
|
|
|
ch <- c.vsize.Desc()
|
|
|
|
ch <- c.rss.Desc()
|
|
|
|
ch <- c.startTime.Desc()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect returns the current state of all metrics of the collector.
|
|
|
|
func (c *processCollector) Collect(ch chan<- Metric) {
|
|
|
|
c.collectFn(ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func noopCollect(ch chan<- Metric) {}
|
|
|
|
|
|
|
|
func (c *processCollector) procfsCollect(ch chan<- Metric) {
|
2015-02-02 22:53:06 +03:00
|
|
|
pid, err := c.pidFn()
|
2014-12-11 02:01:46 +03:00
|
|
|
if err != nil {
|
2015-02-02 22:53:06 +03:00
|
|
|
c.reportCollectErrors(ch, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := procfs.NewProc(pid)
|
|
|
|
if err != nil {
|
|
|
|
c.reportCollectErrors(ch, err)
|
2014-12-11 02:01:46 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
Allow error reporting during metrics collection and simplify Register().
Both are interface changes I want to get in before public
announcement. They only break rare usage cases, and are always easy to
fix, but still we want to avoid breaking changes after a wider
announcement of the project.
The change of Register() simply removes the return of the Collector,
which nobody was using in practice. It was just bloating the call
syntax. Note that this is different from RegisterOrGet(), which is
used at various occasions where you want to register something that
might or might not be registered already, but if it is, you want the
previously registered Collector back (because that's the relevant
one).
WRT error reporting: I first tried the obvious way of letting the
Collector methods Describe() and Collect() return error. However, I
had to conclude that that bloated _many_ calls and their handling in
very obnoxious ways. On the other hand, the case where you actually
want to report errors during registration or collection is very
rare. Hence, this approach has the wrong trade-off. The approach taken
here might at first appear clunky but is in practice quite handy,
mostly because there is almost no change for the "normal" case of "no
special error handling", but also because it plays well with the way
descriptors and metrics are handled (via channels).
Explaining the approach in more detail:
- During registration / describe: Error handling was actually already
in place (for invalid descriptors, which carry an error anyway). I
only added a convenience function to create an invalid descriptor
with a given error on purpose.
- Metrics are now treated in a similar way. The Write method returns
an error now (the only change in interface). An "invalid metric" is
provided that can be sent via the channel to signal that that metric
could not be collected. It alse transports an error.
NON-GOALS OF THIS COMMIT:
This is NOT yet the major improvement of the whole registry part,
where we want a public Registry interface and plenty of modular
configurations (for error handling, various auto-metrics, http
instrumentation, testing, ...). However, we can do that whole thing
without breaking existing interfaces. For now (which is a significant
issue) any error during collection will either cause a 500 HTTP
response or a panic (depending on registry config). Later, we
definitely want to have a possibility to skip (and only report
somehow) non-collectible metrics instead of aborting the whole scrape.
2015-01-12 21:16:09 +03:00
|
|
|
if stat, err := p.NewStat(); err != nil {
|
|
|
|
// Report collect errors for metrics depending on stat.
|
|
|
|
ch <- NewInvalidMetric(c.vsize.Desc(), err)
|
|
|
|
ch <- NewInvalidMetric(c.rss.Desc(), err)
|
|
|
|
ch <- NewInvalidMetric(c.startTime.Desc(), err)
|
|
|
|
ch <- NewInvalidMetric(c.cpuTotal.Desc(), err)
|
|
|
|
} else {
|
2014-12-11 02:01:46 +03:00
|
|
|
c.cpuTotal.Set(stat.CPUTime())
|
|
|
|
ch <- c.cpuTotal
|
|
|
|
c.vsize.Set(float64(stat.VirtualMemory()))
|
|
|
|
ch <- c.vsize
|
|
|
|
c.rss.Set(float64(stat.ResidentMemory()))
|
|
|
|
ch <- c.rss
|
|
|
|
|
Allow error reporting during metrics collection and simplify Register().
Both are interface changes I want to get in before public
announcement. They only break rare usage cases, and are always easy to
fix, but still we want to avoid breaking changes after a wider
announcement of the project.
The change of Register() simply removes the return of the Collector,
which nobody was using in practice. It was just bloating the call
syntax. Note that this is different from RegisterOrGet(), which is
used at various occasions where you want to register something that
might or might not be registered already, but if it is, you want the
previously registered Collector back (because that's the relevant
one).
WRT error reporting: I first tried the obvious way of letting the
Collector methods Describe() and Collect() return error. However, I
had to conclude that that bloated _many_ calls and their handling in
very obnoxious ways. On the other hand, the case where you actually
want to report errors during registration or collection is very
rare. Hence, this approach has the wrong trade-off. The approach taken
here might at first appear clunky but is in practice quite handy,
mostly because there is almost no change for the "normal" case of "no
special error handling", but also because it plays well with the way
descriptors and metrics are handled (via channels).
Explaining the approach in more detail:
- During registration / describe: Error handling was actually already
in place (for invalid descriptors, which carry an error anyway). I
only added a convenience function to create an invalid descriptor
with a given error on purpose.
- Metrics are now treated in a similar way. The Write method returns
an error now (the only change in interface). An "invalid metric" is
provided that can be sent via the channel to signal that that metric
could not be collected. It alse transports an error.
NON-GOALS OF THIS COMMIT:
This is NOT yet the major improvement of the whole registry part,
where we want a public Registry interface and plenty of modular
configurations (for error handling, various auto-metrics, http
instrumentation, testing, ...). However, we can do that whole thing
without breaking existing interfaces. For now (which is a significant
issue) any error during collection will either cause a 500 HTTP
response or a panic (depending on registry config). Later, we
definitely want to have a possibility to skip (and only report
somehow) non-collectible metrics instead of aborting the whole scrape.
2015-01-12 21:16:09 +03:00
|
|
|
if startTime, err := stat.StartTime(); err != nil {
|
|
|
|
ch <- NewInvalidMetric(c.startTime.Desc(), err)
|
|
|
|
} else {
|
2014-12-11 02:01:46 +03:00
|
|
|
c.startTime.Set(startTime)
|
|
|
|
ch <- c.startTime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Allow error reporting during metrics collection and simplify Register().
Both are interface changes I want to get in before public
announcement. They only break rare usage cases, and are always easy to
fix, but still we want to avoid breaking changes after a wider
announcement of the project.
The change of Register() simply removes the return of the Collector,
which nobody was using in practice. It was just bloating the call
syntax. Note that this is different from RegisterOrGet(), which is
used at various occasions where you want to register something that
might or might not be registered already, but if it is, you want the
previously registered Collector back (because that's the relevant
one).
WRT error reporting: I first tried the obvious way of letting the
Collector methods Describe() and Collect() return error. However, I
had to conclude that that bloated _many_ calls and their handling in
very obnoxious ways. On the other hand, the case where you actually
want to report errors during registration or collection is very
rare. Hence, this approach has the wrong trade-off. The approach taken
here might at first appear clunky but is in practice quite handy,
mostly because there is almost no change for the "normal" case of "no
special error handling", but also because it plays well with the way
descriptors and metrics are handled (via channels).
Explaining the approach in more detail:
- During registration / describe: Error handling was actually already
in place (for invalid descriptors, which carry an error anyway). I
only added a convenience function to create an invalid descriptor
with a given error on purpose.
- Metrics are now treated in a similar way. The Write method returns
an error now (the only change in interface). An "invalid metric" is
provided that can be sent via the channel to signal that that metric
could not be collected. It alse transports an error.
NON-GOALS OF THIS COMMIT:
This is NOT yet the major improvement of the whole registry part,
where we want a public Registry interface and plenty of modular
configurations (for error handling, various auto-metrics, http
instrumentation, testing, ...). However, we can do that whole thing
without breaking existing interfaces. For now (which is a significant
issue) any error during collection will either cause a 500 HTTP
response or a panic (depending on registry config). Later, we
definitely want to have a possibility to skip (and only report
somehow) non-collectible metrics instead of aborting the whole scrape.
2015-01-12 21:16:09 +03:00
|
|
|
if fds, err := p.FileDescriptorsLen(); err != nil {
|
|
|
|
ch <- NewInvalidMetric(c.openFDs.Desc(), err)
|
|
|
|
} else {
|
2014-12-11 02:01:46 +03:00
|
|
|
c.openFDs.Set(float64(fds))
|
|
|
|
ch <- c.openFDs
|
|
|
|
}
|
|
|
|
|
Allow error reporting during metrics collection and simplify Register().
Both are interface changes I want to get in before public
announcement. They only break rare usage cases, and are always easy to
fix, but still we want to avoid breaking changes after a wider
announcement of the project.
The change of Register() simply removes the return of the Collector,
which nobody was using in practice. It was just bloating the call
syntax. Note that this is different from RegisterOrGet(), which is
used at various occasions where you want to register something that
might or might not be registered already, but if it is, you want the
previously registered Collector back (because that's the relevant
one).
WRT error reporting: I first tried the obvious way of letting the
Collector methods Describe() and Collect() return error. However, I
had to conclude that that bloated _many_ calls and their handling in
very obnoxious ways. On the other hand, the case where you actually
want to report errors during registration or collection is very
rare. Hence, this approach has the wrong trade-off. The approach taken
here might at first appear clunky but is in practice quite handy,
mostly because there is almost no change for the "normal" case of "no
special error handling", but also because it plays well with the way
descriptors and metrics are handled (via channels).
Explaining the approach in more detail:
- During registration / describe: Error handling was actually already
in place (for invalid descriptors, which carry an error anyway). I
only added a convenience function to create an invalid descriptor
with a given error on purpose.
- Metrics are now treated in a similar way. The Write method returns
an error now (the only change in interface). An "invalid metric" is
provided that can be sent via the channel to signal that that metric
could not be collected. It alse transports an error.
NON-GOALS OF THIS COMMIT:
This is NOT yet the major improvement of the whole registry part,
where we want a public Registry interface and plenty of modular
configurations (for error handling, various auto-metrics, http
instrumentation, testing, ...). However, we can do that whole thing
without breaking existing interfaces. For now (which is a significant
issue) any error during collection will either cause a 500 HTTP
response or a panic (depending on registry config). Later, we
definitely want to have a possibility to skip (and only report
somehow) non-collectible metrics instead of aborting the whole scrape.
2015-01-12 21:16:09 +03:00
|
|
|
if limits, err := p.NewLimits(); err != nil {
|
|
|
|
ch <- NewInvalidMetric(c.maxFDs.Desc(), err)
|
|
|
|
} else {
|
2014-12-11 02:01:46 +03:00
|
|
|
c.maxFDs.Set(float64(limits.OpenFiles))
|
|
|
|
ch <- c.maxFDs
|
|
|
|
}
|
|
|
|
}
|
2015-02-02 22:53:06 +03:00
|
|
|
|
|
|
|
func (c *processCollector) reportCollectErrors(ch chan<- Metric, err error) {
|
|
|
|
ch <- NewInvalidMetric(c.cpuTotal.Desc(), err)
|
|
|
|
ch <- NewInvalidMetric(c.openFDs.Desc(), err)
|
|
|
|
ch <- NewInvalidMetric(c.maxFDs.Desc(), err)
|
|
|
|
ch <- NewInvalidMetric(c.vsize.Desc(), err)
|
|
|
|
ch <- NewInvalidMetric(c.rss.Desc(), err)
|
|
|
|
ch <- NewInvalidMetric(c.startTime.Desc(), err)
|
|
|
|
}
|