Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ NOTE: As semantic versioning states all 0.y.z releases can contain breaking chan
- [#256](https://github.com/kobsio/kobs/pull/256): [grafana] Add tests for Grafana plugin.
- [#257](https://github.com/kobsio/kobs/pull/257): Add [codecov.io](https://about.codecov.io) for code coverage.
- [#258](https://github.com/kobsio/kobs/pull/258): [rss] Add test for RSS plugin.
- [#261](https://github.com/kobsio/kobs/pull/261): [elasticsearch] Add test for Elasticsearch plugin.

### Fixed

Expand All @@ -49,6 +50,7 @@ NOTE: As semantic versioning states all 0.y.z releases can contain breaking chan
- [#240](https://github.com/kobsio/kobs/pull/240): [core] Switch from `github.com/sirupsen/logrus` to `go.uber.org/zap` for logging and enrich log lines via `context.Context`.
- [#241](https://github.com/kobsio/kobs/pull/241): [core] :warning: _Breaking change:_ :warning: Rework authentication / authorization middleware and adjust the Custom Resource Definition for Users and Teams.
- [#236](https://github.com/kobsio/kobs/pull/236): [core] Improve filtering in select components for various plugins.
- [#260](https://github.com/kobsio/kobs/pull/260): [opsgenie] Adjust permission handling and add actions for incidents.

## [v0.7.0](https://github.com/kobsio/kobs/releases/tag/v0.7.0) (2021-11-19)

Expand Down
2 changes: 1 addition & 1 deletion cmd/kobs/plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func Register(clusters *clusters.Clusters, config Config) chi.Router {
usersRouter := users.Register(clusters, router.plugins, config.Users)
dashboardsRouter := dashboards.Register(clusters, router.plugins, config.Dashboards)
prometheusRouter, prometheusInstances := prometheus.Register(clusters, router.plugins, config.Prometheus)
elasticsearchRouter := elasticsearch.Register(clusters, router.plugins, config.Elasticsearch)
elasticsearchRouter := elasticsearch.Register(router.plugins, config.Elasticsearch)
klogsRouter, klogsInstances := klogs.Register(clusters, router.plugins, config.Klogs)
jaegerRouter := jaeger.Register(clusters, router.plugins, config.Jaeger)
kialiRouter := kiali.Register(clusters, router.plugins, config.Kiali)
Expand Down
27 changes: 11 additions & 16 deletions plugins/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/http"
"strconv"

"github.com/kobsio/kobs/pkg/api/clusters"
"github.com/kobsio/kobs/pkg/api/middleware/errresponse"
"github.com/kobsio/kobs/pkg/api/plugins/plugin"
"github.com/kobsio/kobs/pkg/log"
Expand All @@ -21,16 +20,17 @@ const Route = "/elasticsearch"
// Config is the structure of the configuration for the elasticsearch plugin.
type Config []instance.Config

// Router implements the router for the resources plugin, which can be registered in the router for our rest api.
// Router implements the router for the Elasticsearch plugin, which can be registered in the router for our rest api.
// Next to the routes for the Elasticsearch plugin it also contains a list of all configured Elasticsearch instances.
type Router struct {
*chi.Mux
clusters *clusters.Clusters
instances []*instance.Instance
instances []instance.Instance
}

func (router *Router) getInstance(name string) *instance.Instance {
// getInstance returns an instance by it's name.
func (router *Router) getInstance(name string) instance.Instance {
for _, i := range router.instances {
if i.Name == name {
if i.GetName() == name {
return i
}
}
Expand Down Expand Up @@ -61,14 +61,14 @@ func (router *Router) getLogs(w http.ResponseWriter, r *http.Request) {
parsedTimeStart, err := strconv.ParseInt(timeStart, 10, 64)
if err != nil {
log.Error(r.Context(), "Could not parse start time.", zap.Error(err))
errresponse.Render(w, r, nil, http.StatusBadRequest, "Could not parse start time")
errresponse.Render(w, r, err, http.StatusBadRequest, "Could not parse start time")
return
}

parsedTimeEnd, err := strconv.ParseInt(timeEnd, 10, 64)
if err != nil {
log.Error(r.Context(), "Could not parse end time.", zap.Error(err))
errresponse.Render(w, r, nil, http.StatusBadRequest, "Could not parse end time")
errresponse.Render(w, r, err, http.StatusBadRequest, "Could not parse end time")
return
}

Expand All @@ -83,15 +83,11 @@ func (router *Router) getLogs(w http.ResponseWriter, r *http.Request) {
}

// Register returns a new router which can be used in the router for the kobs rest api.
func Register(clusters *clusters.Clusters, plugins *plugin.Plugins, config Config) chi.Router {
var instances []*instance.Instance
func Register(plugins *plugin.Plugins, config Config) chi.Router {
var instances []instance.Instance

for _, cfg := range config {
instance, err := instance.New(cfg)
if err != nil {
log.Fatal(nil, "Could not create Elasticsearch instance.", zap.Error(err), zap.String("name", cfg.Name))
}

instance := instance.New(cfg)
instances = append(instances, instance)

plugins.Append(plugin.Plugin{
Expand All @@ -104,7 +100,6 @@ func Register(clusters *clusters.Clusters, plugins *plugin.Plugins, config Confi

router := Router{
chi.NewRouter(),
clusters,
instances,
}

Expand Down
120 changes: 120 additions & 0 deletions plugins/elasticsearch/elasticsearch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package elasticsearch

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/kobsio/kobs/pkg/api/plugins/plugin"
"github.com/kobsio/kobs/plugins/elasticsearch/pkg/instance"

"github.com/go-chi/chi/v5"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

var testConfig = Config{
{
Name: "elasticsearch",
DisplayName: "Elasticsearch",
Description: "Elasticsearch can be used for the logs of your application.",
},
}

func TestGetLogs(t *testing.T) {
for _, tt := range []struct {
name string
url string
expectedStatusCode int
expectedBody string
prepare func(*instance.MockInstance)
}{
{
name: "invalid instance name",
url: "/invalidname/logs",
expectedStatusCode: http.StatusBadRequest,
expectedBody: "{\"error\":\"Could not find instance name\"}\n",
prepare: func(mockInstance *instance.MockInstance) {
mockInstance.On("GetName").Return("elasticsearch")
},
},
{
name: "parse time start fails",
url: "/elasticsearch/logs?timeStart=test",
expectedStatusCode: http.StatusBadRequest,
expectedBody: "{\"error\":\"Could not parse start time: strconv.ParseInt: parsing \\\"test\\\": invalid syntax\"}\n",
prepare: func(mockInstance *instance.MockInstance) {
mockInstance.On("GetName").Return("elasticsearch")
},
},
{
name: "parse time end fails",
url: "/elasticsearch/logs?timeStart=0&timeEnd=test",
expectedStatusCode: http.StatusBadRequest,
expectedBody: "{\"error\":\"Could not parse end time: strconv.ParseInt: parsing \\\"test\\\": invalid syntax\"}\n",
prepare: func(mockInstance *instance.MockInstance) {
mockInstance.On("GetName").Return("elasticsearch")
},
},
{
name: "get logs error",
url: "/elasticsearch/logs?timeStart=0&timeEnd=0",
expectedStatusCode: http.StatusInternalServerError,
expectedBody: "{\"error\":\"Could not get logs: bad request\"}\n",
prepare: func(mockInstance *instance.MockInstance) {
mockInstance.On("GetName").Return("elasticsearch")
mockInstance.On("GetLogs", mock.Anything, "", int64(0), int64(0)).Return(nil, fmt.Errorf("bad request"))
},
},
{
name: "get logs success",
url: "/elasticsearch/logs?timeStart=0&timeEnd=0",
expectedStatusCode: http.StatusOK,
expectedBody: "{\"took\":0,\"hits\":0,\"documents\":null,\"buckets\":null}\n",
prepare: func(mockInstance *instance.MockInstance) {
mockInstance.On("GetName").Return("elasticsearch")
mockInstance.On("GetLogs", mock.Anything, "", int64(0), int64(0)).Return(&instance.Data{}, nil)
},
},
} {
t.Run(tt.name, func(t *testing.T) {
mockInstance := &instance.MockInstance{}
mockInstance.AssertExpectations(t)
tt.prepare(mockInstance)

router := Router{chi.NewRouter(), []instance.Instance{mockInstance}}
router.Route("/{name}", func(r chi.Router) {
r.Get("/logs", router.getLogs)
})

req, _ := http.NewRequest(http.MethodGet, tt.url, nil)
rctx := chi.NewRouteContext()
rctx.URLParams.Add("name", strings.Split(tt.url, "/")[1])
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

w := httptest.NewRecorder()
router.getLogs(w, req)

require.Equal(t, tt.expectedStatusCode, w.Code)
require.Equal(t, tt.expectedBody, string(w.Body.Bytes()))
})
}
}

func TestRegister(t *testing.T) {
plugins := &plugin.Plugins{}
router := Register(plugins, testConfig)

require.NotEmpty(t, router)
require.Equal(t, &plugin.Plugins{
plugin.Plugin{
Name: testConfig[0].Name,
DisplayName: testConfig[0].DisplayName,
Description: testConfig[0].Description,
Type: "elasticsearch",
},
}, plugins)
}
32 changes: 22 additions & 10 deletions plugins/elasticsearch/pkg/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,26 @@ type Config struct {
Token string `json:"token"`
}

// Instance represents a single Elasticsearch instance, which can be added via the configuration file.
type Instance struct {
Name string
// Instance is the interface which must be implemented by each configured Elasticsearch instance.
type Instance interface {
GetName() string
GetLogs(ctx context.Context, query string, timeStart, timeEnd int64) (*Data, error)
}

type instance struct {
name string
address string
client *http.Client
}

// Getname returns the name of the Elasticsearch instance, as it was configured by the user.
func (i *instance) GetName() string {
return i.name
}

// GetLogs returns the raw log documents and the buckets for the distribution of the logs accross the selected time
// range. We have to pass a query, start and end time to the function.
func (i *Instance) GetLogs(ctx context.Context, query string, timeStart, timeEnd int64) (*Data, error) {
func (i *instance) GetLogs(ctx context.Context, query string, timeStart, timeEnd int64) (*Data, error) {
var err error
var body []byte
var url string
Expand All @@ -44,7 +54,7 @@ func (i *Instance) GetLogs(ctx context.Context, query string, timeStart, timeEnd

log.Debug(ctx, "Run Elasticsearch query.", zap.ByteString("query", body))

req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(body))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -94,8 +104,10 @@ func (i *Instance) GetLogs(ctx context.Context, query string, timeStart, timeEnd
return nil, fmt.Errorf("%s: %s", res.Error.Type, res.Error.Reason)
}

// New returns a new Elasticsearch instance for the given configuration.
func New(config Config) (*Instance, error) {
// New returns a new Elasticsearch instance for the given configuration. If the configuration contains a username and
// password we will add a basic auth header to each request against the Elasticsearch api. If the config contains a
// token we are adding an authentication header with the token.
func New(config Config) Instance {
roundTripper := roundtripper.DefaultRoundTripper

if config.Username != "" && config.Password != "" {
Expand All @@ -113,11 +125,11 @@ func New(config Config) (*Instance, error) {
}
}

return &Instance{
Name: config.Name,
return &instance{
name: config.Name,
address: config.Address,
client: &http.Client{
Transport: roundTripper,
},
}, nil
}
}
51 changes: 51 additions & 0 deletions plugins/elasticsearch/pkg/instance/instance_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading