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