Skip to content

Commit 84c6b9d

Browse files
authored
Merge pull request #762 from prometheus/beorn7/doc
Improve doc comments in promauto
2 parents 056e8af + 3ba240a commit 84c6b9d

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

prometheus/promauto/auto.go

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -127,29 +127,30 @@
127127
// separate package?
128128
//
129129
// The main problem is that registration may fail, e.g. if a metric inconsistent
130-
// with the newly to be registered one is already registered. Therefore, the
131-
// Register method in the prometheus.Registerer interface returns an error, and
132-
// the same is the case for the top-level prometheus.Register function that
133-
// registers with the global registry. The prometheus package also provides
134-
// MustRegister versions for both. They panic if the registration fails, and
135-
// they clearly call this out by using the Must… idiom. Panicking is a bit
136-
// problematic here because it doesn't just happen on input provided by the
137-
// caller that is invalid on its own. Things are a bit more subtle here: Metric
138-
// creation and registration tend to be spread widely over the codebase. It can
139-
// easily happen that an incompatible metric is added to an unrelated part of
140-
// the code, and suddenly code that used to work perfectly fine starts to panic
141-
// (provided that the registration of the newly added metric happens before the
142-
// registration of the previously existing metric). This may come as an even
143-
// bigger surprise with the global registry, where simply importing another
144-
// package can trigger a panic (if the newly imported package registers metrics
145-
// in its init function). At least, in the prometheus package, creation of
146-
// metrics and other collectors is separate from registration. You first create
147-
// the metric, and then you decide explicitly if you want to register it with a
148-
// local or the global registry, and if you want to handle the error or risk a
149-
// panic. With the constructors in the promauto package, registration is
150-
// automatic, and if it fails, it will always panic. Furthermore, the
151-
// constructors will often be called in the var section of a file, which means
152-
// that panicking will happen as a side effect of merely importing a package.
130+
// with or equal to the newly to be registered one is already registered.
131+
// Therefore, the Register method in the prometheus.Registerer interface returns
132+
// an error, and the same is the case for the top-level prometheus.Register
133+
// function that registers with the global registry. The prometheus package also
134+
// provides MustRegister versions for both. They panic if the registration
135+
// fails, and they clearly call this out by using the Must… idiom. Panicking is
136+
// problematic in this case because it doesn't just happen on input provided by
137+
// the caller that is invalid on its own. Things are a bit more subtle here:
138+
// Metric creation and registration tend to be spread widely over the
139+
// codebase. It can easily happen that an incompatible metric is added to an
140+
// unrelated part of the code, and suddenly code that used to work perfectly
141+
// fine starts to panic (provided that the registration of the newly added
142+
// metric happens before the registration of the previously existing
143+
// metric). This may come as an even bigger surprise with the global registry,
144+
// where simply importing another package can trigger a panic (if the newly
145+
// imported package registers metrics in its init function). At least, in the
146+
// prometheus package, creation of metrics and other collectors is separate from
147+
// registration. You first create the metric, and then you decide explicitly if
148+
// you want to register it with a local or the global registry, and if you want
149+
// to handle the error or risk a panic. With the constructors in the promauto
150+
// package, registration is automatic, and if it fails, it will always
151+
// panic. Furthermore, the constructors will often be called in the var section
152+
// of a file, which means that panicking will happen as a side effect of merely
153+
// importing a package.
153154
//
154155
// A separate package allows conservative users to entirely ignore it. And
155156
// whoever wants to use it, will do so explicitly, with an opportunity to read
@@ -252,7 +253,8 @@ type Factory struct {
252253
}
253254

254255
// With creates a Factory using the provided Registerer for registration of the
255-
// created Collectors.
256+
// created Collectors. If the provided Registerer is nil, the returned Factory
257+
// creates Collectors that are not registered with any Registerer.
256258
func With(r prometheus.Registerer) Factory { return Factory{r} }
257259

258260
// NewCounter works like the function of the same name in the prometheus package

0 commit comments

Comments
 (0)