|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "regexp" |
| 10 | + "sort" |
| 11 | + |
| 12 | + "gopkg.in/yaml.v2" |
| 13 | +) |
| 14 | + |
| 15 | +// globalConfig is our top-level configuration object |
| 16 | +type globalConfig struct { |
| 17 | + AppDir string `app_dir` // directory with app configurations |
| 18 | + LogLevel string |
| 19 | + Listen httpConfig |
| 20 | + |
| 21 | + Applications map[string]*appConfig |
| 22 | +} |
| 23 | + |
| 24 | +func (gc *globalConfig) init() error { |
| 25 | + // Load application config files |
| 26 | + if gc.AppDir != "" { |
| 27 | + fileInfos, err := ioutil.ReadDir(filepath.Join(gc.AppDir)) |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + |
| 32 | + gc.Applications = map[string]*appConfig{} |
| 33 | + |
| 34 | + for _, fi := range fileInfos { |
| 35 | + if filepath.Ext(fi.Name()) != ".yaml" || fi.IsDir() { |
| 36 | + continue |
| 37 | + } |
| 38 | + path := filepath.Join(gc.AppDir, fi.Name()) |
| 39 | + |
| 40 | + // Check whether symlink and if it points to a regular file |
| 41 | + if !fi.Mode().IsRegular() { |
| 42 | + fi2, err := os.Stat(path) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + if !fi2.Mode().IsRegular() { |
| 48 | + continue |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + yml, err := ioutil.ReadFile(path) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + ac := &appConfig{} |
| 58 | + if err = yaml.UnmarshalStrict(yml, ac); err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + // "/foo/bar.yaml" -> "bar" |
| 62 | + appName := filepath.Base(path[:(len(path) - 5)]) |
| 63 | + gc.Applications[appName] = ac |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + for name := range gc.Applications { |
| 68 | + if err := gc.Applications[name].init(); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + } |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +// httpConfig represents the configuration of the http server. |
| 76 | +type httpConfig struct { |
| 77 | + Address string |
| 78 | + Port int |
| 79 | +} |
| 80 | + |
| 81 | +// appConfig represents a single nginx "application" to export log files for. |
| 82 | +type appConfig struct { |
| 83 | + Format string |
| 84 | + FromBeginning bool `from_beginning` |
| 85 | + Labels map[string]string |
| 86 | + LogFiles []string `log_files` |
| 87 | + |
| 88 | + Exclude []filterConfig |
| 89 | + Include []filterConfig |
| 90 | + Replace []replaceConfig |
| 91 | + |
| 92 | + orderedLabelNames []string |
| 93 | + orderedLabelValues []string |
| 94 | +} |
| 95 | + |
| 96 | +// compileRegexes compiles the various regex strings in appConfig. |
| 97 | +func (ac *appConfig) compileRegexes() error { |
| 98 | + for i := range (*ac).Exclude { |
| 99 | + if err := (*ac).Exclude[i].compileRegex(); err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + for i := range (*ac).Include { |
| 105 | + if err := (*ac).Include[i].compileRegex(); err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + for i := range (*ac).Replace { |
| 111 | + if err := (*ac).Replace[i].compileRegex(); err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + } |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +// init prepares an application config for use |
| 119 | +func (ac *appConfig) init() error { |
| 120 | + for _, lf := range ac.LogFiles { |
| 121 | + if !filepath.IsAbs(lf) { |
| 122 | + return errors.New(fmt.Sprintf("log file '%s': not an absolute path", lf)) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + keys := make([]string, len(ac.Labels)) |
| 127 | + values := make([]string, len(ac.Labels)) |
| 128 | + |
| 129 | + for k := range ac.Labels { |
| 130 | + keys = append(keys, k) |
| 131 | + } |
| 132 | + |
| 133 | + sort.Strings(keys) |
| 134 | + |
| 135 | + for i, k := range keys { |
| 136 | + values[i] = ac.Labels[k] |
| 137 | + } |
| 138 | + |
| 139 | + ac.orderedLabelNames = keys |
| 140 | + ac.orderedLabelValues = values |
| 141 | + |
| 142 | + if err := ac.compileRegexes(); err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +// filterConfig represents an include or exclude filter for paths. |
| 149 | +type filterConfig struct { |
| 150 | + Path *string |
| 151 | + Methods []string |
| 152 | + |
| 153 | + pathRe *regexp.Regexp |
| 154 | +} |
| 155 | + |
| 156 | +// compileRegex compiles the path field regex of filterConfig. |
| 157 | +func (fc *filterConfig) compileRegex() error { |
| 158 | + var err error |
| 159 | + if fc.pathRe, err = regexp.Compile(*fc.Path); err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +// match checks if a method/path combination matches the filter. |
| 166 | +func (fc *filterConfig) match(method string, path string) bool { |
| 167 | + if !fc.pathRe.MatchString(path) { |
| 168 | + return false |
| 169 | + } |
| 170 | + |
| 171 | + if len(fc.Methods) == 0 { |
| 172 | + return true |
| 173 | + } |
| 174 | + |
| 175 | + for i := range fc.Methods { |
| 176 | + if method == fc.Methods[i] { |
| 177 | + return true |
| 178 | + } |
| 179 | + } |
| 180 | + return false |
| 181 | +} |
| 182 | + |
| 183 | +// replaceConfig represents a replacement option for paths. |
| 184 | +type replaceConfig struct { |
| 185 | + filterConfig `,inline` |
| 186 | + |
| 187 | + With string |
| 188 | +} |
| 189 | + |
| 190 | +// replace performs replaceConfig's string replacement. |
| 191 | +func (rc *replaceConfig) replace(s string) string { |
| 192 | + return rc.pathRe.ReplaceAllString(s, rc.With) |
| 193 | +} |
| 194 | + |
| 195 | +// newConfig creates a new global configuration from configuration file |
| 196 | +func newConfig(fileName string) (*globalConfig, error) { |
| 197 | + yml, err := ioutil.ReadFile(fileName) |
| 198 | + if err != nil { |
| 199 | + return nil, err |
| 200 | + } |
| 201 | + |
| 202 | + gc := &globalConfig{ |
| 203 | + LogLevel: "INFO", |
| 204 | + Listen: httpConfig{ |
| 205 | + Address: "0.0.0.0", |
| 206 | + Port: 9900, |
| 207 | + }, |
| 208 | + } |
| 209 | + if err = yaml.UnmarshalStrict(yml, gc); err != nil { |
| 210 | + return nil, err |
| 211 | + } |
| 212 | + |
| 213 | + if err = gc.init(); err != nil { |
| 214 | + return nil, err |
| 215 | + } |
| 216 | + |
| 217 | + return gc, nil |
| 218 | +} |
0 commit comments