config.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. package collector
  15. import (
  16. "time"
  17. "encoding/json"
  18. "github.com/google/cadvisor/info/v1"
  19. )
  20. type Config struct {
  21. // the endpoint to hit to scrape metrics
  22. Endpoint EndpointConfig `json:"endpoint"`
  23. // holds information about different metrics that can be collected
  24. MetricsConfig []MetricConfig `json:"metrics_config"`
  25. }
  26. // metricConfig holds information extracted from the config file about a metric
  27. type MetricConfig struct {
  28. // the name of the metric
  29. Name string `json:"name"`
  30. // enum type for the metric type
  31. MetricType v1.MetricType `json:"metric_type"`
  32. // metric units to display on UI and in storage (eg: MB, cores)
  33. // this is only used for display.
  34. Units string `json:"units"`
  35. // data type of the metric (eg: int, float)
  36. DataType v1.DataType `json:"data_type"`
  37. // the frequency at which the metric should be collected
  38. PollingFrequency time.Duration `json:"polling_frequency"`
  39. // the regular expression that can be used to extract the metric
  40. Regex string `json:"regex"`
  41. }
  42. type Prometheus struct {
  43. // the endpoint to hit to scrape metrics
  44. Endpoint EndpointConfig `json:"endpoint"`
  45. // the frequency at which metrics should be collected
  46. PollingFrequency time.Duration `json:"polling_frequency"`
  47. // holds names of different metrics that can be collected
  48. MetricsConfig []string `json:"metrics_config"`
  49. }
  50. type EndpointConfig struct {
  51. // The full URL of the endpoint to reach
  52. URL string
  53. // A configuration in which an actual URL is constructed from, using the container's ip address
  54. URLConfig URLConfig
  55. }
  56. type URLConfig struct {
  57. // the protocol to use for connecting to the endpoint. Eg 'http' or 'https'
  58. Protocol string `json:"protocol"`
  59. // the port to use for connecting to the endpoint. Eg '8778'
  60. Port json.Number `json:"port"`
  61. // the path to use for the endpoint. Eg '/metrics'
  62. Path string `json:"path"`
  63. }
  64. func (ec *EndpointConfig) UnmarshalJSON(b []byte) error {
  65. url := ""
  66. config := URLConfig{
  67. Protocol: "http",
  68. Port: "8000",
  69. }
  70. if err := json.Unmarshal(b, &url); err == nil {
  71. ec.URL = url
  72. return nil
  73. }
  74. err := json.Unmarshal(b, &config)
  75. if err == nil {
  76. ec.URLConfig = config
  77. return nil
  78. }
  79. return err
  80. }