Skip to content

Commit 5f9a66f

Browse files
committed
Allow to read from env a service-uri var, security improvements
1 parent f4ae8cb commit 5f9a66f

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

exporter/exporter.go

-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ func (e *Exporter) process(ch chan<- prometheus.Metric) {
106106
value = 0
107107
}
108108

109-
// check last filed for the specific ValueType
110109
// TODO: custom filter by a map file
111110
if len(m) > 3 {
112111
mType = prometheus.UntypedValue

exporter/http.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ func FetchMetrics(uri string) (any, error) {
4747
}
4848

4949
func get(uri string) ([]byte, error) {
50-
res, err := http.Get(uri)
50+
req, err := http.NewRequest(http.MethodGet, uri, nil)
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
res, err := http.DefaultClient.Do(req)
5156
if err != nil {
5257
log.Errorf("Could not fetch metrics for endpoint of target: %s", uri)
5358
return nil, err

main.go

+39-16
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,52 @@ import (
1616
)
1717

1818
var (
19-
internalVersion string
20-
21-
listenAddress = flag.String("listen-address", ":19100", "Address to listen on for telemetry.")
22-
metricsPath = flag.String("telemetry-path", "/metrics", "Base path under which to expose metrics.")
23-
serviceName = flag.String("service-name", "", "Service name to reference")
24-
serviceUri = flag.String("service-uri", "http://localhost:5066", "HTTP address of the service.")
19+
internalVersion, serviceEndpoint string
20+
21+
// internal settings
22+
listenAddress = flag.String("listen-address", ":19100", "Address to listen on to be scraped.")
23+
metricsPath = flag.String("telemetry-path", "/metrics", "Base path under which to expose metrics.")
24+
debugLevel = flag.Bool("debug", false, "Enable debug mode.")
25+
26+
// remote service related flags
27+
serviceName = flag.String("service-name", "", "Remote service name to reference.")
28+
serviceProtocol = flag.String("service-protocol", "http", "HTTP Schema of the remote service (http or https).")
29+
serviceUri = flag.String("service-uri", "", "Endpoint address of the remote service.")
30+
servicePort = flag.String("service-port", "80", "HTTP Port of the remote service.")
2531
serviceMetricsPath = flag.String("service-metrics-path", "metrics", "Service path to scrape metrics from.")
26-
serviceVersionScrape = flag.Bool("service-version-scrape", false, "Enable whether the service will be internally scraped for fetching remote build version or not")
27-
debugLevel = flag.Bool("debug", false, "Enable debug mode")
32+
serviceVersionScrape = flag.Bool("service-version-scrape", false, "Enable whether the service will be internally scraped for fetching remote build version or not.")
2833
)
2934

3035
func main() {
3136
flag.Parse()
3237

3338
name := *serviceName
3439
if strings.Trim(name, " ") == "" {
35-
log.Fatalln("Service name not known! Specify by -service-name SERVICE")
40+
log.Fatalln("Service name not set! Specify by -service-name SERVICE")
3641
}
3742

43+
// build service endpoint
44+
// if not set try to fetch from env
45+
serviceEndpoint = fmt.Sprintf("%s://%s:%s", *serviceProtocol, *serviceUri, *servicePort)
46+
if *serviceUri == "" {
47+
host, exists := os.LookupEnv("SERVICE_ENDPOINT")
48+
if exists && host != "" {
49+
serviceEndpoint = fmt.Sprintf("%s://%s:%s", *serviceProtocol, host, *servicePort)
50+
} else {
51+
log.Fatalln("Service URI is not set! Specify either a '-service-uri' flag OR an environment variable 'SERVICE_ENDPOINT'")
52+
}
53+
}
54+
55+
// enable debug mode if required
3856
if *debugLevel {
3957
log.SetLevel(log.DebugLevel)
4058
}
4159

4260
log.Info("Check if target is reachable...")
4361
if *serviceVersionScrape {
44-
internalVersion = checkEndpoint(*serviceUri)
62+
internalVersion = checkEndpoint(serviceEndpoint)
4563
} else {
46-
checkEndpoint(*serviceUri)
64+
checkEndpoint(serviceEndpoint)
4765
}
4866
log.Info("Target endpoint is reachable")
4967

@@ -54,7 +72,7 @@ func main() {
5472
registry.MustRegister(versionMetric)
5573

5674
// register remote service metrics
57-
exporter := exporter.NewCollector(name, fmt.Sprintf("%s/%s", *serviceUri, *serviceMetricsPath), internalVersion)
75+
exporter := exporter.NewCollector(name, fmt.Sprintf("%s/%s", serviceEndpoint, *serviceMetricsPath), internalVersion)
5876
registry.MustRegister(exporter)
5977

6078
http.Handle(*metricsPath, promhttp.HandlerFor(
@@ -68,7 +86,7 @@ func main() {
6886

6987
srv := &http.Server{
7088
Addr: *listenAddress,
71-
ReadHeaderTimeout: 5 * time.Second,
89+
ReadHeaderTimeout: 30 * time.Second,
7290
}
7391
if err := srv.ListenAndServe(); err != nil {
7492
log.WithFields(log.Fields{
@@ -77,10 +95,12 @@ func main() {
7795
}
7896
}
7997

80-
// TODO: improve this method
98+
// check if service endpoint is reachable
99+
// increases duration by 5secs for the next ticket if unavailable
81100
func checkEndpoint(endpoint string) string {
101+
var duration = 5
82102
stopCh := make(chan bool)
83-
t := time.NewTicker(2 * time.Second)
103+
t := time.NewTicker(time.Duration(duration) * time.Second)
84104

85105
stats := &exporter.HttpStats{}
86106

@@ -91,7 +111,10 @@ discovery:
91111
if stats = exporter.FetchStats(endpoint); stats != nil {
92112
break discovery
93113
}
94-
log.Errorln("base endpoint not available, retrying in 2s")
114+
log.Errorf("service endpoint not available, retrying in %ds", duration)
115+
116+
t = time.NewTicker(time.Duration(duration) * time.Second)
117+
duration = duration + 5
95118
continue
96119

97120
case <-stopCh:

0 commit comments

Comments
 (0)