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

Commit

Permalink
feat: simplejson implemented as a chi.Router, rather than a full HTTP…
Browse files Browse the repository at this point in the history
… server.
  • Loading branch information
clambin committed Jan 11, 2023
1 parent c046191 commit 3837ac8
Show file tree
Hide file tree
Showing 16 changed files with 214 additions and 213 deletions.
7 changes: 4 additions & 3 deletions annotation_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package simplejson
package simplejson_test

import (
"encoding/json"
"github.com/clambin/simplejson/v6"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
Expand All @@ -12,12 +13,12 @@ import (
)

func TestAnnotation_MarshalJSON(t *testing.T) {
ann := Annotation{
ann := simplejson.Annotation{
Time: time.Date(2022, time.January, 23, 0, 0, 0, 0, time.UTC),
Title: "foo",
Text: "bar",
Tags: []string{"A", "B"},
Request: RequestDetails{
Request: simplejson.RequestDetails{
Name: "snafu",
Datasource: "datasource",
Enable: true,
Expand Down
21 changes: 9 additions & 12 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@ Package simplejson provides a Go implementation for Grafana's SimpleJSON datasou
# Overview
A simplejson server is an HTTP server that supports one or more handlers. Each handler can support multiple query targets,
which can be either timeseries or table query. A handler may support tag key/value pairs, which can be passed to the query
to adapt its behaviour (e.g. filtering what data should be returned) through 'ad hoc filters'. Finally, a handler can support
annotations, i.e. a set of timestamps with associated text.
A simplejson server supports one or more handlers. Each handler can support multiple query targets,which can be either timeseries or table query.
A handler may support tag key/value pairs, which can be passed to the query to adapt its behaviour (e.g. filtering what data should be returned)
through 'ad hoc filters'. Finally, a handler can support annotations, i.e. a set of timestamps with associated text.
# Server
To create a SimpleJSON server, create a Server and run it:
simplejson.New() creates a SimpleJSON server. The server is implemented as an http router, compatible with net/http:
handlers := map[string]simplejson.Handler{
"A": &handler{},
"B": &handler{table: true},
}
s, err := simplejson.New(handlers, simplejson.WithHTTPServerOption{Option: httpserver.WithPort{Port: 8080}})
r := simplejson.New(handlers, simplejson.WithHTTPServerOption{Option: httpserver.WithPort{Port: 8080}})
if err == nil {
err = s.Serve()
}
_ = http.ListenAndServe(":8080", r)
This starts a server, listening on port 8080, with one target "my-target", served by myHandler.
Expand Down Expand Up @@ -124,17 +121,17 @@ When the dashboard performs a query with a tag selected, that tag & value will b
# Metrics
simplejson exports two Prometheus metrics for performance analytics:
When provided with the WithQueryMetrics option, 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
The underlying http server is implemented by [github.com/clambin/go-common/httpserver], which exports its own set of metrics.
The underlying http router uses [PrometheusMetrics], which exports its own set of metrics. See WithHTTPMetrics for details.
# Other topics
For information on query arguments and tags, refer to the documentation for those data structures.
[github.com/clambin/go-common/httpserver]: https://github.com/clambin/go-common/tree/httpserver/httpserver
[PrometheusMetrics]: https://pkg.go.dev/github.com/clambin/go-common/httpserver/middleware
*/
package simplejson
13 changes: 5 additions & 8 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@ package simplejson_test
import (
"context"
"fmt"
"github.com/clambin/go-common/httpserver"
"github.com/clambin/simplejson/v5"
"github.com/clambin/simplejson/v6"
"net/http"
"time"
)

func Example() {
handlers := map[string]simplejson.Handler{
r := simplejson.New(map[string]simplejson.Handler{
"A": &handler{},
"B": &handler{table: true},
}
s, err := simplejson.New(handlers, simplejson.WithHTTPServerOption{Option: httpserver.WithPort{Port: 8080}})
})

if err == nil {
_ = s.Serve()
}
_ = http.ListenAndServe(":8080", r)
}

type handler struct{ table bool }
Expand Down
16 changes: 11 additions & 5 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import (
"encoding/json"
"github.com/go-http-utils/headers"
"net/http"
"sort"
)

func (s *Server) Search(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set(headers.ContentType, "application/json")
var targets []string
for target := range s.Handlers {
targets = append(targets, target)
}
sort.Strings(targets)

output, _ := json.Marshal(s.Targets())
//w.WriteHeader(http.StatusOK)
w.Header().Set(headers.ContentType, "application/json")
output, _ := json.Marshal(targets)
_, _ = w.Write(output)
}

Expand All @@ -23,7 +29,7 @@ func (s *Server) Query(w http.ResponseWriter, req *http.Request) {

func (s *Server) Annotations(w http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
//w.WriteHeader(http.StatusOK)
w.Header().Set(headers.AccessControlAllowHeaders, "accept, content-type")
w.Header().Set(headers.AccessControlAllowMethods, "POST")
w.Header().Set(headers.AccessControlAllowOrigin, "*")
Expand Down Expand Up @@ -136,7 +142,7 @@ func handleEndpoint(w http.ResponseWriter, req *http.Request, request json.Unmar
return
}

w.WriteHeader(http.StatusOK)
//w.WriteHeader(http.StatusOK)
w.Header().Set(headers.ContentType, "application/json")

if err = json.NewEncoder(w).Encode(response); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion endpoints_query_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package simplejson
package simplejson_test

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion endpoints_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package simplejson
package simplejson_test

import (
"bytes"
Expand Down
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module github.com/clambin/simplejson/v5
module github.com/clambin/simplejson/v6

go 1.18
go 1.19

require (
github.com/clambin/go-common/httpserver v0.4.0
github.com/clambin/go-common/httpserver v0.4.1
github.com/clambin/go-common/set v0.1.2
github.com/go-chi/chi/v5 v5.0.8
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a
github.com/grafana/grafana-plugin-sdk-go v0.147.0
github.com/mailru/easyjson v0.7.7
Expand Down Expand Up @@ -38,7 +39,8 @@ require (
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.39.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/exp v0.0.0-20230105202349-8879d0199aa3 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/cheekybits/genny v1.0.0 h1:uGGa4nei+j20rOSeDeP5Of12XVm7TGUd4dJA9RDitf
github.com/cheekybits/genny v1.0.0/go.mod h1:+tQajlRqAUrPI7DOSpB0XAqZYtQakVtB7wXkRAgjxjQ=
github.com/clambin/go-common/httpserver v0.4.0 h1:t9wH0W9P/mMHacWSdDqRdOrxhNRV58o5pLK5fF8630s=
github.com/clambin/go-common/httpserver v0.4.0/go.mod h1:DQrnJehMlHai3Y/l9mF53evzCRfiXVc2O8+WqBLLhHQ=
github.com/clambin/go-common/httpserver v0.4.1 h1:TIHswNcj5hVbrAIrSPVvkTWqTU4M2770qZctvR6jQGE=
github.com/clambin/go-common/httpserver v0.4.1/go.mod h1:qCWVjj3pASnqGTmLJhLrMKCxw7AcVANug3a0kC4qW6c=
github.com/clambin/go-common/set v0.1.2 h1:Pr0FXPVJsH4dc/OelhjXYo0xVmOLIUurg3saMh8KTz8=
github.com/clambin/go-common/set v0.1.2/go.mod h1:GFjAZynNo4BNjlx2hzDMHIU4ays7QjpJUSHXB/3b1ZE=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
Expand All @@ -38,6 +40,8 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-fonts/dejavu v0.1.0/go.mod h1:4Wt4I4OU2Nq9asgDCteaAaWZOV24E+0/Pwo0gppep4g=
github.com/go-fonts/latin-modern v0.2.0/go.mod h1:rQVLdDMK+mK1xscDwsqM5J8U2jrRa3T0ecnM9pNujks=
github.com/go-fonts/liberation v0.1.1/go.mod h1:K6qoJYypsmfVjWg8KOVDQhLc8UDgIK2HYqyqAO9z7GY=
Expand Down Expand Up @@ -154,6 +158,8 @@ golang.org/x/exp v0.0.0-20190125153040-c74c464bbbf2/go.mod h1:CJ0aWSM057203Lf6IL
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20191002040644-a1355ae1e2c3 h1:n9HxLrNxWWtEb1cA950nuEEj3QnKbtsCJ6KjcgisNUs=
golang.org/x/exp v0.0.0-20191002040644-a1355ae1e2c3/go.mod h1:NOZ3BPKG0ec/BKJQgnvsSFpcKLM5xXVWnvZS97DWHgE=
golang.org/x/exp v0.0.0-20230105202349-8879d0199aa3 h1:fJwx88sMf5RXwDwziL0/Mn9Wqs+efMSo/RYcL+37W9c=
golang.org/x/exp v0.0.0-20230105202349-8879d0199aa3/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
Expand Down Expand Up @@ -201,6 +207,8 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
12 changes: 6 additions & 6 deletions option.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package simplejson

import (
"github.com/clambin/go-common/httpserver"
"github.com/clambin/go-common/httpserver/middleware"
)

// Option specified configuration options for Server
Expand All @@ -21,11 +21,11 @@ func (o WithQueryMetrics) apply(s *Server) {
s.queryMetrics = newQueryMetrics(o.Name)
}

// WithHTTPServerOption will pass the provided option to the underlying HTTP Server
type WithHTTPServerOption struct {
Option httpserver.Option
// WithHTTPMetrics will configure the http router to gather statistics on SimpleJson endpoint calls and record them as Prometheus metrics
type WithHTTPMetrics struct {
Option middleware.PrometheusMetricsOptions
}

func (o WithHTTPServerOption) apply(s *Server) {
s.httpServerOptions = append(s.httpServerOptions, o.Option)
func (o WithHTTPMetrics) apply(s *Server) {
s.prometheusMetrics = middleware.NewPrometheusMetrics(o.Option)
}
2 changes: 1 addition & 1 deletion pkg/data/filter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package data

import (
"github.com/clambin/simplejson/v5"
"github.com/clambin/simplejson/v6"
"time"
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/data/filter_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package data_test

import (
"github.com/clambin/simplejson/v5"
"github.com/clambin/simplejson/v5/pkg/data"
"github.com/clambin/simplejson/v6"
"github.com/clambin/simplejson/v6/pkg/data"
grafanaData "github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/stretchr/testify/assert"
"testing"
Expand Down
2 changes: 1 addition & 1 deletion pkg/data/response.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package data

import (
"github.com/clambin/simplejson/v5"
"github.com/clambin/simplejson/v6"
"github.com/grafana/grafana-plugin-sdk-go/data"
"time"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/data/table_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package data_test

import (
"github.com/clambin/simplejson/v5/pkg/data"
"github.com/clambin/simplejson/v6/pkg/data"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"strconv"
Expand Down
Loading

0 comments on commit 3837ac8

Please sign in to comment.