prometheus

package
v0.0.0-...-5655933 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2023 License: MIT Imports: 18 Imported by: 0

README

prometheus

prometheus 插件的作用,就是抓取 /metrics 接口的数据,上报给服务端。通过,各类 exporter 会暴露 /metrics 接口数据,越来越多的开源组件也会内置 prometheus SDK,吐出 prometheus 格式的监控数据,比如 rabbitmq 插件,其 README 中就有介绍。

这个插件 fork 自 telegraf/prometheus,做了一些删减改造,仍然支持通过 consul 做服务发现,管理所有的目标地址,删掉了 Kubernetes 部分,Kubernetes 部分准备放到其他插件里实现。

增加了两个配置:url_label_key 和 url_label_value。为了标识监控数据是从哪个 scrape url 拉取的,会为监控数据附一个标签来标识这个 url,默认的标签 KEY 是用 instance,当然,也可以改成别的,不过不建议。url_label_value 是标签值,支持 go template 语法,如果为空,就是整个 url 的内容,也可以通过模板变量只取一部分,比如 http://localhost:9104/metrics,只想取 IP 和端口部分,就可以写成:

url_label_value = "{{.Host}}"

如果 HTTP scheme 部分和 /metrics Path 部分都想取,可以这么写:

url_label_value = "{{.Scheme}}://{{.Host}}{{.Path}}"

相关变量是用这个方法生成的,供大家参考:

func (ul *UrlLabel) GenerateLabel(u *url.URL) (string, string, error) {
	if ul.LabelValue == "" {
		return ul.LabelKey, u.String(), nil
	}

	dict := map[string]string{
		"Scheme":   u.Scheme,
		"Host":     u.Host,
		"Hostname": u.Hostname(),
		"Port":     u.Port(),
		"Path":     u.Path,
		"Query":    u.RawQuery,
		"Fragment": u.Fragment,
	}

	var buffer bytes.Buffer
	err := ul.LabelValueTpl.Execute(&buffer, dict)
	if err != nil {
		return "", "", err
	}

	return ul.LabelKey, buffer.String(), nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConsulConfig

type ConsulConfig struct {
	// Address of the Consul agent. The address must contain a hostname or an IP address
	// and optionally a port (format: "host:port").
	Enabled bool           `toml:"enabled"`
	Agent   string         `toml:"agent"`
	Queries []*ConsulQuery `toml:"query"`
	Catalog *api.Catalog   `toml:"-"`
}

type ConsulQuery

type ConsulQuery struct {
	// A name of the searched services (not ID)
	ServiceName string `toml:"name"`

	// A tag of the searched services
	ServiceTag string `toml:"tag"`

	// A DC of the searched services
	ServiceDc string `toml:"dc"`

	// A template URL of the Prometheus gathering interface. The hostname part
	// of the URL will be replaced by discovered address and port.
	ServiceURL string `toml:"url"`

	// Extra tags to add to metrics found in Consul
	ServiceExtraTags map[string]string `toml:"tags"`
	// contains filtered or unexported fields
}

One Consul service discovery query

type Instance

type Instance struct {
	config.InstanceConfig

	URLs              []string        `toml:"urls"`
	ConsulConfig      ConsulConfig    `toml:"consul"`
	NamePrefix        string          `toml:"name_prefix"`
	BearerTokenString string          `toml:"bearer_token_string"`
	BearerTokeFile    string          `toml:"bearer_token_file"`
	Username          string          `toml:"username"`
	Password          string          `toml:"password"`
	Timeout           config.Duration `toml:"timeout"`
	IgnoreMetrics     []string        `toml:"ignore_metrics"`
	IgnoreLabelKeys   []string        `toml:"ignore_label_keys"`
	Headers           []string        `toml:"headers"`

	config.UrlLabel

	tls.ClientConfig
	// contains filtered or unexported fields
}

func (*Instance) Empty

func (ins *Instance) Empty() bool

func (*Instance) Gather

func (ins *Instance) Gather(slist *types.SampleList)

func (*Instance) Init

func (ins *Instance) Init() error

func (*Instance) InitConsulClient

func (ins *Instance) InitConsulClient() error

func (*Instance) UrlsFromConsul

func (ins *Instance) UrlsFromConsul() ([]ScrapeUrl, error)

type Prometheus

type Prometheus struct {
	config.PluginConfig
	Instances []*Instance `toml:"instances"`
}

func (*Prometheus) Clone

func (p *Prometheus) Clone() inputs.Input

func (*Prometheus) GetInstances

func (p *Prometheus) GetInstances() []inputs.Instance

func (*Prometheus) Name

func (p *Prometheus) Name() string

type ScrapeUrl

type ScrapeUrl struct {
	URL  *url.URL
	Tags map[string]string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL