diff --git a/cloud/observability/promql-to-scrape/cmd/genconfig/main.go b/cloud/observability/promql-to-scrape/cmd/genconfig/main.go index 0ea94e7..cb32b01 100644 --- a/cloud/observability/promql-to-scrape/cmd/genconfig/main.go +++ b/cloud/observability/promql-to-scrape/cmd/genconfig/main.go @@ -45,9 +45,9 @@ func main() { if err != nil { log.Fatalf("Failed to pull metric names: %s", err) } - fmt.Println(counters) - fmt.Println(gauges) - fmt.Println(histograms) + fmt.Println("counters: ", counters, "\n") + fmt.Println("gauges: ", gauges, "\n") + fmt.Println("histograms: ", histograms, "\n") conf := internal.Config{} diff --git a/cloud/observability/promql-to-scrape/internal/client.go b/cloud/observability/promql-to-scrape/internal/client.go index 4a7ade5..90bbb90 100644 --- a/cloud/observability/promql-to-scrape/internal/client.go +++ b/cloud/observability/promql-to-scrape/internal/client.go @@ -71,9 +71,10 @@ func (c *APIClient) ListMetrics(metricPrefix string) ([]string, []string, []stri if !strings.HasPrefix(string(v), metricPrefix) { continue } - if strings.HasSuffix(string(v), "_bucket") { + t := getMetricType(string(v)) + if t == metricTypeHistogram { histograms = append(histograms, string(v)) - } else if strings.HasSuffix(string(v), "_count") || strings.HasSuffix(string(v), "_sum") { + } else if t == metricTypeCounter { counts = append(counts, string(v)) } else { gauges = append(gauges, string(v)) diff --git a/cloud/observability/promql-to-scrape/internal/metric-types.go b/cloud/observability/promql-to-scrape/internal/metric-types.go new file mode 100644 index 0000000..91ac9ed --- /dev/null +++ b/cloud/observability/promql-to-scrape/internal/metric-types.go @@ -0,0 +1,18 @@ +package internal + +import "strings" + +const ( + metricTypeHistogram = "histogram" + metricTypeCounter = "counter" + metricTypeGauge = "gauge" +) + +func getMetricType(v string) string { + if strings.HasSuffix(v, "_bucket") { + return metricTypeHistogram + } else if strings.HasSuffix(v, "_count") || strings.HasSuffix(v, "_sum") { + return metricTypeCounter + } + return metricTypeGauge +} diff --git a/cloud/observability/promql-to-scrape/internal/serialize.go b/cloud/observability/promql-to-scrape/internal/serialize.go index 7d0225e..3524943 100644 --- a/cloud/observability/promql-to-scrape/internal/serialize.go +++ b/cloud/observability/promql-to-scrape/internal/serialize.go @@ -38,7 +38,7 @@ func SamplesToString(queriedMetrics map[string][]*model.Sample) string { sb.WriteString("# TYPE ") sb.WriteString(nameWithoutSuffix) sb.WriteByte(' ') - sb.WriteString("gauge") + sb.WriteString(getMetricType(metricName)) sb.WriteByte('\n') for _, s := range samples { diff --git a/cloud/observability/promql-to-scrape/internal/server.go b/cloud/observability/promql-to-scrape/internal/server.go index acb267a..afb45d2 100644 --- a/cloud/observability/promql-to-scrape/internal/server.go +++ b/cloud/observability/promql-to-scrape/internal/server.go @@ -46,7 +46,11 @@ func (s *PromToScrapeServer) metricsHandler(w http.ResponseWriter, r *http.Reque s.RLock() defer s.RUnlock() if time.Since(s.lastSuccessfulTime) < 5*time.Minute { - fmt.Fprint(w, s.data) + _, err := fmt.Fprint(w, s.data) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + slog.Error("can't serve metrics", "error", err) + } } else { w.WriteHeader(http.StatusInternalServerError) slog.Error("can't serve metrics", "error", "metrics queried are stale (more than 5 minutes old)") @@ -71,7 +75,7 @@ func (s *PromToScrapeServer) run() string { // // keep the objects returned from the query, or convert them into something a bit more ergonomic // and create ConstMetrics with the prometheus client. I happened to have the code lying around for working -// with model.Sample, but the CosntMetrics route is probably more idiomatic and safe. +// with model.Sample, but the ConstMetrics route is probably more idiomatic and safe. func (s *PromToScrapeServer) queryMetrics() { start := time.Now() queriedMetrics, err := QueryMetrics(s.conf, s.client) @@ -88,5 +92,6 @@ func (s *PromToScrapeServer) queryMetrics() { // Start runs the embedded http.Server. func (s *PromToScrapeServer) Start() error { + slog.Info("listening on", "addr", s.server.Addr) return s.server.ListenAndServe() }