- Decouple exporter from the DefaultRegistry.

This commit is contained in:
Matt T. Proud 2012-05-21 10:23:22 +02:00
parent 7675c256ad
commit 2c4784f8ee
3 changed files with 24 additions and 25 deletions

View File

@ -92,32 +92,31 @@ func (r *Registry) Register(name string, metric metrics.Metric) {
}
}
func handleJson(w http.ResponseWriter, r *http.Request) {
// 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
if strings.HasSuffix(url.Path, ".json") {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
composite := make(map[string]interface{}, len(DefaultRegistry.NameToMetric))
for name, metric := range DefaultRegistry.NameToMetric {
composite := make(map[string]interface{}, len(registry.NameToMetric))
for name, metric := range registry.NameToMetric {
composite[name] = metric.Marshallable()
}
data, _ := json.Marshal(composite)
w.Write(data)
} else {
w.WriteHeader(http.StatusNotFound)
}
}
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)
}
}

View File

@ -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)
}

View File

@ -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 (