diff --git a/export/registry.go b/export/registry.go index ab77b7f..2b3ca53 100644 --- a/export/registry.go +++ b/export/registry.go @@ -92,32 +92,31 @@ func (r *Registry) Register(name string, metric metrics.Metric) { } } -func handleJson(w http.ResponseWriter, r *http.Request) { - var instrumentable metrics.InstrumentableCall = func() { +// Create a http.HandlerFunc that is tied to r Registry such that requests +// against it generate a representation of the housed metrics. +func (registry *Registry) YieldExporter() *http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var instrumentable metrics.InstrumentableCall = func() { + requestCount.Increment() + url := r.URL - requestCount.Increment() + if strings.HasSuffix(url.Path, ".json") { + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json") + composite := make(map[string]interface{}, len(registry.NameToMetric)) + for name, metric := range registry.NameToMetric { + composite[name] = metric.Marshallable() + } - w.Header().Set("Content-Type", "application/json") + data, _ := json.Marshal(composite) + + w.Write(data) + } else { + w.WriteHeader(http.StatusNotFound) + } - composite := make(map[string]interface{}, len(DefaultRegistry.NameToMetric)) - for name, metric := range DefaultRegistry.NameToMetric { - composite[name] = metric.Marshallable() } - - data, _ := json.Marshal(composite) - - w.Write(data) - } - - metrics.InstrumentCall(instrumentable, requestLatencyAccumulator) -} - -// TODO(mtp): Make instance-specific. -var Exporter http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) { - url := r.URL - - if strings.HasSuffix(url.Path, ".json") { - handleJson(w, r) + metrics.InstrumentCall(instrumentable, requestLatencyAccumulator) } } diff --git a/main.go b/main.go index e6beebe..851c50f 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,8 @@ import ( ) func main() { - http.Handle("/metrics.json", export.Exporter) + exporter := export.DefaultRegistry.YieldExporter() + + http.Handle("/metrics.json", exporter) http.ListenAndServe(":8080", nil) } diff --git a/metrics/histogram.go b/metrics/histogram.go index 5fc2900..f0c412c 100644 --- a/metrics/histogram.go +++ b/metrics/histogram.go @@ -8,8 +8,6 @@ // event values or samples. The underlying histogram implementation is designed // to be performant in that it accepts tolerable inaccuracies. -// TOOD(mtp): Implement visualization and exporting. - package metrics import (