github.com/influxdata/influxdb/v2@v2.7.6/telegraf/plugins/inputs/prometheus.go (about) 1 package inputs 2 3 import ( 4 "errors" 5 "fmt" 6 "strconv" 7 "strings" 8 ) 9 10 // Prometheus is based on telegraf Prometheus plugin. 11 type Prometheus struct { 12 baseInput 13 URLs []string `json:"urls"` 14 } 15 16 // PluginName is based on telegraf plugin name. 17 func (p *Prometheus) PluginName() string { 18 return "prometheus" 19 } 20 21 // TOML encodes to toml string 22 func (p *Prometheus) TOML() string { 23 s := make([]string, len(p.URLs)) 24 for k, v := range p.URLs { 25 s[k] = strconv.Quote(v) 26 } 27 return fmt.Sprintf(`[[inputs.%s]] 28 ## An array of urls to scrape metrics from. 29 urls = [%s] 30 31 ## Metric version controls the mapping from Prometheus metrics into 32 ## Telegraf metrics. When using the prometheus_client output, use the same 33 ## value in both plugins to ensure metrics are round-tripped without 34 ## modification. 35 ## 36 ## example: metric_version = 1; 37 ## metric_version = 2; recommended version 38 # metric_version = 1 39 40 ## Url tag name (tag containing scrapped url. optional, default is "url") 41 # url_tag = "url" 42 43 ## An array of Kubernetes services to scrape metrics from. 44 # kubernetes_services = ["http://my-service-dns.my-namespace:9100/metrics"] 45 46 ## Kubernetes config file to create client from. 47 # kube_config = "/path/to/kubernetes.config" 48 49 ## Scrape Kubernetes pods for the following prometheus annotations: 50 ## - prometheus.io/scrape: Enable scraping for this pod 51 ## - prometheus.io/scheme: If the metrics endpoint is secured then you will need to 52 ## set this to 'https' & most likely set the tls config. 53 ## - prometheus.io/path: If the metrics path is not /metrics, define it with this annotation. 54 ## - prometheus.io/port: If port is not 9102 use this annotation 55 # monitor_kubernetes_pods = true 56 ## Get the list of pods to scrape with either the scope of 57 ## - cluster: the kubernetes watch api (default, no need to specify) 58 ## - node: the local cadvisor api; for scalability. Note that the config node_ip or the environment variable NODE_IP must be set to the host IP. 59 # pod_scrape_scope = "cluster" 60 ## Only for node scrape scope: node IP of the node that telegraf is running on. 61 ## Either this config or the environment variable NODE_IP must be set. 62 # node_ip = "10.180.1.1" 63 # ## Only for node scrape scope: interval in seconds for how often to get updated pod list for scraping. 64 # ## Default is 60 seconds. 65 # # pod_scrape_interval = 60 66 ## Restricts Kubernetes monitoring to a single namespace 67 ## ex: monitor_kubernetes_pods_namespace = "default" 68 # monitor_kubernetes_pods_namespace = "" 69 # label selector to target pods which have the label 70 # kubernetes_label_selector = "env=dev,app=nginx" 71 # field selector to target pods 72 # eg. To scrape pods on a specific node 73 # kubernetes_field_selector = "spec.nodeName=$HOSTNAME" 74 75 ## Use bearer token for authorization. ('bearer_token' takes priority) 76 # bearer_token = "/path/to/bearer/token" 77 ## OR 78 # bearer_token_string = "abc_123" 79 80 ## HTTP Basic Authentication username and password. ('bearer_token' and 81 ## 'bearer_token_string' take priority) 82 # username = "" 83 # password = "" 84 85 ## Specify timeout duration for slower prometheus clients (default is 3s) 86 # response_timeout = "3s" 87 88 ## Optional TLS Config 89 # tls_ca = /path/to/cafile 90 # tls_cert = /path/to/certfile 91 # tls_key = /path/to/keyfile 92 ## Use TLS but skip chain & host verification 93 # insecure_skip_verify = false 94 `, p.PluginName(), strings.Join(s, ", ")) 95 } 96 97 // UnmarshalTOML decodes the parsed data to the object 98 func (p *Prometheus) UnmarshalTOML(data interface{}) error { 99 dataOK, ok := data.(map[string]interface{}) 100 if !ok { 101 return errors.New("bad urls for prometheus input plugin") 102 } 103 urls, ok := dataOK["urls"].([]interface{}) 104 if !ok { 105 return errors.New("urls is not an array for prometheus input plugin") 106 } 107 for _, url := range urls { 108 p.URLs = append(p.URLs, url.(string)) 109 } 110 return nil 111 }