github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/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  const (
    24  	CustomMetricPrefix = "custom/"
    25  )
    26  
    27  // Provided by Kubelet/cadvisor.
    28  var StandardMetrics = []Metric{
    29  	MetricUptime,
    30  	MetricCpuUsage,
    31  	MetricMemoryUsage,
    32  	MetricMemoryWorkingSet,
    33  	MetricMemoryPageFaults,
    34  	MetricMemoryMajorPageFaults,
    35  	MetricNetworkRx,
    36  	MetricNetworkRxErrors,
    37  	MetricNetworkTx,
    38  	MetricNetworkTxErrors}
    39  
    40  // Metrics computed based on cluster state using Kubernetes API.
    41  var AdditionalMetrics = []Metric{
    42  	MetricCpuRequest,
    43  	MetricCpuLimit,
    44  	MetricMemoryRequest,
    45  	MetricMemoryLimit}
    46  
    47  // Computed based on corresponding StandardMetrics.
    48  var RateMetrics = []Metric{
    49  	MetricCpuUsageRate,
    50  	MetricMemoryPageFaultsRate,
    51  	MetricMemoryMajorPageFaultsRate,
    52  	MetricNetworkRxRate,
    53  	MetricNetworkRxErrorsRate,
    54  	MetricNetworkTxRate,
    55  	MetricNetworkTxErrorsRate}
    56  
    57  var RateMetricsMapping = map[string]Metric{
    58  	MetricCpuUsage.MetricDescriptor.Name:              MetricCpuUsageRate,
    59  	MetricMemoryPageFaults.MetricDescriptor.Name:      MetricMemoryPageFaultsRate,
    60  	MetricMemoryMajorPageFaults.MetricDescriptor.Name: MetricMemoryMajorPageFaultsRate,
    61  	MetricNetworkRx.MetricDescriptor.Name:             MetricNetworkRxRate,
    62  	MetricNetworkRxErrors.MetricDescriptor.Name:       MetricNetworkRxErrorsRate,
    63  	MetricNetworkTx.MetricDescriptor.Name:             MetricNetworkTxRate,
    64  	MetricNetworkTxErrors.MetricDescriptor.Name:       MetricNetworkTxErrorsRate}
    65  
    66  var LabeledMetrics = []Metric{
    67  	MetricFilesystemUsage,
    68  	MetricFilesystemLimit,
    69  	MetricFilesystemAvailable,
    70  }
    71  
    72  var NodeAutoscalingMetrics = []Metric{
    73  	MetricNodeCpuUtilization,
    74  	MetricNodeMemoryUtilization,
    75  	MetricNodeCpuReservation,
    76  	MetricNodeMemoryReservation,
    77  }
    78  
    79  var AllMetrics = append(append(append(append(StandardMetrics, AdditionalMetrics...), RateMetrics...), LabeledMetrics...),
    80  	NodeAutoscalingMetrics...)
    81  
    82  // Definition of Standard Metrics.
    83  var MetricUptime = Metric{
    84  	MetricDescriptor: MetricDescriptor{
    85  		Name:        "uptime",
    86  		Description: "Number of milliseconds since the container was started",
    87  		Type:        MetricCumulative,
    88  		ValueType:   ValueInt64,
    89  		Units:       UnitsMilliseconds,
    90  	},
    91  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
    92  		return !spec.CreationTime.IsZero()
    93  	},
    94  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
    95  		return MetricValue{
    96  			ValueType:  ValueInt64,
    97  			MetricType: MetricCumulative,
    98  			IntValue:   time.Since(spec.CreationTime).Nanoseconds() / time.Millisecond.Nanoseconds()}
    99  	},
   100  }
   101  
   102  var MetricCpuUsage = Metric{
   103  	MetricDescriptor: MetricDescriptor{
   104  		Name:        "cpu/usage",
   105  		Description: "Cumulative CPU usage on all cores",
   106  		Type:        MetricCumulative,
   107  		ValueType:   ValueInt64,
   108  		Units:       UnitsNanoseconds,
   109  	},
   110  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   111  		return spec.HasCpu
   112  	},
   113  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   114  		return MetricValue{
   115  			ValueType:  ValueInt64,
   116  			MetricType: MetricCumulative,
   117  			IntValue:   int64(stat.Cpu.Usage.Total)}
   118  	},
   119  }
   120  
   121  var MetricMemoryUsage = Metric{
   122  	MetricDescriptor: MetricDescriptor{
   123  		Name:        "memory/usage",
   124  		Description: "Total memory usage",
   125  		Type:        MetricGauge,
   126  		ValueType:   ValueInt64,
   127  		Units:       UnitsBytes,
   128  	},
   129  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   130  		return spec.HasMemory
   131  	},
   132  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   133  		return MetricValue{
   134  			ValueType:  ValueInt64,
   135  			MetricType: MetricGauge,
   136  			IntValue:   int64(stat.Memory.Usage)}
   137  	},
   138  }
   139  
   140  var MetricMemoryWorkingSet = Metric{
   141  	MetricDescriptor: MetricDescriptor{
   142  		Name:        "memory/working_set",
   143  		Description: "Total working set usage. Working set is the memory being used and not easily dropped by the kernel",
   144  		Type:        MetricGauge,
   145  		ValueType:   ValueInt64,
   146  		Units:       UnitsBytes,
   147  	},
   148  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   149  		return spec.HasMemory
   150  	},
   151  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   152  		return MetricValue{
   153  			ValueType:  ValueInt64,
   154  			MetricType: MetricGauge,
   155  			IntValue:   int64(stat.Memory.WorkingSet)}
   156  	},
   157  }
   158  
   159  var MetricMemoryPageFaults = Metric{
   160  	MetricDescriptor: MetricDescriptor{
   161  		Name:        "memory/page_faults",
   162  		Description: "Number of page faults",
   163  		Type:        MetricCumulative,
   164  		ValueType:   ValueInt64,
   165  		Units:       UnitsCount,
   166  	},
   167  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   168  		return spec.HasMemory
   169  	},
   170  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   171  		return MetricValue{
   172  			ValueType:  ValueInt64,
   173  			MetricType: MetricCumulative,
   174  			IntValue:   int64(stat.Memory.ContainerData.Pgfault)}
   175  	},
   176  }
   177  
   178  var MetricMemoryMajorPageFaults = Metric{
   179  	MetricDescriptor: MetricDescriptor{
   180  		Name:        "memory/major_page_faults",
   181  		Description: "Number of major page faults",
   182  		Type:        MetricCumulative,
   183  		ValueType:   ValueInt64,
   184  		Units:       UnitsCount,
   185  	},
   186  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   187  		return spec.HasMemory
   188  	},
   189  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   190  		return MetricValue{
   191  			ValueType:  ValueInt64,
   192  			MetricType: MetricCumulative,
   193  			IntValue:   int64(stat.Memory.ContainerData.Pgmajfault)}
   194  	},
   195  }
   196  
   197  var MetricNetworkRx = Metric{
   198  	MetricDescriptor: MetricDescriptor{
   199  		Name:        "network/rx",
   200  		Description: "Cumulative number of bytes received over the network",
   201  		Type:        MetricCumulative,
   202  		ValueType:   ValueInt64,
   203  		Units:       UnitsBytes,
   204  	},
   205  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   206  		return spec.HasNetwork
   207  	},
   208  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   209  		return MetricValue{
   210  			ValueType:  ValueInt64,
   211  			MetricType: MetricCumulative,
   212  			IntValue:   int64(stat.Network.RxBytes)}
   213  	},
   214  }
   215  
   216  var MetricNetworkRxErrors = Metric{
   217  	MetricDescriptor: MetricDescriptor{
   218  		Name:        "network/rx_errors",
   219  		Description: "Cumulative number of errors while receiving over the network",
   220  		Type:        MetricCumulative,
   221  		ValueType:   ValueInt64,
   222  		Units:       UnitsCount,
   223  	},
   224  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   225  		return spec.HasNetwork
   226  	},
   227  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   228  		return MetricValue{
   229  			ValueType:  ValueInt64,
   230  			MetricType: MetricCumulative,
   231  			IntValue:   int64(stat.Network.RxErrors)}
   232  	},
   233  }
   234  
   235  var MetricNetworkTx = Metric{
   236  	MetricDescriptor: MetricDescriptor{
   237  		Name:        "network/tx",
   238  		Description: "Cumulative number of bytes sent over the network",
   239  		Type:        MetricCumulative,
   240  		ValueType:   ValueInt64,
   241  		Units:       UnitsBytes,
   242  	},
   243  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   244  		return spec.HasNetwork
   245  	},
   246  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   247  		return MetricValue{
   248  			ValueType:  ValueInt64,
   249  			MetricType: MetricCumulative,
   250  			IntValue:   int64(stat.Network.TxBytes)}
   251  	},
   252  }
   253  
   254  var MetricNetworkTxErrors = Metric{
   255  	MetricDescriptor: MetricDescriptor{
   256  		Name:        "network/tx_errors",
   257  		Description: "Cumulative number of errors while sending over the network",
   258  		Type:        MetricCumulative,
   259  		ValueType:   ValueInt64,
   260  		Units:       UnitsCount,
   261  	},
   262  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   263  		return spec.HasNetwork
   264  	},
   265  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   266  		return MetricValue{
   267  			ValueType:  ValueInt64,
   268  			MetricType: MetricCumulative,
   269  			IntValue:   int64(stat.Network.TxErrors)}
   270  	},
   271  }
   272  
   273  // Definition of Additional Metrics.
   274  var MetricCpuRequest = Metric{
   275  	MetricDescriptor: MetricDescriptor{
   276  		Name:        "cpu/request",
   277  		Description: "CPU request (the guaranteed amount of resources) in millicores. This metric is Kubernetes specific.",
   278  		Type:        MetricGauge,
   279  		ValueType:   ValueInt64,
   280  		Units:       UnitsCount,
   281  	},
   282  }
   283  
   284  var MetricCpuLimit = Metric{
   285  	MetricDescriptor: MetricDescriptor{
   286  		Name:        "cpu/limit",
   287  		Description: "CPU hard limit in millicores.",
   288  		Type:        MetricGauge,
   289  		ValueType:   ValueInt64,
   290  		Units:       UnitsCount,
   291  	},
   292  }
   293  
   294  var MetricMemoryRequest = Metric{
   295  	MetricDescriptor: MetricDescriptor{
   296  		Name:        "memory/request",
   297  		Description: "Memory request (the guaranteed amount of resources) in bytes. This metric is Kubernetes specific.",
   298  		Type:        MetricGauge,
   299  		ValueType:   ValueInt64,
   300  		Units:       UnitsBytes,
   301  	},
   302  }
   303  
   304  var MetricMemoryLimit = Metric{
   305  	MetricDescriptor: MetricDescriptor{
   306  		Name:        "memory/limit",
   307  		Description: "Memory hard limit in bytes.",
   308  		Type:        MetricGauge,
   309  		ValueType:   ValueInt64,
   310  		Units:       UnitsBytes,
   311  	},
   312  }
   313  
   314  // Definition of Rate Metrics.
   315  var MetricCpuUsageRate = Metric{
   316  	MetricDescriptor: MetricDescriptor{
   317  		Name:        "cpu/usage_rate",
   318  		Description: "CPU usage on all cores in millicores",
   319  		Type:        MetricGauge,
   320  		ValueType:   ValueInt64,
   321  		Units:       UnitsCount,
   322  	},
   323  }
   324  
   325  var MetricMemoryPageFaultsRate = Metric{
   326  	MetricDescriptor: MetricDescriptor{
   327  		Name:        "memory/page_faults_rate",
   328  		Description: "Rate of page faults in counts per second",
   329  		Type:        MetricGauge,
   330  		ValueType:   ValueFloat,
   331  		Units:       UnitsCount,
   332  	},
   333  }
   334  
   335  var MetricMemoryMajorPageFaultsRate = Metric{
   336  	MetricDescriptor: MetricDescriptor{
   337  		Name:        "memory/major_page_faults_rate",
   338  		Description: "Rate of major page faults in counts per second",
   339  		Type:        MetricGauge,
   340  		ValueType:   ValueFloat,
   341  		Units:       UnitsCount,
   342  	},
   343  }
   344  
   345  var MetricNetworkRxRate = Metric{
   346  	MetricDescriptor: MetricDescriptor{
   347  		Name:        "network/rx_rate",
   348  		Description: "Rate of bytes received over the network in bytes per second",
   349  		Type:        MetricGauge,
   350  		ValueType:   ValueFloat,
   351  		Units:       UnitsCount,
   352  	},
   353  }
   354  
   355  var MetricNetworkRxErrorsRate = Metric{
   356  	MetricDescriptor: MetricDescriptor{
   357  		Name:        "network/rx_errors_rate",
   358  		Description: "Rate of errors sending over the network in errors per second",
   359  		Type:        MetricGauge,
   360  		ValueType:   ValueFloat,
   361  		Units:       UnitsCount,
   362  	},
   363  }
   364  
   365  var MetricNetworkTxRate = Metric{
   366  	MetricDescriptor: MetricDescriptor{
   367  		Name:        "network/tx_rate",
   368  		Description: "Rate of bytes transmitted over the network in bytes per second",
   369  		Type:        MetricGauge,
   370  		ValueType:   ValueFloat,
   371  		Units:       UnitsCount,
   372  	},
   373  }
   374  
   375  var MetricNetworkTxErrorsRate = Metric{
   376  	MetricDescriptor: MetricDescriptor{
   377  		Name:        "network/tx_errors_rate",
   378  		Description: "Rate of errors transmitting over the network in errors per second",
   379  		Type:        MetricGauge,
   380  		ValueType:   ValueFloat,
   381  		Units:       UnitsCount,
   382  	},
   383  }
   384  
   385  var MetricNodeCpuUtilization = Metric{
   386  	MetricDescriptor: MetricDescriptor{
   387  		Name:        "cpu/node_utilization",
   388  		Description: "Cpu utilization as a share of node capacity",
   389  		Type:        MetricGauge,
   390  		ValueType:   ValueFloat,
   391  		Units:       UnitsCount,
   392  	},
   393  }
   394  
   395  var MetricNodeMemoryUtilization = Metric{
   396  	MetricDescriptor: MetricDescriptor{
   397  		Name:        "memory/node_utilization",
   398  		Description: "Memory utilization as a share of memory capacity",
   399  		Type:        MetricGauge,
   400  		ValueType:   ValueFloat,
   401  		Units:       UnitsCount,
   402  	},
   403  }
   404  
   405  var MetricNodeCpuReservation = Metric{
   406  	MetricDescriptor: MetricDescriptor{
   407  		Name:        "cpu/node_reservation",
   408  		Description: "Share of cpu that is reserved on the node",
   409  		Type:        MetricGauge,
   410  		ValueType:   ValueFloat,
   411  		Units:       UnitsCount,
   412  	},
   413  }
   414  
   415  var MetricNodeMemoryReservation = Metric{
   416  	MetricDescriptor: MetricDescriptor{
   417  		Name:        "memory/node_reservation",
   418  		Description: "Share of memory that is reserved on the node",
   419  		Type:        MetricGauge,
   420  		ValueType:   ValueFloat,
   421  		Units:       UnitsCount,
   422  	},
   423  }
   424  
   425  // Labeled metrics
   426  
   427  var MetricFilesystemUsage = Metric{
   428  	MetricDescriptor: MetricDescriptor{
   429  		Name:        "filesystem/usage",
   430  		Description: "Total number of bytes consumed on a filesystem",
   431  		Type:        MetricGauge,
   432  		ValueType:   ValueInt64,
   433  		Units:       UnitsBytes,
   434  		Labels:      metricLabels,
   435  	},
   436  	HasLabeledMetric: func(spec *cadvisor.ContainerSpec) bool {
   437  		return spec.HasFilesystem
   438  	},
   439  	GetLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) []LabeledMetric {
   440  		result := make([]LabeledMetric, 0, len(stat.Filesystem))
   441  		for _, fs := range stat.Filesystem {
   442  			result = append(result, LabeledMetric{
   443  				Name: "filesystem/usage",
   444  				Labels: map[string]string{
   445  					LabelResourceID.Key: fs.Device,
   446  				},
   447  				MetricValue: MetricValue{
   448  					ValueType:  ValueInt64,
   449  					MetricType: MetricGauge,
   450  					IntValue:   int64(fs.Usage),
   451  				},
   452  			})
   453  		}
   454  		return result
   455  	},
   456  }
   457  
   458  var MetricFilesystemLimit = Metric{
   459  	MetricDescriptor: MetricDescriptor{
   460  		Name:        "filesystem/limit",
   461  		Description: "The total size of filesystem in bytes",
   462  		Type:        MetricGauge,
   463  		ValueType:   ValueInt64,
   464  		Units:       UnitsBytes,
   465  		Labels:      metricLabels,
   466  	},
   467  	HasLabeledMetric: func(spec *cadvisor.ContainerSpec) bool {
   468  		return spec.HasFilesystem
   469  	},
   470  	GetLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) []LabeledMetric {
   471  		result := make([]LabeledMetric, 0, len(stat.Filesystem))
   472  		for _, fs := range stat.Filesystem {
   473  			result = append(result, LabeledMetric{
   474  				Name: "filesystem/limit",
   475  				Labels: map[string]string{
   476  					LabelResourceID.Key: fs.Device,
   477  				},
   478  				MetricValue: MetricValue{
   479  					ValueType:  ValueInt64,
   480  					MetricType: MetricGauge,
   481  					IntValue:   int64(fs.Limit),
   482  				},
   483  			})
   484  		}
   485  		return result
   486  	},
   487  }
   488  
   489  var MetricFilesystemAvailable = Metric{
   490  	MetricDescriptor: MetricDescriptor{
   491  		Name:        "filesystem/available",
   492  		Description: "The number of available bytes remaining in a the filesystem",
   493  		Type:        MetricGauge,
   494  		ValueType:   ValueInt64,
   495  		Units:       UnitsBytes,
   496  		Labels:      metricLabels,
   497  	},
   498  }
   499  
   500  func IsNodeAutoscalingMetric(name string) bool {
   501  	for _, autoscalingMetric := range NodeAutoscalingMetrics {
   502  		if autoscalingMetric.MetricDescriptor.Name == name {
   503  			return true
   504  		}
   505  	}
   506  	return false
   507  }
   508  
   509  type MetricDescriptor struct {
   510  	// The unique name of the metric.
   511  	Name string `json:"name,omitempty"`
   512  
   513  	// Description of the metric.
   514  	Description string `json:"description,omitempty"`
   515  
   516  	// Descriptor of the labels specific to this metric.
   517  	Labels []LabelDescriptor `json:"labels,omitempty"`
   518  
   519  	// Type and value of metric data.
   520  	Type      MetricType `json:"type,omitempty"`
   521  	ValueType ValueType  `json:"value_type,omitempty"`
   522  	Units     UnitsType  `json:"units,omitempty"`
   523  }
   524  
   525  // Metric represents a resource usage stat metric.
   526  type Metric struct {
   527  	MetricDescriptor
   528  
   529  	// Returns whether this metric is present.
   530  	HasValue func(*cadvisor.ContainerSpec) bool
   531  
   532  	// Returns a slice of internal point objects that contain metric values and associated labels.
   533  	GetValue func(*cadvisor.ContainerSpec, *cadvisor.ContainerStats) MetricValue
   534  
   535  	// Returns whether this metric is present.
   536  	HasLabeledMetric func(*cadvisor.ContainerSpec) bool
   537  
   538  	// Returns a slice of internal point objects that contain metric values and associated labels.
   539  	GetLabeledMetric func(*cadvisor.ContainerSpec, *cadvisor.ContainerStats) []LabeledMetric
   540  }