Commit 066d61ce authored by David Ashpole's avatar David Ashpole

update cadvisor godeps

parent 3837d951
...@@ -281,6 +281,7 @@ filegroup( ...@@ -281,6 +281,7 @@ filegroup(
"//vendor/github.com/prometheus/client_golang/prometheus:all-srcs", "//vendor/github.com/prometheus/client_golang/prometheus:all-srcs",
"//vendor/github.com/prometheus/client_model/go:all-srcs", "//vendor/github.com/prometheus/client_model/go:all-srcs",
"//vendor/github.com/prometheus/common/expfmt:all-srcs", "//vendor/github.com/prometheus/common/expfmt:all-srcs",
"//vendor/github.com/prometheus/common/internal/bitbucket.org/ww/goautoneg:all-srcs",
"//vendor/github.com/prometheus/common/model:all-srcs", "//vendor/github.com/prometheus/common/model:all-srcs",
"//vendor/github.com/prometheus/procfs:all-srcs", "//vendor/github.com/prometheus/procfs:all-srcs",
"//vendor/github.com/quobyte/api:all-srcs", "//vendor/github.com/quobyte/api:all-srcs",
...@@ -348,7 +349,6 @@ filegroup( ...@@ -348,7 +349,6 @@ filegroup(
"//vendor/golang.org/x/time/rate:all-srcs", "//vendor/golang.org/x/time/rate:all-srcs",
"//vendor/golang.org/x/tools/container/intsets:all-srcs", "//vendor/golang.org/x/tools/container/intsets:all-srcs",
"//vendor/google.golang.org/api/cloudmonitoring/v2beta2:all-srcs", "//vendor/google.golang.org/api/cloudmonitoring/v2beta2:all-srcs",
"//vendor/google.golang.org/api/compute/v0.alpha:all-srcs",
"//vendor/google.golang.org/api/compute/v0.beta:all-srcs", "//vendor/google.golang.org/api/compute/v0.beta:all-srcs",
"//vendor/google.golang.org/api/compute/v1:all-srcs", "//vendor/google.golang.org/api/compute/v1:all-srcs",
"//vendor/google.golang.org/api/container/v1:all-srcs", "//vendor/google.golang.org/api/container/v1:all-srcs",
......
...@@ -223,3 +223,71 @@ func ListContainers(name string, cgroupPaths map[string]string, listType contain ...@@ -223,3 +223,71 @@ func ListContainers(name string, cgroupPaths map[string]string, listType contain
return ret, nil return ret, nil
} }
// AssignDeviceNamesToDiskStats assigns the Device field on the provided DiskIoStats by looking up
// the device major and minor identifiers in the provided device namer.
func AssignDeviceNamesToDiskStats(namer DeviceNamer, stats *info.DiskIoStats) {
assignDeviceNamesToPerDiskStats(
namer,
stats.IoMerged,
stats.IoQueued,
stats.IoServiceBytes,
stats.IoServiceTime,
stats.IoServiced,
stats.IoTime,
stats.IoWaitTime,
stats.Sectors,
)
}
// assignDeviceNamesToPerDiskStats looks up device names for the provided stats, caching names
// if necessary.
func assignDeviceNamesToPerDiskStats(namer DeviceNamer, diskStats ...[]info.PerDiskStats) {
devices := make(deviceIdentifierMap)
for _, stats := range diskStats {
for i, stat := range stats {
stats[i].Device = devices.Find(stat.Major, stat.Minor, namer)
}
}
}
// DeviceNamer returns string names for devices by their major and minor id.
type DeviceNamer interface {
// DeviceName returns the name of the device by its major and minor ids, or false if no
// such device is recognized.
DeviceName(major, minor uint64) (string, bool)
}
type MachineInfoNamer info.MachineInfo
func (n *MachineInfoNamer) DeviceName(major, minor uint64) (string, bool) {
for _, info := range n.DiskMap {
if info.Major == major && info.Minor == minor {
return "/dev/" + info.Name, true
}
}
for _, info := range n.Filesystems {
if info.DeviceMajor == major && info.DeviceMinor == minor {
return info.Device, true
}
}
return "", false
}
type deviceIdentifier struct {
major uint64
minor uint64
}
type deviceIdentifierMap map[deviceIdentifier]string
// Find locates the device name by device identifier out of from, caching the result as necessary.
func (m deviceIdentifierMap) Find(major, minor uint64, namer DeviceNamer) string {
d := deviceIdentifier{major, minor}
if s, ok := m[d]; ok {
return s
}
s, _ := namer.DeviceName(major, minor)
m[d] = s
return s
}
...@@ -37,7 +37,10 @@ func Status() (v1.DockerStatus, error) { ...@@ -37,7 +37,10 @@ func Status() (v1.DockerStatus, error) {
if err != nil { if err != nil {
return v1.DockerStatus{}, err return v1.DockerStatus{}, err
} }
return StatusFromDockerInfo(dockerInfo), nil
}
func StatusFromDockerInfo(dockerInfo dockertypes.Info) v1.DockerStatus {
out := v1.DockerStatus{} out := v1.DockerStatus{}
out.Version = VersionString() out.Version = VersionString()
out.APIVersion = APIVersionString() out.APIVersion = APIVersionString()
...@@ -53,7 +56,7 @@ func Status() (v1.DockerStatus, error) { ...@@ -53,7 +56,7 @@ func Status() (v1.DockerStatus, error) {
for _, v := range dockerInfo.DriverStatus { for _, v := range dockerInfo.DriverStatus {
out.DriverStatus[v[0]] = v[1] out.DriverStatus[v[0]] = v[1]
} }
return out, nil return out
} }
func Images() ([]v1.DockerImage, error) { func Images() ([]v1.DockerImage, error) {
......
...@@ -84,6 +84,7 @@ const ( ...@@ -84,6 +84,7 @@ const (
devicemapperStorageDriver storageDriver = "devicemapper" devicemapperStorageDriver storageDriver = "devicemapper"
aufsStorageDriver storageDriver = "aufs" aufsStorageDriver storageDriver = "aufs"
overlayStorageDriver storageDriver = "overlay" overlayStorageDriver storageDriver = "overlay"
overlay2StorageDriver storageDriver = "overlay2"
zfsStorageDriver storageDriver = "zfs" zfsStorageDriver storageDriver = "zfs"
) )
...@@ -107,6 +108,7 @@ type dockerFactory struct { ...@@ -107,6 +108,7 @@ type dockerFactory struct {
ignoreMetrics container.MetricSet ignoreMetrics container.MetricSet
thinPoolName string
thinPoolWatcher *devicemapper.ThinPoolWatcher thinPoolWatcher *devicemapper.ThinPoolWatcher
zfsWatcher *zfs.ZfsWatcher zfsWatcher *zfs.ZfsWatcher
...@@ -136,6 +138,7 @@ func (self *dockerFactory) NewContainerHandler(name string, inHostNamespace bool ...@@ -136,6 +138,7 @@ func (self *dockerFactory) NewContainerHandler(name string, inHostNamespace bool
metadataEnvs, metadataEnvs,
self.dockerVersion, self.dockerVersion,
self.ignoreMetrics, self.ignoreMetrics,
self.thinPoolName,
self.thinPoolWatcher, self.thinPoolWatcher,
self.zfsWatcher, self.zfsWatcher,
) )
...@@ -323,12 +326,18 @@ func Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, ignoreMetrics c ...@@ -323,12 +326,18 @@ func Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, ignoreMetrics c
return fmt.Errorf("failed to get cgroup subsystems: %v", err) return fmt.Errorf("failed to get cgroup subsystems: %v", err)
} }
var thinPoolWatcher *devicemapper.ThinPoolWatcher var (
thinPoolWatcher *devicemapper.ThinPoolWatcher
thinPoolName string
)
if storageDriver(dockerInfo.Driver) == devicemapperStorageDriver { if storageDriver(dockerInfo.Driver) == devicemapperStorageDriver {
thinPoolWatcher, err = startThinPoolWatcher(dockerInfo) thinPoolWatcher, err = startThinPoolWatcher(dockerInfo)
if err != nil { if err != nil {
glog.Errorf("devicemapper filesystem stats will not be reported: %v", err) glog.Errorf("devicemapper filesystem stats will not be reported: %v", err)
} }
status := StatusFromDockerInfo(*dockerInfo)
thinPoolName = status.DriverStatus[dockerutil.DriverStatusPoolName]
} }
var zfsWatcher *zfs.ZfsWatcher var zfsWatcher *zfs.ZfsWatcher
...@@ -350,6 +359,7 @@ func Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, ignoreMetrics c ...@@ -350,6 +359,7 @@ func Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, ignoreMetrics c
storageDriver: storageDriver(dockerInfo.Driver), storageDriver: storageDriver(dockerInfo.Driver),
storageDir: RootDir(), storageDir: RootDir(),
ignoreMetrics: ignoreMetrics, ignoreMetrics: ignoreMetrics,
thinPoolName: thinPoolName,
thinPoolWatcher: thinPoolWatcher, thinPoolWatcher: thinPoolWatcher,
zfsWatcher: zfsWatcher, zfsWatcher: zfsWatcher,
} }
......
...@@ -19,6 +19,7 @@ import ( ...@@ -19,6 +19,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"path" "path"
"strconv"
"strings" "strings"
"time" "time"
...@@ -112,6 +113,9 @@ type dockerContainerHandler struct { ...@@ -112,6 +113,9 @@ type dockerContainerHandler struct {
// zfs watcher // zfs watcher
zfsWatcher *zfs.ZfsWatcher zfsWatcher *zfs.ZfsWatcher
// container restart count
restartCount int
} }
var _ container.ContainerHandler = &dockerContainerHandler{} var _ container.ContainerHandler = &dockerContainerHandler{}
...@@ -146,6 +150,7 @@ func newDockerContainerHandler( ...@@ -146,6 +150,7 @@ func newDockerContainerHandler(
metadataEnvs []string, metadataEnvs []string,
dockerVersion []int, dockerVersion []int,
ignoreMetrics container.MetricSet, ignoreMetrics container.MetricSet,
thinPoolName string,
thinPoolWatcher *devicemapper.ThinPoolWatcher, thinPoolWatcher *devicemapper.ThinPoolWatcher,
zfsWatcher *zfs.ZfsWatcher, zfsWatcher *zfs.ZfsWatcher,
) (container.ContainerHandler, error) { ) (container.ContainerHandler, error) {
...@@ -180,18 +185,18 @@ func newDockerContainerHandler( ...@@ -180,18 +185,18 @@ func newDockerContainerHandler(
return nil, err return nil, err
} }
// Determine the rootfs storage dir OR the pool name to determine the device // Determine the rootfs storage dir OR the pool name to determine the device.
// For devicemapper, we only need the thin pool name, and that is passed in to this call
var ( var (
rootfsStorageDir string rootfsStorageDir string
poolName string
zfsFilesystem string zfsFilesystem string
zfsParent string zfsParent string
) )
switch storageDriver { switch storageDriver {
case aufsStorageDriver: case aufsStorageDriver:
rootfsStorageDir = path.Join(storageDir, string(aufsStorageDriver), aufsRWLayer, rwLayerID) rootfsStorageDir = path.Join(storageDir, string(aufsStorageDriver), aufsRWLayer, rwLayerID)
case overlayStorageDriver: case overlayStorageDriver, overlay2StorageDriver:
rootfsStorageDir = path.Join(storageDir, string(overlayStorageDriver), rwLayerID) rootfsStorageDir = path.Join(storageDir, string(storageDriver), rwLayerID)
case zfsStorageDriver: case zfsStorageDriver:
status, err := Status() status, err := Status()
if err != nil { if err != nil {
...@@ -199,13 +204,6 @@ func newDockerContainerHandler( ...@@ -199,13 +204,6 @@ func newDockerContainerHandler(
} }
zfsParent = status.DriverStatus[dockerutil.DriverStatusParentDataset] zfsParent = status.DriverStatus[dockerutil.DriverStatusParentDataset]
zfsFilesystem = path.Join(zfsParent, rwLayerID) zfsFilesystem = path.Join(zfsParent, rwLayerID)
case devicemapperStorageDriver:
status, err := Status()
if err != nil {
return nil, fmt.Errorf("unable to determine docker status: %v", err)
}
poolName = status.DriverStatus[dockerutil.DriverStatusPoolName]
} }
// TODO: extract object mother method // TODO: extract object mother method
...@@ -219,7 +217,7 @@ func newDockerContainerHandler( ...@@ -219,7 +217,7 @@ func newDockerContainerHandler(
storageDriver: storageDriver, storageDriver: storageDriver,
fsInfo: fsInfo, fsInfo: fsInfo,
rootFs: rootFs, rootFs: rootFs,
poolName: poolName, poolName: thinPoolName,
zfsFilesystem: zfsFilesystem, zfsFilesystem: zfsFilesystem,
rootfsStorageDir: rootfsStorageDir, rootfsStorageDir: rootfsStorageDir,
envs: make(map[string]string), envs: make(map[string]string),
...@@ -248,6 +246,7 @@ func newDockerContainerHandler( ...@@ -248,6 +246,7 @@ func newDockerContainerHandler(
handler.image = ctnr.Config.Image handler.image = ctnr.Config.Image
handler.networkMode = ctnr.HostConfig.NetworkMode handler.networkMode = ctnr.HostConfig.NetworkMode
handler.deviceID = ctnr.GraphDriver.Data["DeviceId"] handler.deviceID = ctnr.GraphDriver.Data["DeviceId"]
handler.restartCount = ctnr.RestartCount
// Obtain the IP address for the contianer. // Obtain the IP address for the contianer.
// If the NetworkMode starts with 'container:' then we need to use the IP address of the container specified. // If the NetworkMode starts with 'container:' then we need to use the IP address of the container specified.
...@@ -383,6 +382,10 @@ func (self *dockerContainerHandler) GetSpec() (info.ContainerSpec, error) { ...@@ -383,6 +382,10 @@ func (self *dockerContainerHandler) GetSpec() (info.ContainerSpec, error) {
spec, err := common.GetSpec(self.cgroupPaths, self.machineInfoFactory, self.needNet(), hasFilesystem) spec, err := common.GetSpec(self.cgroupPaths, self.machineInfoFactory, self.needNet(), hasFilesystem)
spec.Labels = self.labels spec.Labels = self.labels
// Only adds restartcount label if it's greater than 0
if self.restartCount > 0 {
spec.Labels["restartcount"] = strconv.Itoa(self.restartCount)
}
spec.Envs = self.envs spec.Envs = self.envs
spec.Image = self.image spec.Image = self.image
...@@ -390,6 +393,15 @@ func (self *dockerContainerHandler) GetSpec() (info.ContainerSpec, error) { ...@@ -390,6 +393,15 @@ func (self *dockerContainerHandler) GetSpec() (info.ContainerSpec, error) {
} }
func (self *dockerContainerHandler) getFsStats(stats *info.ContainerStats) error { func (self *dockerContainerHandler) getFsStats(stats *info.ContainerStats) error {
mi, err := self.machineInfoFactory.GetMachineInfo()
if err != nil {
return err
}
if !self.ignoreMetrics.Has(container.DiskIOMetrics) {
common.AssignDeviceNamesToDiskStats((*common.MachineInfoNamer)(mi), &stats.DiskIo)
}
if self.ignoreMetrics.Has(container.DiskUsageMetrics) { if self.ignoreMetrics.Has(container.DiskUsageMetrics) {
return nil return nil
} }
...@@ -399,7 +411,7 @@ func (self *dockerContainerHandler) getFsStats(stats *info.ContainerStats) error ...@@ -399,7 +411,7 @@ func (self *dockerContainerHandler) getFsStats(stats *info.ContainerStats) error
// Device has to be the pool name to correlate with the device name as // Device has to be the pool name to correlate with the device name as
// set in the machine info filesystems. // set in the machine info filesystems.
device = self.poolName device = self.poolName
case aufsStorageDriver, overlayStorageDriver: case aufsStorageDriver, overlayStorageDriver, overlay2StorageDriver:
deviceInfo, err := self.fsInfo.GetDirFsDevice(self.rootfsStorageDir) deviceInfo, err := self.fsInfo.GetDirFsDevice(self.rootfsStorageDir)
if err != nil { if err != nil {
return fmt.Errorf("unable to determine device info for dir: %v: %v", self.rootfsStorageDir, err) return fmt.Errorf("unable to determine device info for dir: %v: %v", self.rootfsStorageDir, err)
...@@ -411,11 +423,6 @@ func (self *dockerContainerHandler) getFsStats(stats *info.ContainerStats) error ...@@ -411,11 +423,6 @@ func (self *dockerContainerHandler) getFsStats(stats *info.ContainerStats) error
return nil return nil
} }
mi, err := self.machineInfoFactory.GetMachineInfo()
if err != nil {
return err
}
var ( var (
limit uint64 limit uint64
fsType string fsType string
......
...@@ -48,6 +48,7 @@ const ( ...@@ -48,6 +48,7 @@ const (
DiskUsageMetrics MetricKind = "disk" DiskUsageMetrics MetricKind = "disk"
NetworkUsageMetrics MetricKind = "network" NetworkUsageMetrics MetricKind = "network"
NetworkTcpUsageMetrics MetricKind = "tcp" NetworkTcpUsageMetrics MetricKind = "tcp"
NetworkUdpUsageMetrics MetricKind = "udp"
AppMetrics MetricKind = "app" AppMetrics MetricKind = "app"
) )
......
...@@ -17,6 +17,7 @@ package libcontainer ...@@ -17,6 +17,7 @@ package libcontainer
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
...@@ -118,6 +119,21 @@ func GetStats(cgroupManager cgroups.Manager, rootFs string, pid int, ignoreMetri ...@@ -118,6 +119,21 @@ func GetStats(cgroupManager cgroups.Manager, rootFs string, pid int, ignoreMetri
stats.Network.Tcp6 = t6 stats.Network.Tcp6 = t6
} }
} }
if !ignoreMetrics.Has(container.NetworkUdpUsageMetrics) {
u, err := udpStatsFromProc(rootFs, pid, "net/udp")
if err != nil {
glog.V(2).Infof("Unable to get udp stats from pid %d: %v", pid, err)
} else {
stats.Network.Udp = u
}
u6, err := udpStatsFromProc(rootFs, pid, "net/udp6")
if err != nil {
glog.V(2).Infof("Unable to get udp6 stats from pid %d: %v", pid, err)
} else {
stats.Network.Udp6 = u6
}
}
// For backwards compatibility. // For backwards compatibility.
if len(stats.Network.Interfaces) > 0 { if len(stats.Network.Interfaces) > 0 {
...@@ -291,6 +307,74 @@ func scanTcpStats(tcpStatsFile string) (info.TcpStat, error) { ...@@ -291,6 +307,74 @@ func scanTcpStats(tcpStatsFile string) (info.TcpStat, error) {
return stats, nil return stats, nil
} }
func udpStatsFromProc(rootFs string, pid int, file string) (info.UdpStat, error) {
var err error
var udpStats info.UdpStat
udpStatsFile := path.Join(rootFs, "proc", strconv.Itoa(pid), file)
r, err := os.Open(udpStatsFile)
if err != nil {
return udpStats, fmt.Errorf("failure opening %s: %v", udpStatsFile, err)
}
udpStats, err = scanUdpStats(r)
if err != nil {
return udpStats, fmt.Errorf("couldn't read udp stats: %v", err)
}
return udpStats, nil
}
func scanUdpStats(r io.Reader) (info.UdpStat, error) {
var stats info.UdpStat
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanLines)
// Discard header line
if b := scanner.Scan(); !b {
return stats, scanner.Err()
}
listening := uint64(0)
dropped := uint64(0)
rxQueued := uint64(0)
txQueued := uint64(0)
for scanner.Scan() {
line := scanner.Text()
// Format: sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode ref pointer drops
listening++
fs := strings.Fields(line)
if len(fs) != 13 {
continue
}
rx, tx := uint64(0), uint64(0)
fmt.Sscanf(fs[4], "%X:%X", &rx, &tx)
rxQueued += rx
txQueued += tx
d, err := strconv.Atoi(string(fs[12]))
if err != nil {
continue
}
dropped += uint64(d)
}
stats = info.UdpStat{
Listen: listening,
Dropped: dropped,
RxQueued: rxQueued,
TxQueued: txQueued,
}
return stats, nil
}
func GetProcesses(cgroupManager cgroups.Manager) ([]int, error) { func GetProcesses(cgroupManager cgroups.Manager) ([]int, error) {
pids, err := cgroupManager.GetPids() pids, err := cgroupManager.GetPids()
if err != nil { if err != nil {
......
...@@ -197,6 +197,7 @@ func fsToFsStats(fs *fs.Fs) info.FsStats { ...@@ -197,6 +197,7 @@ func fsToFsStats(fs *fs.Fs) info.FsStats {
} }
func (self *rawContainerHandler) getFsStats(stats *info.ContainerStats) error { func (self *rawContainerHandler) getFsStats(stats *info.ContainerStats) error {
var allFs []fs.Fs
// Get Filesystem information only for the root cgroup. // Get Filesystem information only for the root cgroup.
if isRootCgroup(self.name) { if isRootCgroup(self.name) {
filesystems, err := self.fsInfo.GetGlobalFsInfo() filesystems, err := self.fsInfo.GetGlobalFsInfo()
...@@ -207,6 +208,7 @@ func (self *rawContainerHandler) getFsStats(stats *info.ContainerStats) error { ...@@ -207,6 +208,7 @@ func (self *rawContainerHandler) getFsStats(stats *info.ContainerStats) error {
fs := filesystems[i] fs := filesystems[i]
stats.Filesystem = append(stats.Filesystem, fsToFsStats(&fs)) stats.Filesystem = append(stats.Filesystem, fsToFsStats(&fs))
} }
allFs = filesystems
} else if len(self.externalMounts) > 0 { } else if len(self.externalMounts) > 0 {
var mountSet map[string]struct{} var mountSet map[string]struct{}
mountSet = make(map[string]struct{}) mountSet = make(map[string]struct{})
...@@ -221,7 +223,10 @@ func (self *rawContainerHandler) getFsStats(stats *info.ContainerStats) error { ...@@ -221,7 +223,10 @@ func (self *rawContainerHandler) getFsStats(stats *info.ContainerStats) error {
fs := filesystems[i] fs := filesystems[i]
stats.Filesystem = append(stats.Filesystem, fsToFsStats(&fs)) stats.Filesystem = append(stats.Filesystem, fsToFsStats(&fs))
} }
allFs = filesystems
} }
common.AssignDeviceNamesToDiskStats(&fsNamer{fs: allFs, factory: self.machineInfoFactory}, &stats.DiskIo)
return nil return nil
} }
...@@ -272,3 +277,25 @@ func (self *rawContainerHandler) Exists() bool { ...@@ -272,3 +277,25 @@ func (self *rawContainerHandler) Exists() bool {
func (self *rawContainerHandler) Type() container.ContainerType { func (self *rawContainerHandler) Type() container.ContainerType {
return container.ContainerTypeRaw return container.ContainerTypeRaw
} }
type fsNamer struct {
fs []fs.Fs
factory info.MachineInfoFactory
info common.DeviceNamer
}
func (n *fsNamer) DeviceName(major, minor uint64) (string, bool) {
for _, info := range n.fs {
if uint64(info.Major) == major && uint64(info.Minor) == minor {
return info.Device, true
}
}
if n.info == nil {
mi, err := n.factory.GetMachineInfo()
if err != nil {
return "", false
}
n.info = (*common.MachineInfoNamer)(mi)
}
return n.info.DeviceName(major, minor)
}
...@@ -202,6 +202,15 @@ func (handler *rktContainerHandler) GetSpec() (info.ContainerSpec, error) { ...@@ -202,6 +202,15 @@ func (handler *rktContainerHandler) GetSpec() (info.ContainerSpec, error) {
} }
func (handler *rktContainerHandler) getFsStats(stats *info.ContainerStats) error { func (handler *rktContainerHandler) getFsStats(stats *info.ContainerStats) error {
mi, err := handler.machineInfoFactory.GetMachineInfo()
if err != nil {
return err
}
if !handler.ignoreMetrics.Has(container.DiskIOMetrics) {
common.AssignDeviceNamesToDiskStats((*common.MachineInfoNamer)(mi), &stats.DiskIo)
}
if handler.ignoreMetrics.Has(container.DiskUsageMetrics) { if handler.ignoreMetrics.Has(container.DiskUsageMetrics) {
return nil return nil
} }
...@@ -211,10 +220,6 @@ func (handler *rktContainerHandler) getFsStats(stats *info.ContainerStats) error ...@@ -211,10 +220,6 @@ func (handler *rktContainerHandler) getFsStats(stats *info.ContainerStats) error
return err return err
} }
mi, err := handler.machineInfoFactory.GetMachineInfo()
if err != nil {
return err
}
var limit uint64 = 0 var limit uint64 = 0
// Use capacity as limit. // Use capacity as limit.
......
...@@ -266,7 +266,7 @@ func getDockerImagePaths(context Context) map[string]struct{} { ...@@ -266,7 +266,7 @@ func getDockerImagePaths(context Context) map[string]struct{} {
// TODO(rjnagal): Detect docker root and graphdriver directories from docker info. // TODO(rjnagal): Detect docker root and graphdriver directories from docker info.
dockerRoot := context.Docker.Root dockerRoot := context.Docker.Root
for _, dir := range []string{"devicemapper", "btrfs", "aufs", "overlay", "zfs"} { for _, dir := range []string{"devicemapper", "btrfs", "aufs", "overlay", "overlay2", "zfs"} {
dockerImagePaths[path.Join(dockerRoot, dir)] = struct{}{} dockerImagePaths[path.Join(dockerRoot, dir)] = struct{}{}
} }
for dockerRoot != "/" && dockerRoot != "." { for dockerRoot != "/" && dockerRoot != "." {
...@@ -455,11 +455,15 @@ func (self *RealFsInfo) GetDirFsDevice(dir string) (*DeviceInfo, error) { ...@@ -455,11 +455,15 @@ func (self *RealFsInfo) GetDirFsDevice(dir string) (*DeviceInfo, error) {
} }
func (self *RealFsInfo) GetDirDiskUsage(dir string, timeout time.Duration) (uint64, error) { func (self *RealFsInfo) GetDirDiskUsage(dir string, timeout time.Duration) (uint64, error) {
claimToken()
defer releaseToken()
return GetDirDiskUsage(dir, timeout)
}
func GetDirDiskUsage(dir string, timeout time.Duration) (uint64, error) {
if dir == "" { if dir == "" {
return 0, fmt.Errorf("invalid directory") return 0, fmt.Errorf("invalid directory")
} }
claimToken()
defer releaseToken()
cmd := exec.Command("nice", "-n", "19", "du", "-s", dir) cmd := exec.Command("nice", "-n", "19", "du", "-s", dir)
stdoutp, err := cmd.StdoutPipe() stdoutp, err := cmd.StdoutPipe()
if err != nil { if err != nil {
...@@ -496,13 +500,17 @@ func (self *RealFsInfo) GetDirDiskUsage(dir string, timeout time.Duration) (uint ...@@ -496,13 +500,17 @@ func (self *RealFsInfo) GetDirDiskUsage(dir string, timeout time.Duration) (uint
} }
func (self *RealFsInfo) GetDirInodeUsage(dir string, timeout time.Duration) (uint64, error) { func (self *RealFsInfo) GetDirInodeUsage(dir string, timeout time.Duration) (uint64, error) {
claimToken()
defer releaseToken()
return GetDirInodeUsage(dir, timeout)
}
func GetDirInodeUsage(dir string, timeout time.Duration) (uint64, error) {
if dir == "" { if dir == "" {
return 0, fmt.Errorf("invalid directory") return 0, fmt.Errorf("invalid directory")
} }
var counter byteCounter var counter byteCounter
var stderr bytes.Buffer var stderr bytes.Buffer
claimToken()
defer releaseToken()
findCmd := exec.Command("find", dir, "-xdev", "-printf", ".") findCmd := exec.Command("find", dir, "-xdev", "-printf", ".")
findCmd.Stdout, findCmd.Stderr = &counter, &stderr findCmd.Stdout, findCmd.Stderr = &counter, &stderr
if err := findCmd.Start(); err != nil { if err := findCmd.Start(); err != nil {
......
...@@ -23,6 +23,7 @@ go_library( ...@@ -23,6 +23,7 @@ go_library(
"//vendor/github.com/google/cadvisor/pages/static:go_default_library", "//vendor/github.com/google/cadvisor/pages/static:go_default_library",
"//vendor/github.com/google/cadvisor/validate:go_default_library", "//vendor/github.com/google/cadvisor/validate:go_default_library",
"//vendor/github.com/prometheus/client_golang/prometheus:go_default_library", "//vendor/github.com/prometheus/client_golang/prometheus:go_default_library",
"//vendor/github.com/prometheus/client_golang/prometheus/promhttp:go_default_library",
], ],
) )
......
...@@ -17,6 +17,7 @@ package http ...@@ -17,6 +17,7 @@ package http
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"os"
"github.com/google/cadvisor/api" "github.com/google/cadvisor/api"
"github.com/google/cadvisor/healthz" "github.com/google/cadvisor/healthz"
...@@ -30,6 +31,7 @@ import ( ...@@ -30,6 +31,7 @@ import (
auth "github.com/abbot/go-http-auth" auth "github.com/abbot/go-http-auth"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
) )
func RegisterHandlers(mux httpmux.Mux, containerManager manager.Manager, httpAuthFile, httpAuthRealm, httpDigestFile, httpDigestRealm string) error { func RegisterHandlers(mux httpmux.Mux, containerManager manager.Manager, httpAuthFile, httpAuthRealm, httpDigestFile, httpDigestRealm string) error {
...@@ -54,7 +56,7 @@ func RegisterHandlers(mux httpmux.Mux, containerManager manager.Manager, httpAut ...@@ -54,7 +56,7 @@ func RegisterHandlers(mux httpmux.Mux, containerManager manager.Manager, httpAut
// Redirect / to containers page. // Redirect / to containers page.
mux.Handle("/", http.RedirectHandler(pages.ContainersPage, http.StatusTemporaryRedirect)) mux.Handle("/", http.RedirectHandler(pages.ContainersPage, http.StatusTemporaryRedirect))
var authenticated bool = false var authenticated bool
// Setup the authenticator object // Setup the authenticator object
if httpAuthFile != "" { if httpAuthFile != "" {
...@@ -89,13 +91,16 @@ func RegisterHandlers(mux httpmux.Mux, containerManager manager.Manager, httpAut ...@@ -89,13 +91,16 @@ func RegisterHandlers(mux httpmux.Mux, containerManager manager.Manager, httpAut
return nil return nil
} }
// RegisterPrometheusHandler creates a new PrometheusCollector, registers it // RegisterPrometheusHandler creates a new PrometheusCollector and configures
// on the global registry and configures the provided HTTP mux to handle the // the provided HTTP mux to handle the given Prometheus endpoint.
// given Prometheus endpoint.
func RegisterPrometheusHandler(mux httpmux.Mux, containerManager manager.Manager, prometheusEndpoint string, f metrics.ContainerLabelsFunc) { func RegisterPrometheusHandler(mux httpmux.Mux, containerManager manager.Manager, prometheusEndpoint string, f metrics.ContainerLabelsFunc) {
collector := metrics.NewPrometheusCollector(containerManager, f) r := prometheus.NewRegistry()
prometheus.MustRegister(collector) r.MustRegister(
mux.Handle(prometheusEndpoint, prometheus.Handler()) metrics.NewPrometheusCollector(containerManager, f),
prometheus.NewGoCollector(),
prometheus.NewProcessCollector(os.Getpid(), ""),
)
mux.Handle(prometheusEndpoint, promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
} }
func staticHandlerNoAuth(w http.ResponseWriter, r *http.Request) { func staticHandlerNoAuth(w http.ResponseWriter, r *http.Request) {
......
...@@ -307,9 +307,10 @@ type CpuStats struct { ...@@ -307,9 +307,10 @@ type CpuStats struct {
} }
type PerDiskStats struct { type PerDiskStats struct {
Major uint64 `json:"major"` Device string `json:"-"`
Minor uint64 `json:"minor"` Major uint64 `json:"major"`
Stats map[string]uint64 `json:"stats"` Minor uint64 `json:"minor"`
Stats map[string]uint64 `json:"stats"`
} }
type DiskIoStats struct { type DiskIoStats struct {
...@@ -386,6 +387,10 @@ type NetworkStats struct { ...@@ -386,6 +387,10 @@ type NetworkStats struct {
Tcp TcpStat `json:"tcp"` Tcp TcpStat `json:"tcp"`
// TCP6 connection stats (Established, Listen...) // TCP6 connection stats (Established, Listen...)
Tcp6 TcpStat `json:"tcp6"` Tcp6 TcpStat `json:"tcp6"`
// UDP connection stats
Udp UdpStat `json:"udp"`
// UDP6 connection stats
Udp6 UdpStat `json:"udp6"`
} }
type TcpStat struct { type TcpStat struct {
...@@ -413,6 +418,20 @@ type TcpStat struct { ...@@ -413,6 +418,20 @@ type TcpStat struct {
Closing uint64 Closing uint64
} }
type UdpStat struct {
// Count of UDP sockets in state "Listen"
Listen uint64
// Count of UDP packets dropped by the IP stack
Dropped uint64
// Count of packets Queued for Receieve
RxQueued uint64
// Count of packets Queued for Transmit
TxQueued uint64
}
type FsStats struct { type FsStats struct {
// The block device name associated with the filesystem. // The block device name associated with the filesystem.
Device string `json:"device,omitempty"` Device string `json:"device,omitempty"`
......
...@@ -17,6 +17,10 @@ package v1 ...@@ -17,6 +17,10 @@ package v1
type FsInfo struct { type FsInfo struct {
// Block device associated with the filesystem. // Block device associated with the filesystem.
Device string `json:"device"` Device string `json:"device"`
// DeviceMajor is the major identifier of the device, used for correlation with blkio stats
DeviceMajor uint64 `json:"-"`
// DeviceMinor is the minor identifier of the device, used for correlation with blkio stats
DeviceMinor uint64 `json:"-"`
// Total number of bytes available on the filesystem. // Total number of bytes available on the filesystem.
Capacity uint64 `json:"capacity"` Capacity uint64 `json:"capacity"`
......
...@@ -269,6 +269,10 @@ type NetworkStats struct { ...@@ -269,6 +269,10 @@ type NetworkStats struct {
Tcp TcpStat `json:"tcp"` Tcp TcpStat `json:"tcp"`
// TCP6 connection stats (Established, Listen...) // TCP6 connection stats (Established, Listen...)
Tcp6 TcpStat `json:"tcp6"` Tcp6 TcpStat `json:"tcp6"`
// UDP connection stats
Udp v1.UdpStat `json:"udp"`
// UDP6 connection stats
Udp6 v1.UdpStat `json:"udp6"`
} }
// Instantaneous CPU stats // Instantaneous CPU stats
......
...@@ -133,7 +133,7 @@ func ContainerStatsFromV1(containerName string, spec *v1.ContainerSpec, stats [] ...@@ -133,7 +133,7 @@ func ContainerStatsFromV1(containerName string, spec *v1.ContainerSpec, stats []
} }
} else if len(val.Filesystem) > 1 && containerName != "/" { } else if len(val.Filesystem) > 1 && containerName != "/" {
// Cannot handle multiple devices per container. // Cannot handle multiple devices per container.
glog.V(2).Infof("failed to handle multiple devices for container %s. Skipping Filesystem stats", containerName) glog.V(4).Infof("failed to handle multiple devices for container %s. Skipping Filesystem stats", containerName)
} }
} }
if spec.HasDiskIo { if spec.HasDiskIo {
...@@ -259,6 +259,7 @@ func ContainerSpecFromV1(specV1 *v1.ContainerSpec, aliases []string, namespace s ...@@ -259,6 +259,7 @@ func ContainerSpecFromV1(specV1 *v1.ContainerSpec, aliases []string, namespace s
HasCustomMetrics: specV1.HasCustomMetrics, HasCustomMetrics: specV1.HasCustomMetrics,
Image: specV1.Image, Image: specV1.Image,
Labels: specV1.Labels, Labels: specV1.Labels,
Envs: specV1.Envs,
} }
if specV1.HasCpu { if specV1.HasCpu {
specV2.Cpu.Limit = specV1.Cpu.Limit specV2.Cpu.Limit = specV1.Cpu.Limit
......
...@@ -116,7 +116,7 @@ func Info(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) (*info.Mach ...@@ -116,7 +116,7 @@ func Info(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) (*info.Mach
if fs.Inodes != nil { if fs.Inodes != nil {
inodes = *fs.Inodes inodes = *fs.Inodes
} }
machineInfo.Filesystems = append(machineInfo.Filesystems, info.FsInfo{Device: fs.Device, Type: fs.Type.String(), Capacity: fs.Capacity, Inodes: inodes, HasInodes: fs.Inodes != nil}) machineInfo.Filesystems = append(machineInfo.Filesystems, info.FsInfo{Device: fs.Device, DeviceMajor: uint64(fs.Major), DeviceMinor: uint64(fs.Minor), Type: fs.Type.String(), Capacity: fs.Capacity, Inodes: inodes, HasInodes: fs.Inodes != nil})
} }
return machineInfo, nil return machineInfo, nil
......
...@@ -566,9 +566,24 @@ func (self *manager) getDockerContainer(containerName string) (*containerData, e ...@@ -566,9 +566,24 @@ func (self *manager) getDockerContainer(containerName string) (*containerData, e
Namespace: docker.DockerNamespace, Namespace: docker.DockerNamespace,
Name: containerName, Name: containerName,
}] }]
// Look for container by short prefix name if no exact match found.
if !ok { if !ok {
return nil, fmt.Errorf("unable to find Docker container %q", containerName) for contName, c := range self.containers {
if contName.Namespace == docker.DockerNamespace && strings.HasPrefix(contName.Name, containerName) {
if cont == nil {
cont = c
} else {
return nil, fmt.Errorf("unable to find container. Container %q is not unique", containerName)
}
}
}
if cont == nil {
return nil, fmt.Errorf("unable to find Docker container %q", containerName)
}
} }
return cont, nil return cont, nil
} }
......
...@@ -45,6 +45,14 @@ type metricValue struct { ...@@ -45,6 +45,14 @@ type metricValue struct {
type metricValues []metricValue type metricValues []metricValue
// asFloat64 converts a uint64 into a float64.
func asFloat64(v uint64) float64 { return float64(v) }
// asNanosecondsToSeconds converts nanoseconds into a float64 representing seconds.
func asNanosecondsToSeconds(v uint64) float64 {
return float64(v) / float64(time.Second)
}
// fsValues is a helper method for assembling per-filesystem stats. // fsValues is a helper method for assembling per-filesystem stats.
func fsValues(fsStats []info.FsStats, valueFn func(*info.FsStats) float64) metricValues { func fsValues(fsStats []info.FsStats, valueFn func(*info.FsStats) float64) metricValues {
values := make(metricValues, 0, len(fsStats)) values := make(metricValues, 0, len(fsStats))
...@@ -57,6 +65,24 @@ func fsValues(fsStats []info.FsStats, valueFn func(*info.FsStats) float64) metri ...@@ -57,6 +65,24 @@ func fsValues(fsStats []info.FsStats, valueFn func(*info.FsStats) float64) metri
return values return values
} }
// ioValues is a helper method for assembling per-disk and per-filesystem stats.
func ioValues(ioStats []info.PerDiskStats, ioType string, ioValueFn func(uint64) float64, fsStats []info.FsStats, valueFn func(*info.FsStats) float64) metricValues {
values := make(metricValues, 0, len(ioStats)+len(fsStats))
for _, stat := range ioStats {
values = append(values, metricValue{
value: ioValueFn(stat.Stats[ioType]),
labels: []string{stat.Device},
})
}
for _, stat := range fsStats {
values = append(values, metricValue{
value: valueFn(&stat),
labels: []string{stat.Device},
})
}
return values
}
// containerMetric describes a multi-dimensional metric used for exposing a // containerMetric describes a multi-dimensional metric used for exposing a
// certain type of container statistic. // certain type of container statistic.
type containerMetric struct { type containerMetric struct {
...@@ -130,10 +156,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo ...@@ -130,10 +156,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo
getValues: func(s *info.ContainerStats) metricValues { getValues: func(s *info.ContainerStats) metricValues {
values := make(metricValues, 0, len(s.Cpu.Usage.PerCpu)) values := make(metricValues, 0, len(s.Cpu.Usage.PerCpu))
for i, value := range s.Cpu.Usage.PerCpu { for i, value := range s.Cpu.Usage.PerCpu {
values = append(values, metricValue{ if value > 0 {
value: float64(value) / float64(time.Second), values = append(values, metricValue{
labels: []string{fmt.Sprintf("cpu%02d", i)}, value: float64(value) / float64(time.Second),
}) labels: []string{fmt.Sprintf("cpu%02d", i)},
})
}
} }
return values return values
}, },
...@@ -269,14 +297,28 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo ...@@ -269,14 +297,28 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo
}) })
}, },
}, { }, {
name: "container_fs_reads_bytes_total",
help: "Cumulative count of bytes read",
valueType: prometheus.CounterValue,
extraLabels: []string{"device"},
getValues: func(s *info.ContainerStats) metricValues {
return ioValues(
s.DiskIo.IoServiceBytes, "Read", asFloat64,
nil, nil,
)
},
}, {
name: "container_fs_reads_total", name: "container_fs_reads_total",
help: "Cumulative count of reads completed", help: "Cumulative count of reads completed",
valueType: prometheus.CounterValue, valueType: prometheus.CounterValue,
extraLabels: []string{"device"}, extraLabels: []string{"device"},
getValues: func(s *info.ContainerStats) metricValues { getValues: func(s *info.ContainerStats) metricValues {
return fsValues(s.Filesystem, func(fs *info.FsStats) float64 { return ioValues(
return float64(fs.ReadsCompleted) s.DiskIo.IoServiced, "Read", asFloat64,
}) s.Filesystem, func(fs *info.FsStats) float64 {
return float64(fs.ReadsCompleted)
},
)
}, },
}, { }, {
name: "container_fs_sector_reads_total", name: "container_fs_sector_reads_total",
...@@ -284,9 +326,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo ...@@ -284,9 +326,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo
valueType: prometheus.CounterValue, valueType: prometheus.CounterValue,
extraLabels: []string{"device"}, extraLabels: []string{"device"},
getValues: func(s *info.ContainerStats) metricValues { getValues: func(s *info.ContainerStats) metricValues {
return fsValues(s.Filesystem, func(fs *info.FsStats) float64 { return ioValues(
return float64(fs.SectorsRead) s.DiskIo.Sectors, "Read", asFloat64,
}) s.Filesystem, func(fs *info.FsStats) float64 {
return float64(fs.SectorsRead)
},
)
}, },
}, { }, {
name: "container_fs_reads_merged_total", name: "container_fs_reads_merged_total",
...@@ -294,9 +339,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo ...@@ -294,9 +339,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo
valueType: prometheus.CounterValue, valueType: prometheus.CounterValue,
extraLabels: []string{"device"}, extraLabels: []string{"device"},
getValues: func(s *info.ContainerStats) metricValues { getValues: func(s *info.ContainerStats) metricValues {
return fsValues(s.Filesystem, func(fs *info.FsStats) float64 { return ioValues(
return float64(fs.ReadsMerged) s.DiskIo.IoMerged, "Read", asFloat64,
}) s.Filesystem, func(fs *info.FsStats) float64 {
return float64(fs.ReadsMerged)
},
)
}, },
}, { }, {
name: "container_fs_read_seconds_total", name: "container_fs_read_seconds_total",
...@@ -304,9 +352,23 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo ...@@ -304,9 +352,23 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo
valueType: prometheus.CounterValue, valueType: prometheus.CounterValue,
extraLabels: []string{"device"}, extraLabels: []string{"device"},
getValues: func(s *info.ContainerStats) metricValues { getValues: func(s *info.ContainerStats) metricValues {
return fsValues(s.Filesystem, func(fs *info.FsStats) float64 { return ioValues(
return float64(fs.ReadTime) / float64(time.Second) s.DiskIo.IoServiceTime, "Read", asNanosecondsToSeconds,
}) s.Filesystem, func(fs *info.FsStats) float64 {
return float64(fs.ReadTime) / float64(time.Second)
},
)
},
}, {
name: "container_fs_writes_bytes_total",
help: "Cumulative count of bytes written",
valueType: prometheus.CounterValue,
extraLabels: []string{"device"},
getValues: func(s *info.ContainerStats) metricValues {
return ioValues(
s.DiskIo.IoServiceBytes, "Write", asFloat64,
nil, nil,
)
}, },
}, { }, {
name: "container_fs_writes_total", name: "container_fs_writes_total",
...@@ -314,9 +376,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo ...@@ -314,9 +376,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo
valueType: prometheus.CounterValue, valueType: prometheus.CounterValue,
extraLabels: []string{"device"}, extraLabels: []string{"device"},
getValues: func(s *info.ContainerStats) metricValues { getValues: func(s *info.ContainerStats) metricValues {
return fsValues(s.Filesystem, func(fs *info.FsStats) float64 { return ioValues(
return float64(fs.WritesCompleted) s.DiskIo.IoServiced, "Write", asFloat64,
}) s.Filesystem, func(fs *info.FsStats) float64 {
return float64(fs.WritesCompleted)
},
)
}, },
}, { }, {
name: "container_fs_sector_writes_total", name: "container_fs_sector_writes_total",
...@@ -324,9 +389,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo ...@@ -324,9 +389,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo
valueType: prometheus.CounterValue, valueType: prometheus.CounterValue,
extraLabels: []string{"device"}, extraLabels: []string{"device"},
getValues: func(s *info.ContainerStats) metricValues { getValues: func(s *info.ContainerStats) metricValues {
return fsValues(s.Filesystem, func(fs *info.FsStats) float64 { return ioValues(
return float64(fs.SectorsWritten) s.DiskIo.Sectors, "Write", asFloat64,
}) s.Filesystem, func(fs *info.FsStats) float64 {
return float64(fs.SectorsWritten)
},
)
}, },
}, { }, {
name: "container_fs_writes_merged_total", name: "container_fs_writes_merged_total",
...@@ -334,9 +402,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo ...@@ -334,9 +402,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo
valueType: prometheus.CounterValue, valueType: prometheus.CounterValue,
extraLabels: []string{"device"}, extraLabels: []string{"device"},
getValues: func(s *info.ContainerStats) metricValues { getValues: func(s *info.ContainerStats) metricValues {
return fsValues(s.Filesystem, func(fs *info.FsStats) float64 { return ioValues(
return float64(fs.WritesMerged) s.DiskIo.IoMerged, "Write", asFloat64,
}) s.Filesystem, func(fs *info.FsStats) float64 {
return float64(fs.WritesMerged)
},
)
}, },
}, { }, {
name: "container_fs_write_seconds_total", name: "container_fs_write_seconds_total",
...@@ -344,9 +415,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo ...@@ -344,9 +415,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo
valueType: prometheus.CounterValue, valueType: prometheus.CounterValue,
extraLabels: []string{"device"}, extraLabels: []string{"device"},
getValues: func(s *info.ContainerStats) metricValues { getValues: func(s *info.ContainerStats) metricValues {
return fsValues(s.Filesystem, func(fs *info.FsStats) float64 { return ioValues(
return float64(fs.WriteTime) / float64(time.Second) s.DiskIo.IoServiceTime, "Write", asNanosecondsToSeconds,
}) s.Filesystem, func(fs *info.FsStats) float64 {
return float64(fs.WriteTime) / float64(time.Second)
},
)
}, },
}, { }, {
name: "container_fs_io_current", name: "container_fs_io_current",
...@@ -354,9 +428,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo ...@@ -354,9 +428,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo
valueType: prometheus.GaugeValue, valueType: prometheus.GaugeValue,
extraLabels: []string{"device"}, extraLabels: []string{"device"},
getValues: func(s *info.ContainerStats) metricValues { getValues: func(s *info.ContainerStats) metricValues {
return fsValues(s.Filesystem, func(fs *info.FsStats) float64 { return ioValues(
return float64(fs.IoInProgress) s.DiskIo.IoQueued, "Total", asFloat64,
}) s.Filesystem, func(fs *info.FsStats) float64 {
return float64(fs.IoInProgress)
},
)
}, },
}, { }, {
name: "container_fs_io_time_seconds_total", name: "container_fs_io_time_seconds_total",
...@@ -364,9 +441,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo ...@@ -364,9 +441,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc) *PrometheusCo
valueType: prometheus.CounterValue, valueType: prometheus.CounterValue,
extraLabels: []string{"device"}, extraLabels: []string{"device"},
getValues: func(s *info.ContainerStats) metricValues { getValues: func(s *info.ContainerStats) metricValues {
return fsValues(s.Filesystem, func(fs *info.FsStats) float64 { return ioValues(
return float64(float64(fs.IoTime) / float64(time.Second)) s.DiskIo.IoServiceTime, "Total", asNanosecondsToSeconds,
}) s.Filesystem, func(fs *info.FsStats) float64 {
return float64(float64(fs.IoTime) / float64(time.Second))
},
)
}, },
}, { }, {
name: "container_fs_io_time_weighted_seconds_total", name: "container_fs_io_time_weighted_seconds_total",
......
The Prometheus project was started by Matt T. Proud (emeritus) and
Julius Volz in 2012.
Maintainers of this repository:
* Björn Rabenstein <beorn@soundcloud.com>
The following individuals have contributed code to this repository
(listed in alphabetical order):
* Bernerd Schaefer <bj.schaefer@gmail.com>
* Björn Rabenstein <beorn@soundcloud.com>
* Daniel Bornkessel <daniel@soundcloud.com>
* Jeff Younker <jeff@drinktomi.com>
* Julius Volz <julius@soundcloud.com>
* Matt T. Proud <matt.proud@gmail.com>
* Tobias Schmidt <ts@soundcloud.com>
...@@ -7,11 +7,6 @@ SoundCloud Ltd. (http://soundcloud.com/). ...@@ -7,11 +7,6 @@ SoundCloud Ltd. (http://soundcloud.com/).
The following components are included in this product: The following components are included in this product:
goautoneg
http://bitbucket.org/ww/goautoneg
Copyright 2011, Open Knowledge Foundation Ltd.
See README.txt for license details.
perks - a fork of https://github.com/bmizerany/perks perks - a fork of https://github.com/bmizerany/perks
https://github.com/beorn7/perks https://github.com/beorn7/perks
Copyright 2013-2015 Blake Mizerany, Björn Rabenstein Copyright 2013-2015 Blake Mizerany, Björn Rabenstein
......
...@@ -14,16 +14,18 @@ go_library( ...@@ -14,16 +14,18 @@ go_library(
"counter.go", "counter.go",
"desc.go", "desc.go",
"doc.go", "doc.go",
"expvar.go", "expvar_collector.go",
"fnv.go",
"gauge.go", "gauge.go",
"go_collector.go", "go_collector.go",
"histogram.go", "histogram.go",
"http.go", "http.go",
"metric.go", "metric.go",
"observer.go",
"process_collector.go", "process_collector.go",
"push.go",
"registry.go", "registry.go",
"summary.go", "summary.go",
"timer.go",
"untyped.go", "untyped.go",
"value.go", "value.go",
"vec.go", "vec.go",
...@@ -34,6 +36,7 @@ go_library( ...@@ -34,6 +36,7 @@ go_library(
"//vendor/github.com/golang/protobuf/proto:go_default_library", "//vendor/github.com/golang/protobuf/proto:go_default_library",
"//vendor/github.com/prometheus/client_model/go:go_default_library", "//vendor/github.com/prometheus/client_model/go:go_default_library",
"//vendor/github.com/prometheus/common/expfmt:go_default_library", "//vendor/github.com/prometheus/common/expfmt:go_default_library",
"//vendor/github.com/prometheus/common/model:go_default_library",
"//vendor/github.com/prometheus/procfs:go_default_library", "//vendor/github.com/prometheus/procfs:go_default_library",
], ],
) )
...@@ -47,6 +50,9 @@ filegroup( ...@@ -47,6 +50,9 @@ filegroup(
filegroup( filegroup(
name = "all-srcs", name = "all-srcs",
srcs = [":package-srcs"], srcs = [
":package-srcs",
"//vendor/github.com/prometheus/client_golang/prometheus/promhttp:all-srcs",
],
tags = ["automanaged"], tags = ["automanaged"],
) )
# Overview See [![go-doc](https://godoc.org/github.com/prometheus/client_golang/prometheus?status.svg)](https://godoc.org/github.com/prometheus/client_golang/prometheus).
This is the [Prometheus](http://www.prometheus.io) telemetric
instrumentation client [Go](http://golang.org) client library. It
enable authors to define process-space metrics for their servers and
expose them through a web service interface for extraction,
aggregation, and a whole slew of other post processing techniques.
# Installing
$ go get github.com/prometheus/client_golang/prometheus
# Example
```go
package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
)
var (
indexed = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "my_company",
Subsystem: "indexer",
Name: "documents_indexed",
Help: "The number of documents indexed.",
})
size = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "my_company",
Subsystem: "storage",
Name: "documents_total_size_bytes",
Help: "The total size of all documents in the storage.",
})
)
func main() {
http.Handle("/metrics", prometheus.Handler())
indexed.Inc()
size.Set(5)
http.ListenAndServe(":8080", nil)
}
func init() {
prometheus.MustRegister(indexed)
prometheus.MustRegister(size)
}
```
# Documentation
[![GoDoc](https://godoc.org/github.com/prometheus/client_golang?status.png)](https://godoc.org/github.com/prometheus/client_golang)
...@@ -15,15 +15,15 @@ package prometheus ...@@ -15,15 +15,15 @@ package prometheus
// Collector is the interface implemented by anything that can be used by // Collector is the interface implemented by anything that can be used by
// Prometheus to collect metrics. A Collector has to be registered for // Prometheus to collect metrics. A Collector has to be registered for
// collection. See Register, MustRegister, RegisterOrGet, and MustRegisterOrGet. // collection. See Registerer.Register.
// //
// The stock metrics provided by this package (like Gauge, Counter, Summary) are // The stock metrics provided by this package (Gauge, Counter, Summary,
// also Collectors (which only ever collect one metric, namely itself). An // Histogram, Untyped) are also Collectors (which only ever collect one metric,
// implementer of Collector may, however, collect multiple metrics in a // namely itself). An implementer of Collector may, however, collect multiple
// coordinated fashion and/or create metrics on the fly. Examples for collectors // metrics in a coordinated fashion and/or create metrics on the fly. Examples
// already implemented in this library are the metric vectors (i.e. collection // for collectors already implemented in this library are the metric vectors
// of multiple instances of the same Metric but with different label values) // (i.e. collection of multiple instances of the same Metric but with different
// like GaugeVec or SummaryVec, and the ExpvarCollector. // label values) like GaugeVec or SummaryVec, and the ExpvarCollector.
type Collector interface { type Collector interface {
// Describe sends the super-set of all possible descriptors of metrics // Describe sends the super-set of all possible descriptors of metrics
// collected by this Collector to the provided channel and returns once // collected by this Collector to the provided channel and returns once
...@@ -37,39 +37,39 @@ type Collector interface { ...@@ -37,39 +37,39 @@ type Collector interface {
// executing this method, it must send an invalid descriptor (created // executing this method, it must send an invalid descriptor (created
// with NewInvalidDesc) to signal the error to the registry. // with NewInvalidDesc) to signal the error to the registry.
Describe(chan<- *Desc) Describe(chan<- *Desc)
// Collect is called by Prometheus when collecting metrics. The // Collect is called by the Prometheus registry when collecting
// implementation sends each collected metric via the provided channel // metrics. The implementation sends each collected metric via the
// and returns once the last metric has been sent. The descriptor of // provided channel and returns once the last metric has been sent. The
// each sent metric is one of those returned by Describe. Returned // descriptor of each sent metric is one of those returned by
// metrics that share the same descriptor must differ in their variable // Describe. Returned metrics that share the same descriptor must differ
// label values. This method may be called concurrently and must // in their variable label values. This method may be called
// therefore be implemented in a concurrency safe way. Blocking occurs // concurrently and must therefore be implemented in a concurrency safe
// at the expense of total performance of rendering all registered // way. Blocking occurs at the expense of total performance of rendering
// metrics. Ideally, Collector implementations support concurrent // all registered metrics. Ideally, Collector implementations support
// readers. // concurrent readers.
Collect(chan<- Metric) Collect(chan<- Metric)
} }
// SelfCollector implements Collector for a single Metric so that that the // selfCollector implements Collector for a single Metric so that the Metric
// Metric collects itself. Add it as an anonymous field to a struct that // collects itself. Add it as an anonymous field to a struct that implements
// implements Metric, and call Init with the Metric itself as an argument. // Metric, and call init with the Metric itself as an argument.
type SelfCollector struct { type selfCollector struct {
self Metric self Metric
} }
// Init provides the SelfCollector with a reference to the metric it is supposed // init provides the selfCollector with a reference to the metric it is supposed
// to collect. It is usually called within the factory function to create a // to collect. It is usually called within the factory function to create a
// metric. See example. // metric. See example.
func (c *SelfCollector) Init(self Metric) { func (c *selfCollector) init(self Metric) {
c.self = self c.self = self
} }
// Describe implements Collector. // Describe implements Collector.
func (c *SelfCollector) Describe(ch chan<- *Desc) { func (c *selfCollector) Describe(ch chan<- *Desc) {
ch <- c.self.Desc() ch <- c.self.Desc()
} }
// Collect implements Collector. // Collect implements Collector.
func (c *SelfCollector) Collect(ch chan<- Metric) { func (c *selfCollector) Collect(ch chan<- Metric) {
ch <- c.self ch <- c.self
} }
...@@ -15,7 +15,6 @@ package prometheus ...@@ -15,7 +15,6 @@ package prometheus
import ( import (
"errors" "errors"
"hash/fnv"
) )
// Counter is a Metric that represents a single numerical value that only ever // Counter is a Metric that represents a single numerical value that only ever
...@@ -31,13 +30,8 @@ type Counter interface { ...@@ -31,13 +30,8 @@ type Counter interface {
Metric Metric
Collector Collector
// Set is used to set the Counter to an arbitrary value. It is only used // Inc increments the counter by 1. Use Add to increment it by arbitrary
// if you have to transfer a value from an external counter into this // non-negative values.
// Prometheus metric. Do not use it for regular handling of a
// Prometheus counter (as it can be used to break the contract of
// monotonically increasing values).
Set(float64)
// Inc increments the counter by 1.
Inc() Inc()
// Add adds the given value to the counter. It panics if the value is < // Add adds the given value to the counter. It panics if the value is <
// 0. // 0.
...@@ -56,7 +50,7 @@ func NewCounter(opts CounterOpts) Counter { ...@@ -56,7 +50,7 @@ func NewCounter(opts CounterOpts) Counter {
opts.ConstLabels, opts.ConstLabels,
) )
result := &counter{value: value{desc: desc, valType: CounterValue, labelPairs: desc.constLabelPairs}} result := &counter{value: value{desc: desc, valType: CounterValue, labelPairs: desc.constLabelPairs}}
result.Init(result) // Init self-collection. result.init(result) // Init self-collection.
return result return result
} }
...@@ -80,7 +74,7 @@ func (c *counter) Add(v float64) { ...@@ -80,7 +74,7 @@ func (c *counter) Add(v float64) {
// CounterVec embeds MetricVec. See there for a full list of methods with // CounterVec embeds MetricVec. See there for a full list of methods with
// detailed documentation. // detailed documentation.
type CounterVec struct { type CounterVec struct {
MetricVec *MetricVec
} }
// NewCounterVec creates a new CounterVec based on the provided CounterOpts and // NewCounterVec creates a new CounterVec based on the provided CounterOpts and
...@@ -94,20 +88,15 @@ func NewCounterVec(opts CounterOpts, labelNames []string) *CounterVec { ...@@ -94,20 +88,15 @@ func NewCounterVec(opts CounterOpts, labelNames []string) *CounterVec {
opts.ConstLabels, opts.ConstLabels,
) )
return &CounterVec{ return &CounterVec{
MetricVec: MetricVec{ MetricVec: newMetricVec(desc, func(lvs ...string) Metric {
children: map[uint64]Metric{}, result := &counter{value: value{
desc: desc, desc: desc,
hash: fnv.New64a(), valType: CounterValue,
newMetric: func(lvs ...string) Metric { labelPairs: makeLabelPairs(desc, lvs),
result := &counter{value: value{ }}
desc: desc, result.init(result) // Init self-collection.
valType: CounterValue, return result
labelPairs: makeLabelPairs(desc, lvs), }),
}}
result.Init(result) // Init self-collection.
return result
},
},
} }
} }
......
// Copyright 2016 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package prometheus package prometheus
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"hash/fnv"
"regexp"
"sort" "sort"
"strings" "strings"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/prometheus/common/model"
dto "github.com/prometheus/client_model/go" dto "github.com/prometheus/client_model/go"
) )
var (
metricNameRE = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_:]*$`)
labelNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
)
// reservedLabelPrefix is a prefix which is not legal in user-supplied // reservedLabelPrefix is a prefix which is not legal in user-supplied
// label names. // label names.
const reservedLabelPrefix = "__" const reservedLabelPrefix = "__"
...@@ -67,7 +73,7 @@ type Desc struct { ...@@ -67,7 +73,7 @@ type Desc struct {
// Help string. Each Desc with the same fqName must have the same // Help string. Each Desc with the same fqName must have the same
// dimHash. // dimHash.
dimHash uint64 dimHash uint64
// err is an error that occured during construction. It is reported on // err is an error that occurred during construction. It is reported on
// registration time. // registration time.
err error err error
} }
...@@ -92,7 +98,7 @@ func NewDesc(fqName, help string, variableLabels []string, constLabels Labels) * ...@@ -92,7 +98,7 @@ func NewDesc(fqName, help string, variableLabels []string, constLabels Labels) *
d.err = errors.New("empty help string") d.err = errors.New("empty help string")
return d return d
} }
if !metricNameRE.MatchString(fqName) { if !model.IsValidMetricName(model.LabelValue(fqName)) {
d.err = fmt.Errorf("%q is not a valid metric name", fqName) d.err = fmt.Errorf("%q is not a valid metric name", fqName)
return d return d
} }
...@@ -131,31 +137,24 @@ func NewDesc(fqName, help string, variableLabels []string, constLabels Labels) * ...@@ -131,31 +137,24 @@ func NewDesc(fqName, help string, variableLabels []string, constLabels Labels) *
d.err = errors.New("duplicate label names") d.err = errors.New("duplicate label names")
return d return d
} }
h := fnv.New64a() vh := hashNew()
var b bytes.Buffer // To copy string contents into, avoiding []byte allocations.
for _, val := range labelValues { for _, val := range labelValues {
b.Reset() vh = hashAdd(vh, val)
b.WriteString(val) vh = hashAddByte(vh, separatorByte)
b.WriteByte(separatorByte)
h.Write(b.Bytes())
} }
d.id = h.Sum64() d.id = vh
// Sort labelNames so that order doesn't matter for the hash. // Sort labelNames so that order doesn't matter for the hash.
sort.Strings(labelNames) sort.Strings(labelNames)
// Now hash together (in this order) the help string and the sorted // Now hash together (in this order) the help string and the sorted
// label names. // label names.
h.Reset() lh := hashNew()
b.Reset() lh = hashAdd(lh, help)
b.WriteString(help) lh = hashAddByte(lh, separatorByte)
b.WriteByte(separatorByte)
h.Write(b.Bytes())
for _, labelName := range labelNames { for _, labelName := range labelNames {
b.Reset() lh = hashAdd(lh, labelName)
b.WriteString(labelName) lh = hashAddByte(lh, separatorByte)
b.WriteByte(separatorByte)
h.Write(b.Bytes())
} }
d.dimHash = h.Sum64() d.dimHash = lh
d.constLabelPairs = make([]*dto.LabelPair, 0, len(constLabels)) d.constLabelPairs = make([]*dto.LabelPair, 0, len(constLabels))
for n, v := range constLabels { for n, v := range constLabels {
...@@ -196,6 +195,6 @@ func (d *Desc) String() string { ...@@ -196,6 +195,6 @@ func (d *Desc) String() string {
} }
func checkLabelName(l string) bool { func checkLabelName(l string) bool {
return labelNameRE.MatchString(l) && return model.LabelName(l).IsValid() &&
!strings.HasPrefix(l, reservedLabelPrefix) !strings.HasPrefix(l, reservedLabelPrefix)
} }
...@@ -18,21 +18,21 @@ import ( ...@@ -18,21 +18,21 @@ import (
"expvar" "expvar"
) )
// ExpvarCollector collects metrics from the expvar interface. It provides a type expvarCollector struct {
// quick way to expose numeric values that are already exported via expvar as
// Prometheus metrics. Note that the data models of expvar and Prometheus are
// fundamentally different, and that the ExpvarCollector is inherently
// slow. Thus, the ExpvarCollector is probably great for experiments and
// prototying, but you should seriously consider a more direct implementation of
// Prometheus metrics for monitoring production systems.
//
// Use NewExpvarCollector to create new instances.
type ExpvarCollector struct {
exports map[string]*Desc exports map[string]*Desc
} }
// NewExpvarCollector returns a newly allocated ExpvarCollector that still has // NewExpvarCollector returns a newly allocated expvar Collector that still has
// to be registered with the Prometheus registry. // to be registered with a Prometheus registry.
//
// An expvar Collector collects metrics from the expvar interface. It provides a
// quick way to expose numeric values that are already exported via expvar as
// Prometheus metrics. Note that the data models of expvar and Prometheus are
// fundamentally different, and that the expvar Collector is inherently slower
// than native Prometheus metrics. Thus, the expvar Collector is probably great
// for experiments and prototying, but you should seriously consider a more
// direct implementation of Prometheus metrics for monitoring production
// systems.
// //
// The exports map has the following meaning: // The exports map has the following meaning:
// //
...@@ -59,21 +59,21 @@ type ExpvarCollector struct { ...@@ -59,21 +59,21 @@ type ExpvarCollector struct {
// sample values. // sample values.
// //
// Anything that does not fit into the scheme above is silently ignored. // Anything that does not fit into the scheme above is silently ignored.
func NewExpvarCollector(exports map[string]*Desc) *ExpvarCollector { func NewExpvarCollector(exports map[string]*Desc) Collector {
return &ExpvarCollector{ return &expvarCollector{
exports: exports, exports: exports,
} }
} }
// Describe implements Collector. // Describe implements Collector.
func (e *ExpvarCollector) Describe(ch chan<- *Desc) { func (e *expvarCollector) Describe(ch chan<- *Desc) {
for _, desc := range e.exports { for _, desc := range e.exports {
ch <- desc ch <- desc
} }
} }
// Collect implements Collector. // Collect implements Collector.
func (e *ExpvarCollector) Collect(ch chan<- Metric) { func (e *expvarCollector) Collect(ch chan<- Metric) {
for name, desc := range e.exports { for name, desc := range e.exports {
var m Metric var m Metric
expVar := expvar.Get(name) expVar := expvar.Get(name)
......
package prometheus
// Inline and byte-free variant of hash/fnv's fnv64a.
const (
offset64 = 14695981039346656037
prime64 = 1099511628211
)
// hashNew initializies a new fnv64a hash value.
func hashNew() uint64 {
return offset64
}
// hashAdd adds a string to a fnv64a hash value, returning the updated hash.
func hashAdd(h uint64, s string) uint64 {
for i := 0; i < len(s); i++ {
h ^= uint64(s[i])
h *= prime64
}
return h
}
// hashAddByte adds a byte to a fnv64a hash value, returning the updated hash.
func hashAddByte(h uint64, b byte) uint64 {
h ^= uint64(b)
h *= prime64
return h
}
...@@ -13,8 +13,6 @@ ...@@ -13,8 +13,6 @@
package prometheus package prometheus
import "hash/fnv"
// Gauge is a Metric that represents a single numerical value that can // Gauge is a Metric that represents a single numerical value that can
// arbitrarily go up and down. // arbitrarily go up and down.
// //
...@@ -29,16 +27,21 @@ type Gauge interface { ...@@ -29,16 +27,21 @@ type Gauge interface {
// Set sets the Gauge to an arbitrary value. // Set sets the Gauge to an arbitrary value.
Set(float64) Set(float64)
// Inc increments the Gauge by 1. // Inc increments the Gauge by 1. Use Add to increment it by arbitrary
// values.
Inc() Inc()
// Dec decrements the Gauge by 1. // Dec decrements the Gauge by 1. Use Sub to decrement it by arbitrary
// values.
Dec() Dec()
// Add adds the given value to the Gauge. (The value can be // Add adds the given value to the Gauge. (The value can be negative,
// negative, resulting in a decrease of the Gauge.) // resulting in a decrease of the Gauge.)
Add(float64) Add(float64)
// Sub subtracts the given value from the Gauge. (The value can be // Sub subtracts the given value from the Gauge. (The value can be
// negative, resulting in an increase of the Gauge.) // negative, resulting in an increase of the Gauge.)
Sub(float64) Sub(float64)
// SetToCurrentTime sets the Gauge to the current Unix time in seconds.
SetToCurrentTime()
} }
// GaugeOpts is an alias for Opts. See there for doc comments. // GaugeOpts is an alias for Opts. See there for doc comments.
...@@ -60,7 +63,7 @@ func NewGauge(opts GaugeOpts) Gauge { ...@@ -60,7 +63,7 @@ func NewGauge(opts GaugeOpts) Gauge {
// (e.g. number of operations queued, partitioned by user and operation // (e.g. number of operations queued, partitioned by user and operation
// type). Create instances with NewGaugeVec. // type). Create instances with NewGaugeVec.
type GaugeVec struct { type GaugeVec struct {
MetricVec *MetricVec
} }
// NewGaugeVec creates a new GaugeVec based on the provided GaugeOpts and // NewGaugeVec creates a new GaugeVec based on the provided GaugeOpts and
...@@ -74,14 +77,9 @@ func NewGaugeVec(opts GaugeOpts, labelNames []string) *GaugeVec { ...@@ -74,14 +77,9 @@ func NewGaugeVec(opts GaugeOpts, labelNames []string) *GaugeVec {
opts.ConstLabels, opts.ConstLabels,
) )
return &GaugeVec{ return &GaugeVec{
MetricVec: MetricVec{ MetricVec: newMetricVec(desc, func(lvs ...string) Metric {
children: map[uint64]Metric{}, return newValue(desc, GaugeValue, 0, lvs...)
desc: desc, }),
hash: fnv.New64a(),
newMetric: func(lvs ...string) Metric {
return newValue(desc, GaugeValue, 0, lvs...)
},
},
} }
} }
......
...@@ -8,8 +8,9 @@ import ( ...@@ -8,8 +8,9 @@ import (
) )
type goCollector struct { type goCollector struct {
goroutines Gauge goroutinesDesc *Desc
gcDesc *Desc threadsDesc *Desc
gcDesc *Desc
// metrics to describe and collect // metrics to describe and collect
metrics memStatsMetrics metrics memStatsMetrics
...@@ -17,13 +18,16 @@ type goCollector struct { ...@@ -17,13 +18,16 @@ type goCollector struct {
// NewGoCollector returns a collector which exports metrics about the current // NewGoCollector returns a collector which exports metrics about the current
// go process. // go process.
func NewGoCollector() *goCollector { func NewGoCollector() Collector {
return &goCollector{ return &goCollector{
goroutines: NewGauge(GaugeOpts{ goroutinesDesc: NewDesc(
Namespace: "go", "go_goroutines",
Name: "goroutines", "Number of goroutines that currently exist.",
Help: "Number of goroutines that currently exist.", nil, nil),
}), threadsDesc: NewDesc(
"go_threads",
"Number of OS threads created",
nil, nil),
gcDesc: NewDesc( gcDesc: NewDesc(
"go_gc_duration_seconds", "go_gc_duration_seconds",
"A summary of the GC invocation durations.", "A summary of the GC invocation durations.",
...@@ -48,7 +52,7 @@ func NewGoCollector() *goCollector { ...@@ -48,7 +52,7 @@ func NewGoCollector() *goCollector {
}, { }, {
desc: NewDesc( desc: NewDesc(
memstatNamespace("sys_bytes"), memstatNamespace("sys_bytes"),
"Number of bytes obtained by system. Sum of all system allocations.", "Number of bytes obtained from system.",
nil, nil, nil, nil,
), ),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Sys) }, eval: func(ms *runtime.MemStats) float64 { return float64(ms.Sys) },
...@@ -111,12 +115,12 @@ func NewGoCollector() *goCollector { ...@@ -111,12 +115,12 @@ func NewGoCollector() *goCollector {
valType: GaugeValue, valType: GaugeValue,
}, { }, {
desc: NewDesc( desc: NewDesc(
memstatNamespace("heap_released_bytes_total"), memstatNamespace("heap_released_bytes"),
"Total number of heap bytes released to OS.", "Number of heap bytes released to OS.",
nil, nil, nil, nil,
), ),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapReleased) }, eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapReleased) },
valType: CounterValue, valType: GaugeValue,
}, { }, {
desc: NewDesc( desc: NewDesc(
memstatNamespace("heap_objects"), memstatNamespace("heap_objects"),
...@@ -211,7 +215,15 @@ func NewGoCollector() *goCollector { ...@@ -211,7 +215,15 @@ func NewGoCollector() *goCollector {
"Number of seconds since 1970 of last garbage collection.", "Number of seconds since 1970 of last garbage collection.",
nil, nil, nil, nil,
), ),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.LastGC*10 ^ 9) }, eval: func(ms *runtime.MemStats) float64 { return float64(ms.LastGC) / 1e9 },
valType: GaugeValue,
}, {
desc: NewDesc(
memstatNamespace("gc_cpu_fraction"),
"The fraction of this program's available CPU time used by the GC since the program started.",
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return ms.GCCPUFraction },
valType: GaugeValue, valType: GaugeValue,
}, },
}, },
...@@ -224,9 +236,9 @@ func memstatNamespace(s string) string { ...@@ -224,9 +236,9 @@ func memstatNamespace(s string) string {
// Describe returns all descriptions of the collector. // Describe returns all descriptions of the collector.
func (c *goCollector) Describe(ch chan<- *Desc) { func (c *goCollector) Describe(ch chan<- *Desc) {
ch <- c.goroutines.Desc() ch <- c.goroutinesDesc
ch <- c.threadsDesc
ch <- c.gcDesc ch <- c.gcDesc
for _, i := range c.metrics { for _, i := range c.metrics {
ch <- i.desc ch <- i.desc
} }
...@@ -234,8 +246,9 @@ func (c *goCollector) Describe(ch chan<- *Desc) { ...@@ -234,8 +246,9 @@ func (c *goCollector) Describe(ch chan<- *Desc) {
// Collect returns the current state of all metrics of the collector. // Collect returns the current state of all metrics of the collector.
func (c *goCollector) Collect(ch chan<- Metric) { func (c *goCollector) Collect(ch chan<- Metric) {
c.goroutines.Set(float64(runtime.NumGoroutine())) ch <- MustNewConstMetric(c.goroutinesDesc, GaugeValue, float64(runtime.NumGoroutine()))
ch <- c.goroutines n, _ := runtime.ThreadCreateProfile(nil)
ch <- MustNewConstMetric(c.threadsDesc, GaugeValue, float64(n))
var stats debug.GCStats var stats debug.GCStats
stats.PauseQuantiles = make([]time.Duration, 5) stats.PauseQuantiles = make([]time.Duration, 5)
......
...@@ -15,7 +15,6 @@ package prometheus ...@@ -15,7 +15,6 @@ package prometheus
import ( import (
"fmt" "fmt"
"hash/fnv"
"math" "math"
"sort" "sort"
"sync/atomic" "sync/atomic"
...@@ -52,11 +51,11 @@ type Histogram interface { ...@@ -52,11 +51,11 @@ type Histogram interface {
// bucket of a histogram ("le" -> "less or equal"). // bucket of a histogram ("le" -> "less or equal").
const bucketLabel = "le" const bucketLabel = "le"
// DefBuckets are the default Histogram buckets. The default buckets are
// tailored to broadly measure the response time (in seconds) of a network
// service. Most likely, however, you will be required to define buckets
// customized to your use case.
var ( var (
// DefBuckets are the default Histogram buckets. The default buckets are
// tailored to broadly measure the response time (in seconds) of a
// network service. Most likely, however, you will be required to define
// buckets customized to your use case.
DefBuckets = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10} DefBuckets = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}
errBucketLabelNotAllowed = fmt.Errorf( errBucketLabelNotAllowed = fmt.Errorf(
...@@ -211,7 +210,7 @@ func newHistogram(desc *Desc, opts HistogramOpts, labelValues ...string) Histogr ...@@ -211,7 +210,7 @@ func newHistogram(desc *Desc, opts HistogramOpts, labelValues ...string) Histogr
// Finally we know the final length of h.upperBounds and can make counts. // Finally we know the final length of h.upperBounds and can make counts.
h.counts = make([]uint64, len(h.upperBounds)) h.counts = make([]uint64, len(h.upperBounds))
h.Init(h) // Init self-collection. h.init(h) // Init self-collection.
return h return h
} }
...@@ -223,7 +222,7 @@ type histogram struct { ...@@ -223,7 +222,7 @@ type histogram struct {
sumBits uint64 sumBits uint64
count uint64 count uint64
SelfCollector selfCollector
// Note that there is no mutex required. // Note that there is no mutex required.
desc *Desc desc *Desc
...@@ -288,7 +287,7 @@ func (h *histogram) Write(out *dto.Metric) error { ...@@ -288,7 +287,7 @@ func (h *histogram) Write(out *dto.Metric) error {
// (e.g. HTTP request latencies, partitioned by status code and method). Create // (e.g. HTTP request latencies, partitioned by status code and method). Create
// instances with NewHistogramVec. // instances with NewHistogramVec.
type HistogramVec struct { type HistogramVec struct {
MetricVec *MetricVec
} }
// NewHistogramVec creates a new HistogramVec based on the provided HistogramOpts and // NewHistogramVec creates a new HistogramVec based on the provided HistogramOpts and
...@@ -302,35 +301,30 @@ func NewHistogramVec(opts HistogramOpts, labelNames []string) *HistogramVec { ...@@ -302,35 +301,30 @@ func NewHistogramVec(opts HistogramOpts, labelNames []string) *HistogramVec {
opts.ConstLabels, opts.ConstLabels,
) )
return &HistogramVec{ return &HistogramVec{
MetricVec: MetricVec{ MetricVec: newMetricVec(desc, func(lvs ...string) Metric {
children: map[uint64]Metric{}, return newHistogram(desc, opts, lvs...)
desc: desc, }),
hash: fnv.New64a(),
newMetric: func(lvs ...string) Metric {
return newHistogram(desc, opts, lvs...)
},
},
} }
} }
// GetMetricWithLabelValues replaces the method of the same name in // GetMetricWithLabelValues replaces the method of the same name in
// MetricVec. The difference is that this method returns a Histogram and not a // MetricVec. The difference is that this method returns an Observer and not a
// Metric so that no type conversion is required. // Metric so that no type conversion to an Observer is required.
func (m *HistogramVec) GetMetricWithLabelValues(lvs ...string) (Histogram, error) { func (m *HistogramVec) GetMetricWithLabelValues(lvs ...string) (Observer, error) {
metric, err := m.MetricVec.GetMetricWithLabelValues(lvs...) metric, err := m.MetricVec.GetMetricWithLabelValues(lvs...)
if metric != nil { if metric != nil {
return metric.(Histogram), err return metric.(Observer), err
} }
return nil, err return nil, err
} }
// GetMetricWith replaces the method of the same name in MetricVec. The // GetMetricWith replaces the method of the same name in MetricVec. The
// difference is that this method returns a Histogram and not a Metric so that no // difference is that this method returns an Observer and not a Metric so that no
// type conversion is required. // type conversion to an Observer is required.
func (m *HistogramVec) GetMetricWith(labels Labels) (Histogram, error) { func (m *HistogramVec) GetMetricWith(labels Labels) (Observer, error) {
metric, err := m.MetricVec.GetMetricWith(labels) metric, err := m.MetricVec.GetMetricWith(labels)
if metric != nil { if metric != nil {
return metric.(Histogram), err return metric.(Observer), err
} }
return nil, err return nil, err
} }
...@@ -339,15 +333,15 @@ func (m *HistogramVec) GetMetricWith(labels Labels) (Histogram, error) { ...@@ -339,15 +333,15 @@ func (m *HistogramVec) GetMetricWith(labels Labels) (Histogram, error) {
// GetMetricWithLabelValues would have returned an error. By not returning an // GetMetricWithLabelValues would have returned an error. By not returning an
// error, WithLabelValues allows shortcuts like // error, WithLabelValues allows shortcuts like
// myVec.WithLabelValues("404", "GET").Observe(42.21) // myVec.WithLabelValues("404", "GET").Observe(42.21)
func (m *HistogramVec) WithLabelValues(lvs ...string) Histogram { func (m *HistogramVec) WithLabelValues(lvs ...string) Observer {
return m.MetricVec.WithLabelValues(lvs...).(Histogram) return m.MetricVec.WithLabelValues(lvs...).(Observer)
} }
// With works as GetMetricWith, but panics where GetMetricWithLabels would have // With works as GetMetricWith, but panics where GetMetricWithLabels would have
// returned an error. By not returning an error, With allows shortcuts like // returned an error. By not returning an error, With allows shortcuts like
// myVec.With(Labels{"code": "404", "method": "GET"}).Observe(42.21) // myVec.With(Labels{"code": "404", "method": "GET"}).Observe(42.21)
func (m *HistogramVec) With(labels Labels) Histogram { func (m *HistogramVec) With(labels Labels) Observer {
return m.MetricVec.With(labels).(Histogram) return m.MetricVec.With(labels).(Observer)
} }
type constHistogram struct { type constHistogram struct {
......
...@@ -22,10 +22,8 @@ import ( ...@@ -22,10 +22,8 @@ import (
const separatorByte byte = 255 const separatorByte byte = 255
// A Metric models a single sample value with its meta data being exported to // A Metric models a single sample value with its meta data being exported to
// Prometheus. Implementers of Metric in this package inclued Gauge, Counter, // Prometheus. Implementations of Metric in this package are Gauge, Counter,
// Untyped, and Summary. Users can implement their own Metric types, but that // Histogram, Summary, and Untyped.
// should be rarely needed. See the example for SelfCollector, which is also an
// example for a user-implemented Metric.
type Metric interface { type Metric interface {
// Desc returns the descriptor for the Metric. This method idempotently // Desc returns the descriptor for the Metric. This method idempotently
// returns the same descriptor throughout the lifetime of the // returns the same descriptor throughout the lifetime of the
...@@ -36,21 +34,23 @@ type Metric interface { ...@@ -36,21 +34,23 @@ type Metric interface {
// Write encodes the Metric into a "Metric" Protocol Buffer data // Write encodes the Metric into a "Metric" Protocol Buffer data
// transmission object. // transmission object.
// //
// Implementers of custom Metric types must observe concurrency safety // Metric implementations must observe concurrency safety as reads of
// as reads of this metric may occur at any time, and any blocking // this metric may occur at any time, and any blocking occurs at the
// occurs at the expense of total performance of rendering all // expense of total performance of rendering all registered
// registered metrics. Ideally Metric implementations should support // metrics. Ideally, Metric implementations should support concurrent
// concurrent readers. // readers.
// //
// The Prometheus client library attempts to minimize memory allocations // While populating dto.Metric, it is the responsibility of the
// and will provide a pre-existing reset dto.Metric pointer. Prometheus // implementation to ensure validity of the Metric protobuf (like valid
// may recycle the dto.Metric proto message, so Metric implementations // UTF-8 strings or syntactically valid metric and label names). It is
// should just populate the provided dto.Metric and then should not keep // recommended to sort labels lexicographically. (Implementers may find
// any reference to it. // LabelPairSorter useful for that.) Callers of Write should still make
// // sure of sorting if they depend on it.
// While populating dto.Metric, labels must be sorted lexicographically.
// (Implementers may find LabelPairSorter useful for that.)
Write(*dto.Metric) error Write(*dto.Metric) error
// TODO(beorn7): The original rationale of passing in a pre-allocated
// dto.Metric protobuf to save allocations has disappeared. The
// signature of this method should be changed to "Write() (*dto.Metric,
// error)".
} }
// Opts bundles the options for creating most Metric types. Each metric // Opts bundles the options for creating most Metric types. Each metric
......
// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package prometheus
// Observer is the interface that wraps the Observe method, which is used by
// Histogram and Summary to add observations.
type Observer interface {
Observe(float64)
}
// The ObserverFunc type is an adapter to allow the use of ordinary
// functions as Observers. If f is a function with the appropriate
// signature, ObserverFunc(f) is an Observer that calls f.
//
// This adapter is usually used in connection with the Timer type, and there are
// two general use cases:
//
// The most common one is to use a Gauge as the Observer for a Timer.
// See the "Gauge" Timer example.
//
// The more advanced use case is to create a function that dynamically decides
// which Observer to use for observing the duration. See the "Complex" Timer
// example.
type ObserverFunc func(float64)
// Observe calls f(value). It implements Observer.
func (f ObserverFunc) Observe(value float64) {
f(value)
}
// ObserverVec is an interface implemented by `HistogramVec` and `SummaryVec`.
type ObserverVec interface {
GetMetricWith(Labels) (Observer, error)
GetMetricWithLabelValues(lvs ...string) (Observer, error)
With(Labels) Observer
WithLabelValues(...string) Observer
Collector
}
...@@ -19,16 +19,16 @@ type processCollector struct { ...@@ -19,16 +19,16 @@ type processCollector struct {
pid int pid int
collectFn func(chan<- Metric) collectFn func(chan<- Metric)
pidFn func() (int, error) pidFn func() (int, error)
cpuTotal Counter cpuTotal *Desc
openFDs, maxFDs Gauge openFDs, maxFDs *Desc
vsize, rss Gauge vsize, rss *Desc
startTime Gauge startTime *Desc
} }
// NewProcessCollector returns a collector which exports the current state of // NewProcessCollector returns a collector which exports the current state of
// process metrics including cpu, memory and file descriptor usage as well as // 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. // the process start time for the given process id under the given namespace.
func NewProcessCollector(pid int, namespace string) *processCollector { func NewProcessCollector(pid int, namespace string) Collector {
return NewProcessCollectorPIDFn( return NewProcessCollectorPIDFn(
func() (int, error) { return pid, nil }, func() (int, error) { return pid, nil },
namespace, namespace,
...@@ -43,41 +43,46 @@ func NewProcessCollector(pid int, namespace string) *processCollector { ...@@ -43,41 +43,46 @@ func NewProcessCollector(pid int, namespace string) *processCollector {
func NewProcessCollectorPIDFn( func NewProcessCollectorPIDFn(
pidFn func() (int, error), pidFn func() (int, error),
namespace string, namespace string,
) *processCollector { ) Collector {
ns := ""
if len(namespace) > 0 {
ns = namespace + "_"
}
c := processCollector{ c := processCollector{
pidFn: pidFn, pidFn: pidFn,
collectFn: func(chan<- Metric) {}, collectFn: func(chan<- Metric) {},
cpuTotal: NewCounter(CounterOpts{ cpuTotal: NewDesc(
Namespace: namespace, ns+"process_cpu_seconds_total",
Name: "process_cpu_seconds_total", "Total user and system CPU time spent in seconds.",
Help: "Total user and system CPU time spent in seconds.", nil, nil,
}), ),
openFDs: NewGauge(GaugeOpts{ openFDs: NewDesc(
Namespace: namespace, ns+"process_open_fds",
Name: "process_open_fds", "Number of open file descriptors.",
Help: "Number of open file descriptors.", nil, nil,
}), ),
maxFDs: NewGauge(GaugeOpts{ maxFDs: NewDesc(
Namespace: namespace, ns+"process_max_fds",
Name: "process_max_fds", "Maximum number of open file descriptors.",
Help: "Maximum number of open file descriptors.", nil, nil,
}), ),
vsize: NewGauge(GaugeOpts{ vsize: NewDesc(
Namespace: namespace, ns+"process_virtual_memory_bytes",
Name: "process_virtual_memory_bytes", "Virtual memory size in bytes.",
Help: "Virtual memory size in bytes.", nil, nil,
}), ),
rss: NewGauge(GaugeOpts{ rss: NewDesc(
Namespace: namespace, ns+"process_resident_memory_bytes",
Name: "process_resident_memory_bytes", "Resident memory size in bytes.",
Help: "Resident memory size in bytes.", nil, nil,
}), ),
startTime: NewGauge(GaugeOpts{ startTime: NewDesc(
Namespace: namespace, ns+"process_start_time_seconds",
Name: "process_start_time_seconds", "Start time of the process since unix epoch in seconds.",
Help: "Start time of the process since unix epoch in seconds.", nil, nil,
}), ),
} }
// Set up process metric collection if supported by the runtime. // Set up process metric collection if supported by the runtime.
...@@ -90,12 +95,12 @@ func NewProcessCollectorPIDFn( ...@@ -90,12 +95,12 @@ func NewProcessCollectorPIDFn(
// Describe returns all descriptions of the collector. // Describe returns all descriptions of the collector.
func (c *processCollector) Describe(ch chan<- *Desc) { func (c *processCollector) Describe(ch chan<- *Desc) {
ch <- c.cpuTotal.Desc() ch <- c.cpuTotal
ch <- c.openFDs.Desc() ch <- c.openFDs
ch <- c.maxFDs.Desc() ch <- c.maxFDs
ch <- c.vsize.Desc() ch <- c.vsize
ch <- c.rss.Desc() ch <- c.rss
ch <- c.startTime.Desc() ch <- c.startTime
} }
// Collect returns the current state of all metrics of the collector. // Collect returns the current state of all metrics of the collector.
...@@ -117,26 +122,19 @@ func (c *processCollector) processCollect(ch chan<- Metric) { ...@@ -117,26 +122,19 @@ func (c *processCollector) processCollect(ch chan<- Metric) {
} }
if stat, err := p.NewStat(); err == nil { if stat, err := p.NewStat(); err == nil {
c.cpuTotal.Set(stat.CPUTime()) ch <- MustNewConstMetric(c.cpuTotal, CounterValue, stat.CPUTime())
ch <- c.cpuTotal ch <- MustNewConstMetric(c.vsize, GaugeValue, float64(stat.VirtualMemory()))
c.vsize.Set(float64(stat.VirtualMemory())) ch <- MustNewConstMetric(c.rss, GaugeValue, float64(stat.ResidentMemory()))
ch <- c.vsize
c.rss.Set(float64(stat.ResidentMemory()))
ch <- c.rss
if startTime, err := stat.StartTime(); err == nil { if startTime, err := stat.StartTime(); err == nil {
c.startTime.Set(startTime) ch <- MustNewConstMetric(c.startTime, GaugeValue, startTime)
ch <- c.startTime
} }
} }
if fds, err := p.FileDescriptorsLen(); err == nil { if fds, err := p.FileDescriptorsLen(); err == nil {
c.openFDs.Set(float64(fds)) ch <- MustNewConstMetric(c.openFDs, GaugeValue, float64(fds))
ch <- c.openFDs
} }
if limits, err := p.NewLimits(); err == nil { if limits, err := p.NewLimits(); err == nil {
c.maxFDs.Set(float64(limits.OpenFiles)) ch <- MustNewConstMetric(c.maxFDs, GaugeValue, float64(limits.OpenFiles))
ch <- c.maxFDs
} }
} }
package(default_visibility = ["//visibility:public"])
licenses(["notice"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"delegator_1_8.go",
"http.go",
"instrument_client.go",
"instrument_client_1_8.go",
"instrument_server.go",
],
tags = ["automanaged"],
deps = [
"//vendor/github.com/prometheus/client_golang/prometheus:go_default_library",
"//vendor/github.com/prometheus/client_model/go:go_default_library",
"//vendor/github.com/prometheus/common/expfmt:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
)
// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !go1.8
package promhttp
import (
"io"
"net/http"
)
func newDelegator(w http.ResponseWriter, observeWriteHeaderFunc func(int)) delegator {
d := &responseWriterDelegator{
ResponseWriter: w,
observeWriteHeader: observeWriteHeaderFunc,
}
_, cn := w.(http.CloseNotifier)
_, fl := w.(http.Flusher)
_, hj := w.(http.Hijacker)
_, rf := w.(io.ReaderFrom)
if cn && fl && hj && rf {
return &fancyDelegator{d}
}
return d
}
// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build go1.8
package promhttp
import (
"io"
"net/http"
)
// newDelegator handles the four different methods of upgrading a
// http.ResponseWriter to delegator.
func newDelegator(w http.ResponseWriter, observeWriteHeaderFunc func(int)) delegator {
d := &responseWriterDelegator{
ResponseWriter: w,
observeWriteHeader: observeWriteHeaderFunc,
}
_, cn := w.(http.CloseNotifier)
_, fl := w.(http.Flusher)
_, hj := w.(http.Hijacker)
_, ps := w.(http.Pusher)
_, rf := w.(io.ReaderFrom)
// Check for the four most common combination of interfaces a
// http.ResponseWriter might implement.
switch {
case cn && fl && hj && rf && ps:
// All interfaces.
return &fancyPushDelegator{
fancyDelegator: &fancyDelegator{d},
p: &pushDelegator{d},
}
case cn && fl && hj && rf:
// All interfaces, except http.Pusher.
return &fancyDelegator{d}
case ps:
// Just http.Pusher.
return &pushDelegator{d}
}
return d
}
type fancyPushDelegator struct {
p *pushDelegator
*fancyDelegator
}
func (f *fancyPushDelegator) Push(target string, opts *http.PushOptions) error {
return f.p.Push(target, opts)
}
type pushDelegator struct {
*responseWriterDelegator
}
func (f *pushDelegator) Push(target string, opts *http.PushOptions) error {
return f.ResponseWriter.(http.Pusher).Push(target, opts)
}
// Copyright 2016 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package promhttp provides tooling around HTTP servers and clients.
//
// First, the package allows the creation of http.Handler instances to expose
// Prometheus metrics via HTTP. promhttp.Handler acts on the
// prometheus.DefaultGatherer. With HandlerFor, you can create a handler for a
// custom registry or anything that implements the Gatherer interface. It also
// allows the creation of handlers that act differently on errors or allow to
// log errors.
//
// Second, the package provides tooling to instrument instances of http.Handler
// via middleware. Middleware wrappers follow the naming scheme
// InstrumentHandlerX, where X describes the intended use of the middleware.
// See each function's doc comment for specific details.
//
// Finally, the package allows for an http.RoundTripper to be instrumented via
// middleware. Middleware wrappers follow the naming scheme
// InstrumentRoundTripperX, where X describes the intended use of the
// middleware. See each function's doc comment for specific details.
package promhttp
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"net/http"
"strings"
"sync"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/client_golang/prometheus"
)
const (
contentTypeHeader = "Content-Type"
contentLengthHeader = "Content-Length"
contentEncodingHeader = "Content-Encoding"
acceptEncodingHeader = "Accept-Encoding"
)
var bufPool sync.Pool
func getBuf() *bytes.Buffer {
buf := bufPool.Get()
if buf == nil {
return &bytes.Buffer{}
}
return buf.(*bytes.Buffer)
}
func giveBuf(buf *bytes.Buffer) {
buf.Reset()
bufPool.Put(buf)
}
// Handler returns an HTTP handler for the prometheus.DefaultGatherer. The
// Handler uses the default HandlerOpts, i.e. report the first error as an HTTP
// error, no error logging, and compression if requested by the client.
//
// If you want to create a Handler for the DefaultGatherer with different
// HandlerOpts, create it with HandlerFor with prometheus.DefaultGatherer and
// your desired HandlerOpts.
func Handler() http.Handler {
return HandlerFor(prometheus.DefaultGatherer, HandlerOpts{})
}
// HandlerFor returns an http.Handler for the provided Gatherer. The behavior
// of the Handler is defined by the provided HandlerOpts.
func HandlerFor(reg prometheus.Gatherer, opts HandlerOpts) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
mfs, err := reg.Gather()
if err != nil {
if opts.ErrorLog != nil {
opts.ErrorLog.Println("error gathering metrics:", err)
}
switch opts.ErrorHandling {
case PanicOnError:
panic(err)
case ContinueOnError:
if len(mfs) == 0 {
http.Error(w, "No metrics gathered, last error:\n\n"+err.Error(), http.StatusInternalServerError)
return
}
case HTTPErrorOnError:
http.Error(w, "An error has occurred during metrics gathering:\n\n"+err.Error(), http.StatusInternalServerError)
return
}
}
contentType := expfmt.Negotiate(req.Header)
buf := getBuf()
defer giveBuf(buf)
writer, encoding := decorateWriter(req, buf, opts.DisableCompression)
enc := expfmt.NewEncoder(writer, contentType)
var lastErr error
for _, mf := range mfs {
if err := enc.Encode(mf); err != nil {
lastErr = err
if opts.ErrorLog != nil {
opts.ErrorLog.Println("error encoding metric family:", err)
}
switch opts.ErrorHandling {
case PanicOnError:
panic(err)
case ContinueOnError:
// Handled later.
case HTTPErrorOnError:
http.Error(w, "An error has occurred during metrics encoding:\n\n"+err.Error(), http.StatusInternalServerError)
return
}
}
}
if closer, ok := writer.(io.Closer); ok {
closer.Close()
}
if lastErr != nil && buf.Len() == 0 {
http.Error(w, "No metrics encoded, last error:\n\n"+err.Error(), http.StatusInternalServerError)
return
}
header := w.Header()
header.Set(contentTypeHeader, string(contentType))
header.Set(contentLengthHeader, fmt.Sprint(buf.Len()))
if encoding != "" {
header.Set(contentEncodingHeader, encoding)
}
w.Write(buf.Bytes())
// TODO(beorn7): Consider streaming serving of metrics.
})
}
// HandlerErrorHandling defines how a Handler serving metrics will handle
// errors.
type HandlerErrorHandling int
// These constants cause handlers serving metrics to behave as described if
// errors are encountered.
const (
// Serve an HTTP status code 500 upon the first error
// encountered. Report the error message in the body.
HTTPErrorOnError HandlerErrorHandling = iota
// Ignore errors and try to serve as many metrics as possible. However,
// if no metrics can be served, serve an HTTP status code 500 and the
// last error message in the body. Only use this in deliberate "best
// effort" metrics collection scenarios. It is recommended to at least
// log errors (by providing an ErrorLog in HandlerOpts) to not mask
// errors completely.
ContinueOnError
// Panic upon the first error encountered (useful for "crash only" apps).
PanicOnError
)
// Logger is the minimal interface HandlerOpts needs for logging. Note that
// log.Logger from the standard library implements this interface, and it is
// easy to implement by custom loggers, if they don't do so already anyway.
type Logger interface {
Println(v ...interface{})
}
// HandlerOpts specifies options how to serve metrics via an http.Handler. The
// zero value of HandlerOpts is a reasonable default.
type HandlerOpts struct {
// ErrorLog specifies an optional logger for errors collecting and
// serving metrics. If nil, errors are not logged at all.
ErrorLog Logger
// ErrorHandling defines how errors are handled. Note that errors are
// logged regardless of the configured ErrorHandling provided ErrorLog
// is not nil.
ErrorHandling HandlerErrorHandling
// If DisableCompression is true, the handler will never compress the
// response, even if requested by the client.
DisableCompression bool
}
// decorateWriter wraps a writer to handle gzip compression if requested. It
// returns the decorated writer and the appropriate "Content-Encoding" header
// (which is empty if no compression is enabled).
func decorateWriter(request *http.Request, writer io.Writer, compressionDisabled bool) (io.Writer, string) {
if compressionDisabled {
return writer, ""
}
header := request.Header.Get(acceptEncodingHeader)
parts := strings.Split(header, ",")
for _, part := range parts {
part := strings.TrimSpace(part)
if part == "gzip" || strings.HasPrefix(part, "gzip;") {
return gzip.NewWriter(writer), "gzip"
}
}
return writer, ""
}
// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package promhttp
import (
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
)
// The RoundTripperFunc type is an adapter to allow the use of ordinary
// functions as RoundTrippers. If f is a function with the appropriate
// signature, RountTripperFunc(f) is a RoundTripper that calls f.
type RoundTripperFunc func(req *http.Request) (*http.Response, error)
// RoundTrip implements the RoundTripper interface.
func (rt RoundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return rt(r)
}
// InstrumentRoundTripperInFlight is a middleware that wraps the provided
// http.RoundTripper. It sets the provided prometheus.Gauge to the number of
// requests currently handled by the wrapped http.RoundTripper.
//
// See the example for ExampleInstrumentRoundTripperDuration for example usage.
func InstrumentRoundTripperInFlight(gauge prometheus.Gauge, next http.RoundTripper) RoundTripperFunc {
return RoundTripperFunc(func(r *http.Request) (*http.Response, error) {
gauge.Inc()
defer gauge.Dec()
return next.RoundTrip(r)
})
}
// InstrumentRoundTripperCounter is a middleware that wraps the provided
// http.RoundTripper to observe the request result with the provided CounterVec.
// The CounterVec must have zero, one, or two labels. The only allowed label
// names are "code" and "method". The function panics if any other instance
// labels are provided. Partitioning of the CounterVec happens by HTTP status
// code and/or HTTP method if the respective instance label names are present
// in the CounterVec. For unpartitioned counting, use a CounterVec with
// zero labels.
//
// If the wrapped RoundTripper panics or returns a non-nil error, the Counter
// is not incremented.
//
// See the example for ExampleInstrumentRoundTripperDuration for example usage.
func InstrumentRoundTripperCounter(counter *prometheus.CounterVec, next http.RoundTripper) RoundTripperFunc {
code, method := checkLabels(counter)
return RoundTripperFunc(func(r *http.Request) (*http.Response, error) {
resp, err := next.RoundTrip(r)
if err == nil {
counter.With(labels(code, method, r.Method, resp.StatusCode)).Inc()
}
return resp, err
})
}
// InstrumentRoundTripperDuration is a middleware that wraps the provided
// http.RoundTripper to observe the request duration with the provided ObserverVec.
// The ObserverVec must have zero, one, or two labels. The only allowed label
// names are "code" and "method". The function panics if any other instance
// labels are provided. The Observe method of the Observer in the ObserverVec
// is called with the request duration in seconds. Partitioning happens by HTTP
// status code and/or HTTP method if the respective instance label names are
// present in the ObserverVec. For unpartitioned observations, use an
// ObserverVec with zero labels. Note that partitioning of Histograms is
// expensive and should be used judiciously.
//
// If the wrapped RoundTripper panics or returns a non-nil error, no values are
// reported.
func InstrumentRoundTripperDuration(obs prometheus.ObserverVec, next http.RoundTripper) RoundTripperFunc {
code, method := checkLabels(obs)
return RoundTripperFunc(func(r *http.Request) (*http.Response, error) {
start := time.Now()
resp, err := next.RoundTrip(r)
if err == nil {
obs.With(labels(code, method, r.Method, resp.StatusCode)).Observe(time.Since(start).Seconds())
}
return resp, err
})
}
// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build go1.8
package promhttp
import (
"context"
"crypto/tls"
"net/http"
"net/http/httptrace"
"time"
)
// InstrumentTrace is used to offer flexibility in instrumenting the available
// httptrace.ClientTrace hook functions. Each function is passed a float64
// representing the time in seconds since the start of the http request. A user
// may choose to use separately buckets Histograms, or implement custom
// instance labels on a per function basis.
type InstrumentTrace struct {
GotConn func(float64)
PutIdleConn func(float64)
GotFirstResponseByte func(float64)
Got100Continue func(float64)
DNSStart func(float64)
DNSDone func(float64)
ConnectStart func(float64)
ConnectDone func(float64)
TLSHandshakeStart func(float64)
TLSHandshakeDone func(float64)
WroteHeaders func(float64)
Wait100Continue func(float64)
WroteRequest func(float64)
}
// InstrumentRoundTripperTrace is a middleware that wraps the provided
// RoundTripper and reports times to hook functions provided in the
// InstrumentTrace struct. Hook functions that are not present in the provided
// InstrumentTrace struct are ignored. Times reported to the hook functions are
// time since the start of the request. Note that partitioning of Histograms
// is expensive and should be used judiciously.
//
// For hook functions that receive an error as an argument, no observations are
// made in the event of a non-nil error value.
//
// See the example for ExampleInstrumentRoundTripperDuration for example usage.
func InstrumentRoundTripperTrace(it *InstrumentTrace, next http.RoundTripper) RoundTripperFunc {
return RoundTripperFunc(func(r *http.Request) (*http.Response, error) {
start := time.Now()
trace := &httptrace.ClientTrace{
GotConn: func(_ httptrace.GotConnInfo) {
if it.GotConn != nil {
it.GotConn(time.Since(start).Seconds())
}
},
PutIdleConn: func(err error) {
if err != nil {
return
}
if it.PutIdleConn != nil {
it.PutIdleConn(time.Since(start).Seconds())
}
},
DNSStart: func(_ httptrace.DNSStartInfo) {
if it.DNSStart != nil {
it.DNSStart(time.Since(start).Seconds())
}
},
DNSDone: func(_ httptrace.DNSDoneInfo) {
if it.DNSStart != nil {
it.DNSStart(time.Since(start).Seconds())
}
},
ConnectStart: func(_, _ string) {
if it.ConnectStart != nil {
it.ConnectStart(time.Since(start).Seconds())
}
},
ConnectDone: func(_, _ string, err error) {
if err != nil {
return
}
if it.ConnectDone != nil {
it.ConnectDone(time.Since(start).Seconds())
}
},
GotFirstResponseByte: func() {
if it.GotFirstResponseByte != nil {
it.GotFirstResponseByte(time.Since(start).Seconds())
}
},
Got100Continue: func() {
if it.Got100Continue != nil {
it.Got100Continue(time.Since(start).Seconds())
}
},
TLSHandshakeStart: func() {
if it.TLSHandshakeStart != nil {
it.TLSHandshakeStart(time.Since(start).Seconds())
}
},
TLSHandshakeDone: func(_ tls.ConnectionState, err error) {
if err != nil {
return
}
if it.TLSHandshakeDone != nil {
it.TLSHandshakeDone(time.Since(start).Seconds())
}
},
WroteHeaders: func() {
if it.WroteHeaders != nil {
it.WroteHeaders(time.Since(start).Seconds())
}
},
Wait100Continue: func() {
if it.Wait100Continue != nil {
it.Wait100Continue(time.Since(start).Seconds())
}
},
WroteRequest: func(_ httptrace.WroteRequestInfo) {
if it.WroteRequest != nil {
it.WroteRequest(time.Since(start).Seconds())
}
},
}
r = r.WithContext(httptrace.WithClientTrace(context.Background(), trace))
return next.RoundTrip(r)
})
}
// Copyright 2015 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Copyright (c) 2013, The Prometheus Authors
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
package prometheus
// Push triggers a metric collection by the default registry and pushes all
// collected metrics to the Pushgateway specified by addr. See the Pushgateway
// documentation for detailed implications of the job and instance
// parameter. instance can be left empty. You can use just host:port or ip:port
// as url, in which case 'http://' is added automatically. You can also include
// the schema in the URL. However, do not include the '/metrics/jobs/...' part.
//
// Note that all previously pushed metrics with the same job and instance will
// be replaced with the metrics pushed by this call. (It uses HTTP method 'PUT'
// to push to the Pushgateway.)
func Push(job, instance, url string) error {
return defRegistry.Push(job, instance, url, "PUT")
}
// PushAdd works like Push, but only previously pushed metrics with the same
// name (and the same job and instance) will be replaced. (It uses HTTP method
// 'POST' to push to the Pushgateway.)
func PushAdd(job, instance, url string) error {
return defRegistry.Push(job, instance, url, "POST")
}
// PushCollectors works like Push, but it does not collect from the default
// registry. Instead, it collects from the provided collectors. It is a
// convenient way to push only a few metrics.
func PushCollectors(job, instance, url string, collectors ...Collector) error {
return pushCollectors(job, instance, url, "PUT", collectors...)
}
// PushAddCollectors works like PushAdd, but it does not collect from the
// default registry. Instead, it collects from the provided collectors. It is a
// convenient way to push only a few metrics.
func PushAddCollectors(job, instance, url string, collectors ...Collector) error {
return pushCollectors(job, instance, url, "POST", collectors...)
}
func pushCollectors(job, instance, url, method string, collectors ...Collector) error {
r := newRegistry()
for _, collector := range collectors {
if _, err := r.Register(collector); err != nil {
return err
}
}
return r.Push(job, instance, url, method)
}
...@@ -15,7 +15,6 @@ package prometheus ...@@ -15,7 +15,6 @@ package prometheus
import ( import (
"fmt" "fmt"
"hash/fnv"
"math" "math"
"sort" "sort"
"sync" "sync"
...@@ -54,8 +53,11 @@ type Summary interface { ...@@ -54,8 +53,11 @@ type Summary interface {
Observe(float64) Observe(float64)
} }
// DefObjectives are the default Summary quantile values.
//
// Deprecated: DefObjectives will not be used as the default objectives in
// v0.10 of the library. The default Summary will have no quantiles then.
var ( var (
// DefObjectives are the default Summary quantile values.
DefObjectives = map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001} DefObjectives = map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}
errQuantileLabelNotAllowed = fmt.Errorf( errQuantileLabelNotAllowed = fmt.Errorf(
...@@ -114,9 +116,15 @@ type SummaryOpts struct { ...@@ -114,9 +116,15 @@ type SummaryOpts struct {
ConstLabels Labels ConstLabels Labels
// Objectives defines the quantile rank estimates with their respective // Objectives defines the quantile rank estimates with their respective
// absolute error. If Objectives[q] = e, then the value reported // absolute error. If Objectives[q] = e, then the value reported for q
// for q will be the φ-quantile value for some φ between q-e and q+e. // will be the φ-quantile value for some φ between q-e and q+e. The
// The default value is DefObjectives. // default value is DefObjectives. It is used if Objectives is left at
// its zero value (i.e. nil). To create a Summary without Objectives,
// set it to an empty map (i.e. map[float64]float64{}).
//
// Deprecated: Note that the current value of DefObjectives is
// deprecated. It will be replaced by an empty map in v0.10 of the
// library. Please explicitly set Objectives to the desired value.
Objectives map[float64]float64 Objectives map[float64]float64
// MaxAge defines the duration for which an observation stays relevant // MaxAge defines the duration for which an observation stays relevant
...@@ -140,11 +148,11 @@ type SummaryOpts struct { ...@@ -140,11 +148,11 @@ type SummaryOpts struct {
BufCap uint32 BufCap uint32
} }
// TODO: Great fuck-up with the sliding-window decay algorithm... The Merge // Great fuck-up with the sliding-window decay algorithm... The Merge method of
// method of perk/quantile is actually not working as advertised - and it might // perk/quantile is actually not working as advertised - and it might be
// be unfixable, as the underlying algorithm is apparently not capable of // unfixable, as the underlying algorithm is apparently not capable of merging
// merging summaries in the first place. To avoid using Merge, we are currently // summaries in the first place. To avoid using Merge, we are currently adding
// adding observations to _each_ age bucket, i.e. the effort to add a sample is // observations to _each_ age bucket, i.e. the effort to add a sample is
// essentially multiplied by the number of age buckets. When rotating age // essentially multiplied by the number of age buckets. When rotating age
// buckets, we empty the previous head stream. On scrape time, we simply take // buckets, we empty the previous head stream. On scrape time, we simply take
// the quantiles from the head stream (no merging required). Result: More effort // the quantiles from the head stream (no merging required). Result: More effort
...@@ -184,7 +192,7 @@ func newSummary(desc *Desc, opts SummaryOpts, labelValues ...string) Summary { ...@@ -184,7 +192,7 @@ func newSummary(desc *Desc, opts SummaryOpts, labelValues ...string) Summary {
} }
} }
if len(opts.Objectives) == 0 { if opts.Objectives == nil {
opts.Objectives = DefObjectives opts.Objectives = DefObjectives
} }
...@@ -228,12 +236,12 @@ func newSummary(desc *Desc, opts SummaryOpts, labelValues ...string) Summary { ...@@ -228,12 +236,12 @@ func newSummary(desc *Desc, opts SummaryOpts, labelValues ...string) Summary {
} }
sort.Float64s(s.sortedObjectives) sort.Float64s(s.sortedObjectives)
s.Init(s) // Init self-collection. s.init(s) // Init self-collection.
return s return s
} }
type summary struct { type summary struct {
SelfCollector selfCollector
bufMtx sync.Mutex // Protects hotBuf and hotBufExpTime. bufMtx sync.Mutex // Protects hotBuf and hotBufExpTime.
mtx sync.Mutex // Protects every other moving part. mtx sync.Mutex // Protects every other moving part.
...@@ -391,7 +399,7 @@ func (s quantSort) Less(i, j int) bool { ...@@ -391,7 +399,7 @@ func (s quantSort) Less(i, j int) bool {
// (e.g. HTTP request latencies, partitioned by status code and method). Create // (e.g. HTTP request latencies, partitioned by status code and method). Create
// instances with NewSummaryVec. // instances with NewSummaryVec.
type SummaryVec struct { type SummaryVec struct {
MetricVec *MetricVec
} }
// NewSummaryVec creates a new SummaryVec based on the provided SummaryOpts and // NewSummaryVec creates a new SummaryVec based on the provided SummaryOpts and
...@@ -405,35 +413,30 @@ func NewSummaryVec(opts SummaryOpts, labelNames []string) *SummaryVec { ...@@ -405,35 +413,30 @@ func NewSummaryVec(opts SummaryOpts, labelNames []string) *SummaryVec {
opts.ConstLabels, opts.ConstLabels,
) )
return &SummaryVec{ return &SummaryVec{
MetricVec: MetricVec{ MetricVec: newMetricVec(desc, func(lvs ...string) Metric {
children: map[uint64]Metric{}, return newSummary(desc, opts, lvs...)
desc: desc, }),
hash: fnv.New64a(),
newMetric: func(lvs ...string) Metric {
return newSummary(desc, opts, lvs...)
},
},
} }
} }
// GetMetricWithLabelValues replaces the method of the same name in // GetMetricWithLabelValues replaces the method of the same name in MetricVec.
// MetricVec. The difference is that this method returns a Summary and not a // The difference is that this method returns an Observer and not a Metric so
// Metric so that no type conversion is required. // that no type conversion to an Observer is required.
func (m *SummaryVec) GetMetricWithLabelValues(lvs ...string) (Summary, error) { func (m *SummaryVec) GetMetricWithLabelValues(lvs ...string) (Observer, error) {
metric, err := m.MetricVec.GetMetricWithLabelValues(lvs...) metric, err := m.MetricVec.GetMetricWithLabelValues(lvs...)
if metric != nil { if metric != nil {
return metric.(Summary), err return metric.(Observer), err
} }
return nil, err return nil, err
} }
// GetMetricWith replaces the method of the same name in MetricVec. The // GetMetricWith replaces the method of the same name in MetricVec. The
// difference is that this method returns a Summary and not a Metric so that no // difference is that this method returns an Observer and not a Metric so that
// type conversion is required. // no type conversion to an Observer is required.
func (m *SummaryVec) GetMetricWith(labels Labels) (Summary, error) { func (m *SummaryVec) GetMetricWith(labels Labels) (Observer, error) {
metric, err := m.MetricVec.GetMetricWith(labels) metric, err := m.MetricVec.GetMetricWith(labels)
if metric != nil { if metric != nil {
return metric.(Summary), err return metric.(Observer), err
} }
return nil, err return nil, err
} }
...@@ -442,15 +445,15 @@ func (m *SummaryVec) GetMetricWith(labels Labels) (Summary, error) { ...@@ -442,15 +445,15 @@ func (m *SummaryVec) GetMetricWith(labels Labels) (Summary, error) {
// GetMetricWithLabelValues would have returned an error. By not returning an // GetMetricWithLabelValues would have returned an error. By not returning an
// error, WithLabelValues allows shortcuts like // error, WithLabelValues allows shortcuts like
// myVec.WithLabelValues("404", "GET").Observe(42.21) // myVec.WithLabelValues("404", "GET").Observe(42.21)
func (m *SummaryVec) WithLabelValues(lvs ...string) Summary { func (m *SummaryVec) WithLabelValues(lvs ...string) Observer {
return m.MetricVec.WithLabelValues(lvs...).(Summary) return m.MetricVec.WithLabelValues(lvs...).(Observer)
} }
// With works as GetMetricWith, but panics where GetMetricWithLabels would have // With works as GetMetricWith, but panics where GetMetricWithLabels would have
// returned an error. By not returning an error, With allows shortcuts like // returned an error. By not returning an error, With allows shortcuts like
// myVec.With(Labels{"code": "404", "method": "GET"}).Observe(42.21) // myVec.With(Labels{"code": "404", "method": "GET"}).Observe(42.21)
func (m *SummaryVec) With(labels Labels) Summary { func (m *SummaryVec) With(labels Labels) Observer {
return m.MetricVec.With(labels).(Summary) return m.MetricVec.With(labels).(Observer)
} }
type constSummary struct { type constSummary struct {
......
// Copyright 2016 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package prometheus
import "time"
// Timer is a helper type to time functions. Use NewTimer to create new
// instances.
type Timer struct {
begin time.Time
observer Observer
}
// NewTimer creates a new Timer. The provided Observer is used to observe a
// duration in seconds. Timer is usually used to time a function call in the
// following way:
// func TimeMe() {
// timer := NewTimer(myHistogram)
// defer timer.ObserveDuration()
// // Do actual work.
// }
func NewTimer(o Observer) *Timer {
return &Timer{
begin: time.Now(),
observer: o,
}
}
// ObserveDuration records the duration passed since the Timer was created with
// NewTimer. It calls the Observe method of the Observer provided during
// construction with the duration in seconds as an argument. ObserveDuration is
// usually called with a defer statement.
func (t *Timer) ObserveDuration() {
if t.observer != nil {
t.observer.Observe(time.Since(t.begin).Seconds())
}
}
...@@ -13,8 +13,6 @@ ...@@ -13,8 +13,6 @@
package prometheus package prometheus
import "hash/fnv"
// Untyped is a Metric that represents a single numerical value that can // Untyped is a Metric that represents a single numerical value that can
// arbitrarily go up and down. // arbitrarily go up and down.
// //
...@@ -22,6 +20,11 @@ import "hash/fnv" ...@@ -22,6 +20,11 @@ import "hash/fnv"
// no type information is implied. // no type information is implied.
// //
// To create Untyped instances, use NewUntyped. // To create Untyped instances, use NewUntyped.
//
// Deprecated: The Untyped type is deprecated because it doesn't make sense in
// direct instrumentation. If you need to mirror an external metric of unknown
// type (usually while writing exporters), Use MustNewConstMetric to create an
// untyped metric instance on the fly.
type Untyped interface { type Untyped interface {
Metric Metric
Collector Collector
...@@ -58,7 +61,7 @@ func NewUntyped(opts UntypedOpts) Untyped { ...@@ -58,7 +61,7 @@ func NewUntyped(opts UntypedOpts) Untyped {
// labels. This is used if you want to count the same thing partitioned by // labels. This is used if you want to count the same thing partitioned by
// various dimensions. Create instances with NewUntypedVec. // various dimensions. Create instances with NewUntypedVec.
type UntypedVec struct { type UntypedVec struct {
MetricVec *MetricVec
} }
// NewUntypedVec creates a new UntypedVec based on the provided UntypedOpts and // NewUntypedVec creates a new UntypedVec based on the provided UntypedOpts and
...@@ -72,14 +75,9 @@ func NewUntypedVec(opts UntypedOpts, labelNames []string) *UntypedVec { ...@@ -72,14 +75,9 @@ func NewUntypedVec(opts UntypedOpts, labelNames []string) *UntypedVec {
opts.ConstLabels, opts.ConstLabels,
) )
return &UntypedVec{ return &UntypedVec{
MetricVec: MetricVec{ MetricVec: newMetricVec(desc, func(lvs ...string) Metric {
children: map[uint64]Metric{}, return newValue(desc, UntypedValue, 0, lvs...)
desc: desc, }),
hash: fnv.New64a(),
newMetric: func(lvs ...string) Metric {
return newValue(desc, UntypedValue, 0, lvs...)
},
},
} }
} }
......
...@@ -19,6 +19,7 @@ import ( ...@@ -19,6 +19,7 @@ import (
"math" "math"
"sort" "sort"
"sync/atomic" "sync/atomic"
"time"
dto "github.com/prometheus/client_model/go" dto "github.com/prometheus/client_model/go"
...@@ -43,12 +44,12 @@ var errInconsistentCardinality = errors.New("inconsistent label cardinality") ...@@ -43,12 +44,12 @@ var errInconsistentCardinality = errors.New("inconsistent label cardinality")
// ValueType. This is a low-level building block used by the library to back the // ValueType. This is a low-level building block used by the library to back the
// implementations of Counter, Gauge, and Untyped. // implementations of Counter, Gauge, and Untyped.
type value struct { type value struct {
// valBits containst the bits of the represented float64 value. It has // valBits contains the bits of the represented float64 value. It has
// to go first in the struct to guarantee alignment for atomic // to go first in the struct to guarantee alignment for atomic
// operations. http://golang.org/pkg/sync/atomic/#pkg-note-BUG // operations. http://golang.org/pkg/sync/atomic/#pkg-note-BUG
valBits uint64 valBits uint64
SelfCollector selfCollector
desc *Desc desc *Desc
valType ValueType valType ValueType
...@@ -68,7 +69,7 @@ func newValue(desc *Desc, valueType ValueType, val float64, labelValues ...strin ...@@ -68,7 +69,7 @@ func newValue(desc *Desc, valueType ValueType, val float64, labelValues ...strin
valBits: math.Float64bits(val), valBits: math.Float64bits(val),
labelPairs: makeLabelPairs(desc, labelValues), labelPairs: makeLabelPairs(desc, labelValues),
} }
result.Init(result) result.init(result)
return result return result
} }
...@@ -80,6 +81,10 @@ func (v *value) Set(val float64) { ...@@ -80,6 +81,10 @@ func (v *value) Set(val float64) {
atomic.StoreUint64(&v.valBits, math.Float64bits(val)) atomic.StoreUint64(&v.valBits, math.Float64bits(val))
} }
func (v *value) SetToCurrentTime() {
v.Set(float64(time.Now().UnixNano()) / 1e9)
}
func (v *value) Inc() { func (v *value) Inc() {
v.Add(1) v.Add(1)
} }
...@@ -113,7 +118,7 @@ func (v *value) Write(out *dto.Metric) error { ...@@ -113,7 +118,7 @@ func (v *value) Write(out *dto.Metric) error {
// library to back the implementations of CounterFunc, GaugeFunc, and // library to back the implementations of CounterFunc, GaugeFunc, and
// UntypedFunc. // UntypedFunc.
type valueFunc struct { type valueFunc struct {
SelfCollector selfCollector
desc *Desc desc *Desc
valType ValueType valType ValueType
...@@ -134,7 +139,7 @@ func newValueFunc(desc *Desc, valueType ValueType, function func() float64) *val ...@@ -134,7 +139,7 @@ func newValueFunc(desc *Desc, valueType ValueType, function func() float64) *val
function: function, function: function,
labelPairs: makeLabelPairs(desc, nil), labelPairs: makeLabelPairs(desc, nil),
} }
result.Init(result) result.init(result)
return result return result
} }
......
...@@ -14,10 +14,10 @@ ...@@ -14,10 +14,10 @@
package prometheus package prometheus
import ( import (
"bytes"
"fmt" "fmt"
"hash"
"sync" "sync"
"github.com/prometheus/common/model"
) )
// MetricVec is a Collector to bundle metrics of the same name that // MetricVec is a Collector to bundle metrics of the same name that
...@@ -26,17 +26,32 @@ import ( ...@@ -26,17 +26,32 @@ import (
// type. GaugeVec, CounterVec, SummaryVec, and UntypedVec are examples already // type. GaugeVec, CounterVec, SummaryVec, and UntypedVec are examples already
// provided in this package. // provided in this package.
type MetricVec struct { type MetricVec struct {
mtx sync.RWMutex // Protects not only children, but also hash and buf. mtx sync.RWMutex // Protects the children.
children map[uint64]Metric children map[uint64][]metricWithLabelValues
desc *Desc desc *Desc
// hash is our own hash instance to avoid repeated allocations. newMetric func(labelValues ...string) Metric
hash hash.Hash64 hashAdd func(h uint64, s string) uint64 // replace hash function for testing collision handling
// buf is used to copy string contents into it for hashing, hashAddByte func(h uint64, b byte) uint64
// again to avoid allocations. }
buf bytes.Buffer
// newMetricVec returns an initialized MetricVec. The concrete value is
// returned for embedding into another struct.
func newMetricVec(desc *Desc, newMetric func(lvs ...string) Metric) *MetricVec {
return &MetricVec{
children: map[uint64][]metricWithLabelValues{},
desc: desc,
newMetric: newMetric,
hashAdd: hashAdd,
hashAddByte: hashAddByte,
}
}
newMetric func(labelValues ...string) Metric // metricWithLabelValues provides the metric and its label values for
// disambiguation on hash collision.
type metricWithLabelValues struct {
values []string
metric Metric
} }
// Describe implements Collector. The length of the returned slice // Describe implements Collector. The length of the returned slice
...@@ -50,8 +65,10 @@ func (m *MetricVec) Collect(ch chan<- Metric) { ...@@ -50,8 +65,10 @@ func (m *MetricVec) Collect(ch chan<- Metric) {
m.mtx.RLock() m.mtx.RLock()
defer m.mtx.RUnlock() defer m.mtx.RUnlock()
for _, metric := range m.children { for _, metrics := range m.children {
ch <- metric for _, metric := range metrics {
ch <- metric.metric
}
} }
} }
...@@ -80,14 +97,12 @@ func (m *MetricVec) Collect(ch chan<- Metric) { ...@@ -80,14 +97,12 @@ func (m *MetricVec) Collect(ch chan<- Metric) {
// with a performance overhead (for creating and processing the Labels map). // with a performance overhead (for creating and processing the Labels map).
// See also the GaugeVec example. // See also the GaugeVec example.
func (m *MetricVec) GetMetricWithLabelValues(lvs ...string) (Metric, error) { func (m *MetricVec) GetMetricWithLabelValues(lvs ...string) (Metric, error) {
m.mtx.Lock()
defer m.mtx.Unlock()
h, err := m.hashLabelValues(lvs) h, err := m.hashLabelValues(lvs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return m.getOrCreateMetric(h, lvs...), nil
return m.getOrCreateMetricWithLabelValues(h, lvs), nil
} }
// GetMetricWith returns the Metric for the given Labels map (the label names // GetMetricWith returns the Metric for the given Labels map (the label names
...@@ -103,18 +118,12 @@ func (m *MetricVec) GetMetricWithLabelValues(lvs ...string) (Metric, error) { ...@@ -103,18 +118,12 @@ func (m *MetricVec) GetMetricWithLabelValues(lvs ...string) (Metric, error) {
// GetMetricWithLabelValues(...string). See there for pros and cons of the two // GetMetricWithLabelValues(...string). See there for pros and cons of the two
// methods. // methods.
func (m *MetricVec) GetMetricWith(labels Labels) (Metric, error) { func (m *MetricVec) GetMetricWith(labels Labels) (Metric, error) {
m.mtx.Lock()
defer m.mtx.Unlock()
h, err := m.hashLabels(labels) h, err := m.hashLabels(labels)
if err != nil { if err != nil {
return nil, err return nil, err
} }
lvs := make([]string, len(labels))
for i, label := range m.desc.variableLabels { return m.getOrCreateMetricWithLabels(h, labels), nil
lvs[i] = labels[label]
}
return m.getOrCreateMetric(h, lvs...), nil
} }
// WithLabelValues works as GetMetricWithLabelValues, but panics if an error // WithLabelValues works as GetMetricWithLabelValues, but panics if an error
...@@ -162,11 +171,7 @@ func (m *MetricVec) DeleteLabelValues(lvs ...string) bool { ...@@ -162,11 +171,7 @@ func (m *MetricVec) DeleteLabelValues(lvs ...string) bool {
if err != nil { if err != nil {
return false return false
} }
if _, has := m.children[h]; !has { return m.deleteByHashWithLabelValues(h, lvs)
return false
}
delete(m.children, h)
return true
} }
// Delete deletes the metric where the variable labels are the same as those // Delete deletes the metric where the variable labels are the same as those
...@@ -187,10 +192,50 @@ func (m *MetricVec) Delete(labels Labels) bool { ...@@ -187,10 +192,50 @@ func (m *MetricVec) Delete(labels Labels) bool {
if err != nil { if err != nil {
return false return false
} }
if _, has := m.children[h]; !has {
return m.deleteByHashWithLabels(h, labels)
}
// deleteByHashWithLabelValues removes the metric from the hash bucket h. If
// there are multiple matches in the bucket, use lvs to select a metric and
// remove only that metric.
func (m *MetricVec) deleteByHashWithLabelValues(h uint64, lvs []string) bool {
metrics, ok := m.children[h]
if !ok {
return false return false
} }
delete(m.children, h)
i := m.findMetricWithLabelValues(metrics, lvs)
if i >= len(metrics) {
return false
}
if len(metrics) > 1 {
m.children[h] = append(metrics[:i], metrics[i+1:]...)
} else {
delete(m.children, h)
}
return true
}
// deleteByHashWithLabels removes the metric from the hash bucket h. If there
// are multiple matches in the bucket, use lvs to select a metric and remove
// only that metric.
func (m *MetricVec) deleteByHashWithLabels(h uint64, labels Labels) bool {
metrics, ok := m.children[h]
if !ok {
return false
}
i := m.findMetricWithLabels(metrics, labels)
if i >= len(metrics) {
return false
}
if len(metrics) > 1 {
m.children[h] = append(metrics[:i], metrics[i+1:]...)
} else {
delete(m.children, h)
}
return true return true
} }
...@@ -208,40 +253,152 @@ func (m *MetricVec) hashLabelValues(vals []string) (uint64, error) { ...@@ -208,40 +253,152 @@ func (m *MetricVec) hashLabelValues(vals []string) (uint64, error) {
if len(vals) != len(m.desc.variableLabels) { if len(vals) != len(m.desc.variableLabels) {
return 0, errInconsistentCardinality return 0, errInconsistentCardinality
} }
m.hash.Reset() h := hashNew()
for _, val := range vals { for _, val := range vals {
m.buf.Reset() h = m.hashAdd(h, val)
m.buf.WriteString(val) h = m.hashAddByte(h, model.SeparatorByte)
m.hash.Write(m.buf.Bytes())
} }
return m.hash.Sum64(), nil return h, nil
} }
func (m *MetricVec) hashLabels(labels Labels) (uint64, error) { func (m *MetricVec) hashLabels(labels Labels) (uint64, error) {
if len(labels) != len(m.desc.variableLabels) { if len(labels) != len(m.desc.variableLabels) {
return 0, errInconsistentCardinality return 0, errInconsistentCardinality
} }
m.hash.Reset() h := hashNew()
for _, label := range m.desc.variableLabels { for _, label := range m.desc.variableLabels {
val, ok := labels[label] val, ok := labels[label]
if !ok { if !ok {
return 0, fmt.Errorf("label name %q missing in label map", label) return 0, fmt.Errorf("label name %q missing in label map", label)
} }
m.buf.Reset() h = m.hashAdd(h, val)
m.buf.WriteString(val) h = m.hashAddByte(h, model.SeparatorByte)
m.hash.Write(m.buf.Bytes())
} }
return m.hash.Sum64(), nil return h, nil
} }
func (m *MetricVec) getOrCreateMetric(hash uint64, labelValues ...string) Metric { // getOrCreateMetricWithLabelValues retrieves the metric by hash and label value
metric, ok := m.children[hash] // or creates it and returns the new one.
//
// This function holds the mutex.
func (m *MetricVec) getOrCreateMetricWithLabelValues(hash uint64, lvs []string) Metric {
m.mtx.RLock()
metric, ok := m.getMetricWithLabelValues(hash, lvs)
m.mtx.RUnlock()
if ok {
return metric
}
m.mtx.Lock()
defer m.mtx.Unlock()
metric, ok = m.getMetricWithLabelValues(hash, lvs)
if !ok { if !ok {
// Copy labelValues. Otherwise, they would be allocated even if we don't go // Copy to avoid allocation in case wo don't go down this code path.
// down this code path. copiedLVs := make([]string, len(lvs))
copiedLabelValues := append(make([]string, 0, len(labelValues)), labelValues...) copy(copiedLVs, lvs)
metric = m.newMetric(copiedLabelValues...) metric = m.newMetric(copiedLVs...)
m.children[hash] = metric m.children[hash] = append(m.children[hash], metricWithLabelValues{values: copiedLVs, metric: metric})
} }
return metric return metric
} }
// getOrCreateMetricWithLabelValues retrieves the metric by hash and label value
// or creates it and returns the new one.
//
// This function holds the mutex.
func (m *MetricVec) getOrCreateMetricWithLabels(hash uint64, labels Labels) Metric {
m.mtx.RLock()
metric, ok := m.getMetricWithLabels(hash, labels)
m.mtx.RUnlock()
if ok {
return metric
}
m.mtx.Lock()
defer m.mtx.Unlock()
metric, ok = m.getMetricWithLabels(hash, labels)
if !ok {
lvs := m.extractLabelValues(labels)
metric = m.newMetric(lvs...)
m.children[hash] = append(m.children[hash], metricWithLabelValues{values: lvs, metric: metric})
}
return metric
}
// getMetricWithLabelValues gets a metric while handling possible collisions in
// the hash space. Must be called while holding read mutex.
func (m *MetricVec) getMetricWithLabelValues(h uint64, lvs []string) (Metric, bool) {
metrics, ok := m.children[h]
if ok {
if i := m.findMetricWithLabelValues(metrics, lvs); i < len(metrics) {
return metrics[i].metric, true
}
}
return nil, false
}
// getMetricWithLabels gets a metric while handling possible collisions in
// the hash space. Must be called while holding read mutex.
func (m *MetricVec) getMetricWithLabels(h uint64, labels Labels) (Metric, bool) {
metrics, ok := m.children[h]
if ok {
if i := m.findMetricWithLabels(metrics, labels); i < len(metrics) {
return metrics[i].metric, true
}
}
return nil, false
}
// findMetricWithLabelValues returns the index of the matching metric or
// len(metrics) if not found.
func (m *MetricVec) findMetricWithLabelValues(metrics []metricWithLabelValues, lvs []string) int {
for i, metric := range metrics {
if m.matchLabelValues(metric.values, lvs) {
return i
}
}
return len(metrics)
}
// findMetricWithLabels returns the index of the matching metric or len(metrics)
// if not found.
func (m *MetricVec) findMetricWithLabels(metrics []metricWithLabelValues, labels Labels) int {
for i, metric := range metrics {
if m.matchLabels(metric.values, labels) {
return i
}
}
return len(metrics)
}
func (m *MetricVec) matchLabelValues(values []string, lvs []string) bool {
if len(values) != len(lvs) {
return false
}
for i, v := range values {
if v != lvs[i] {
return false
}
}
return true
}
func (m *MetricVec) matchLabels(values []string, labels Labels) bool {
if len(labels) != len(values) {
return false
}
for i, k := range m.desc.variableLabels {
if values[i] != labels[k] {
return false
}
}
return true
}
func (m *MetricVec) extractLabelValues(labels Labels) []string {
labelValues := make([]string, len(labels))
for i, k := range m.desc.variableLabels {
labelValues[i] = labels[k]
}
return labelValues
}
Maintainers of this repository:
* Fabian Reinartz <fabian@soundcloud.com>
The following individuals have contributed code to this repository
(listed in alphabetical order):
* Björn Rabenstein <beorn@soundcloud.com>
* Fabian Reinartz <fabian@soundcloud.com>
* Julius Volz <julius@soundcloud.com>
* Miguel Molina <hi@mvader.me>
...@@ -13,16 +13,15 @@ go_library( ...@@ -13,16 +13,15 @@ go_library(
"decode.go", "decode.go",
"encode.go", "encode.go",
"expfmt.go", "expfmt.go",
"json_decode.go",
"text_create.go", "text_create.go",
"text_parse.go", "text_parse.go",
], ],
tags = ["automanaged"], tags = ["automanaged"],
deps = [ deps = [
"//vendor/bitbucket.org/ww/goautoneg:go_default_library",
"//vendor/github.com/golang/protobuf/proto:go_default_library", "//vendor/github.com/golang/protobuf/proto:go_default_library",
"//vendor/github.com/matttproud/golang_protobuf_extensions/pbutil:go_default_library", "//vendor/github.com/matttproud/golang_protobuf_extensions/pbutil:go_default_library",
"//vendor/github.com/prometheus/client_model/go:go_default_library", "//vendor/github.com/prometheus/client_model/go:go_default_library",
"//vendor/github.com/prometheus/common/internal/bitbucket.org/ww/goautoneg:go_default_library",
"//vendor/github.com/prometheus/common/model:go_default_library", "//vendor/github.com/prometheus/common/model:go_default_library",
], ],
) )
......
...@@ -31,6 +31,7 @@ type Decoder interface { ...@@ -31,6 +31,7 @@ type Decoder interface {
Decode(*dto.MetricFamily) error Decode(*dto.MetricFamily) error
} }
// DecodeOptions contains options used by the Decoder and in sample extraction.
type DecodeOptions struct { type DecodeOptions struct {
// Timestamp is added to each value from the stream that has no explicit timestamp set. // Timestamp is added to each value from the stream that has no explicit timestamp set.
Timestamp model.Time Timestamp model.Time
...@@ -46,10 +47,7 @@ func ResponseFormat(h http.Header) Format { ...@@ -46,10 +47,7 @@ func ResponseFormat(h http.Header) Format {
return FmtUnknown return FmtUnknown
} }
const ( const textType = "text/plain"
textType = "text/plain"
jsonType = "application/json"
)
switch mediatype { switch mediatype {
case ProtoType: case ProtoType:
...@@ -66,22 +64,6 @@ func ResponseFormat(h http.Header) Format { ...@@ -66,22 +64,6 @@ func ResponseFormat(h http.Header) Format {
return FmtUnknown return FmtUnknown
} }
return FmtText return FmtText
case jsonType:
var prometheusAPIVersion string
if params["schema"] == "prometheus/telemetry" && params["version"] != "" {
prometheusAPIVersion = params["version"]
} else {
prometheusAPIVersion = h.Get("X-Prometheus-API-Version")
}
switch prometheusAPIVersion {
case "0.0.2", "":
return fmtJSON2
default:
return FmtUnknown
}
} }
return FmtUnknown return FmtUnknown
...@@ -93,8 +75,6 @@ func NewDecoder(r io.Reader, format Format) Decoder { ...@@ -93,8 +75,6 @@ func NewDecoder(r io.Reader, format Format) Decoder {
switch format { switch format {
case FmtProtoDelim: case FmtProtoDelim:
return &protoDecoder{r: r} return &protoDecoder{r: r}
case fmtJSON2:
return newJSON2Decoder(r)
} }
return &textDecoder{r: r} return &textDecoder{r: r}
} }
...@@ -107,10 +87,32 @@ type protoDecoder struct { ...@@ -107,10 +87,32 @@ type protoDecoder struct {
// Decode implements the Decoder interface. // Decode implements the Decoder interface.
func (d *protoDecoder) Decode(v *dto.MetricFamily) error { func (d *protoDecoder) Decode(v *dto.MetricFamily) error {
_, err := pbutil.ReadDelimited(d.r, v) _, err := pbutil.ReadDelimited(d.r, v)
return err if err != nil {
return err
}
if !model.IsValidMetricName(model.LabelValue(v.GetName())) {
return fmt.Errorf("invalid metric name %q", v.GetName())
}
for _, m := range v.GetMetric() {
if m == nil {
continue
}
for _, l := range m.GetLabel() {
if l == nil {
continue
}
if !model.LabelValue(l.GetValue()).IsValid() {
return fmt.Errorf("invalid label value %q", l.GetValue())
}
if !model.LabelName(l.GetName()).IsValid() {
return fmt.Errorf("invalid label name %q", l.GetName())
}
}
}
return nil
} }
// textDecoder implements the Decoder interface for the text protcol. // textDecoder implements the Decoder interface for the text protocol.
type textDecoder struct { type textDecoder struct {
r io.Reader r io.Reader
p TextParser p TextParser
...@@ -141,6 +143,8 @@ func (d *textDecoder) Decode(v *dto.MetricFamily) error { ...@@ -141,6 +143,8 @@ func (d *textDecoder) Decode(v *dto.MetricFamily) error {
return nil return nil
} }
// SampleDecoder wraps a Decoder to extract samples from the metric families
// decoded by the wrapped Decoder.
type SampleDecoder struct { type SampleDecoder struct {
Dec Decoder Dec Decoder
Opts *DecodeOptions Opts *DecodeOptions
...@@ -148,37 +152,51 @@ type SampleDecoder struct { ...@@ -148,37 +152,51 @@ type SampleDecoder struct {
f dto.MetricFamily f dto.MetricFamily
} }
// Decode calls the Decode method of the wrapped Decoder and then extracts the
// samples from the decoded MetricFamily into the provided model.Vector.
func (sd *SampleDecoder) Decode(s *model.Vector) error { func (sd *SampleDecoder) Decode(s *model.Vector) error {
if err := sd.Dec.Decode(&sd.f); err != nil { err := sd.Dec.Decode(&sd.f)
if err != nil {
return err return err
} }
*s = extractSamples(&sd.f, sd.Opts) *s, err = extractSamples(&sd.f, sd.Opts)
return nil return err
} }
// Extract samples builds a slice of samples from the provided metric families. // ExtractSamples builds a slice of samples from the provided metric
func ExtractSamples(o *DecodeOptions, fams ...*dto.MetricFamily) model.Vector { // families. If an error occurs during sample extraction, it continues to
var all model.Vector // extract from the remaining metric families. The returned error is the last
// error that has occured.
func ExtractSamples(o *DecodeOptions, fams ...*dto.MetricFamily) (model.Vector, error) {
var (
all model.Vector
lastErr error
)
for _, f := range fams { for _, f := range fams {
all = append(all, extractSamples(f, o)...) some, err := extractSamples(f, o)
if err != nil {
lastErr = err
continue
}
all = append(all, some...)
} }
return all return all, lastErr
} }
func extractSamples(f *dto.MetricFamily, o *DecodeOptions) model.Vector { func extractSamples(f *dto.MetricFamily, o *DecodeOptions) (model.Vector, error) {
switch f.GetType() { switch f.GetType() {
case dto.MetricType_COUNTER: case dto.MetricType_COUNTER:
return extractCounter(o, f) return extractCounter(o, f), nil
case dto.MetricType_GAUGE: case dto.MetricType_GAUGE:
return extractGauge(o, f) return extractGauge(o, f), nil
case dto.MetricType_SUMMARY: case dto.MetricType_SUMMARY:
return extractSummary(o, f) return extractSummary(o, f), nil
case dto.MetricType_UNTYPED: case dto.MetricType_UNTYPED:
return extractUntyped(o, f) return extractUntyped(o, f), nil
case dto.MetricType_HISTOGRAM: case dto.MetricType_HISTOGRAM:
return extractHistogram(o, f) return extractHistogram(o, f), nil
} }
panic("expfmt.extractSamples: unknown metric family type") return nil, fmt.Errorf("expfmt.extractSamples: unknown metric family type %v", f.GetType())
} }
func extractCounter(o *DecodeOptions, f *dto.MetricFamily) model.Vector { func extractCounter(o *DecodeOptions, f *dto.MetricFamily) model.Vector {
......
...@@ -18,9 +18,9 @@ import ( ...@@ -18,9 +18,9 @@ import (
"io" "io"
"net/http" "net/http"
"bitbucket.org/ww/goautoneg"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/matttproud/golang_protobuf_extensions/pbutil" "github.com/matttproud/golang_protobuf_extensions/pbutil"
"github.com/prometheus/common/internal/bitbucket.org/ww/goautoneg"
dto "github.com/prometheus/client_model/go" dto "github.com/prometheus/client_model/go"
) )
......
...@@ -11,14 +11,15 @@ ...@@ -11,14 +11,15 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// A package for reading and writing Prometheus metrics. // Package expfmt contains tools for reading and writing Prometheus metrics.
package expfmt package expfmt
// Format specifies the HTTP content type of the different wire protocols.
type Format string type Format string
// Constants to assemble the Content-Type values for the different wire protocols.
const ( const (
TextVersion = "0.0.4" TextVersion = "0.0.4"
ProtoType = `application/vnd.google.protobuf` ProtoType = `application/vnd.google.protobuf`
ProtoProtocol = `io.prometheus.client.MetricFamily` ProtoProtocol = `io.prometheus.client.MetricFamily`
ProtoFmt = ProtoType + "; proto=" + ProtoProtocol + ";" ProtoFmt = ProtoType + "; proto=" + ProtoProtocol + ";"
...@@ -29,9 +30,6 @@ const ( ...@@ -29,9 +30,6 @@ const (
FmtProtoDelim Format = ProtoFmt + ` encoding=delimited` FmtProtoDelim Format = ProtoFmt + ` encoding=delimited`
FmtProtoText Format = ProtoFmt + ` encoding=text` FmtProtoText Format = ProtoFmt + ` encoding=text`
FmtProtoCompact Format = ProtoFmt + ` encoding=compact-text` FmtProtoCompact Format = ProtoFmt + ` encoding=compact-text`
// fmtJSON2 is hidden as it is deprecated.
fmtJSON2 Format = `application/json; version=0.0.2`
) )
const ( const (
......
...@@ -20,8 +20,8 @@ import "bytes" ...@@ -20,8 +20,8 @@ import "bytes"
// Fuzz text metric parser with with github.com/dvyukov/go-fuzz: // Fuzz text metric parser with with github.com/dvyukov/go-fuzz:
// //
// go-fuzz-build github.com/prometheus/client_golang/text // go-fuzz-build github.com/prometheus/common/expfmt
// go-fuzz -bin text-fuzz.zip -workdir fuzz // go-fuzz -bin expfmt-fuzz.zip -workdir fuzz
// //
// Further input samples should go in the folder fuzz/corpus. // Further input samples should go in the folder fuzz/corpus.
func Fuzz(in []byte) int { func Fuzz(in []byte) int {
......
// Copyright 2015 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package expfmt
import (
"encoding/json"
"fmt"
"io"
"sort"
"github.com/golang/protobuf/proto"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/model"
)
type json2Decoder struct {
dec *json.Decoder
fams []*dto.MetricFamily
}
func newJSON2Decoder(r io.Reader) Decoder {
return &json2Decoder{
dec: json.NewDecoder(r),
}
}
type histogram002 struct {
Labels model.LabelSet `json:"labels"`
Values map[string]float64 `json:"value"`
}
type counter002 struct {
Labels model.LabelSet `json:"labels"`
Value float64 `json:"value"`
}
func protoLabelSet(base, ext model.LabelSet) []*dto.LabelPair {
labels := base.Clone().Merge(ext)
delete(labels, model.MetricNameLabel)
names := make([]string, 0, len(labels))
for ln := range labels {
names = append(names, string(ln))
}
sort.Strings(names)
pairs := make([]*dto.LabelPair, 0, len(labels))
for _, ln := range names {
lv := labels[model.LabelName(ln)]
pairs = append(pairs, &dto.LabelPair{
Name: proto.String(ln),
Value: proto.String(string(lv)),
})
}
return pairs
}
func (d *json2Decoder) more() error {
var entities []struct {
BaseLabels model.LabelSet `json:"baseLabels"`
Docstring string `json:"docstring"`
Metric struct {
Type string `json:"type"`
Values json.RawMessage `json:"value"`
} `json:"metric"`
}
if err := d.dec.Decode(&entities); err != nil {
return err
}
for _, e := range entities {
f := &dto.MetricFamily{
Name: proto.String(string(e.BaseLabels[model.MetricNameLabel])),
Help: proto.String(e.Docstring),
Type: dto.MetricType_UNTYPED.Enum(),
Metric: []*dto.Metric{},
}
d.fams = append(d.fams, f)
switch e.Metric.Type {
case "counter", "gauge":
var values []counter002
if err := json.Unmarshal(e.Metric.Values, &values); err != nil {
return fmt.Errorf("could not extract %s value: %s", e.Metric.Type, err)
}
for _, ctr := range values {
f.Metric = append(f.Metric, &dto.Metric{
Label: protoLabelSet(e.BaseLabels, ctr.Labels),
Untyped: &dto.Untyped{
Value: proto.Float64(ctr.Value),
},
})
}
case "histogram":
var values []histogram002
if err := json.Unmarshal(e.Metric.Values, &values); err != nil {
return fmt.Errorf("could not extract %s value: %s", e.Metric.Type, err)
}
for _, hist := range values {
quants := make([]string, 0, len(values))
for q := range hist.Values {
quants = append(quants, q)
}
sort.Strings(quants)
for _, q := range quants {
value := hist.Values[q]
// The correct label is "quantile" but to not break old expressions
// this remains "percentile"
hist.Labels["percentile"] = model.LabelValue(q)
f.Metric = append(f.Metric, &dto.Metric{
Label: protoLabelSet(e.BaseLabels, hist.Labels),
Untyped: &dto.Untyped{
Value: proto.Float64(value),
},
})
}
}
default:
return fmt.Errorf("unknown metric type %q", e.Metric.Type)
}
}
return nil
}
// Decode implements the Decoder interface.
func (d *json2Decoder) Decode(v *dto.MetricFamily) error {
if len(d.fams) == 0 {
if err := d.more(); err != nil {
return err
}
}
*v = *d.fams[0]
d.fams = d.fams[1:]
return nil
}
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
package expfmt package expfmt
import ( import (
"bytes"
"fmt" "fmt"
"io" "io"
"math" "math"
...@@ -26,9 +25,12 @@ import ( ...@@ -26,9 +25,12 @@ import (
// MetricFamilyToText converts a MetricFamily proto message into text format and // MetricFamilyToText converts a MetricFamily proto message into text format and
// writes the resulting lines to 'out'. It returns the number of bytes written // writes the resulting lines to 'out'. It returns the number of bytes written
// and any error encountered. This function does not perform checks on the // and any error encountered. The output will have the same order as the input,
// content of the metric and label names, i.e. invalid metric or label names // no further sorting is performed. Furthermore, this function assumes the input
// is already sanitized and does not perform any sanity checks. If the input
// contains duplicate metrics or invalid metric or label names, the conversion
// will result in invalid text format output. // will result in invalid text format output.
//
// This method fulfills the type 'prometheus.encoder'. // This method fulfills the type 'prometheus.encoder'.
func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (int, error) { func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (int, error) {
var written int var written int
...@@ -285,21 +287,17 @@ func labelPairsToText( ...@@ -285,21 +287,17 @@ func labelPairsToText(
return written, nil return written, nil
} }
var (
escape = strings.NewReplacer("\\", `\\`, "\n", `\n`)
escapeWithDoubleQuote = strings.NewReplacer("\\", `\\`, "\n", `\n`, "\"", `\"`)
)
// escapeString replaces '\' by '\\', new line character by '\n', and - if // escapeString replaces '\' by '\\', new line character by '\n', and - if
// includeDoubleQuote is true - '"' by '\"'. // includeDoubleQuote is true - '"' by '\"'.
func escapeString(v string, includeDoubleQuote bool) string { func escapeString(v string, includeDoubleQuote bool) string {
result := bytes.NewBuffer(make([]byte, 0, len(v))) if includeDoubleQuote {
for _, c := range v { return escapeWithDoubleQuote.Replace(v)
switch {
case c == '\\':
result.WriteString(`\\`)
case includeDoubleQuote && c == '"':
result.WriteString(`\"`)
case c == '\n':
result.WriteString(`\n`)
default:
result.WriteRune(c)
}
} }
return result.String()
return escape.Replace(v)
} }
...@@ -47,7 +47,7 @@ func (e ParseError) Error() string { ...@@ -47,7 +47,7 @@ func (e ParseError) Error() string {
} }
// TextParser is used to parse the simple and flat text-based exchange format. Its // TextParser is used to parse the simple and flat text-based exchange format. Its
// nil value is ready to use. // zero value is ready to use.
type TextParser struct { type TextParser struct {
metricFamiliesByName map[string]*dto.MetricFamily metricFamiliesByName map[string]*dto.MetricFamily
buf *bufio.Reader // Where the parsed input is read through. buf *bufio.Reader // Where the parsed input is read through.
...@@ -108,6 +108,13 @@ func (p *TextParser) TextToMetricFamilies(in io.Reader) (map[string]*dto.MetricF ...@@ -108,6 +108,13 @@ func (p *TextParser) TextToMetricFamilies(in io.Reader) (map[string]*dto.MetricF
delete(p.metricFamiliesByName, k) delete(p.metricFamiliesByName, k)
} }
} }
// If p.err is io.EOF now, we have run into a premature end of the input
// stream. Turn this error into something nicer and more
// meaningful. (io.EOF is often used as a signal for the legitimate end
// of an input stream.)
if p.err == io.EOF {
p.parseError("unexpected end of input stream")
}
return p.metricFamiliesByName, p.err return p.metricFamiliesByName, p.err
} }
......
...@@ -2,6 +2,17 @@ package(default_visibility = ["//visibility:public"]) ...@@ -2,6 +2,17 @@ package(default_visibility = ["//visibility:public"])
licenses(["notice"]) licenses(["notice"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["autoneg.go"],
tags = ["automanaged"],
)
filegroup( filegroup(
name = "package-srcs", name = "package-srcs",
srcs = glob(["**"]), srcs = glob(["**"]),
......
PACKAGE
package goautoneg
import "bitbucket.org/ww/goautoneg"
HTTP Content-Type Autonegotiation.
The functions in this package implement the behaviour specified in
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
Copyright (c) 2011, Open Knowledge Foundation Ltd.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
Neither the name of the Open Knowledge Foundation Ltd. nor the
names of its contributors may be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
FUNCTIONS
func Negotiate(header string, alternatives []string) (content_type string)
Negotiate the most appropriate content_type given the accept header
and a list of alternatives.
func ParseAccept(header string) (accept []Accept)
Parse an Accept Header string returning a sorted list
of clauses
TYPES
type Accept struct {
Type, SubType string
Q float32
Params map[string]string
}
Structure to represent a clause in an HTTP Accept Header
SUBDIRECTORIES
.hg
/*
HTTP Content-Type Autonegotiation.
The functions in this package implement the behaviour specified in
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
Copyright (c) 2011, Open Knowledge Foundation Ltd.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
Neither the name of the Open Knowledge Foundation Ltd. nor the
names of its contributors may be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package goautoneg
import (
"sort"
"strconv"
"strings"
)
// Structure to represent a clause in an HTTP Accept Header
type Accept struct {
Type, SubType string
Q float64
Params map[string]string
}
// For internal use, so that we can use the sort interface
type accept_slice []Accept
func (accept accept_slice) Len() int {
slice := []Accept(accept)
return len(slice)
}
func (accept accept_slice) Less(i, j int) bool {
slice := []Accept(accept)
ai, aj := slice[i], slice[j]
if ai.Q > aj.Q {
return true
}
if ai.Type != "*" && aj.Type == "*" {
return true
}
if ai.SubType != "*" && aj.SubType == "*" {
return true
}
return false
}
func (accept accept_slice) Swap(i, j int) {
slice := []Accept(accept)
slice[i], slice[j] = slice[j], slice[i]
}
// Parse an Accept Header string returning a sorted list
// of clauses
func ParseAccept(header string) (accept []Accept) {
parts := strings.Split(header, ",")
accept = make([]Accept, 0, len(parts))
for _, part := range parts {
part := strings.Trim(part, " ")
a := Accept{}
a.Params = make(map[string]string)
a.Q = 1.0
mrp := strings.Split(part, ";")
media_range := mrp[0]
sp := strings.Split(media_range, "/")
a.Type = strings.Trim(sp[0], " ")
switch {
case len(sp) == 1 && a.Type == "*":
a.SubType = "*"
case len(sp) == 2:
a.SubType = strings.Trim(sp[1], " ")
default:
continue
}
if len(mrp) == 1 {
accept = append(accept, a)
continue
}
for _, param := range mrp[1:] {
sp := strings.SplitN(param, "=", 2)
if len(sp) != 2 {
continue
}
token := strings.Trim(sp[0], " ")
if token == "q" {
a.Q, _ = strconv.ParseFloat(sp[1], 32)
} else {
a.Params[token] = strings.Trim(sp[1], " ")
}
}
accept = append(accept, a)
}
slice := accept_slice(accept)
sort.Sort(slice)
return
}
// Negotiate the most appropriate content_type given the accept header
// and a list of alternatives.
func Negotiate(header string, alternatives []string) (content_type string) {
asp := make([][]string, 0, len(alternatives))
for _, ctype := range alternatives {
asp = append(asp, strings.SplitN(ctype, "/", 2))
}
for _, clause := range ParseAccept(header) {
for i, ctsp := range asp {
if clause.Type == ctsp[0] && clause.SubType == ctsp[1] {
content_type = alternatives[i]
return
}
if clause.Type == ctsp[0] && clause.SubType == "*" {
content_type = alternatives[i]
return
}
if clause.Type == "*" && clause.SubType == "*" {
content_type = alternatives[i]
return
}
}
}
return
}
...@@ -12,6 +12,7 @@ go_library( ...@@ -12,6 +12,7 @@ go_library(
srcs = [ srcs = [
"alert.go", "alert.go",
"fingerprinting.go", "fingerprinting.go",
"fnv.go",
"labels.go", "labels.go",
"labelset.go", "labelset.go",
"metric.go", "metric.go",
......
...@@ -35,8 +35,9 @@ type Alert struct { ...@@ -35,8 +35,9 @@ type Alert struct {
Annotations LabelSet `json:"annotations"` Annotations LabelSet `json:"annotations"`
// The known time range for this alert. Both ends are optional. // The known time range for this alert. Both ends are optional.
StartsAt time.Time `json:"startsAt,omitempty"` StartsAt time.Time `json:"startsAt,omitempty"`
EndsAt time.Time `json:"endsAt,omitempty"` EndsAt time.Time `json:"endsAt,omitempty"`
GeneratorURL string `json:"generatorURL"`
} }
// Name returns the name of the alert. It is equivalent to the "alertname" label. // Name returns the name of the alert. It is equivalent to the "alertname" label.
...@@ -60,10 +61,16 @@ func (a *Alert) String() string { ...@@ -60,10 +61,16 @@ func (a *Alert) String() string {
// Resolved returns true iff the activity interval ended in the past. // Resolved returns true iff the activity interval ended in the past.
func (a *Alert) Resolved() bool { func (a *Alert) Resolved() bool {
return a.ResolvedAt(time.Now())
}
// ResolvedAt returns true off the activity interval ended before
// the given timestamp.
func (a *Alert) ResolvedAt(ts time.Time) bool {
if a.EndsAt.IsZero() { if a.EndsAt.IsZero() {
return false return false
} }
return !a.EndsAt.After(time.Now()) return !a.EndsAt.After(ts)
} }
// Status returns the status of the alert. // Status returns the status of the alert.
...@@ -74,6 +81,26 @@ func (a *Alert) Status() AlertStatus { ...@@ -74,6 +81,26 @@ func (a *Alert) Status() AlertStatus {
return AlertFiring return AlertFiring
} }
// Validate checks whether the alert data is inconsistent.
func (a *Alert) Validate() error {
if a.StartsAt.IsZero() {
return fmt.Errorf("start time missing")
}
if !a.EndsAt.IsZero() && a.EndsAt.Before(a.StartsAt) {
return fmt.Errorf("start time must be before end time")
}
if err := a.Labels.Validate(); err != nil {
return fmt.Errorf("invalid label set: %s", err)
}
if len(a.Labels) == 0 {
return fmt.Errorf("at least one label pair required")
}
if err := a.Annotations.Validate(); err != nil {
return fmt.Errorf("invalid annotations: %s", err)
}
return nil
}
// Alert is a list of alerts that can be sorted in chronological order. // Alert is a list of alerts that can be sorted in chronological order.
type Alerts []*Alert type Alerts []*Alert
......
// Copyright 2015 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package model
// Inline and byte-free variant of hash/fnv's fnv64a.
const (
offset64 = 14695981039346656037
prime64 = 1099511628211
)
// hashNew initializies a new fnv64a hash value.
func hashNew() uint64 {
return offset64
}
// hashAdd adds a string to a fnv64a hash value, returning the updated hash.
func hashAdd(h uint64, s string) uint64 {
for i := 0; i < len(s); i++ {
h ^= uint64(s[i])
h *= prime64
}
return h
}
// hashAddByte adds a byte to a fnv64a hash value, returning the updated hash.
func hashAddByte(h uint64, b byte) uint64 {
h ^= uint64(b)
h *= prime64
return h
}
...@@ -17,8 +17,8 @@ import ( ...@@ -17,8 +17,8 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"regexp" "regexp"
"sort"
"strings" "strings"
"unicode/utf8"
) )
const ( const (
...@@ -80,20 +80,37 @@ const ( ...@@ -80,20 +80,37 @@ const (
QuantileLabel = "quantile" QuantileLabel = "quantile"
) )
// LabelNameRE is a regular expression matching valid label names. // LabelNameRE is a regular expression matching valid label names. Note that the
// IsValid method of LabelName performs the same check but faster than a match
// with this regular expression.
var LabelNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$") var LabelNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
// A LabelName is a key for a LabelSet or Metric. It has a value associated // A LabelName is a key for a LabelSet or Metric. It has a value associated
// therewith. // therewith.
type LabelName string type LabelName string
// IsValid is true iff the label name matches the pattern of LabelNameRE. This
// method, however, does not use LabelNameRE for the check but a much faster
// hardcoded implementation.
func (ln LabelName) IsValid() bool {
if len(ln) == 0 {
return false
}
for i, b := range ln {
if !((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_' || (b >= '0' && b <= '9' && i > 0)) {
return false
}
}
return true
}
// UnmarshalYAML implements the yaml.Unmarshaler interface. // UnmarshalYAML implements the yaml.Unmarshaler interface.
func (ln *LabelName) UnmarshalYAML(unmarshal func(interface{}) error) error { func (ln *LabelName) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string var s string
if err := unmarshal(&s); err != nil { if err := unmarshal(&s); err != nil {
return err return err
} }
if !LabelNameRE.MatchString(s) { if !LabelName(s).IsValid() {
return fmt.Errorf("%q is not a valid label name", s) return fmt.Errorf("%q is not a valid label name", s)
} }
*ln = LabelName(s) *ln = LabelName(s)
...@@ -106,7 +123,7 @@ func (ln *LabelName) UnmarshalJSON(b []byte) error { ...@@ -106,7 +123,7 @@ func (ln *LabelName) UnmarshalJSON(b []byte) error {
if err := json.Unmarshal(b, &s); err != nil { if err := json.Unmarshal(b, &s); err != nil {
return err return err
} }
if !LabelNameRE.MatchString(s) { if !LabelName(s).IsValid() {
return fmt.Errorf("%q is not a valid label name", s) return fmt.Errorf("%q is not a valid label name", s)
} }
*ln = LabelName(s) *ln = LabelName(s)
...@@ -139,6 +156,11 @@ func (l LabelNames) String() string { ...@@ -139,6 +156,11 @@ func (l LabelNames) String() string {
// A LabelValue is an associated value for a LabelName. // A LabelValue is an associated value for a LabelName.
type LabelValue string type LabelValue string
// IsValid returns true iff the string is a valid UTF8.
func (lv LabelValue) IsValid() bool {
return utf8.ValidString(string(lv))
}
// LabelValues is a sortable LabelValue slice. It implements sort.Interface. // LabelValues is a sortable LabelValue slice. It implements sort.Interface.
type LabelValues []LabelValue type LabelValues []LabelValue
...@@ -147,7 +169,7 @@ func (l LabelValues) Len() int { ...@@ -147,7 +169,7 @@ func (l LabelValues) Len() int {
} }
func (l LabelValues) Less(i, j int) bool { func (l LabelValues) Less(i, j int) bool {
return sort.StringsAreSorted([]string{string(l[i]), string(l[j])}) return string(l[i]) < string(l[j])
} }
func (l LabelValues) Swap(i, j int) { func (l LabelValues) Swap(i, j int) {
......
...@@ -27,6 +27,21 @@ import ( ...@@ -27,6 +27,21 @@ import (
// match. // match.
type LabelSet map[LabelName]LabelValue type LabelSet map[LabelName]LabelValue
// Validate checks whether all names and values in the label set
// are valid.
func (ls LabelSet) Validate() error {
for ln, lv := range ls {
if !ln.IsValid() {
return fmt.Errorf("invalid name %q", ln)
}
if !lv.IsValid() {
return fmt.Errorf("invalid value %q", lv)
}
}
return nil
}
// Equal returns true iff both label sets have exactly the same key/value pairs.
func (ls LabelSet) Equal(o LabelSet) bool { func (ls LabelSet) Equal(o LabelSet) bool {
if len(ls) != len(o) { if len(ls) != len(o) {
return false return false
...@@ -90,6 +105,7 @@ func (ls LabelSet) Before(o LabelSet) bool { ...@@ -90,6 +105,7 @@ func (ls LabelSet) Before(o LabelSet) bool {
return false return false
} }
// Clone returns a copy of the label set.
func (ls LabelSet) Clone() LabelSet { func (ls LabelSet) Clone() LabelSet {
lsn := make(LabelSet, len(ls)) lsn := make(LabelSet, len(ls))
for ln, lv := range ls { for ln, lv := range ls {
...@@ -144,7 +160,7 @@ func (l *LabelSet) UnmarshalJSON(b []byte) error { ...@@ -144,7 +160,7 @@ func (l *LabelSet) UnmarshalJSON(b []byte) error {
// LabelName as a string and does not call its UnmarshalJSON method. // LabelName as a string and does not call its UnmarshalJSON method.
// Thus, we have to replicate the behavior here. // Thus, we have to replicate the behavior here.
for ln := range m { for ln := range m {
if !LabelNameRE.MatchString(string(ln)) { if !ln.IsValid() {
return fmt.Errorf("%q is not a valid label name", ln) return fmt.Errorf("%q is not a valid label name", ln)
} }
} }
......
...@@ -15,11 +15,18 @@ package model ...@@ -15,11 +15,18 @@ package model
import ( import (
"fmt" "fmt"
"regexp"
"sort" "sort"
"strings" "strings"
) )
var separator = []byte{0} var (
separator = []byte{0}
// MetricNameRE is a regular expression matching valid metric
// names. Note that the IsValidMetricName function performs the same
// check but faster than a match with this regular expression.
MetricNameRE = regexp.MustCompile(`^[a-zA-Z_:][a-zA-Z0-9_:]*$`)
)
// A Metric is similar to a LabelSet, but the key difference is that a Metric is // A Metric is similar to a LabelSet, but the key difference is that a Metric is
// a singleton and refers to one and only one stream of samples. // a singleton and refers to one and only one stream of samples.
...@@ -37,7 +44,7 @@ func (m Metric) Before(o Metric) bool { ...@@ -37,7 +44,7 @@ func (m Metric) Before(o Metric) bool {
// Clone returns a copy of the Metric. // Clone returns a copy of the Metric.
func (m Metric) Clone() Metric { func (m Metric) Clone() Metric {
clone := Metric{} clone := make(Metric, len(m))
for k, v := range m { for k, v := range m {
clone[k] = v clone[k] = v
} }
...@@ -79,3 +86,18 @@ func (m Metric) Fingerprint() Fingerprint { ...@@ -79,3 +86,18 @@ func (m Metric) Fingerprint() Fingerprint {
func (m Metric) FastFingerprint() Fingerprint { func (m Metric) FastFingerprint() Fingerprint {
return LabelSet(m).FastFingerprint() return LabelSet(m).FastFingerprint()
} }
// IsValidMetricName returns true iff name matches the pattern of MetricNameRE.
// This function, however, does not use MetricNameRE for the check but a much
// faster hardcoded implementation.
func IsValidMetricName(n LabelValue) bool {
if len(n) == 0 {
return false
}
for i, b := range n {
if !((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_' || b == ':' || (b >= '0' && b <= '9' && i > 0)) {
return false
}
}
return true
}
...@@ -12,5 +12,5 @@ ...@@ -12,5 +12,5 @@
// limitations under the License. // limitations under the License.
// Package model contains common data structures that are shared across // Package model contains common data structures that are shared across
// Prometheus componenets and libraries. // Prometheus components and libraries.
package model package model
...@@ -14,11 +14,7 @@ ...@@ -14,11 +14,7 @@
package model package model
import ( import (
"bytes"
"hash"
"hash/fnv"
"sort" "sort"
"sync"
) )
// SeparatorByte is a byte that cannot occur in valid UTF-8 sequences and is // SeparatorByte is a byte that cannot occur in valid UTF-8 sequences and is
...@@ -28,30 +24,9 @@ const SeparatorByte byte = 255 ...@@ -28,30 +24,9 @@ const SeparatorByte byte = 255
var ( var (
// cache the signature of an empty label set. // cache the signature of an empty label set.
emptyLabelSignature = fnv.New64a().Sum64() emptyLabelSignature = hashNew()
hashAndBufPool sync.Pool
) )
type hashAndBuf struct {
h hash.Hash64
b bytes.Buffer
}
func getHashAndBuf() *hashAndBuf {
hb := hashAndBufPool.Get()
if hb == nil {
return &hashAndBuf{h: fnv.New64a()}
}
return hb.(*hashAndBuf)
}
func putHashAndBuf(hb *hashAndBuf) {
hb.h.Reset()
hb.b.Reset()
hashAndBufPool.Put(hb)
}
// LabelsToSignature returns a quasi-unique signature (i.e., fingerprint) for a // LabelsToSignature returns a quasi-unique signature (i.e., fingerprint) for a
// given label set. (Collisions are possible but unlikely if the number of label // given label set. (Collisions are possible but unlikely if the number of label
// sets the function is applied to is small.) // sets the function is applied to is small.)
...@@ -66,18 +41,14 @@ func LabelsToSignature(labels map[string]string) uint64 { ...@@ -66,18 +41,14 @@ func LabelsToSignature(labels map[string]string) uint64 {
} }
sort.Strings(labelNames) sort.Strings(labelNames)
hb := getHashAndBuf() sum := hashNew()
defer putHashAndBuf(hb)
for _, labelName := range labelNames { for _, labelName := range labelNames {
hb.b.WriteString(labelName) sum = hashAdd(sum, labelName)
hb.b.WriteByte(SeparatorByte) sum = hashAddByte(sum, SeparatorByte)
hb.b.WriteString(labels[labelName]) sum = hashAdd(sum, labels[labelName])
hb.b.WriteByte(SeparatorByte) sum = hashAddByte(sum, SeparatorByte)
hb.h.Write(hb.b.Bytes())
hb.b.Reset()
} }
return hb.h.Sum64() return sum
} }
// labelSetToFingerprint works exactly as LabelsToSignature but takes a LabelSet as // labelSetToFingerprint works exactly as LabelsToSignature but takes a LabelSet as
...@@ -93,18 +64,14 @@ func labelSetToFingerprint(ls LabelSet) Fingerprint { ...@@ -93,18 +64,14 @@ func labelSetToFingerprint(ls LabelSet) Fingerprint {
} }
sort.Sort(labelNames) sort.Sort(labelNames)
hb := getHashAndBuf() sum := hashNew()
defer putHashAndBuf(hb)
for _, labelName := range labelNames { for _, labelName := range labelNames {
hb.b.WriteString(string(labelName)) sum = hashAdd(sum, string(labelName))
hb.b.WriteByte(SeparatorByte) sum = hashAddByte(sum, SeparatorByte)
hb.b.WriteString(string(ls[labelName])) sum = hashAdd(sum, string(ls[labelName]))
hb.b.WriteByte(SeparatorByte) sum = hashAddByte(sum, SeparatorByte)
hb.h.Write(hb.b.Bytes())
hb.b.Reset()
} }
return Fingerprint(hb.h.Sum64()) return Fingerprint(sum)
} }
// labelSetToFastFingerprint works similar to labelSetToFingerprint but uses a // labelSetToFastFingerprint works similar to labelSetToFingerprint but uses a
...@@ -116,17 +83,12 @@ func labelSetToFastFingerprint(ls LabelSet) Fingerprint { ...@@ -116,17 +83,12 @@ func labelSetToFastFingerprint(ls LabelSet) Fingerprint {
} }
var result uint64 var result uint64
hb := getHashAndBuf()
defer putHashAndBuf(hb)
for labelName, labelValue := range ls { for labelName, labelValue := range ls {
hb.b.WriteString(string(labelName)) sum := hashNew()
hb.b.WriteByte(SeparatorByte) sum = hashAdd(sum, string(labelName))
hb.b.WriteString(string(labelValue)) sum = hashAddByte(sum, SeparatorByte)
hb.h.Write(hb.b.Bytes()) sum = hashAdd(sum, string(labelValue))
result ^= hb.h.Sum64() result ^= sum
hb.h.Reset()
hb.b.Reset()
} }
return Fingerprint(result) return Fingerprint(result)
} }
...@@ -136,24 +98,20 @@ func labelSetToFastFingerprint(ls LabelSet) Fingerprint { ...@@ -136,24 +98,20 @@ func labelSetToFastFingerprint(ls LabelSet) Fingerprint {
// specified LabelNames into the signature calculation. The labels passed in // specified LabelNames into the signature calculation. The labels passed in
// will be sorted by this function. // will be sorted by this function.
func SignatureForLabels(m Metric, labels ...LabelName) uint64 { func SignatureForLabels(m Metric, labels ...LabelName) uint64 {
if len(m) == 0 || len(labels) == 0 { if len(labels) == 0 {
return emptyLabelSignature return emptyLabelSignature
} }
sort.Sort(LabelNames(labels)) sort.Sort(LabelNames(labels))
hb := getHashAndBuf() sum := hashNew()
defer putHashAndBuf(hb)
for _, label := range labels { for _, label := range labels {
hb.b.WriteString(string(label)) sum = hashAdd(sum, string(label))
hb.b.WriteByte(SeparatorByte) sum = hashAddByte(sum, SeparatorByte)
hb.b.WriteString(string(m[label])) sum = hashAdd(sum, string(m[label]))
hb.b.WriteByte(SeparatorByte) sum = hashAddByte(sum, SeparatorByte)
hb.h.Write(hb.b.Bytes())
hb.b.Reset()
} }
return hb.h.Sum64() return sum
} }
// SignatureWithoutLabels works like LabelsToSignature but takes a Metric as // SignatureWithoutLabels works like LabelsToSignature but takes a Metric as
...@@ -175,16 +133,12 @@ func SignatureWithoutLabels(m Metric, labels map[LabelName]struct{}) uint64 { ...@@ -175,16 +133,12 @@ func SignatureWithoutLabels(m Metric, labels map[LabelName]struct{}) uint64 {
} }
sort.Sort(labelNames) sort.Sort(labelNames)
hb := getHashAndBuf() sum := hashNew()
defer putHashAndBuf(hb)
for _, labelName := range labelNames { for _, labelName := range labelNames {
hb.b.WriteString(string(labelName)) sum = hashAdd(sum, string(labelName))
hb.b.WriteByte(SeparatorByte) sum = hashAddByte(sum, SeparatorByte)
hb.b.WriteString(string(m[labelName])) sum = hashAdd(sum, string(m[labelName]))
hb.b.WriteByte(SeparatorByte) sum = hashAddByte(sum, SeparatorByte)
hb.h.Write(hb.b.Bytes())
hb.b.Reset()
} }
return hb.h.Sum64() return sum
} }
...@@ -44,6 +44,21 @@ func (m *Matcher) UnmarshalJSON(b []byte) error { ...@@ -44,6 +44,21 @@ func (m *Matcher) UnmarshalJSON(b []byte) error {
return nil return nil
} }
// Validate returns true iff all fields of the matcher have valid values.
func (m *Matcher) Validate() error {
if !m.Name.IsValid() {
return fmt.Errorf("invalid name %q", m.Name)
}
if m.IsRegex {
if _, err := regexp.Compile(m.Value); err != nil {
return fmt.Errorf("invalid regular expression %q", m.Value)
}
} else if !LabelValue(m.Value).IsValid() || len(m.Value) == 0 {
return fmt.Errorf("invalid value %q", m.Value)
}
return nil
}
// Silence defines the representation of a silence definiton // Silence defines the representation of a silence definiton
// in the Prometheus eco-system. // in the Prometheus eco-system.
type Silence struct { type Silence struct {
...@@ -58,3 +73,34 @@ type Silence struct { ...@@ -58,3 +73,34 @@ type Silence struct {
CreatedBy string `json:"createdBy"` CreatedBy string `json:"createdBy"`
Comment string `json:"comment,omitempty"` Comment string `json:"comment,omitempty"`
} }
// Validate returns true iff all fields of the silence have valid values.
func (s *Silence) Validate() error {
if len(s.Matchers) == 0 {
return fmt.Errorf("at least one matcher required")
}
for _, m := range s.Matchers {
if err := m.Validate(); err != nil {
return fmt.Errorf("invalid matcher: %s", err)
}
}
if s.StartsAt.IsZero() {
return fmt.Errorf("start time missing")
}
if s.EndsAt.IsZero() {
return fmt.Errorf("end time missing")
}
if s.EndsAt.Before(s.StartsAt) {
return fmt.Errorf("start time must be before end time")
}
if s.CreatedBy == "" {
return fmt.Errorf("creator information missing")
}
if s.Comment == "" {
return fmt.Errorf("comment missing")
}
if s.CreatedAt.IsZero() {
return fmt.Errorf("creation timestamp missing")
}
return nil
}
...@@ -163,51 +163,70 @@ func (t *Time) UnmarshalJSON(b []byte) error { ...@@ -163,51 +163,70 @@ func (t *Time) UnmarshalJSON(b []byte) error {
// This type should not propagate beyond the scope of input/output processing. // This type should not propagate beyond the scope of input/output processing.
type Duration time.Duration type Duration time.Duration
var durationRE = regexp.MustCompile("^([0-9]+)(y|w|d|h|m|s|ms)$")
// StringToDuration parses a string into a time.Duration, assuming that a year // StringToDuration parses a string into a time.Duration, assuming that a year
// a day always has 24h. // always has 365d, a week always has 7d, and a day always has 24h.
func ParseDuration(durationStr string) (Duration, error) { func ParseDuration(durationStr string) (Duration, error) {
matches := durationRE.FindStringSubmatch(durationStr) matches := durationRE.FindStringSubmatch(durationStr)
if len(matches) != 3 { if len(matches) != 3 {
return 0, fmt.Errorf("not a valid duration string: %q", durationStr) return 0, fmt.Errorf("not a valid duration string: %q", durationStr)
} }
durSeconds, _ := strconv.Atoi(matches[1]) var (
dur := time.Duration(durSeconds) * time.Second n, _ = strconv.Atoi(matches[1])
unit := matches[2] dur = time.Duration(n) * time.Millisecond
switch unit { )
switch unit := matches[2]; unit {
case "y":
dur *= 1000 * 60 * 60 * 24 * 365
case "w":
dur *= 1000 * 60 * 60 * 24 * 7
case "d": case "d":
dur *= 60 * 60 * 24 dur *= 1000 * 60 * 60 * 24
case "h": case "h":
dur *= 60 * 60 dur *= 1000 * 60 * 60
case "m": case "m":
dur *= 60 dur *= 1000 * 60
case "s": case "s":
dur *= 1 dur *= 1000
case "ms":
// Value already correct
default: default:
return 0, fmt.Errorf("invalid time unit in duration string: %q", unit) return 0, fmt.Errorf("invalid time unit in duration string: %q", unit)
} }
return Duration(dur), nil return Duration(dur), nil
} }
var durationRE = regexp.MustCompile("^([0-9]+)([ywdhms]+)$")
func (d Duration) String() string { func (d Duration) String() string {
seconds := int64(time.Duration(d) / time.Second) var (
ms = int64(time.Duration(d) / time.Millisecond)
unit = "ms"
)
factors := map[string]int64{ factors := map[string]int64{
"d": 60 * 60 * 24, "y": 1000 * 60 * 60 * 24 * 365,
"h": 60 * 60, "w": 1000 * 60 * 60 * 24 * 7,
"m": 60, "d": 1000 * 60 * 60 * 24,
"s": 1, "h": 1000 * 60 * 60,
"m": 1000 * 60,
"s": 1000,
"ms": 1,
} }
unit := "s"
switch int64(0) { switch int64(0) {
case seconds % factors["d"]: case ms % factors["y"]:
unit = "y"
case ms % factors["w"]:
unit = "w"
case ms % factors["d"]:
unit = "d" unit = "d"
case seconds % factors["h"]: case ms % factors["h"]:
unit = "h" unit = "h"
case seconds % factors["m"]: case ms % factors["m"]:
unit = "m" unit = "m"
case ms % factors["s"]:
unit = "s"
} }
return fmt.Sprintf("%v%v", seconds/factors[unit], unit) return fmt.Sprintf("%v%v", ms/factors[unit], unit)
} }
// MarshalYAML implements the yaml.Marshaler interface. // MarshalYAML implements the yaml.Marshaler interface.
......
...@@ -16,11 +16,28 @@ package model ...@@ -16,11 +16,28 @@ package model
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"math"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
) )
var (
// ZeroSamplePair is the pseudo zero-value of SamplePair used to signal a
// non-existing sample pair. It is a SamplePair with timestamp Earliest and
// value 0.0. Note that the natural zero value of SamplePair has a timestamp
// of 0, which is possible to appear in a real SamplePair and thus not
// suitable to signal a non-existing SamplePair.
ZeroSamplePair = SamplePair{Timestamp: Earliest}
// ZeroSample is the pseudo zero-value of Sample used to signal a
// non-existing sample. It is a Sample with timestamp Earliest, value 0.0,
// and metric nil. Note that the natural zero value of Sample has a timestamp
// of 0, which is possible to appear in a real Sample and thus not suitable
// to signal a non-existing Sample.
ZeroSample = Sample{Timestamp: Earliest}
)
// A SampleValue is a representation of a value for a given sample at a given // A SampleValue is a representation of a value for a given sample at a given
// time. // time.
type SampleValue float64 type SampleValue float64
...@@ -43,8 +60,14 @@ func (v *SampleValue) UnmarshalJSON(b []byte) error { ...@@ -43,8 +60,14 @@ func (v *SampleValue) UnmarshalJSON(b []byte) error {
return nil return nil
} }
// Equal returns true if the value of v and o is equal or if both are NaN. Note
// that v==o is false if both are NaN. If you want the conventional float
// behavior, use == to compare two SampleValues.
func (v SampleValue) Equal(o SampleValue) bool { func (v SampleValue) Equal(o SampleValue) bool {
return v == o if v == o {
return true
}
return math.IsNaN(float64(v)) && math.IsNaN(float64(o))
} }
func (v SampleValue) String() string { func (v SampleValue) String() string {
...@@ -77,9 +100,9 @@ func (s *SamplePair) UnmarshalJSON(b []byte) error { ...@@ -77,9 +100,9 @@ func (s *SamplePair) UnmarshalJSON(b []byte) error {
} }
// Equal returns true if this SamplePair and o have equal Values and equal // Equal returns true if this SamplePair and o have equal Values and equal
// Timestamps. // Timestamps. The sematics of Value equality is defined by SampleValue.Equal.
func (s *SamplePair) Equal(o *SamplePair) bool { func (s *SamplePair) Equal(o *SamplePair) bool {
return s == o || (s.Value == o.Value && s.Timestamp.Equal(o.Timestamp)) return s == o || (s.Value.Equal(o.Value) && s.Timestamp.Equal(o.Timestamp))
} }
func (s SamplePair) String() string { func (s SamplePair) String() string {
...@@ -93,7 +116,8 @@ type Sample struct { ...@@ -93,7 +116,8 @@ type Sample struct {
Timestamp Time `json:"timestamp"` Timestamp Time `json:"timestamp"`
} }
// Equal compares first the metrics, then the timestamp, then the value. // Equal compares first the metrics, then the timestamp, then the value. The
// sematics of value equality is defined by SampleValue.Equal.
func (s *Sample) Equal(o *Sample) bool { func (s *Sample) Equal(o *Sample) bool {
if s == o { if s == o {
return true return true
...@@ -105,11 +129,8 @@ func (s *Sample) Equal(o *Sample) bool { ...@@ -105,11 +129,8 @@ func (s *Sample) Equal(o *Sample) bool {
if !s.Timestamp.Equal(o.Timestamp) { if !s.Timestamp.Equal(o.Timestamp) {
return false return false
} }
if s.Value != o.Value {
return false
}
return true return s.Value.Equal(o.Value)
} }
func (s Sample) String() string { func (s Sample) String() string {
......
sudo: false sudo: false
language: go language: go
go: go:
- 1.3 - 1.6.4
- 1.4 - 1.7.4
- 1.5
- tip
The Prometheus project was started by Matt T. Proud (emeritus) and
Julius Volz in 2012.
Maintainers of this repository:
* Tobias Schmidt <ts@soundcloud.com>
The following individuals have contributed code to this repository
(listed in alphabetical order):
* Armen Baghumian <abaghumian@noggin.com.au>
* Bjoern Rabenstein <beorn@soundcloud.com>
* David Cournapeau <cournape@gmail.com>
* Ji-Hoon, Seol <jihoon.seol@gmail.com>
* Jonas Große Sundrup <cherti@letopolis.de>
* Julius Volz <julius@soundcloud.com>
* Matthias Rampke <mr@soundcloud.com>
* Nicky Gerritsen <nicky@streamone.nl>
* Rémi Audebert <contact@halfr.net>
* Tobias Schmidt <tobidt@gmail.com>
...@@ -10,17 +10,21 @@ load( ...@@ -10,17 +10,21 @@ load(
go_library( go_library(
name = "go_default_library", name = "go_default_library",
srcs = [ srcs = [
"buddyinfo.go",
"doc.go", "doc.go",
"fs.go", "fs.go",
"ipvs.go", "ipvs.go",
"mdstat.go", "mdstat.go",
"mountstats.go",
"proc.go", "proc.go",
"proc_io.go", "proc_io.go",
"proc_limits.go", "proc_limits.go",
"proc_stat.go", "proc_stat.go",
"stat.go", "stat.go",
"xfrm.go",
], ],
tags = ["automanaged"], tags = ["automanaged"],
deps = ["//vendor/github.com/prometheus/procfs/xfs:go_default_library"],
) )
filegroup( filegroup(
...@@ -32,6 +36,9 @@ filegroup( ...@@ -32,6 +36,9 @@ filegroup(
filegroup( filegroup(
name = "all-srcs", name = "all-srcs",
srcs = [":package-srcs"], srcs = [
":package-srcs",
"//vendor/github.com/prometheus/procfs/xfs:all-srcs",
],
tags = ["automanaged"], tags = ["automanaged"],
) )
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
Prometheus uses GitHub to manage reviews of pull requests. Prometheus uses GitHub to manage reviews of pull requests.
* If you have a trivial fix or improvement, go ahead and create a pull * If you have a trivial fix or improvement, go ahead and create a pull request,
request, addressing (with `@...`) one or more of the maintainers addressing (with `@...`) the maintainer of this repository (see
(see [AUTHORS.md](AUTHORS.md)) in the description of the pull request. [MAINTAINERS.md](MAINTAINERS.md)) in the description of the pull request.
* If you plan to do something more involved, first discuss your ideas * If you plan to do something more involved, first discuss your ideas
on our [mailing list](https://groups.google.com/forum/?fromgroups#!forum/prometheus-developers). on our [mailing list](https://groups.google.com/forum/?fromgroups#!forum/prometheus-developers).
......
* Tobias Schmidt <tobidt@gmail.com>
ci: ci:
go fmt ! gofmt -l *.go | read nothing
go vet go vet
go test -v ./... go test -v ./...
go get github.com/golang/lint/golint go get github.com/golang/lint/golint
......
...@@ -8,3 +8,4 @@ backwards-incompatible ways without warnings. Use it at your own risk. ...@@ -8,3 +8,4 @@ backwards-incompatible ways without warnings. Use it at your own risk.
[![GoDoc](https://godoc.org/github.com/prometheus/procfs?status.png)](https://godoc.org/github.com/prometheus/procfs) [![GoDoc](https://godoc.org/github.com/prometheus/procfs?status.png)](https://godoc.org/github.com/prometheus/procfs)
[![Build Status](https://travis-ci.org/prometheus/procfs.svg?branch=master)](https://travis-ci.org/prometheus/procfs) [![Build Status](https://travis-ci.org/prometheus/procfs.svg?branch=master)](https://travis-ci.org/prometheus/procfs)
[![Go Report Card](https://goreportcard.com/badge/github.com/prometheus/procfs)](https://goreportcard.com/report/github.com/prometheus/procfs)
// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package procfs
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
// A BuddyInfo is the details parsed from /proc/buddyinfo.
// The data is comprised of an array of free fragments of each size.
// The sizes are 2^n*PAGE_SIZE, where n is the array index.
type BuddyInfo struct {
Node string
Zone string
Sizes []float64
}
// NewBuddyInfo reads the buddyinfo statistics.
func NewBuddyInfo() ([]BuddyInfo, error) {
fs, err := NewFS(DefaultMountPoint)
if err != nil {
return nil, err
}
return fs.NewBuddyInfo()
}
// NewBuddyInfo reads the buddyinfo statistics from the specified `proc` filesystem.
func (fs FS) NewBuddyInfo() ([]BuddyInfo, error) {
file, err := os.Open(fs.Path("buddyinfo"))
if err != nil {
return nil, err
}
defer file.Close()
return parseBuddyInfo(file)
}
func parseBuddyInfo(r io.Reader) ([]BuddyInfo, error) {
var (
buddyInfo = []BuddyInfo{}
scanner = bufio.NewScanner(r)
bucketCount = -1
)
for scanner.Scan() {
var err error
line := scanner.Text()
parts := strings.Fields(string(line))
if len(parts) < 4 {
return nil, fmt.Errorf("invalid number of fields when parsing buddyinfo")
}
node := strings.TrimRight(parts[1], ",")
zone := strings.TrimRight(parts[3], ",")
arraySize := len(parts[4:])
if bucketCount == -1 {
bucketCount = arraySize
} else {
if bucketCount != arraySize {
return nil, fmt.Errorf("mismatch in number of buddyinfo buckets, previous count %d, new count %d", bucketCount, arraySize)
}
}
sizes := make([]float64, arraySize)
for i := 0; i < arraySize; i++ {
sizes[i], err = strconv.ParseFloat(parts[i+4], 64)
if err != nil {
return nil, fmt.Errorf("invalid value in buddyinfo: %s", err)
}
}
buddyInfo = append(buddyInfo, BuddyInfo{node, zone, sizes})
}
return buddyInfo, scanner.Err()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment