diff --git a/zipserver/metrics.go b/zipserver/metrics.go index 1a0f82f..c11087e 100644 --- a/zipserver/metrics.go +++ b/zipserver/metrics.go @@ -3,6 +3,7 @@ package zipserver import ( "fmt" "net/http" + "os" "reflect" "strings" "sync/atomic" @@ -24,6 +25,11 @@ func (m *MetricsCounter) RenderMetrics() string { valueOfMetrics := reflect.ValueOf(m).Elem() + hostname, ok := os.LookupEnv("ZIPSERVER_METRICS_HOST") + if !ok { + hostname, _ = os.Hostname() + } + for i := 0; i < valueOfMetrics.NumField(); i++ { metricTag := valueOfMetrics.Type().Field(i).Tag.Get("metric") if metricTag == "" { @@ -31,7 +37,8 @@ func (m *MetricsCounter) RenderMetrics() string { } fieldValue := valueOfMetrics.Field(i).Addr().Interface().(*atomic.Int64).Load() - metrics.WriteString(fmt.Sprintf("%s %v\n", metricTag, fieldValue)) + metrics.WriteString(fmt.Sprintf("%s{host=\"%s\"} %v\n", metricTag, hostname, fieldValue)) + } return metrics.String() diff --git a/zipserver/metrics_test.go b/zipserver/metrics_test.go index 22cf609..4371d2e 100644 --- a/zipserver/metrics_test.go +++ b/zipserver/metrics_test.go @@ -20,10 +20,11 @@ func Test_Metrics(t *testing.T) { assert.Equal(t, int64(1), metrics.TotalExtractedFiles.Load()) // Test RenderMetrics - expectedMetrics := `zipserver_requests_total 1 -zipserver_errors_total 0 -zipserver_extracted_files_total 1 -zipserver_copied_files_total 0 + t.Setenv("ZIPSERVER_METRICS_HOST", "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())