Extract and export GetFQDN()

Signed-off-by: austin ce <austin.cawley@gmail.com>
This commit is contained in:
austin ce 2021-07-07 16:11:49 -04:00
parent b1ed4a0a66
commit 5bdfba1d20
No known key found for this signature in database
GPG Key ID: BACD4A6A54EBE93C
2 changed files with 65 additions and 47 deletions

View File

@ -14,11 +14,8 @@
package scrape package scrape
import ( import (
"encoding"
"fmt" "fmt"
"hash/fnv" "hash/fnv"
"net"
"os"
"reflect" "reflect"
"sync" "sync"
"time" "time"
@ -32,6 +29,7 @@ import (
"github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/util/osutil"
) )
var targetMetadataCache = newMetadataMetricsCollector() var targetMetadataCache = newMetadataMetricsCollector()
@ -206,7 +204,7 @@ func (m *Manager) reload() {
// setJitterSeed calculates a global jitterSeed per server relying on extra label set. // setJitterSeed calculates a global jitterSeed per server relying on extra label set.
func (m *Manager) setJitterSeed(labels labels.Labels) error { func (m *Manager) setJitterSeed(labels labels.Labels) error {
h := fnv.New64a() h := fnv.New64a()
hostname, err := getFqdn() hostname, err := osutil.GetFQDN()
if err != nil { if err != nil {
return err return err
} }
@ -319,46 +317,3 @@ func (m *Manager) TargetsDropped() map[string][]*Target {
} }
return targets return targets
} }
// getFqdn returns a FQDN if it's possible, otherwise falls back to hostname.
func getFqdn() (string, error) {
hostname, err := os.Hostname()
if err != nil {
return "", err
}
ips, err := net.LookupIP(hostname)
if err != nil {
// Return the system hostname if we can't look up the IP address.
return hostname, nil
}
lookup := func(ipStr encoding.TextMarshaler) (string, error) {
ip, err := ipStr.MarshalText()
if err != nil {
return "", err
}
hosts, err := net.LookupAddr(string(ip))
if err != nil || len(hosts) == 0 {
return "", err
}
return hosts[0], nil
}
for _, addr := range ips {
if ip := addr.To4(); ip != nil {
if fqdn, err := lookup(ip); err == nil {
return fqdn, nil
}
}
if ip := addr.To16(); ip != nil {
if fqdn, err := lookup(ip); err == nil {
return fqdn, nil
}
}
}
return hostname, nil
}

63
util/osutil/hostname.go Normal file
View File

@ -0,0 +1,63 @@
// Copyright 2021 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package osutil
import (
"encoding"
"net"
"os"
)
// GetFQDN returns a FQDN if it's possible, otherwise falls back to hostname.
func GetFQDN() (string, error) {
hostname, err := os.Hostname()
if err != nil {
return "", err
}
ips, err := net.LookupIP(hostname)
if err != nil {
// Return the system hostname if we can't look up the IP address.
return hostname, nil
}
lookup := func(ipStr encoding.TextMarshaler) (string, error) {
ip, err := ipStr.MarshalText()
if err != nil {
return "", err
}
hosts, err := net.LookupAddr(string(ip))
if err != nil || len(hosts) == 0 {
return "", err
}
return hosts[0], nil
}
for _, addr := range ips {
if ip := addr.To4(); ip != nil {
if fqdn, err := lookup(ip); err == nil {
return fqdn, nil
}
}
if ip := addr.To16(); ip != nil {
if fqdn, err := lookup(ip); err == nil {
return fqdn, nil
}
}
}
return hostname, nil
}