@@ -36,8 +36,8 @@ type Config struct {
3636 LogFormat LogFormat `yaml:"log_format"`
3737 PrometheusURL string `yaml:"prometheus_url"`
3838 RESTAPIPort uint64 `yaml:"rest_api_port"`
39- // Interval is the time in second used to retrieve label and metrics from Prometheus
40- Interval model.Duration `yaml:"interval "`
39+ // MetadataLookbackInterval is the time in second used to retrieve label and metrics from Prometheus
40+ MetadataLookbackInterval model.Duration `yaml:"metadataLookbackInterval "`
4141}
4242
4343// LogFormat is the type used for describing the format of logs.
@@ -55,7 +55,6 @@ var mapLogFormat = map[LogFormat]bool{ // nolint: gochecknoglobals
5555 TextFormat : true ,
5656}
5757
58- // Parameterizable
5958// UnmarshalYAML overrides a function used internally by the yaml.v3 lib.
6059func (c * Config ) UnmarshalYAML (unmarshal func (interface {}) error ) error {
6160 tmp := & Config {}
@@ -78,8 +77,8 @@ func (c *Config) unmarshalENV() error {
7877 PrometheusURL string
7978 // the envconfig lib is not able to convert an empty string to the value 0
8079 // so we have to convert it manually
81- RESTAPIPort string
82- Interval string
80+ RESTAPIPort string
81+ MetadataLookbackInterval string
8382 }{}
8483 if err := envconfig .Process (prefix , conf ); err != nil {
8584 return err
@@ -91,9 +90,9 @@ func (c *Config) unmarshalENV() error {
9190 return parseError
9291 }
9392 }
94- if len (conf .Interval ) > 0 {
93+ if len (conf .MetadataLookbackInterval ) > 0 {
9594 var parseError error
96- c .Interval , parseError = model .ParseDuration (conf .Interval )
95+ c .MetadataLookbackInterval , parseError = model .ParseDuration (conf .MetadataLookbackInterval )
9796 if parseError != nil {
9897 return parseError
9998 }
@@ -120,8 +119,8 @@ func (c *Config) Validate() error {
120119 // default value
121120 c .LogFormat = TextFormat
122121 }
123- if c .Interval <= 0 {
124- c .Interval = defaultInterval
122+ if c .MetadataLookbackInterval <= 0 {
123+ c .MetadataLookbackInterval = defaultInterval
125124 }
126125
127126 return nil
@@ -155,41 +154,91 @@ func readConfigFromENV() (*Config, error) {
155154
156155// DidChangeConfiguration is required by the protocol.Server interface.
157156func (s * server ) DidChangeConfiguration (ctx context.Context , params * protocol.DidChangeConfigurationParams ) error {
158- langserverAddressConfigPath := []string {"promql" , "url" }
157+ if params == nil {
158+ return nil
159+ }
160+ // nolint: errcheck
161+ s .client .LogMessage (
162+ s .lifetime ,
163+ & protocol.LogMessageParams {
164+ Type : protocol .Info ,
165+ Message : fmt .Sprintf ("Received notification change: %v\n " , params ),
166+ })
167+
168+ setting := params .Settings
169+
170+ // the struct expected is the following
171+ // promql:
172+ // url: http://
173+ // interval: 3w
174+ m , ok := setting .(map [string ]map [string ]string )
175+ if ! ok {
176+ // nolint: errcheck
177+ s .client .LogMessage (ctx , & protocol.LogMessageParams {
178+ Type : protocol .Error ,
179+ Message : fmt .Sprint ("unexpected format of the configuration" ),
180+ })
181+ return nil
182+ }
183+ config , ok := m ["promql" ]
184+ if ! ok {
185+ // nolint: errcheck
186+ s .client .LogMessage (ctx , & protocol.LogMessageParams {
187+ Type : protocol .Error ,
188+ Message : fmt .Sprint ("promQL key not found" ),
189+ })
190+ return nil
191+ }
159192
160- if params != nil {
193+ if err := s . setURLFromChangeConfiguration ( config ); err != nil {
161194 // nolint: errcheck
162- s .client .LogMessage (
163- s .lifetime ,
164- & protocol.LogMessageParams {
165- Type : protocol .Info ,
166- Message : fmt .Sprintf ("Received notification change: %v\n " , params ),
167- })
168-
169- setting := params .Settings
170-
171- for _ , e := range langserverAddressConfigPath {
172- m , ok := setting .(map [string ]interface {})
173- if ! ok {
174- break
175- }
176-
177- setting , ok = m [e ]
178- if ! ok {
179- break
180- }
181- }
195+ s .client .LogMessage (ctx , & protocol.LogMessageParams {
196+ Type : protocol .Info ,
197+ Message : err .Error (),
198+ })
199+ }
182200
183- if str , ok := setting .(string ); ok {
184- if err := s .connectPrometheus (str ); err != nil {
185- // nolint: errcheck
186- s .client .LogMessage (ctx , & protocol.LogMessageParams {
187- Type : protocol .Info ,
188- Message : err .Error (),
189- })
190- }
201+ if err := s .setMetadataLookbackIntervalFromChangeConfiguration (config ); err != nil {
202+ // nolint: errcheck
203+ s .client .LogMessage (ctx , & protocol.LogMessageParams {
204+ Type : protocol .Info ,
205+ Message : err .Error (),
206+ })
207+ }
208+ return nil
209+ }
210+
211+ func (s * server ) setURLFromChangeConfiguration (settings map [string ]string ) error {
212+ if promURL , ok := settings ["url" ]; ok {
213+ if _ , err := url .Parse (promURL ); err != nil {
214+ return err
215+ }
216+ if err := s .connectPrometheus (promURL ); err != nil {
217+ return err
191218 }
192219 }
220+ return nil
221+ }
193222
223+ func (s * server ) connectPrometheus (url string ) error {
224+ if err := s .metadataService .ChangeDataSource (url ); err != nil {
225+ // nolint: errcheck
226+ s .client .ShowMessage (s .lifetime , & protocol.ShowMessageParams {
227+ Type : protocol .Error ,
228+ Message : fmt .Sprintf ("Failed to connect to Prometheus at %s:\n \n %s " , url , err .Error ()),
229+ })
230+ return err
231+ }
232+ return nil
233+ }
234+
235+ func (s * server ) setMetadataLookbackIntervalFromChangeConfiguration (settings map [string ]string ) error {
236+ if interval , ok := settings ["metadataLookbackInterval" ]; ok {
237+ duration , err := model .ParseDuration (interval )
238+ if err != nil {
239+ return err
240+ }
241+ s .config .MetadataLookbackInterval = duration
242+ }
194243 return nil
195244}
0 commit comments