Skip to content

Commit 900fab5

Browse files
committed
add config ActivateRPCLog
Signed-off-by: Augustin Husson <husson.augustin@gmail.com>
1 parent 868946d commit 900fab5

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

config/config.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ var mapLogFormat = map[LogFormat]bool{ // nolint: gochecknoglobals
6666

6767
// Config contains the configuration for a server.
6868
type Config struct {
69-
LogFormat LogFormat `yaml:"log_format"`
70-
PrometheusURL string `yaml:"prometheus_url"`
71-
RESTAPIPort uint64 `yaml:"rest_api_port"`
69+
ActivateRPCLog bool `yaml:"activate_rpc_log"`
70+
LogFormat LogFormat `yaml:"log_format"`
71+
PrometheusURL string `yaml:"prometheus_url"`
72+
RESTAPIPort uint64 `yaml:"rest_api_port"`
7273
}
7374

7475
// UnmarshalYAML overrides a function used internally by the yaml.v3 lib.
@@ -88,8 +89,9 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
8889
func (c *Config) unmarshalENV() error {
8990
prefix := "LANGSERVER"
9091
conf := &struct {
91-
LogFormat string
92-
PrometheusURL string
92+
ActivateRPCLog bool
93+
LogFormat string
94+
PrometheusURL string
9395
// the envconfig lib is not able to convert an empty string to the value 0
9496
// so we have to convert it manually
9597
RESTAPIPort string
@@ -104,6 +106,7 @@ func (c *Config) unmarshalENV() error {
104106
return parseError
105107
}
106108
}
109+
c.ActivateRPCLog = conf.ActivateRPCLog
107110
c.PrometheusURL = conf.PrometheusURL
108111
c.LogFormat = LogFormat(conf.LogFormat)
109112
return c.Validate()

config/config_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,23 @@ func TestUnmarshalENV(t *testing.T) {
3030
title: "empty config",
3131
variables: map[string]string{},
3232
expected: &Config{
33-
LogFormat: TextFormat,
33+
ActivateRPCLog: false,
34+
LogFormat: TextFormat,
3435
},
3536
},
3637
{
3738
title: "full config",
3839
variables: map[string]string{
39-
"LANGSERVER_PROMETHEUSURL": "http://localhost:9090",
40-
"LANGSERVER_RESTAPIPORT": "8080",
41-
"LANGSERVER_LOGFORMAT": "json",
40+
"LANGSERVER_ACTIVATERPCLOG": "true",
41+
"LANGSERVER_PROMETHEUSURL": "http://localhost:9090",
42+
"LANGSERVER_RESTAPIPORT": "8080",
43+
"LANGSERVER_LOGFORMAT": "json",
4244
},
4345
expected: &Config{
44-
PrometheusURL: "http://localhost:9090",
45-
RESTAPIPort: 8080,
46-
LogFormat: JSONFormat,
46+
ActivateRPCLog: true,
47+
PrometheusURL: "http://localhost:9090",
48+
RESTAPIPort: 8080,
49+
LogFormat: JSONFormat,
4750
},
4851
},
4952
}

langserver/server.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,21 @@ func CreateHeadlessServer(ctx context.Context, metadataService promClient.Metada
106106
func ServerFromStream(ctx context.Context, stream jsonrpc2.Stream, conf *config.Config) (context.Context, Server) {
107107
s := &server{}
108108

109-
switch conf.LogFormat {
110-
case config.TextFormat:
111-
stream = protocol.LoggingStream(stream, os.Stderr)
112-
case config.JSONFormat:
113-
stream = jSONLogStream(stream, os.Stderr)
114-
default:
115-
err := fmt.Errorf("invalid log format: '%s'", conf.LogFormat)
116-
// nolint: errcheck
117-
s.client.ShowMessage(s.lifetime, &protocol.ShowMessageParams{
118-
Type: protocol.Error,
119-
Message: err.Error(),
120-
})
121-
panic(err)
109+
if conf.ActivateRPCLog {
110+
switch conf.LogFormat {
111+
case config.TextFormat:
112+
stream = protocol.LoggingStream(stream, os.Stderr)
113+
case config.JSONFormat:
114+
stream = jSONLogStream(stream, os.Stderr)
115+
default:
116+
err := fmt.Errorf("invalid log format: '%s'", conf.LogFormat)
117+
// nolint: errcheck
118+
s.client.ShowMessage(s.lifetime, &protocol.ShowMessageParams{
119+
Type: protocol.Error,
120+
Message: err.Error(),
121+
})
122+
panic(err)
123+
}
122124
}
123125

124126
s.Conn = jsonrpc2.NewConn(stream)

0 commit comments

Comments
 (0)