scanDirectory recursively walks a directory and extracts metrics from all Go files.
(root string)
| 147 | |
| 148 | // scanDirectory recursively walks a directory and extracts metrics from all Go files. |
| 149 | func scanDirectory(root string) ([]Metric, error) { |
| 150 | var metrics []Metric |
| 151 | |
| 152 | err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { |
| 153 | if err != nil { |
| 154 | return err |
| 155 | } |
| 156 | |
| 157 | // Skip non-Go files. |
| 158 | if d.IsDir() || !strings.HasSuffix(path, ".go") { |
| 159 | return nil |
| 160 | } |
| 161 | |
| 162 | // Skip test files. |
| 163 | if strings.HasSuffix(path, "_test.go") { |
| 164 | return nil |
| 165 | } |
| 166 | |
| 167 | // Skip files listed in skipPaths. |
| 168 | for _, sp := range skipPaths { |
| 169 | if path == sp { |
| 170 | return nil |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | fileMetrics, err := scanFile(path) |
| 175 | if err != nil { |
| 176 | return xerrors.Errorf("scanning %s: %w", path, err) |
| 177 | } |
| 178 | |
| 179 | if len(fileMetrics) > 0 { |
| 180 | logf("scanning %s: found %d metrics", path, len(fileMetrics)) |
| 181 | } |
| 182 | metrics = append(metrics, fileMetrics...) |
| 183 | |
| 184 | return nil |
| 185 | }) |
| 186 | |
| 187 | return metrics, err |
| 188 | } |
| 189 | |
| 190 | // scanFile parses a single Go file and extracts all Prometheus metric definitions. |
| 191 | func scanFile(path string) ([]Metric, error) { |
no test coverage detected