1# Contributing
2
3Prometheus uses GitHub to manage reviews of pull requests.
4
5* If you are a new contributor see: [Steps to Contribute](#steps-to-contribute)
6
7* If you have a trivial fix or improvement, go ahead and create a pull request,
8  addressing (with `@...`) a suitable maintainer of this repository (see
9  [MAINTAINERS.md](MAINTAINERS.md)) in the description of the pull request.
10
11* If you plan to do something more involved, first discuss your ideas
12  on our [mailing list](https://groups.google.com/forum/?fromgroups#!forum/prometheus-developers).
13  This will avoid unnecessary work and surely give you and us a good deal
14  of inspiration. Also please see our [non-goals issue](https://github.com/prometheus/docs/issues/149) on areas that the Prometheus community doesn't plan to work on.
15
16* Relevant coding style guidelines are the [Go Code Review
17  Comments](https://code.google.com/p/go-wiki/wiki/CodeReviewComments)
18  and the _Formatting and style_ section of Peter Bourgon's [Go: Best
19  Practices for Production
20  Environments](https://peter.bourgon.org/go-in-production/#formatting-and-style).
21
22* Be sure to sign off on the [DCO](https://github.com/probot/dco#how-it-works)
23
24## Steps to Contribute
25
26Should you wish to work on an issue, please claim it first by commenting on the GitHub issue that you want to work on it. This is to prevent duplicated efforts from contributors on the same issue.
27
28Please check the [`help-wanted`](https://github.com/prometheus/procfs/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) label to find issues that are good for getting started. If you have questions about one of the issues, with or without the tag, please comment on them and one of the maintainers will clarify it. For a quicker response, contact us over [IRC](https://prometheus.io/community).
29
30For quickly compiling and testing your changes do:
31```
32make test         # Make sure all the tests pass before you commit and push :)
33```
34
35We use [`golangci-lint`](https://github.com/golangci/golangci-lint) for linting the code. If it reports an issue and you think that the warning needs to be disregarded or is a false-positive, you can add a special comment `//nolint:linter1[,linter2,...]` before the offending line. Use this sparingly though, fixing the code to comply with the linter's recommendation is in general the preferred course of action.
36
37## Pull Request Checklist
38
39* Branch from the master branch and, if needed, rebase to the current master branch before submitting your pull request. If it doesn't merge cleanly with master you may be asked to rebase your changes.
40
41* Commits should be as small as possible, while ensuring that each commit is correct independently (i.e., each commit should compile and pass tests).
42
43* If your patch is not getting reviewed or you need a specific person to review it, you can @-reply a reviewer asking for a review in the pull request or a comment, or you can ask for a review on IRC channel [#prometheus](https://webchat.freenode.net/?channels=#prometheus) on irc.freenode.net (for the easiest start, [join via Riot](https://riot.im/app/#/room/#prometheus:matrix.org)).
44
45* Add tests relevant to the fixed bug or new feature.
46
47## Dependency management
48
49The Prometheus project uses [Go modules](https://golang.org/cmd/go/#hdr-Modules__module_versions__and_more) to manage dependencies on external packages. This requires a working Go environment with version 1.12 or greater installed.
50
51All dependencies are vendored in the `vendor/` directory.
52
53To add or update a new dependency, use the `go get` command:
54
55```bash
56# Pick the latest tagged release.
57go get example.com/some/module/pkg
58
59# Pick a specific version.
60go get example.com/some/module/pkg@vX.Y.Z
61```
62
63Tidy up the `go.mod` and `go.sum` files and copy the new/updated dependency to the `vendor/` directory:
64
65
66```bash
67# The GO111MODULE variable can be omitted when the code isn't located in GOPATH.
68GO111MODULE=on go mod tidy
69
70GO111MODULE=on go mod vendor
71```
72
73You have to commit the changes to `go.mod`, `go.sum` and the `vendor/` directory before submitting the pull request.
74
75
76## API Implementation Guidelines
77
78### Naming and Documentation
79
80Public functions and structs should normally be named according to the file(s) being read and parsed.  For example,
81the `fs.BuddyInfo()` function reads the file `/proc/buddyinfo`.  In addition, the godoc for each public function
82should contain the path to the file(s) being read and a URL of the linux kernel documentation describing the file(s).
83
84### Reading vs. Parsing
85
86Most functionality in this library consists of reading files and then parsing the text into structured data.  In most
87cases reading and parsing should be separated into different functions/methods with a public `fs.Thing()` method and
88a private `parseThing(r Reader)` function.  This provides a logical separation and allows parsing to be tested
89directly without the need to read from the filesystem.  Using a `Reader` argument is preferred over other data types
90such as `string` or `*File` because it provides the most flexibility regarding the data source.  When a set of files
91in a directory needs to be parsed, then a `path` string parameter to the parse function can be used instead.
92
93### /proc and /sys filesystem I/O
94
95The `proc` and `sys` filesystems are pseudo file systems and work a bit differently from standard disk I/O.
96Many of the files are changing continuously and the data being read can in some cases change between subsequent
97reads in the same file.  Also, most of the files are relatively small (less than a few KBs), and system calls
98to the `stat` function will often return the wrong size.  Therefore, for most files it's recommended to read the
99full file in a single operation using an internal utility function called `util.ReadFileNoStat`.
100This function is similar to `ioutil.ReadFile`, but it avoids the system call to `stat` to get the current size of
101the file.
102
103Note that parsing the file's contents can still be performed one line at a time.  This is done by first reading
104the full file, and then using a scanner on the `[]byte` or `string` containing the data.
105
106```
107    data, err := util.ReadFileNoStat("/proc/cpuinfo")
108    if err != nil {
109        return err
110    }
111    reader := bytes.NewReader(data)
112    scanner := bufio.NewScanner(reader)
113```
114
115The `/sys` filesystem contains many very small files which contain only a single numeric or text value.  These files
116can be read using an internal function called `util.SysReadFile` which is similar to `ioutil.ReadFile` but does
117not bother to check the size of the file before reading.
118```
119    data, err := util.SysReadFile("/sys/class/power_supply/BAT0/capacity")
120```
121
122