Skip to content
This repository has been archived by the owner on Jul 18, 2023. It is now read-only.

Commit

Permalink
refactor: put prometheus metrics in own namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
clambin committed Jan 22, 2022
1 parent 41ff6a1 commit 4829262
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
9 changes: 8 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Timeseries queries return values as a list of timestamp/value tuples. Here's an
Table Queries
Table Queries, on the other hand, return data organized in columns & rows. Each column needs to have the same number of rows:
Table Queries, on the other hand, return data organized in columns and rows. Each column needs to have the same number of rows:
func (handler *myHandler) TableQuery(_ context.Context, _ string, _ *simplejson.TableQueryArgs) (response *simplejson.QueryResponse, err error) {
response = &simplejson.TableQueryResponse{
Expand All @@ -80,6 +80,13 @@ Table Queries, on the other hand, return data organized in columns & rows. Each
return
}
Metrics
simplejson exports two Prometheus metrics for performance analytics:
simplejson_query_duration_seconds: duration of query requests by target, in seconds
simplejson_query_failed_count: number of failed query requests
Other topics
For information on query arguments, annotation and tags, refer to the documentation for those data structures.
Expand Down
66 changes: 66 additions & 0 deletions doc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package simplejson_test

import (
"context"
"github.com/clambin/simplejson"
"time"
)

type handler struct{}

func (h handler) Endpoints() simplejson.Endpoints {
return simplejson.Endpoints{
Query: h.Query,
TableQuery: h.TableQuery,
}
}

func (h *handler) Query(_ context.Context, _ *simplejson.TimeSeriesQueryArgs) (response *simplejson.TimeSeriesResponse, err error) {
response = &simplejson.TimeSeriesResponse{
DataPoints: make([]simplejson.DataPoint, 60),
}

timestamp := time.Now().Add(-1 * time.Hour)
for i := 0; i < 60; i++ {
response.DataPoints[i] = simplejson.DataPoint{
Timestamp: timestamp,
Value: int64(i),
}
timestamp = timestamp.Add(1 * time.Minute)
}
return
}

func (h *handler) TableQuery(_ context.Context, _ *simplejson.TableQueryArgs) (response *simplejson.TableQueryResponse, err error) {
timestamps := make(simplejson.TableQueryResponseTimeColumn, 60)
seriesA := make(simplejson.TableQueryResponseNumberColumn, 60)
seriesB := make(simplejson.TableQueryResponseNumberColumn, 60)

timestamp := time.Now().Add(-1 * time.Hour)
for i := 0; i < 60; i++ {
timestamps[i] = timestamp
seriesA[i] = float64(i)
seriesB[i] = float64(-i)
timestamp = timestamp.Add(1 * time.Minute)
}

response = &simplejson.TableQueryResponse{
Columns: []simplejson.TableQueryResponseColumn{
{Text: "timestamp", Data: timestamps},
{Text: "series A", Data: seriesA},
{Text: "series B", Data: seriesB},
},
}

return
}

func Example() {
s := simplejson.Server{
Handlers: map[string]simplejson.Handler{
"A": &handler{},
},
}

_ = s.Run(8088)
}
1 change: 0 additions & 1 deletion endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func handleEndpoint(w http.ResponseWriter, req *http.Request, request interface{
err = json.Unmarshal(body, &request)
}
}
_ = req.Body.Close()

if err != nil {
http.Error(w, "failed to parse request: "+err.Error(), http.StatusBadRequest)
Expand Down
4 changes: 2 additions & 2 deletions ep_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
)

var queryDuration = promauto.NewSummaryVec(prometheus.SummaryOpts{
Name: "grafana_api_query_duration_seconds",
Name: prometheus.BuildFQName("simplejson", "query", "duration_seconds"),
Help: "Grafana SimpleJSON server duration of query requests by target",
}, []string{"app", "type", "target"})

var queryFailure = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "grafana_api_query_failed_count",
Name: prometheus.BuildFQName("simplejson", "query", "failed_count"),
Help: "Grafana SimpleJSON server count of failed requests",
}, []string{"app", "type", "target"})

Expand Down
2 changes: 1 addition & 1 deletion testserver/testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {

type handler struct{}

func (h *handler) Endpoints() simplejson.Endpoints {
func (h handler) Endpoints() simplejson.Endpoints {
return simplejson.Endpoints{
Query: h.Query,
TableQuery: h.TableQuery,
Expand Down

0 comments on commit 4829262

Please sign in to comment.