lib/promscrape/discovery/dns: add support for resolving MX records

See https://github.com/prometheus/prometheus/pull/10099
This commit is contained in:
Aliaksandr Valialkin 2022-08-15 00:32:32 +03:00
parent 10402459d8
commit 2b58bd9876
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1
3 changed files with 56 additions and 2 deletions

View File

@ -18,6 +18,7 @@ The following tip changes can be tested by building VictoriaMetrics components f
* SECURITY: [vmalert](https://docs.victoriametrics.com/vmalert.html): do not expose `-remoteWrite.url`, `-remoteRead.url` and `-datasource.url` command-line flag values in logs and at `http://vmalert:8880/flags` page by default, since they may contain sensitive data such as auth keys. This aligns `vmalert` behaviour with [vmagent](https://docs.victoriametrics.com/vmagent.html), which doesn't expose `-remoteWrite.url` command-line flag value in logs and at `http://vmagent:8429/flags` page by default. Specify `-remoteWrite.showURL`, `-remoteRead.showURL` and `-datasource.showURL` command-line flags for showing values for the corresponding `-*.url` flags in logs. Thanks to @mble for [the pull request](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/2965).
* FEATURE: [VictoriaMetrics cluster](https://docs.victoriametrics.com/Cluster-VictoriaMetrics.html): improve performance for heavy queries on systems with many CPU cores.
* FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): add support for MX record types in [dns_sd_configs](https://docs.victoriametrics.com/sd_configs.html#dns_sd_configs) in the same way as Prometheus 2.38 [does](https://github.com/prometheus/prometheus/pull/10099).
* BUGFIX: prevent from excess CPU usage when the storage enters [read-only mode](https://docs.victoriametrics.com/Cluster-VictoriaMetrics.html#readonly-mode).

View File

@ -215,7 +215,7 @@ scrape_configs:
- names: ["...", "..."]
# type is an optional type of DNS query to perform.
# Supported values are: SRV, A, or AAAA.
# Supported values are: SRV, A, AAAA or MX.
# By default SRV is used.
# type: ...
@ -228,6 +228,7 @@ The following meta labels are available on discovered targets during [relabeling
* `__meta_dns_name`: the record name that produced the discovered target.
* `__meta_dns_srv_record_target`: the target field of the SRV record
* `__meta_dns_srv_record_port`: the port field of the SRV record
* `__meta_dns_mx_record_target`: the target field of the MX record.
## docker_sd_configs

View File

@ -45,6 +45,9 @@ func (sdc *SDConfig) GetLabels(baseDir string) ([]map[string]string, error) {
case "SRV":
ms := getSRVAddrLabels(ctx, sdc)
return ms, nil
case "MX":
ms := getMXAddrLabels(ctx, sdc)
return ms, nil
case "A", "AAAA":
return getAAddrLabels(ctx, sdc, typ)
default:
@ -57,6 +60,45 @@ func (sdc *SDConfig) MustStop() {
// nothing to do
}
func getMXAddrLabels(ctx context.Context, sdc *SDConfig) []map[string]string {
port := 25
if sdc.Port != nil {
port = *sdc.Port
}
type result struct {
name string
mx []*net.MX
err error
}
ch := make(chan result, len(sdc.Names))
for _, name := range sdc.Names {
go func(name string) {
mx, err := resolver.LookupMX(ctx, name)
ch <- result{
name: name,
mx: mx,
err: err,
}
}(name)
}
var ms []map[string]string
for range sdc.Names {
r := <-ch
if r.err != nil {
logger.Errorf("error in MX lookup for %q; skipping it; error: %s", r.name, r.err)
continue
}
for _, mx := range r.mx {
target := mx.Host
for strings.HasSuffix(target, ".") {
target = target[:len(target)-1]
}
ms = appendMXLabels(ms, r.name, target, port)
}
}
return ms
}
func getSRVAddrLabels(ctx context.Context, sdc *SDConfig) []map[string]string {
type result struct {
name string
@ -94,7 +136,7 @@ func getSRVAddrLabels(ctx context.Context, sdc *SDConfig) []map[string]string {
func getAAddrLabels(ctx context.Context, sdc *SDConfig, lookupType string) ([]map[string]string, error) {
if sdc.Port == nil {
return nil, fmt.Errorf("missing `port` in `dns_sd_config`")
return nil, fmt.Errorf("missing `port` in `dns_sd_config` for `type: %s`", lookupType)
}
port := *sdc.Port
type result struct {
@ -131,6 +173,16 @@ func getAAddrLabels(ctx context.Context, sdc *SDConfig, lookupType string) ([]ma
return ms, nil
}
func appendMXLabels(ms []map[string]string, name, target string, port int) []map[string]string {
addr := discoveryutils.JoinHostPort(target, port)
m := map[string]string{
"__address__": addr,
"__meta_dns_name": name,
"__meta_dns_mx_record_target": target,
}
return append(ms, m)
}
func appendAddrLabels(ms []map[string]string, name, target string, port int) []map[string]string {
addr := discoveryutils.JoinHostPort(target, port)
m := map[string]string{