Skip to content

Commit 7f28def

Browse files
committed
initialized the prometheus client into two steps
Signed-off-by: Augustin Husson <husson.augustin@gmail.com>
1 parent 900fab5 commit 7f28def

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

langserver/general.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,32 @@ func (s *server) Initialize(_ context.Context, _ *protocol.ParamInitialize) (*pr
5858

5959
// Initialized receives a confirmation by the client that the connection has been initialized
6060
// required by the protocol.Server interface.
61-
func (s *server) Initialized(_ context.Context, _ *protocol.InitializedParams) (err error) {
61+
func (s *server) Initialized(ctx context.Context, _ *protocol.InitializedParams) error {
6262
s.stateMu.Lock()
6363
defer s.stateMu.Unlock()
6464

6565
if s.state != serverInitializing {
6666
return errors.New("cannot initialize server: wrong server state")
6767
}
6868

69-
s.state = serverInitialized
69+
if len(s.prometheusURL) > 0 {
70+
if err := s.connectPrometheus(s.prometheusURL); err != nil {
71+
// nolint: errcheck
72+
s.client.LogMessage(ctx, &protocol.LogMessageParams{
73+
Type: protocol.Info,
74+
Message: err.Error(),
75+
})
76+
}
77+
} else {
78+
// nolint: errcheck
79+
s.client.LogMessage(ctx, &protocol.LogMessageParams{
80+
Type: protocol.Info,
81+
Message: "No Prometheus",
82+
})
83+
}
7084

71-
return err
85+
s.state = serverInitialized
86+
return nil
7287
}
7388

7489
// Shutdown receives a call from the client to shutdown the connection

langserver/server.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ type server struct {
5959
cache cache.DocumentCache
6060

6161
metadataService promClient.MetadataService
62+
// prometheusURL is the url to the prometheus datasource
63+
// this attribute shouldn't be used or set by other things except the configuration and the method server#Initialized
64+
// it shouldn't be used basically when the server is used as an HTTP server
65+
prometheusURL string
6266

6367
lifetime context.Context
6468
exit func()
@@ -130,7 +134,9 @@ func ServerFromStream(ctx context.Context, stream jsonrpc2.Stream, conf *config.
130134

131135
s.lifetime, s.exit = context.WithCancel(ctx)
132136

133-
prometheusClient, err := promClient.NewClient(conf.PrometheusURL)
137+
// In order to have an error message in the IDE/editor, we are going to set the prometheusURL in the method server#Initialized.
138+
s.prometheusURL = conf.PrometheusURL
139+
prometheusClient, err := promClient.NewClient("")
134140
if err != nil {
135141
// nolint: errcheck
136142
s.client.ShowMessage(s.lifetime, &protocol.ShowMessageParams{

0 commit comments

Comments
 (0)