diff --git a/zipserver/config.go b/zipserver/config.go index fb5c99b..41f4abf 100644 --- a/zipserver/config.go +++ b/zipserver/config.go @@ -115,6 +115,7 @@ type Config struct { ClientEmail string Bucket string ExtractPrefix string + MetricsHost string `json:",omitempty"` MaxFileSize uint64 MaxTotalSize uint64 diff --git a/zipserver/metrics.go b/zipserver/metrics.go index c11087e..e1abed4 100644 --- a/zipserver/metrics.go +++ b/zipserver/metrics.go @@ -20,13 +20,13 @@ type MetricsCounter struct { } // render the metrics in a prometheus compatible format -func (m *MetricsCounter) RenderMetrics() string { +func (m *MetricsCounter) RenderMetrics(config *Config) string { var metrics strings.Builder valueOfMetrics := reflect.ValueOf(m).Elem() - hostname, ok := os.LookupEnv("ZIPSERVER_METRICS_HOST") - if !ok { + hostname := config.MetricsHost + if hostname == "" { hostname, _ = os.Hostname() } @@ -47,6 +47,6 @@ func (m *MetricsCounter) RenderMetrics() string { // render the global metrics func metricsHandler(w http.ResponseWriter, r *http.Request) error { w.Header().Set("Content-Type", "text/plain") - w.Write([]byte(globalMetrics.RenderMetrics())) + w.Write([]byte(globalMetrics.RenderMetrics(config))) return nil } diff --git a/zipserver/metrics_test.go b/zipserver/metrics_test.go index 4371d2e..29afa99 100644 --- a/zipserver/metrics_test.go +++ b/zipserver/metrics_test.go @@ -19,13 +19,14 @@ func Test_Metrics(t *testing.T) { metrics.TotalExtractedFiles.Add(1) assert.Equal(t, int64(1), metrics.TotalExtractedFiles.Load()) - // Test RenderMetrics - t.Setenv("ZIPSERVER_METRICS_HOST", "localhost") + config := &Config{ + MetricsHost: "localhost", + } + expectedMetrics := `zipserver_requests_total{host="localhost"} 1 zipserver_errors_total{host="localhost"} 0 zipserver_extracted_files_total{host="localhost"} 1 zipserver_copied_files_total{host="localhost"} 0 ` - - assert.Equal(t, expectedMetrics, metrics.RenderMetrics()) + assert.Equal(t, expectedMetrics, metrics.RenderMetrics(config)) }