* Refactor promlint validation structure
Signed-off-by: João Vilaça <jvilaca@redhat.com>
* Add support for custom validations in promlint
Signed-off-by: João Vilaça <jvilaca@redhat.com>
* Keep backwards compatibility
Signed-off-by: João Vilaça <jvilaca@redhat.com>
---------
Signed-off-by: João Vilaça <jvilaca@redhat.com>
* proccess_collector: add wasip1 stub
This resolves build failures like this when using the wasip1 support:
# github.com/prometheus/procfs
../../../go/pkg/mod/github.com/prometheus/procfs@v0.11.1/fs_statfs_type.go:25:18: undefined: syscall.Statfs_t
../../../go/pkg/mod/github.com/prometheus/procfs@v0.11.1/fs_statfs_type.go:26:17: undefined: syscall.Statfs
See https://go.dev/blog/wasi.
Signed-off-by: Stephan Renatus <stephan@styra.com>
* add build tags
Signed-off-by: Stephan Renatus <stephan@styra.com>
---------
Signed-off-by: Stephan Renatus <stephan@styra.com>
* Extend Counters, Summaries and Histograms with creation timestamp
Signed-off-by: Arthur Silva Sens <arthur.sens@coralogix.com>
* Backport created timestamp to existing tests
Signed-off-by: Arthur Silva Sens <arthur.sens@coralogix.com>
* Last touches (readability and consistency)
Changes:
* Comments for "now" are more explicit and not inlined.
* populateMetrics is simpler and bit more efficient without timestamp to time to timestamp conversionts for more common code.
* Test consistency and simplicity - the fewer variables the better.
* Fixed inconsistency for v2 and MetricVec - let's pass opt.now consistently.
* We don't need TestCounterXXXTimestamp - we test CT in many other places already.
* Added more involved test for counter vectors with created timestamp.
* Refactored normalization for simplicity.
* Make histogram, summaries now consistent.
* Simplified histograms CT flow and implemented proper CT on reset.
TODO for next PRs:
* NewConstSummary and NewConstHistogram - ability to specify CTs there.
Signed-off-by: bwplotka <bwplotka@gmail.com>
* Update prometheus/counter_test.go
Co-authored-by: Arthur Silva Sens <arthursens2005@gmail.com>
Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
---------
Signed-off-by: Arthur Silva Sens <arthur.sens@coralogix.com>
Signed-off-by: bwplotka <bwplotka@gmail.com>
Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
Co-authored-by: bwplotka <bwplotka@gmail.com>
By upgrading prometheus/client_model, several test functions had to be re-written due to 2 breaking changes made in protobuf when parsing messages to text:
1. '<' and '>' characters were replaced with '{' and '}' respectively.
2. The text format is non-deterministic. More information in https://github.com/golang/protobuf/issues/1121
Signed-off-by: Arthur Silva Sens <arthur.sens@coralogix.com>
* Add Go 1.21 support
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
* Update README.md
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
---------
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
Fix a few typos / grammatical errors in the Godoc comments for promauto.
Only trivial stuff but slightly helps readability of the docs.
Signed-off-by: Chris Banks <chris.banks@digital.cabinet-office.gov.uk>
This commit fixes a data race that exists when the metric used in any
`promhttp` middleware doesn't collect the `code` and `method` but uses
`WithLabelFromCtx` to collect values from context.
The problem happens because when no `code` and `method` tags are
collected, the `labels` function returns a pre-initialized map
`emptyLabels` for every request.
When one or multipe `WithLabelFromCtx` options are configured, the
returned map from the `labels` function call is used to collect the
metrics from context which creates a multi-write data race.
Signed-off-by: Tiago Silva <tiago.silva@goteleport.com>
Fixes#1127.
If a native histogram has no observations and a zero threshold of
zero, then it is indistinguishable from a classic histogram. To give
scrapers a hint that it is indeed a native histogram, we add a no-op
span.
This needs follow-up PRs in prometheus/prometheus and
prometheus/client_model.
Signed-off-by: beorn7 <beorn@grafana.com>
We tested this function runtime in both cases using "testing",
and the runtime for this pr is much shorter.
Signed-off-by: alitman <alitman@redhat.com>
* add process start time header to client_golang prometheus
Signed-off-by: Han Kang <hankang@google.com>
* Apply suggestions from code review
Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com>
Signed-off-by: Han Kang <hankang@google.com>
* fix up changes due to incorporation of feedback
* fix lint issues
---------
Signed-off-by: Han Kang <hankang@google.com>
Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com>
The current code doesn't work fork negative schemas if the observed
value should go into a bucket with a non-positive key.
Signed-off-by: beorn7 <beorn@grafana.com>
The current code doesn't work fork negative schemas if the observed
value should go into a bucket with a non-positive key.
Signed-off-by: beorn7 <beorn@grafana.com>
Switch tests over to new proto libary, eliminating the direct dependency
on the old proto library. Note the indirect requirement is still needed
due to upstream use by the client_model package.
Related: https://github.com/prometheus/client_golang/issues/1175
Signed-off-by: SuperQ <superq@gmail.com>
* Indent example in godoc consistently
Signed-off-by: Jon Kartago Lamida <me@lamida.net>
* Add missed one line indentation fix
Signed-off-by: Jon Kartago Lamida <me@lamida.net>
---------
Signed-off-by: Jon Kartago Lamida <me@lamida.net>
This replaces usage of proto.{Float64,Int32,Int64,String,Uint32,Uint64},
which doesn't break the interface.
And remove usage of proto.MarshalTextString in wrap_test.go
Updates: #1175
Signed-off-by: Shengjing Zhu <zhsj@debian.org>
Signed-off-by: Shengjing Zhu <zhsj@debian.org>
* Introduce MetricVecOpts and add constraints to VariableLabels
MetricVecOpts exposes options specific to MetricVec initialisation. The
first option exposed by MetricVecOpts are constraints on VariableLabels,
allowing restrictions on the possible values a label can take, to
prevent cardinality explosion when the label value comes from a
non-trusted source (as a user input or HTTP header).
Signed-off-by: Quentin Devos <4972091+Okhoshi@users.noreply.github.com>
* Add tests
Signed-off-by: Quentin Devos <4972091+Okhoshi@users.noreply.github.com>
Signed-off-by: Quentin Devos <4972091+Okhoshi@users.noreply.github.com>
* Add new Go 1.19 metrics
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
* Format files with the latest formatter
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
* Fix float64 comparison test failure on archs using FMA
Architectures using FMA optimization yield slightly different results so
we cannot assume floating point values will be precisely the same across
different architectures.
The solution in this change is to check "abs(a-b) < tolerance" instead
of comparing the exact values. This will give us confidence that the
histogram buckets are near identical.
Signed-off-by: Seth Bunce <seth.bunce@getcruise.com>
* Apply suggestions from code review
Co-authored-by: Daniel Swarbrick <daniel.swarbrick@gmail.com>
Signed-off-by: Seth Bunce <seth.bunce@getcruise.com>
* copy float compare dependency
Per discussion in the pull request, we'd like to avoid having an extra
dependency on a float comparison package. Instead, we copy the float compare
functions from the float comparison package.
The float comparison package we're choosing is this. The author of this
package has commented in the pull request and it looks like we have consensus
that this is the best option.
github.com/beorn7/floats
Signed-off-by: Seth Bunce <seth.bunce@gmail.com>
* remove float32 variant, relocate into separate file
This change removes the float32 variant of the AlmostEqual funcs, that we will
likely never use. This change also relocates the function into a separate file
to avoid modifying a file that's a fork of another vendored package.
Signed-off-by: Seth Bunce <seth.bunce@gmail.com>
Signed-off-by: Seth Bunce <seth.bunce@getcruise.com>
Signed-off-by: Seth Bunce <seth.bunce@gmail.com>
Co-authored-by: Daniel Swarbrick <daniel.swarbrick@gmail.com>
This intends to avoid confusing users by the subtle difference between
a native histogram and a sparse bucket.
Signed-off-by: beorn7 <beorn@grafana.com>
NaN observations now go to no bucket, but increment count (and
effectively set sum to NaN, too).
±Inf observations now go to the bucket following the bucket that would
have received math.MaxFloat64. The former is now the last bucket that
can be created.
The getLe is modified to return math.MaxFloat64 for the penultimate
possible bucket.
Also add a test for getLe.
Signed-off-by: beorn7 <beorn@grafana.com>
The wording of the documentation is slightly misleading. Before this
commit, it says that the returned collectors are "already registered".
This could be interpreted in two ways, one could think that promauto
keeps some sort of cache of already registered Collectors that are
returned by this package, and the other way is that the Collectors
constructed are registered before being returned. What is actually
happening is the latter, and the wording after this PR leaves no room to
think that the former could be the case.
Signed-off-by: Rafael Franco <me@rafaelfranco.es>
Signed-off-by: Rafael Franco <me@rafaelfranco.es>
* prometheus: implement Collector interface for Registry
This change allows Registries to be used as Collectors.
This enables new instances of Registry to be passed to ephemeral
subroutines for collecting metrics from subroutines which are still
running:
```go
package main
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
)
func main() {
globalReg := prometheus.NewRegistry()
for i := 0; i < 100; i++ {
workerReg := prometheus.WrapRegistererWith(prometheus.Labels{
// Add an ID label so registered metrics from workers don't
// collide.
"worker_id": fmt.Sprintf("%d", i),
}, prometheus.NewRegistry()
globalReg.MustRegister(workerReg)
go func(i int) {
runWorker(workerReg)
// Unregister any metrics the worker may have created.
globalReg.Unregister(workerReg)
}(i)
}
}
// runWorker runs a worker, registering worker-specific metrics.
func runWorker(reg *prometheus.Registry) {
// ... register metrics ...
// ... do work ...
}
```
This change makes it easier to avoid leaking metrics from subroutines
which do not consistently properly unregister metrics.
Signed-off-by: Robert Fratto <robertfratto@gmail.com>
* fix grammar in doc comment
Signed-off-by: Robert Fratto <robertfratto@gmail.com>
* document why Registry implements Collector with example
Signed-off-by: Robert Fratto <robertfratto@gmail.com>
Signed-off-by: Robert Fratto <robertfratto@gmail.com>
* Clarify exemplarAdd by renaming to addWithExemplar
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
* Documenting addWithExemplar
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
* Also rename exemplarObserve to follow the same pattern
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
* Fix build against GopherJS
When building against GopherJS, ThreadCreateProfile and Getpid are not
available.
Return 1 to shim the functions.
Signed-off-by: Christian Stewart <christian@paral.in>
* Fix formatting
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
* Fix linter issue
Move build tags for licence header checks
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
Co-authored-by: Kemal Akkoyun <kakkoyun@gmail.com>
* testutil: Add ScrapeAndCompare
Signed-off-by: sazary <soroosh@azary.ir>
* testutil: Use %w verb wherever we're using an error in fmt.Errorf
Signed-off-by: sazary <soroosh@azary.ir>
* Format
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
Co-authored-by: Kemal Akkoyun <kakkoyun@users.noreply.github.com>
Co-authored-by: Kemal Akkoyun <kakkoyun@gmail.com>
* fix assorted oddities found by golangci-lint
Signed-off-by: Christoph Mewes <christoph@kubermatic.com>
* permanently enable the linters
Signed-off-by: Christoph Mewes <christoph@kubermatic.com>
* post-rebase blues
Signed-off-by: Christoph Mewes <christoph@kubermatic.com>
Note that this is an incompatible change. To scrape this new format,
the Prometheus server needs to be updated at the same time. PR
incoming.
Signed-off-by: beorn7 <beorn@grafana.com>
* Fix convention violating names for generated collector metrics
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
* Add new Go collector example
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
* WIP partial match
Signed-off-by: Zach Stone <zach@giantswarm.io>
* Cleanup
Signed-off-by: Zach Stone <zach@giantswarm.io>
* Comments
Signed-off-by: Zach Stone <zach@giantswarm.io>
* Tests and comments
Signed-off-by: Zach Stone <zach@giantswarm.io>
* Handle properly deleting multiple metrics, update tests
Signed-off-by: Zach Stone <zach@giantswarm.io>
* Comments
Signed-off-by: Zach Stone <zach@giantswarm.io>
* Try using curry values
Signed-off-by: Zach Stone <zach@giantswarm.io>
* Skip curry value to demo
Signed-off-by: Zach Stone <zach@giantswarm.io>
* Fix curry deletion, remove outdated comment.
Signed-off-by: Zach Stone <zach@giantswarm.io>
* Fix logic for deletion of metrics from prior to currying
Signed-off-by: Zach Stone <zach@giantswarm.io>
* Don't match curried values. Update tests.
Signed-off-by: Zach Stone <zach@giantswarm.io>
* Remove unneccesasry helper and todo comments
Signed-off-by: Zach Stone <zach@giantswarm.io>
* Comment about partial matching curried labels
Signed-off-by: Zach Stone <zach@giantswarm.io>
* Simplify curried value check
Signed-off-by: Zach Stone <zach@giantswarm.io>
* Renamed files.
Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
* gocollector: Added options to Go Collector for diffetent collections.
Fixes https://github.com/prometheus/client_golang/issues/983
Also:
* fixed TestMemStatsEquivalence, it was noop before (:
* Removed gc_cpu_fraction metric completely, since it's not working completely for Go1.17+
Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
Add PushContext and AddContext to Pusher, which are context-aware
version of Push and Add respectively. They give a caller the ability
to cancel an HTTP request.
Signed-off-by: Tatsuhiro Tsujikawa <ttsujika@zlab.co.jp>
* Add config to test against go1.18
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
* Try to fix circleci
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
* Add support for exemplars on constHistogram
Co-authored-by: William Perron <william.perron@shopify.com>
Signed-off-by: William Perron <william.perron@shopify.com>
* remove GetExemplars function
Signed-off-by: William Perron <william.perron@shopify.com>
* fixed linting warnings
reduce repetition in constHistogram w/ exemplar
Signed-off-by: William Perron <william.perron@shopify.com>
* Add values to correct bucket
Signed-off-by: William Perron <william.perron@shopify.com>
* Misc fixes
Co-authored-by: Francis Bogsanyi <francis.bogsanyi@shopify.com>
Signed-off-by: William Perron <william.perron@shopify.com>
* avoid panic when there are fewer buckets than exemplars
Co-authored-by: Arun Mahendra <arun.mahendra@shopify.com>
Signed-off-by: William Perron <william.perron@shopify.com>
* Added MustNewMetricWithExemplars that wraps metrics with exemplar (#3)
Changes:
* Make sure to not "leak" dto.Metric
* Reused upper bounds we already have for histogram
* Common code for all types.
Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
Co-authored-by: Arun Mahendra <arun.mahendra@shopify.com>
Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com>
The Go runtime/metrics package currently exports extremely granular
histograms. Exponentially bucket any histogram with unit "seconds"
or "bytes" instead to dramatically reduce the number of buckets, and
thus the number of metrics.
This change also adds a test to check for expected cardinality to
prevent cardinality surprises in the future.
Signed-off-by: Michael Anthony Knyszek <mknyszek@google.com>
A previous PR made it so that the Go 1.17 collector locked only around
uses of rmSampleBuf, but really that means that Metric values may be
sent over the channel containing some values from future metrics.Read
calls. While generally-speaking this isn't a problem, we lose any
consistency guarantees provided by the runtime/metrics package.
Also, that optimization to not just lock around all of Collect was
premature. Truthfully, Collect is called relatively infrequently, and
its critical path is fairly fast (10s of µs). To prove it, this change
also adds a benchmark.
name old time/op new time/op delta
GoCollector-16 43.7µs ± 2% 43.2µs ± 2% ~ (p=0.190 n=9+9)
Note that because the benchmark is single-threaded it actually looks
like it might be getting *slightly* faster, because all those Collect
calls for the Metrics are direct calls instead of interface calls.
Signed-off-by: Michael Anthony Knyszek <mknyszek@google.com>
* Check validity of method and code label values
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
* Use more flexibly functional option pattern for configuration
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
* Update documentation
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
* Simplify
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
* Fix inconsistent method naming
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
This change introduces use of the runtime/metrics package in place of
runtime.MemStats for Go 1.17 or later. The runtime/metrics package was
introduced in Go 1.16, but not all the old metrics were accounted for
until 1.17.
The runtime/metrics package offers several advantages over using
runtime.MemStats:
* The list of metrics and their descriptions are machine-readable,
allowing new metrics to get added without any additional work.
* Detailed histogram-based metrics are now available, offering much
deeper insights into the Go runtime.
* The runtime/metrics API is significantly more efficient than
runtime.MemStats, even with the additional metrics added, because
it does not require any stop-the-world events.
That being said, integrating the package comes with some caveats, some
of which were discussed in #842. Namely:
* The old MemStats-based metrics need to continue working, so they're
exported under their old names backed by equivalent runtime/metrics
metrics.
* Earlier versions of Go need to continue working, so the old code
remains, but behind a build tag.
Finally, a few notes about the implementation:
* This change includes a whole bunch of refactoring to avoid significant
code duplication.
* This change adds a new histogram metric type specifically optimized
for runtime/metrics histograms. This type's methods also include
additional logic to deal with differences in bounds conventions.
* This change makes a whole bunch of decisions about how runtime/metrics
names are translated.
* This change adds a `go generate` script to generate a list of expected
runtime/metrics names for a given Go version for auditing. Users of
new versions of Go will transparently be allowed to use new metrics,
however.
Signed-off-by: Michael Anthony Knyszek <mknyszek@google.com>