Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loki: Implement custom /config handler #4785

Merged
merged 2 commits into from
Nov 26, 2021
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: 1 addition & 1 deletion cmd/loki/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ func main() {

level.Info(util_log.Logger).Log("msg", "Starting Loki", "version", version.Info())

err = t.Run()
err = t.Run(loki.RunOpts{})
util_log.CheckFatal("running loki", err)
}
21 changes: 18 additions & 3 deletions pkg/loki/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,23 @@ func newDefaultConfig() *Config {
return defaultConfig
}

// RunOpts configures custom behavior for running Loki.
type RunOpts struct {
// CustomConfigEndpointHandlerFn is the handlerFunc to be used by the /config endpoint.
// If empty, default handlerFunc will be used.
CustomConfigEndpointHandlerFn func(http.ResponseWriter, *http.Request)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why is this required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, other projects (ex: GEL) aren't able to customize the /config endpoint. That's a problem for projects that uses a superset of configurations that include Loki configs because their configs that aren't part of Loki aren't shown in the /config endpoint.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a very good point. It relates to https://github.com/grafana/loki-private/issues/96 though 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it has some relation to it.

}

func (t *Loki) bindConfigEndpoint(opts RunOpts) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichelHollands Sounds like something you could have used.

configEndpointHandlerFn := configHandler(t.Cfg, newDefaultConfig())
if opts.CustomConfigEndpointHandlerFn != nil {
configEndpointHandlerFn = opts.CustomConfigEndpointHandlerFn
}
t.Server.HTTP.Path("/config").Methods("GET").HandlerFunc(configEndpointHandlerFn)
}

// Run starts Loki running, and blocks until a Loki stops.
func (t *Loki) Run() error {
func (t *Loki) Run(opts RunOpts) error {
serviceMap, err := t.ModuleManager.InitModuleServices(t.Cfg.Target...)
if err != nil {
return err
Expand All @@ -306,8 +321,8 @@ func (t *Loki) Run() error {

grpc_health_v1.RegisterHealthServer(t.Server.GRPC, grpcutil.NewHealthCheck(sm))

// This adds a way to see the config and the changes compared to the defaults
t.Server.HTTP.Path("/config").Methods("GET").HandlerFunc(configHandler(t.Cfg, newDefaultConfig()))
// Config endpoint adds a way to see the config and the changes compared to the defaults.
t.bindConfigEndpoint(opts)

// Each component serves its version.
t.Server.HTTP.Path("/loki/api/v1/status/buildinfo").Methods("GET").HandlerFunc(versionHandler())
Expand Down
80 changes: 80 additions & 0 deletions pkg/loki/loki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package loki
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -86,3 +89,80 @@ func TestLoki_isModuleEnabled(t1 *testing.T) {
})
}
}

func TestLoki_CustomRunOptsBehavior(t *testing.T) {
yamlConfig := `target: querier
server:
http_listen_port: 3100
common:
path_prefix: /tmp/loki
ring:
kvstore:
store: inmemory
schema_config:
configs:
- from: 2020-10-24
store: boltdb-shipper
row_shards: 10
object_store: filesystem
schema: v11
index:
prefix: index_
period: 24h`

cfgWrapper, _, err := configWrapperFromYAML(t, yamlConfig, nil)
require.NoError(t, err)

loki, err := New(cfgWrapper.Config)
require.NoError(t, err)

lokiHealthCheck := func() error {
// wait for Loki HTTP server to be ready.
// retries at most 10 times (1 second in total) to avoid infinite loops when no timeout is set.
for i := 0; i < 10; i++ {
// waits until request to /ready doesn't error.
resp, err := http.DefaultClient.Get("http://localhost:3100/ready")
if err != nil {
time.Sleep(time.Millisecond * 200)
continue
}

// waits until /ready returns OK.
if resp.StatusCode != http.StatusOK {
time.Sleep(time.Millisecond * 200)
continue
}

// Loki is healthy.
return nil
}

return fmt.Errorf("loki HTTP not healthy")
}

customHandlerInvoked := false
customHandler := func(w http.ResponseWriter, _ *http.Request) {
customHandlerInvoked = true
_, err := w.Write([]byte("abc"))
require.NoError(t, err)
}

// Run Loki querier in a different go routine and with custom /config handler.
go func() {
err := loki.Run(RunOpts{CustomConfigEndpointHandlerFn: customHandler})
require.NoError(t, err)
}()

err = lokiHealthCheck()
require.NoError(t, err)

resp, err := http.DefaultClient.Get("http://localhost:3100/config")
require.NoError(t, err)

defer resp.Body.Close()

bBytes, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, string(bBytes), "abc")
assert.True(t, customHandlerInvoked)
}