Skip to content

Commit

Permalink
use MetricsHost config setting instead
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Nov 24, 2023
1 parent 1e66858 commit 41baf68
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions zipserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ type Config struct {
ClientEmail string
Bucket string
ExtractPrefix string
MetricsHost string `json:",omitempty"`

MaxFileSize uint64
MaxTotalSize uint64
Expand Down
8 changes: 4 additions & 4 deletions zipserver/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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
}
9 changes: 5 additions & 4 deletions zipserver/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 41baf68

Please sign in to comment.