From 5eecc011231d1ae585fc00eeb29fa03bfca6af83 Mon Sep 17 00:00:00 2001 From: Gabriel MABILLE Date: Wed, 29 May 2024 10:02:35 +0200 Subject: [PATCH] VSCode: Launch Grafana with Storage server (#88351) * VSCode: Launch Grafana with Storage server * Fix module_server_test --- .vscode/launch.json | 14 +++++++++++--- pkg/server/instrumentation_service.go | 8 +++++--- pkg/server/instrumentation_service_test.go | 7 +++++-- pkg/server/module_server.go | 2 +- pkg/server/module_server_test.go | 3 ++- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index f7f33fa8f60..3acd628a047 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -38,15 +38,23 @@ "--hg-key=$HGAPIKEY" ] }, + { + "name": "Run Server (query GRPC Storage Server)", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${workspaceFolder}/pkg/cmd/grafana/", + "env": {"GF_GRAFANA_APISERVER_STORAGE_TYPE": "unified-grpc"}, + "cwd": "${workspaceFolder}", + "args": ["server", "--homepath", "${workspaceFolder}", "--packaging", "dev"] + }, { "name": "Run Storage Server", "type": "go", "request": "launch", "mode": "auto", "program": "${workspaceFolder}/pkg/cmd/grafana/", - "env": { - "GF_DEFAULT_TARGET": "storage-server" - }, + "env": {"GF_DEFAULT_TARGET": "storage-server","GF_SERVER_HTTP_PORT": "3001"}, "cwd": "${workspaceFolder}", "args": ["server", "target", "--homepath", "${workspaceFolder}", "--packaging", "dev"] }, diff --git a/pkg/server/instrumentation_service.go b/pkg/server/instrumentation_service.go index 138658800cc..7eca2a54fda 100644 --- a/pkg/server/instrumentation_service.go +++ b/pkg/server/instrumentation_service.go @@ -8,18 +8,20 @@ import ( "github.com/grafana/dskit/services" "github.com/grafana/grafana/pkg/infra/log" + "github.com/grafana/grafana/pkg/setting" "github.com/prometheus/client_golang/prometheus/promhttp" ) type instrumentationService struct { *services.BasicService + cfg *setting.Cfg httpServ *http.Server log log.Logger errChan chan error } -func NewInstrumentationService(log log.Logger) (*instrumentationService, error) { - s := &instrumentationService{log: log} +func NewInstrumentationService(log log.Logger, cfg *setting.Cfg) (*instrumentationService, error) { + s := &instrumentationService{log: log, cfg: cfg} s.BasicService = services.NewBasicService(s.start, s.running, s.stop) return s, nil } @@ -59,7 +61,7 @@ func (s *instrumentationService) newInstrumentationServer(ctx context.Context) * srv := &http.Server{ // 5s timeout for header reads to avoid Slowloris attacks (https://thetooth.io/blog/slowloris-attack/) ReadHeaderTimeout: 5 * time.Second, - Addr: ":3000", // TODO - make configurable? + Addr: ":" + s.cfg.HTTPPort, Handler: router, BaseContext: func(_ net.Listener) context.Context { return ctx }, } diff --git a/pkg/server/instrumentation_service_test.go b/pkg/server/instrumentation_service_test.go index 1c4d9bc14cb..51af5ee4032 100644 --- a/pkg/server/instrumentation_service_test.go +++ b/pkg/server/instrumentation_service_test.go @@ -9,13 +9,16 @@ import ( "github.com/grafana/dskit/services" "github.com/grafana/grafana/pkg/infra/log" + "github.com/grafana/grafana/pkg/setting" "github.com/prometheus/client_golang/prometheus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestRunInstrumentationService(t *testing.T) { - s, err := NewInstrumentationService(log.New("test-logger")) + cfg := setting.NewCfg() + cfg.HTTPPort = "3001" + s, err := NewInstrumentationService(log.New("test-logger"), cfg) require.NoError(t, err) ctx, cancel := context.WithTimeout(context.Background(), 300*time.Second) @@ -35,7 +38,7 @@ func TestRunInstrumentationService(t *testing.T) { time.Sleep(100 * time.Millisecond) client := http.Client{} - res, err := client.Get("http://localhost:3000/metrics") + res, err := client.Get("http://localhost:3001/metrics") require.NoError(t, err) assert.Equal(t, 200, res.StatusCode) diff --git a/pkg/server/module_server.go b/pkg/server/module_server.go index a58acaf963b..afafe5803f5 100644 --- a/pkg/server/module_server.go +++ b/pkg/server/module_server.go @@ -113,7 +113,7 @@ func (s *ModuleServer) Run() error { if m.IsModuleEnabled(modules.All) || m.IsModuleEnabled(modules.Core) { return services.NewBasicService(nil, nil, nil).WithName(modules.InstrumentationServer), nil } - return NewInstrumentationService(s.log) + return NewInstrumentationService(s.log, s.cfg) }) m.RegisterModule(modules.Core, func() (services.Service, error) { diff --git a/pkg/server/module_server_test.go b/pkg/server/module_server_test.go index ecfb9c36d7a..a1d25884fc1 100644 --- a/pkg/server/module_server_test.go +++ b/pkg/server/module_server_test.go @@ -34,6 +34,7 @@ func TestIntegrationWillRunInstrumentationServerWhenTargetHasNoHttpServer(t *tes } _, cfg := db.InitTestDBWithCfg(t) + cfg.HTTPPort = "3001" cfg.GRPCServerNetwork = "tcp" cfg.GRPCServerAddress = "localhost:10000" addStorageServerToConfig(t, cfg, dbType) @@ -51,7 +52,7 @@ func TestIntegrationWillRunInstrumentationServerWhenTargetHasNoHttpServer(t *tes time.Sleep(500 * time.Millisecond) // wait for http server to be running client := http.Client{} - res, err := client.Get("http://localhost:3000/metrics") + res, err := client.Get("http://localhost:3001/metrics") require.NoError(t, err) err = res.Body.Close() require.NoError(t, err)