|
| 1 | +// Copyright 2020 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +package config |
| 14 | + |
| 15 | +import ( |
| 16 | + "fmt" |
| 17 | + "io/ioutil" |
| 18 | + "net/url" |
| 19 | + "os" |
| 20 | + "strconv" |
| 21 | + |
| 22 | + "github.com/kelseyhightower/envconfig" |
| 23 | + "gopkg.in/yaml.v3" |
| 24 | +) |
| 25 | + |
| 26 | +// ReadConfig gets the GlobalConfig from a configFile (that is a path to the file). |
| 27 | +func ReadConfig(configFile string) (*Config, error) { |
| 28 | + if len(configFile) == 0 { |
| 29 | + fmt.Fprintln(os.Stderr, "No config file provided, configuration is reading from System environment") |
| 30 | + return readConfigFromENV() |
| 31 | + } |
| 32 | + fmt.Fprintln(os.Stderr, "Configuration is reading from configuration file") |
| 33 | + return readConfigFromYAML(configFile) |
| 34 | +} |
| 35 | + |
| 36 | +func readConfigFromYAML(configFile string) (*Config, error) { |
| 37 | + b, err := ioutil.ReadFile(configFile) |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + res := new(Config) |
| 42 | + err = yaml.Unmarshal(b, res) |
| 43 | + return res, err |
| 44 | +} |
| 45 | + |
| 46 | +func readConfigFromENV() (*Config, error) { |
| 47 | + res := new(Config) |
| 48 | + err := res.unmarshalENV() |
| 49 | + return res, err |
| 50 | +} |
| 51 | + |
| 52 | +// LogFormat is the type used for describing the format of logs. |
| 53 | +type LogFormat string |
| 54 | + |
| 55 | +const ( |
| 56 | + // JSONFormat is used for JSON logs. |
| 57 | + JSONFormat LogFormat = "json" |
| 58 | + // TextFormat is used of structured text logs. |
| 59 | + TextFormat LogFormat = "text" |
| 60 | +) |
| 61 | + |
| 62 | +var mapLogFormat = map[LogFormat]bool{ // nolint: gochecknoglobals |
| 63 | + JSONFormat: true, |
| 64 | + TextFormat: true, |
| 65 | +} |
| 66 | + |
| 67 | +// Config contains the configuration for a server. |
| 68 | +type Config struct { |
| 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"` |
| 73 | +} |
| 74 | + |
| 75 | +// UnmarshalYAML overrides a function used internally by the yaml.v3 lib. |
| 76 | +func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { |
| 77 | + tmp := &Config{} |
| 78 | + type plain Config |
| 79 | + if err := unmarshal((*plain)(tmp)); err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + if err := tmp.Validate(); err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + *c = *tmp |
| 86 | + return nil |
| 87 | +} |
| 88 | + |
| 89 | +func (c *Config) unmarshalENV() error { |
| 90 | + prefix := "LANGSERVER" |
| 91 | + conf := &struct { |
| 92 | + ActivateRPCLog bool |
| 93 | + LogFormat string |
| 94 | + PrometheusURL string |
| 95 | + // the envconfig lib is not able to convert an empty string to the value 0 |
| 96 | + // so we have to convert it manually |
| 97 | + RESTAPIPort string |
| 98 | + }{} |
| 99 | + if err := envconfig.Process(prefix, conf); err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + if len(conf.RESTAPIPort) > 0 { |
| 103 | + var parseError error |
| 104 | + c.RESTAPIPort, parseError = strconv.ParseUint(conf.RESTAPIPort, 10, 64) |
| 105 | + if parseError != nil { |
| 106 | + return parseError |
| 107 | + } |
| 108 | + } |
| 109 | + c.ActivateRPCLog = conf.ActivateRPCLog |
| 110 | + c.PrometheusURL = conf.PrometheusURL |
| 111 | + c.LogFormat = LogFormat(conf.LogFormat) |
| 112 | + return c.Validate() |
| 113 | +} |
| 114 | + |
| 115 | +// Validate returns an error if the config is not valid. |
| 116 | +func (c *Config) Validate() error { |
| 117 | + if len(c.PrometheusURL) > 0 { |
| 118 | + if _, err := url.Parse(c.PrometheusURL); err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if len(c.LogFormat) > 0 { |
| 124 | + if !mapLogFormat[c.LogFormat] { |
| 125 | + return fmt.Errorf(`invalid value for logFormat. "%s" Valid values are "%s" or "%s"`, c.LogFormat, TextFormat, JSONFormat) |
| 126 | + } |
| 127 | + } else { |
| 128 | + // default value |
| 129 | + c.LogFormat = TextFormat |
| 130 | + } |
| 131 | + |
| 132 | + return nil |
| 133 | +} |
0 commit comments