Skip to content

Commit

Permalink
Add support for Loki datasource definition
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Dec 23, 2021
1 parent 327484e commit 94087fe
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 0 deletions.
43 changes: 43 additions & 0 deletions datasource/loki/loki.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package loki

import (
"encoding/json"

"github.com/K-Phoen/grabana/datasource"
"github.com/K-Phoen/sdk"
)

var _ datasource.Datasource = Loki{}

type Loki struct {
builder *sdk.Datasource
}

type Option func(datasource *Loki)

func New(name string, url string, options ...Option) Loki {
jaeger := &Loki{
builder: &sdk.Datasource{
Name: name,
Type: "loki",
Access: "proxy",
URL: url,
JSONData: map[string]interface{}{},
SecureJSONData: map[string]interface{}{},
},
}

for _, opt := range options {
opt(jaeger)
}

return *jaeger
}

func (datasource Loki) Name() string {
return datasource.builder.Name
}

func (datasource Loki) MarshalJSON() ([]byte, error) {
return json.Marshal(datasource.builder)
}
22 changes: 22 additions & 0 deletions datasource/loki/loki_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package loki

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestNewLoki(t *testing.T) {
req := require.New(t)

datasource := New("ds-loki", "http://localhost:3100")

req.Equal("ds-loki", datasource.Name())
req.Equal("http://localhost:3100", datasource.builder.URL)
req.Equal("loki", datasource.builder.Type)
req.NotNil(datasource.builder.JSONData)
req.NotNil(datasource.builder.SecureJSONData)

_, err := datasource.MarshalJSON()
req.NoError(err)
}
76 changes: 76 additions & 0 deletions datasource/loki/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package loki

import "time"

// Default configures this datasource to be the default one.
func Default() Option {
return func(datasource *Loki) {
datasource.builder.IsDefault = true
}
}

// Timeout sets the timeout for HTTP requests.
func Timeout(timeout time.Duration) Option {
return func(datasource *Loki) {
datasource.builder.JSONData.(map[string]interface{})["timeout"] = int(timeout.Seconds())
}
}

// BasicAuth configures basic authentication for this datasource.
func BasicAuth(username string, password string) Option {
return func(datasource *Loki) {
yep := true
datasource.builder.BasicAuth = &yep
datasource.builder.BasicAuthUser = &username
datasource.builder.BasicAuthPassword = &password
}
}

// SkipTLSVerify disables verification of SSL certificates.
func SkipTLSVerify() Option {
return func(datasource *Loki) {
datasource.builder.JSONData.(map[string]interface{})["tlsSkipVerify"] = true
}
}

// WithCertificate sets a self-signed certificate that can be verified against.
func WithCertificate(certificate string) Option {
return func(datasource *Loki) {
datasource.builder.JSONData.(map[string]interface{})["tlsSkipVerify"] = false
datasource.builder.JSONData.(map[string]interface{})["tlsAuthWithCACert"] = true
datasource.builder.SecureJSONData.(map[string]interface{})["tlsCACert"] = certificate
}
}

// WithCredentials joins credentials such as cookies or auth headers to cross-site requests.
func WithCredentials() Option {
return func(datasource *Loki) {
datasource.builder.WithCredentials = true
}
}

// ForwardOauthIdentity forward the user's upstream OAuth identity to the data
// source (Their access token gets passed along).
func ForwardOauthIdentity() Option {
return func(datasource *Loki) {
datasource.builder.JSONData.(map[string]interface{})["oauthPassThru"] = true
}
}

// ForwardCookies configures a list of cookies that should be forwarded to the
// datasource.
func ForwardCookies(cookies ...string) Option {
return func(datasource *Loki) {
datasource.builder.JSONData.(map[string]interface{})["keepCookies"] = cookies
}
}

// MaximumLines sets the maximum number of lines returned by Loki (default: 1000).
// Increase this value to have a bigger result set for ad-hoc analysis.
// Decrease this limit if your browser becomes sluggish when displaying the
// log results.
func MaximumLines(max int) Option {
return func(datasource *Loki) {
datasource.builder.JSONData.(map[string]interface{})["maxLines"] = max
}
}
84 changes: 84 additions & 0 deletions datasource/loki/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package loki

import (
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestDefault(t *testing.T) {
req := require.New(t)

datasource := New("", "", Default())

req.True(datasource.builder.IsDefault)
}

func TestBasicAuth(t *testing.T) {
req := require.New(t)

datasource := New("", "", BasicAuth("joe", "lafrite"))

req.True(*datasource.builder.BasicAuth)
req.Equal("joe", *datasource.builder.BasicAuthUser)
req.Equal("lafrite", *datasource.builder.BasicAuthPassword)
}

func TestTimeout(t *testing.T) {
req := require.New(t)

datasource := New("", "", Timeout(30*time.Second))

req.Equal(30, datasource.builder.JSONData.(map[string]interface{})["timeout"])
}

func TestSkipTlsVerify(t *testing.T) {
req := require.New(t)

datasource := New("", "", SkipTLSVerify())

req.Equal(true, datasource.builder.JSONData.(map[string]interface{})["tlsSkipVerify"])
}

func TestWithCertificate(t *testing.T) {
req := require.New(t)

datasource := New("", "", WithCertificate("certificate-content"))

req.Equal(false, datasource.builder.JSONData.(map[string]interface{})["tlsSkipVerify"])
req.Equal(true, datasource.builder.JSONData.(map[string]interface{})["tlsAuthWithCACert"])
req.Equal("certificate-content", datasource.builder.SecureJSONData.(map[string]interface{})["tlsCACert"])
}

func TestWithCredentials(t *testing.T) {
req := require.New(t)

datasource := New("", "", WithCredentials())

req.True(datasource.builder.WithCredentials)
}

func TestForwardOauthIdentity(t *testing.T) {
req := require.New(t)

datasource := New("", "", ForwardOauthIdentity())

req.Equal(true, datasource.builder.JSONData.(map[string]interface{})["oauthPassThru"])
}

func TestForwardCookies(t *testing.T) {
req := require.New(t)

datasource := New("", "", ForwardCookies("foo", "bar"))

req.ElementsMatch([]string{"foo", "bar"}, datasource.builder.JSONData.(map[string]interface{})["keepCookies"])
}

func TestWithNodeGraph(t *testing.T) {
req := require.New(t)

datasource := New("", "", MaximumLines(2000))

req.Equal(2000, datasource.builder.JSONData.(map[string]interface{})["maxLines"])
}

0 comments on commit 94087fe

Please sign in to comment.