github.com/timstclair/heapster@v0.20.0-alpha1/metrics/core/metrics.go (about)

     1  // Copyright 2015 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package core
    16  
    17  import (
    18  	"time"
    19  
    20  	cadvisor "github.com/google/cadvisor/info/v1"
    21  )
    22  
    23  // Provided by Kubelet/cadvisor.
    24  var StandardMetrics = []Metric{
    25  	MetricUptime,
    26  	MetricCpuUsage,
    27  	MetricMemoruUsage,
    28  	MetricMemoryWorkingSet,
    29  	MetricMemoryPageFaults,
    30  	MetricMemoryMajorPageFaults,
    31  	MetricNetworkRx,
    32  	MetricNetworkRxErrors,
    33  	MetricNetworkTx,
    34  	MetricNetworkTxErrors}
    35  
    36  // Metrics computed based on cluster state using Kubernetes API.
    37  var AdditionalMetrics = []Metric{
    38  	MetricCpuRequest,
    39  	MetricCpuLimit,
    40  	MetricMemoryRequest,
    41  	MetricMemoryLimit}
    42  
    43  // Computed based on corresponding StandardMetrics.
    44  var RateMetrics = []Metric{
    45  	MetricCpuUsageRate,
    46  	MetricMemoryPageFaultsRate,
    47  	MetricMemoryMajorPageFaultsRate,
    48  	MetricNetworkRxRate,
    49  	MetricNetworkRxErrorsRate,
    50  	MetricNetworkTxRate,
    51  	MetricNetworkTxErrorsRate}
    52  
    53  var RateMetricsMapping = map[string]Metric{
    54  	MetricCpuUsage.MetricDescriptor.Name:              MetricCpuUsageRate,
    55  	MetricMemoryPageFaults.MetricDescriptor.Name:      MetricMemoryPageFaultsRate,
    56  	MetricMemoryMajorPageFaults.MetricDescriptor.Name: MetricMemoryMajorPageFaultsRate,
    57  	MetricNetworkRx.MetricDescriptor.Name:             MetricNetworkRxRate,
    58  	MetricNetworkRxErrors.MetricDescriptor.Name:       MetricNetworkRxErrorsRate,
    59  	MetricNetworkTx.MetricDescriptor.Name:             MetricNetworkTxRate,
    60  	MetricNetworkTxErrors.MetricDescriptor.Name:       MetricNetworkTxErrorsRate}
    61  
    62  var LabeledMetrics = []Metric{
    63  	MetricFilesystemUsage,
    64  	MetricFilesystemLimit,
    65  }
    66  
    67  var AllMetrics = append(append(append(StandardMetrics, AdditionalMetrics...), RateMetrics...), LabeledMetrics...)
    68  
    69  // Definition of Standard Metrics.
    70  var MetricUptime = Metric{
    71  	MetricDescriptor: MetricDescriptor{
    72  		Name:        "uptime",
    73  		Description: "Number of milliseconds since the container was started",
    74  		Type:        MetricCumulative,
    75  		ValueType:   ValueInt64,
    76  		Units:       UnitsMilliseconds,
    77  	},
    78  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
    79  		return !spec.CreationTime.IsZero()
    80  	},
    81  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
    82  		return MetricValue{
    83  			ValueType:  ValueInt64,
    84  			MetricType: MetricCumulative,
    85  			IntValue:   time.Since(spec.CreationTime).Nanoseconds() / time.Millisecond.Nanoseconds(),
    86  			Start:      spec.CreationTime}
    87  	},
    88  }
    89  
    90  var MetricCpuUsage = Metric{
    91  	MetricDescriptor: MetricDescriptor{
    92  		Name:        "cpu/usage",
    93  		Description: "Cumulative CPU usage on all cores",
    94  		Type:        MetricCumulative,
    95  		ValueType:   ValueInt64,
    96  		Units:       UnitsNanoseconds,
    97  	},
    98  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
    99  		return spec.HasCpu
   100  	},
   101  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   102  		return MetricValue{
   103  			ValueType:  ValueInt64,
   104  			MetricType: MetricCumulative,
   105  			IntValue:   int64(stat.Cpu.Usage.Total),
   106  			Start:      spec.CreationTime}
   107  	},
   108  }
   109  
   110  var MetricMemoruUsage = Metric{
   111  	MetricDescriptor: MetricDescriptor{
   112  		Name:        "memory/usage",
   113  		Description: "Total memory usage",
   114  		Type:        MetricGauge,
   115  		ValueType:   ValueInt64,
   116  		Units:       UnitsBytes,
   117  	},
   118  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   119  		return spec.HasMemory
   120  	},
   121  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   122  		return MetricValue{
   123  			ValueType:  ValueInt64,
   124  			MetricType: MetricGauge,
   125  			IntValue:   int64(stat.Memory.Usage)}
   126  	},
   127  }
   128  
   129  var MetricMemoryWorkingSet = Metric{
   130  	MetricDescriptor: MetricDescriptor{
   131  		Name:        "memory/working_set",
   132  		Description: "Total working set usage. Working set is the memory being used and not easily dropped by the kernel",
   133  		Type:        MetricGauge,
   134  		ValueType:   ValueInt64,
   135  		Units:       UnitsBytes,
   136  	},
   137  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   138  		return spec.HasMemory
   139  	},
   140  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   141  		return MetricValue{
   142  			ValueType:  ValueInt64,
   143  			MetricType: MetricGauge,
   144  			IntValue:   int64(stat.Memory.WorkingSet)}
   145  	},
   146  }
   147  
   148  var MetricMemoryPageFaults = Metric{
   149  	MetricDescriptor: MetricDescriptor{
   150  		Name:        "memory/page_faults",
   151  		Description: "Number of page faults",
   152  		Type:        MetricCumulative,
   153  		ValueType:   ValueInt64,
   154  		Units:       UnitsCount,
   155  	},
   156  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   157  		return spec.HasMemory
   158  	},
   159  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   160  		return MetricValue{
   161  			ValueType:  ValueInt64,
   162  			MetricType: MetricCumulative,
   163  			IntValue:   int64(stat.Memory.ContainerData.Pgfault),
   164  			Start:      spec.CreationTime}
   165  	},
   166  }
   167  
   168  var MetricMemoryMajorPageFaults = Metric{
   169  	MetricDescriptor: MetricDescriptor{
   170  		Name:        "memory/major_page_faults",
   171  		Description: "Number of major page faults",
   172  		Type:        MetricCumulative,
   173  		ValueType:   ValueInt64,
   174  		Units:       UnitsCount,
   175  	},
   176  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   177  		return spec.HasMemory
   178  	},
   179  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   180  		return MetricValue{
   181  			ValueType:  ValueInt64,
   182  			MetricType: MetricCumulative,
   183  			IntValue:   int64(stat.Memory.ContainerData.Pgmajfault),
   184  			Start:      spec.CreationTime}
   185  	},
   186  }
   187  
   188  var MetricNetworkRx = Metric{
   189  	MetricDescriptor: MetricDescriptor{
   190  		Name:        "network/rx",
   191  		Description: "Cumulative number of bytes received over the network",
   192  		Type:        MetricCumulative,
   193  		ValueType:   ValueInt64,
   194  		Units:       UnitsBytes,
   195  	},
   196  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   197  		return spec.HasNetwork
   198  	},
   199  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   200  		return MetricValue{
   201  			ValueType:  ValueInt64,
   202  			MetricType: MetricCumulative,
   203  			IntValue:   int64(stat.Network.RxBytes),
   204  			Start:      spec.CreationTime}
   205  	},
   206  }
   207  
   208  var MetricNetworkRxErrors = Metric{
   209  	MetricDescriptor: MetricDescriptor{
   210  		Name:        "network/rx_errors",
   211  		Description: "Cumulative number of errors while receiving over the network",
   212  		Type:        MetricCumulative,
   213  		ValueType:   ValueInt64,
   214  		Units:       UnitsCount,
   215  	},
   216  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   217  		return spec.HasNetwork
   218  	},
   219  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   220  		return MetricValue{
   221  			ValueType:  ValueInt64,
   222  			MetricType: MetricCumulative,
   223  			IntValue:   int64(stat.Network.RxErrors),
   224  			Start:      spec.CreationTime}
   225  	},
   226  }
   227  
   228  var MetricNetworkTx = Metric{
   229  	MetricDescriptor: MetricDescriptor{
   230  		Name:        "network/tx",
   231  		Description: "Cumulative number of bytes sent over the network",
   232  		Type:        MetricCumulative,
   233  		ValueType:   ValueInt64,
   234  		Units:       UnitsBytes,
   235  	},
   236  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   237  		return spec.HasNetwork
   238  	},
   239  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   240  		return MetricValue{
   241  			ValueType:  ValueInt64,
   242  			MetricType: MetricCumulative,
   243  			IntValue:   int64(stat.Network.TxBytes),
   244  			Start:      spec.CreationTime}
   245  	},
   246  }
   247  
   248  var MetricNetworkTxErrors = Metric{
   249  	MetricDescriptor: MetricDescriptor{
   250  		Name:        "network/tx_errors",
   251  		Description: "Cumulative number of errors while sending over the network",
   252  		Type:        MetricCumulative,
   253  		ValueType:   ValueInt64,
   254  		Units:       UnitsCount,
   255  	},
   256  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   257  		return spec.HasNetwork
   258  	},
   259  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   260  		return MetricValue{
   261  			ValueType:  ValueInt64,
   262  			MetricType: MetricCumulative,
   263  			IntValue:   int64(stat.Network.TxErrors),
   264  			Start:      spec.CreationTime}
   265  	},
   266  }
   267  
   268  // Definition of Additional Metrics.
   269  var MetricCpuRequest = Metric{
   270  	MetricDescriptor: MetricDescriptor{
   271  		Name:        "cpu/request",
   272  		Description: "CPU request (the guaranteed amount of resources) in millicores. This metric is Kubernetes specific.",
   273  		Type:        MetricGauge,
   274  		ValueType:   ValueInt64,
   275  		Units:       UnitsCount,
   276  	},
   277  }
   278  
   279  var MetricCpuLimit = Metric{
   280  	MetricDescriptor: MetricDescriptor{
   281  		Name:        "cpu/limit",
   282  		Description: "CPU hard limit in millicores.",
   283  		Type:        MetricGauge,
   284  		ValueType:   ValueInt64,
   285  		Units:       UnitsCount,
   286  	},
   287  }
   288  
   289  var MetricMemoryRequest = Metric{
   290  	MetricDescriptor: MetricDescriptor{
   291  		Name:        "memory/request",
   292  		Description: "Memory request (the guaranteed amount of resources) in bytes. This metric is Kubernetes specific.",
   293  		Type:        MetricGauge,
   294  		ValueType:   ValueInt64,
   295  		Units:       UnitsBytes,
   296  	},
   297  }
   298  
   299  var MetricMemoryLimit = Metric{
   300  	MetricDescriptor: MetricDescriptor{
   301  		Name:        "memory/limit",
   302  		Description: "Memory hard limit in bytes.",
   303  		Type:        MetricGauge,
   304  		ValueType:   ValueInt64,
   305  		Units:       UnitsBytes,
   306  	},
   307  }
   308  
   309  // Definition of Rate Metrics.
   310  var MetricCpuUsageRate = Metric{
   311  	MetricDescriptor: MetricDescriptor{
   312  		Name:        "cpu/usage_rate",
   313  		Description: "CPU usage on all cores in millicores",
   314  		Type:        MetricGauge,
   315  		ValueType:   ValueInt64,
   316  		Units:       UnitsCount,
   317  	},
   318  }
   319  
   320  var MetricMemoryPageFaultsRate = Metric{
   321  	MetricDescriptor: MetricDescriptor{
   322  		Name:        "memory/page_faults_rate",
   323  		Description: "Rate of page faults in counts per second",
   324  		Type:        MetricGauge,
   325  		ValueType:   ValueFloat,
   326  		Units:       UnitsCount,
   327  	},
   328  }
   329  
   330  var MetricMemoryMajorPageFaultsRate = Metric{
   331  	MetricDescriptor: MetricDescriptor{
   332  		Name:        "memory/major_page_faults_rate",
   333  		Description: "Rate of major page faults in counts per second",
   334  		Type:        MetricGauge,
   335  		ValueType:   ValueFloat,
   336  		Units:       UnitsCount,
   337  	},
   338  }
   339  
   340  var MetricNetworkRxRate = Metric{
   341  	MetricDescriptor: MetricDescriptor{
   342  		Name:        "network/rx_rate",
   343  		Description: "Rate of bytes received over the network in bytes per second",
   344  		Type:        MetricGauge,
   345  		ValueType:   ValueFloat,
   346  		Units:       UnitsCount,
   347  	},
   348  }
   349  
   350  var MetricNetworkRxErrorsRate = Metric{
   351  	MetricDescriptor: MetricDescriptor{
   352  		Name:        "network/rx_errors_rate",
   353  		Description: "Rate of errors sending over the network in errors per second",
   354  		Type:        MetricGauge,
   355  		ValueType:   ValueFloat,
   356  		Units:       UnitsCount,
   357  	},
   358  }
   359  
   360  var MetricNetworkTxRate = Metric{
   361  	MetricDescriptor: MetricDescriptor{
   362  		Name:        "network/tx_rate",
   363  		Description: "Rate of bytes transmitted over the network in bytes per second",
   364  		Type:        MetricGauge,
   365  		ValueType:   ValueFloat,
   366  		Units:       UnitsCount,
   367  	},
   368  }
   369  
   370  var MetricNetworkTxErrorsRate = Metric{
   371  	MetricDescriptor: MetricDescriptor{
   372  		Name:        "network/tx_errors_rate",
   373  		Description: "Rate of errors transmitting over the network in errors per second",
   374  		Type:        MetricGauge,
   375  		ValueType:   ValueFloat,
   376  		Units:       UnitsCount,
   377  	},
   378  }
   379  
   380  // Labeled metrics
   381  
   382  var MetricFilesystemUsage = Metric{
   383  	MetricDescriptor: MetricDescriptor{
   384  		Name:        "filesystem/usage",
   385  		Description: "Total number of bytes consumed on a filesystem",
   386  		Type:        MetricGauge,
   387  		ValueType:   ValueInt64,
   388  		Units:       UnitsBytes,
   389  		Labels:      metricLabels,
   390  	},
   391  	HasLabeledMetric: func(spec *cadvisor.ContainerSpec) bool {
   392  		return spec.HasFilesystem
   393  	},
   394  	GetLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) []LabeledMetric {
   395  		result := make([]LabeledMetric, 0, len(stat.Filesystem))
   396  		for _, fs := range stat.Filesystem {
   397  			result = append(result, LabeledMetric{
   398  				Name: "filesystem/usage",
   399  				Labels: map[string]string{
   400  					LabelResourceID.Key: fs.Device,
   401  				},
   402  				ValueType:  ValueInt64,
   403  				MetricType: MetricCumulative,
   404  				IntValue:   int64(fs.Usage),
   405  			})
   406  		}
   407  		return result
   408  	},
   409  }
   410  
   411  var MetricFilesystemLimit = Metric{
   412  	MetricDescriptor: MetricDescriptor{
   413  		Name:        "filesystem/limit",
   414  		Description: "The total size of filesystem in bytes",
   415  		Type:        MetricGauge,
   416  		ValueType:   ValueInt64,
   417  		Units:       UnitsBytes,
   418  		Labels:      metricLabels,
   419  	},
   420  	HasLabeledMetric: func(spec *cadvisor.ContainerSpec) bool {
   421  		return spec.HasFilesystem
   422  	},
   423  	GetLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) []LabeledMetric {
   424  		result := make([]LabeledMetric, 0, len(stat.Filesystem))
   425  		for _, fs := range stat.Filesystem {
   426  			result = append(result, LabeledMetric{
   427  				Name: "filesystem/limit",
   428  				Labels: map[string]string{
   429  					LabelResourceID.Key: fs.Device,
   430  				},
   431  				ValueType:  ValueInt64,
   432  				MetricType: MetricCumulative,
   433  				IntValue:   int64(fs.Limit),
   434  			})
   435  		}
   436  		return result
   437  	},
   438  }
   439  
   440  type MetricDescriptor struct {
   441  	// The unique name of the metric.
   442  	Name string `json:"name,omitempty"`
   443  
   444  	// Description of the metric.
   445  	Description string `json:"description,omitempty"`
   446  
   447  	// Descriptor of the labels specific to this metric.
   448  	Labels []LabelDescriptor `json:"labels,omitempty"`
   449  
   450  	// Type and value of metric data.
   451  	Type      MetricType `json:"type,omitempty"`
   452  	ValueType ValueType  `json:"value_type,omitempty"`
   453  	Units     UnitsType  `json:"units,omitempty"`
   454  }
   455  
   456  // Metric represents a resource usage stat metric.
   457  type Metric struct {
   458  	MetricDescriptor
   459  
   460  	// Returns whether this metric is present.
   461  	HasValue func(*cadvisor.ContainerSpec) bool
   462  
   463  	// Returns a slice of internal point objects that contain metric values and associated labels.
   464  	GetValue func(*cadvisor.ContainerSpec, *cadvisor.ContainerStats) MetricValue
   465  
   466  	// Returns whether this metric is present.
   467  	HasLabeledMetric func(*cadvisor.ContainerSpec) bool
   468  
   469  	// Returns a slice of internal point objects that contain metric values and associated labels.
   470  	GetLabeledMetric func(*cadvisor.ContainerSpec, *cadvisor.ContainerStats) []LabeledMetric
   471  }