diff --git a/api/api.go b/api/api.go index 7a6bf7ca5..7aa4e891a 100644 --- a/api/api.go +++ b/api/api.go @@ -2,6 +2,7 @@ package api import ( "code.google.com/p/gorest" + "github.com/prometheus/prometheus/storage/metric" "github.com/prometheus/prometheus/utility" ) @@ -10,5 +11,14 @@ type MetricsService struct { query gorest.EndPoint `method:"GET" path:"/query?{expr:string}&{json:string}" output:"string"` queryRange gorest.EndPoint `method:"GET" path:"/query_range?{expr:string}&{end:int64}&{range:int64}&{step:int64}" output:"string"` - time utility.Time + metrics gorest.EndPoint `method:"GET" path:"/metrics" output:"string"` + + persistence metric.MetricPersistence + time utility.Time +} + +func NewMetricsService(p metric.MetricPersistence) *MetricsService { + return &MetricsService{ + persistence: p, + } } diff --git a/api/query.go b/api/query.go index c25082024..a13bfeb5e 100644 --- a/api/query.go +++ b/api/query.go @@ -2,9 +2,11 @@ package api import ( "code.google.com/p/gorest" + "encoding/json" "errors" "github.com/prometheus/prometheus/rules" "github.com/prometheus/prometheus/rules/ast" + "log" "sort" "time" ) @@ -65,3 +67,22 @@ func (serv MetricsService) QueryRange(Expr string, End int64, Range int64, Step sort.Sort(matrix) return ast.TypedValueToJSON(matrix, "matrix") } + +func (serv MetricsService) Metrics() string { + metricNames, err := serv.persistence.GetAllMetricNames() + rb := serv.ResponseBuilder() + rb.SetContentType(gorest.Application_Json) + if err != nil { + log.Printf("Error loading metric names: %v", err) + rb.SetResponseCode(500) + return err.Error() + } + sort.Strings(metricNames) + resultBytes, err := json.Marshal(metricNames) + if err != nil { + log.Printf("Error marshalling metric names: %v", err) + rb.SetResponseCode(500) + return err.Error() + } + return string(resultBytes) +} diff --git a/main.go b/main.go index fef264131..b5c7995dd 100644 --- a/main.go +++ b/main.go @@ -49,8 +49,7 @@ func main() { persistence, err := leveldb.NewLevelDBMetricPersistence(*metricsStoragePath) if err != nil { - log.Print(err) - os.Exit(1) + log.Fatalf("Error opening storage: %v", err) } go func() { @@ -79,7 +78,7 @@ func main() { } go func() { - gorest.RegisterService(new(api.MetricsService)) + gorest.RegisterService(api.NewMetricsService(persistence)) exporter := registry.DefaultRegistry.YieldExporter() http.Handle("/", gorest.Handle()) diff --git a/static/graph.html b/static/graph.html index b3bc371b2..2d64d9e6a 100644 --- a/static/graph.html +++ b/static/graph.html @@ -22,7 +22,12 @@
-
+ + + +
diff --git a/static/js/graph.js b/static/js/graph.js index 9532d3d91..ee6cfa397 100644 --- a/static/js/graph.js +++ b/static/js/graph.js @@ -69,6 +69,7 @@ Prometheus.Graph.prototype.initialize = function() { self.expr = graphWrapper.find("input[name=expr]"); self.rangeInput = self.queryForm.find("input[name=range_input]"); self.stacked = self.queryForm.find("input[name=stacked]"); + self.insertMetric = self.queryForm.find("select[name=insert_metric]"); self.graph = graphWrapper.find(".graph"); self.legend = graphWrapper.find(".legend"); @@ -81,18 +82,40 @@ Prometheus.Graph.prototype.initialize = function() { self.queryForm.find("input[name=inc_range]").click(function() { self.increaseRange(); }); self.queryForm.find("input[name=dec_range]").click(function() { self.decreaseRange(); }); + self.insertMetric.change(function() { + self.expr.val(self.expr.val() + self.insertMetric.val()); + }); self.expr.focus(); // TODO: move to external Graph method. + self.populateInsertableMetrics(); + if (self.expr.val()) { self.submitQuery(); } }; +Prometheus.Graph.prototype.populateInsertableMetrics = function() { + var self = this; + $.ajax({ + method: "GET", + url: "/api/metrics", + dataType: "json", + success: function(json, textStatus) { + for (var i = 0; i < json.length; i++) { + self.insertMetric[0].options.add(new Option(json[i], json[i])); + } + }, + error: function() { + alert("Error loading available metrics!"); + }, + }); +}; + Prometheus.Graph.prototype.onChange = function(handler) { this.changeHandler = handler; }; -Prometheus.Graph.prototype.getOptions = function(handler) { +Prometheus.Graph.prototype.getOptions = function() { var self = this; var options = {}; @@ -306,8 +329,6 @@ function storeGraphOptionsInUrl(options) { var allGraphsOptions = []; for (var i = 0; i < graphs.length; i++) { allGraphsOptions.push(graphs[i].getOptions()); - console.log(graphs[i].id); - console.log(graphs[i].getOptions()); } var optionsJSON = JSON.stringify(allGraphsOptions); window.location.hash = encodeURIComponent(optionsJSON);