Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync with Prometheus up to 2023-04-14 (7309ac27) #480

Merged
merged 36 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
c9d06f2
tsdb: Replay m-map chunk only when required
codesome Mar 13, 2023
1c3f121
tsdb: Test querying after missing wbl with snapshots enabled
codesome Mar 13, 2023
2af44f9
tsdb: Update OOO min/max time properly after replaying m-map chunks
codesome Mar 13, 2023
b987afa
labels: simplify call to get Labels from Builder
bboreham Mar 22, 2023
2fc2e23
remote-write: raise default samples per send to 2,000
bboreham Mar 31, 2023
889ef99
remote-write: adjust MaxShards and Capacity for larger MaxSamplesPerSend
bboreham Apr 2, 2023
e917202
labels: make sure estimated size is not negative
bboreham Apr 2, 2023
c8b408c
build(deps): bump github.com/Azure/go-autorest/autorest/adal
dependabot[bot] Apr 3, 2023
1936868
Allow populate block logic in compact to be overriden outside Prometh…
alexqyle Apr 4, 2023
5588cab
Merge pull request #12173 from bboreham/builder-no-empty-labels
codesome Apr 4, 2023
e709b0b
Merge pull request #12127 from codesome/ooo-mmap-replay
codesome Apr 4, 2023
044b3ec
Merge pull request #12203 from bboreham/raise-max-samples-per-send
csmarchbanks Apr 4, 2023
6cecb87
Generalized rule group iteration evaluation hook (#11885)
soonping-amzn Apr 4, 2023
3914731
Check health & ready: move to flags (#12223)
roidelapluie Apr 5, 2023
8dba916
Merge pull request #12216 from prometheus/dependabot/go_modules/githu…
roidelapluie Apr 5, 2023
83f4398
Add support for native histograms to concreteSeriesIterator
leizor Mar 28, 2023
f90013a
Update storage/remote/codec.go
leizor Apr 5, 2023
cca7178
tsdb: Improve a couple of histogram documentation comments
aknuds1 Apr 7, 2023
6e0a469
Merge pull request #12192 from leizor/leizor/prometheus/issues/11204
beorn7 Apr 11, 2023
8ed90b5
Merge pull request #12234 from aknuds1/chore/improve-histogram-comments
beorn7 Apr 12, 2023
01d0dda
Rename PopulateBlockFunc to BlockPopulator
alexqyle Apr 12, 2023
10cc60a
labels: add ScratchBuilder.Overwrite for slice implementation
bboreham Apr 13, 2023
136b488
Merge pull request #12259 from bboreham/labels-overwrite
bwplotka Apr 13, 2023
c0879d6
promql: Separate `Point` into `FPoint` and `HPoint`
beorn7 Oct 28, 2022
630bcb4
storage: Use separate sample types for histogram vs. float
beorn7 Dec 8, 2022
462240b
storage: add specialized buffers to sampleRing
beorn7 Dec 8, 2022
817a239
Name float values as "floats", not as "values"
beorn7 Mar 30, 2023
551de03
promql: Do not return nil slices to the pool
beorn7 Apr 5, 2023
717a3f8
storage: Manually expand `genericAdd` for specific types
beorn7 Apr 13, 2023
6b8573a
Merge pull request #11687 from prometheus/beorn7/histogram
beorn7 Apr 13, 2023
7309ac2
Merge pull request #12257 from alexqyle/block-populator-rename
codesome Apr 14, 2023
1570114
Merge remote-tracking branch 'upstream/main'
zenador Apr 14, 2023
a537d6c
Merge remote-tracking branch 'upstream/main'
zenador Apr 14, 2023
894f657
Fix bugs from merge
zenador Apr 14, 2023
c22b4ac
Fix comment according to code review
zenador Apr 18, 2023
8bf0d6f
Merge branch 'main' into sync-upstream-14-apr-2023
zenador Apr 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions cmd/promtool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ func main() {
app.HelpFlag.Short('h')

checkCmd := app.Command("check", "Check the resources for validity.")
checkCmd.Flag("http.config.file", "HTTP client configuration file for promtool to connect to Prometheus.").PlaceHolder("<filename>").ExistingFileVar(&httpConfigFilePath)

sdCheckCmd := checkCmd.Command("service-discovery", "Perform service discovery for the given job name and report the results, including relabeling.")
sdConfigFile := sdCheckCmd.Arg("config-file", "The prometheus config file.").Required().ExistingFile()
Expand Down Expand Up @@ -117,16 +116,12 @@ func main() {
).Required().ExistingFiles()

checkServerHealthCmd := checkCmd.Command("healthy", "Check if the Prometheus server is healthy.")
serverHealthURLArg := checkServerHealthCmd.Arg(
"server",
"The URL of the Prometheus server to check (e.g. http://localhost:9090)",
).URL()
checkServerHealthCmd.Flag("http.config.file", "HTTP client configuration file for promtool to connect to Prometheus.").PlaceHolder("<filename>").ExistingFileVar(&httpConfigFilePath)
checkServerHealthCmd.Flag("url", "The URL for the Prometheus server.").Default("http://localhost:9090").URLVar(&serverURL)

checkServerReadyCmd := checkCmd.Command("ready", "Check if the Prometheus server is ready.")
serverReadyURLArg := checkServerReadyCmd.Arg(
"server",
"The URL of the Prometheus server to check (e.g. http://localhost:9090)",
).URL()
checkServerReadyCmd.Flag("http.config.file", "HTTP client configuration file for promtool to connect to Prometheus.").PlaceHolder("<filename>").ExistingFileVar(&httpConfigFilePath)
checkServerReadyCmd.Flag("url", "The URL for the Prometheus server.").Default("http://localhost:9090").URLVar(&serverURL)

checkRulesCmd := checkCmd.Command("rules", "Check if the rule files are valid or not.")
ruleFiles := checkRulesCmd.Arg(
Expand Down Expand Up @@ -292,10 +287,10 @@ func main() {
os.Exit(CheckConfig(*agentMode, *checkConfigSyntaxOnly, newLintConfig(*checkConfigLint, *checkConfigLintFatal), *configFiles...))

case checkServerHealthCmd.FullCommand():
os.Exit(checkErr(CheckServerStatus(*serverHealthURLArg, checkHealth, httpRoundTripper)))
os.Exit(checkErr(CheckServerStatus(serverURL, checkHealth, httpRoundTripper)))

case checkServerReadyCmd.FullCommand():
os.Exit(checkErr(CheckServerStatus(*serverReadyURLArg, checkReadiness, httpRoundTripper)))
os.Exit(checkErr(CheckServerStatus(serverURL, checkReadiness, httpRoundTripper)))

case checkWebConfigCmd.FullCommand():
os.Exit(CheckWebConfig(*webConfigFiles...))
Expand Down Expand Up @@ -390,12 +385,10 @@ func (ls lintConfig) lintDuplicateRules() bool {
return ls.all || ls.duplicateRules
}

const promDefaultURL = "http://localhost:9090"

// Check server status - healthy & ready.
func CheckServerStatus(serverURL *url.URL, checkEndpoint string, roundTripper http.RoundTripper) error {
if serverURL == nil {
serverURL, _ = url.Parse(promDefaultURL)
if serverURL.Scheme == "" {
serverURL.Scheme = "http"
}

config := api.Config{
Expand Down
2 changes: 1 addition & 1 deletion cmd/promtool/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (importer *ruleImporter) importRule(ctx context.Context, ruleExpr, ruleName
})

lb.Set(labels.MetricName, ruleName)
lbls := lb.Labels(labels.EmptyLabels())
lbls := lb.Labels()

for _, value := range sample.Values {
if err := app.add(ctx, lbls, timestamp.FromTime(value.Timestamp.Time()), float64(value.Value)); err != nil {
Expand Down
5 changes: 3 additions & 2 deletions cmd/promtool/unittest.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ Outer:
for _, s := range got {
gotSamples = append(gotSamples, parsedSample{
Labels: s.Metric.Copy(),
Value: s.V,
Value: s.F,
})
}

Expand Down Expand Up @@ -447,7 +447,8 @@ func query(ctx context.Context, qs string, t time.Time, engine *promql.Engine, q
return v, nil
case promql.Scalar:
return promql.Vector{promql.Sample{
Point: promql.Point{T: v.T, V: v.V},
T: v.T,
F: v.V,
Metric: labels.Labels{},
}}, nil
default:
Expand Down
18 changes: 9 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,16 @@ var (

// DefaultQueueConfig is the default remote queue configuration.
DefaultQueueConfig = QueueConfig{
// With a maximum of 200 shards, assuming an average of 100ms remote write
// time and 500 samples per batch, we will be able to push 1M samples/s.
MaxShards: 200,
// With a maximum of 50 shards, assuming an average of 100ms remote write
// time and 2000 samples per batch, we will be able to push 1M samples/s.
MaxShards: 50,
MinShards: 1,
MaxSamplesPerSend: 500,
MaxSamplesPerSend: 2000,

// Each shard will have a max of 2500 samples pending in its channel, plus the pending
// samples that have been enqueued. Theoretically we should only ever have about 3000 samples
// per shard pending. At 200 shards that's 600k.
Capacity: 2500,
// Each shard will have a max of 10,000 samples pending in its channel, plus the pending
// samples that have been enqueued. Theoretically we should only ever have about 12,000 samples
// per shard pending. At 50 shards that's 600k.
Capacity: 10000,
BatchSendDeadline: model.Duration(5 * time.Second),

// Backoff times for retrying a batch of samples on recoverable errors.
Expand All @@ -194,7 +194,7 @@ var (
DefaultMetadataConfig = MetadataConfig{
Send: true,
SendInterval: model.Duration(1 * time.Minute),
MaxSamplesPerSend: 500,
MaxSamplesPerSend: 2000,
}

// DefaultRemoteReadConfig is the default remote read configuration.
Expand Down
19 changes: 10 additions & 9 deletions docs/command-line/promtool.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ Check the resources for validity.

| Flag | Description |
| --- | --- |
| <code class="text-nowrap">--http.config.file</code> | HTTP client configuration file for promtool to connect to Prometheus. |
| <code class="text-nowrap">--extended</code> | Print extended information related to the cardinality of the metrics. |


Expand Down Expand Up @@ -137,11 +136,12 @@ Check if the Prometheus server is healthy.



###### Arguments
###### Flags

| Argument | Description |
| --- | --- |
| server | The URL of the Prometheus server to check (e.g. http://localhost:9090) |
| Flag | Description | Default |
| --- | --- | --- |
| <code class="text-nowrap">--http.config.file</code> | HTTP client configuration file for promtool to connect to Prometheus. | |
| <code class="text-nowrap">--url</code> | The URL for the Prometheus server. | `http://localhost:9090` |



Expand All @@ -152,11 +152,12 @@ Check if the Prometheus server is ready.



###### Arguments
###### Flags

| Argument | Description |
| --- | --- |
| server | The URL of the Prometheus server to check (e.g. http://localhost:9090) |
| Flag | Description | Default |
| --- | --- | --- |
| <code class="text-nowrap">--http.config.file</code> | HTTP client configuration file for promtool to connect to Prometheus. | |
| <code class="text-nowrap">--url</code> | The URL for the Prometheus server. | `http://localhost:9090` |



Expand Down
37 changes: 27 additions & 10 deletions docs/querying/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ _Notes about the experimental native histograms:_
flag](../feature_flags/#native-histograms). As long as no native histograms
have been ingested into the TSDB, all functions will behave as usual.
* Functions that do not explicitly mention native histograms in their
documentation (see below) effectively treat a native histogram as a float
sample of value 0. (This is confusing and will change before native
histograms become a stable feature.)
documentation (see below) will ignore histogram samples.
* Functions that do already act on native histograms might still change their
behavior in the future.
* If a function requires the same bucket layout between multiple native
Expand Down Expand Up @@ -404,6 +402,8 @@ For each timeseries in `v`, `label_join(v instant-vector, dst_label string, sepa
using `separator` and returns the timeseries with the label `dst_label` containing the joined value.
There can be any number of `src_labels` in this function.

`label_join` acts on float and histogram samples in the same way.

This example will return a vector with each time series having a `foo` label with the value `a,b,c` added to it:

```
Expand All @@ -419,6 +419,8 @@ of `replacement`, together with the original labels in the input. Capturing grou
regular expression can be referenced with `$1`, `$2`, etc. If the regular expression doesn't
match then the timeseries is returned unchanged.

`label_replace` acts on float and histogram samples in the same way.

This example will return timeseries with the values `a:c` at label `service` and `a` at label `foo`:

```
Expand Down Expand Up @@ -501,10 +503,21 @@ counter resets when your target restarts.

For each input time series, `resets(v range-vector)` returns the number of
counter resets within the provided time range as an instant vector. Any
decrease in the value between two consecutive samples is interpreted as a
counter reset.

`resets` should only be used with counters.
decrease in the value between two consecutive float samples is interpreted as a
counter reset. A reset in a native histogram is detected in a more complex way:
Any decrease in any bucket, including the zero bucket, or in the count of
observation constitutes a counter reset, but also the disappearance of any
previously populated bucket, an increase in bucket resolution, or a decrease of
the zero-bucket width.

`resets` should only be used with counters and counter-like native
histograms.

If the range vector contains a mix of float and histogram samples for the same
series, counter resets are detected separately and their numbers added up. The
change from a float to a histogram sample is _not_ considered a counter
reset. Each float sample is compared to the next float sample, and each
histogram is comprared to the next histogram.

## `round()`

Expand All @@ -526,7 +539,7 @@ have exactly one element, `scalar` will return `NaN`.
## `sort()`

`sort(v instant-vector)` returns vector elements sorted by their sample values,
in ascending order.
in ascending order. Native histograms are sorted by their sum of observations.

## `sort_desc()`

Expand All @@ -545,7 +558,8 @@ expression is to be evaluated.
## `timestamp()`

`timestamp(v instant-vector)` returns the timestamp of each of the samples of
the given vector as the number of seconds since January 1, 1970 UTC.
the given vector as the number of seconds since January 1, 1970 UTC. It also
works with histogram samples.

## `vector()`

Expand All @@ -569,12 +583,15 @@ over time and return an instant vector with per-series aggregation results:
* `quantile_over_time(scalar, range-vector)`: the φ-quantile (0 ≤ φ ≤ 1) of the values in the specified interval.
* `stddev_over_time(range-vector)`: the population standard deviation of the values in the specified interval.
* `stdvar_over_time(range-vector)`: the population standard variance of the values in the specified interval.
* `last_over_time(range-vector)`: the most recent point value in specified interval.
* `last_over_time(range-vector)`: the most recent point value in the specified interval.
* `present_over_time(range-vector)`: the value 1 for any series in the specified interval.

Note that all values in the specified interval have the same weight in the
aggregation even if the values are not equally spaced throughout the interval.

`count_over_time`, `last_over_time`, and `present_over_time` handle native
histograms as expected. All other functions ignore histogram samples.

## Trigonometric Functions

The trigonometric functions work in radians:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
github.com/Azure/azure-sdk-for-go v65.0.0+incompatible
github.com/Azure/go-autorest/autorest v0.11.28
github.com/Azure/go-autorest/autorest/adal v0.9.22
github.com/Azure/go-autorest/autorest/adal v0.9.23
github.com/alecthomas/kingpin/v2 v2.3.2
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137
github.com/aws/aws-sdk-go v1.44.217
Expand Down
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ github.com/Azure/go-autorest v14.2.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSW
github.com/Azure/go-autorest/autorest v0.11.28 h1:ndAExarwr5Y+GaHE6VCaY1kyS/HwwGGyuimVhWsHOEM=
github.com/Azure/go-autorest/autorest v0.11.28/go.mod h1:MrkzG3Y3AH668QyF9KRk5neJnGgmhQ6krbhR8Q5eMvA=
github.com/Azure/go-autorest/autorest/adal v0.9.18/go.mod h1:XVVeme+LZwABT8K5Lc3hA4nAe8LDBVle26gTrguhhPQ=
github.com/Azure/go-autorest/autorest/adal v0.9.22 h1:/GblQdIudfEM3AWWZ0mrYJQSd7JS4S/Mbzh6F0ov0Xc=
github.com/Azure/go-autorest/autorest/adal v0.9.22/go.mod h1:XuAbAEUv2Tta//+voMI038TrJBqjKam0me7qR+L8Cmk=
github.com/Azure/go-autorest/autorest/adal v0.9.23 h1:Yepx8CvFxwNKpH6ja7RZ+sKX+DWYNldbLiALMC3BTz8=
github.com/Azure/go-autorest/autorest/adal v0.9.23/go.mod h1:5pcMqFkdPhviJdlEy3kC/v1ZLnQl0MH6XA5YCcMhy4c=
github.com/Azure/go-autorest/autorest/date v0.3.0 h1:7gUk1U5M/CQbp9WoqinNzJar+8KY+LPI6wiWrP/myHw=
github.com/Azure/go-autorest/autorest/date v0.3.0/go.mod h1:BI0uouVdmngYNUzGWeSYnokU+TrmwEsOqdt8Y6sso74=
github.com/Azure/go-autorest/autorest/mocks v0.4.1/go.mod h1:LTp+uSrOhSkaKrUy935gNZuuIPPVsHlr9DSOxSayd+k=
Expand Down Expand Up @@ -828,6 +828,7 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -911,6 +912,7 @@ golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -1002,11 +1004,13 @@ golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -1018,6 +1022,7 @@ golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
24 changes: 12 additions & 12 deletions model/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,24 +570,18 @@ func contains(s []Label, n string) bool {
return false
}

// Labels returns the labels from the builder, adding them to res if non-nil.
// Argument res can be the same as b.base, if caller wants to overwrite that slice.
// Labels returns the labels from the builder.
// If no modifications were made, the original labels are returned.
func (b *Builder) Labels(res Labels) Labels {
func (b *Builder) Labels() Labels {
if len(b.del) == 0 && len(b.add) == 0 {
return b.base
}

if res == nil {
// In the general case, labels are removed, modified or moved
// rather than added.
res = make(Labels, 0, len(b.base))
} else {
res = res[:0]
expectedSize := len(b.base) + len(b.add) - len(b.del)
if expectedSize < 1 {
expectedSize = 1
}
// Justification that res can be the same slice as base: in this loop
// we move forward through base, and either skip an element or assign
// it to res at its current position or an earlier position.
res := make(Labels, 0, expectedSize)
for _, l := range b.base {
if slices.Contains(b.del, l.Name) || contains(b.add, l.Name) {
continue
Expand Down Expand Up @@ -637,3 +631,9 @@ func (b *ScratchBuilder) Labels() Labels {
// Copy the slice, so the next use of ScratchBuilder doesn't overwrite.
return append([]Label{}, b.add...)
}

// Write the newly-built Labels out to ls.
// Callers must ensure that there are no other references to ls, or any strings fetched from it.
func (b *ScratchBuilder) Overwrite(ls *Labels) {
*ls = append((*ls)[:0], b.add...)
}
11 changes: 5 additions & 6 deletions model/labels/labels_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (ls Labels) MatchLabels(on bool, names ...string) Labels {
b.Del(MetricName)
b.Del(names...)
}
return b.Labels(EmptyLabels())
return b.Labels()
}

// Hash returns a hash value for the label set.
Expand Down Expand Up @@ -624,10 +624,9 @@ func contains(s []Label, n string) bool {
return false
}

// Labels returns the labels from the builder, adding them to res if non-nil.
// Argument res can be the same as b.base, if caller wants to overwrite that slice.
// Labels returns the labels from the builder.
// If no modifications were made, the original labels are returned.
func (b *Builder) Labels(res Labels) Labels {
func (b *Builder) Labels() Labels {
if len(b.del) == 0 && len(b.add) == 0 {
return b.base
}
Expand All @@ -637,7 +636,7 @@ func (b *Builder) Labels(res Labels) Labels {
a, d := 0, 0

bufSize := len(b.base.data) + labelsSize(b.add)
buf := make([]byte, 0, bufSize) // TODO: see if we can re-use the buffer from res.
buf := make([]byte, 0, bufSize)
for pos := 0; pos < len(b.base.data); {
oldPos := pos
var lName string
Expand Down Expand Up @@ -812,7 +811,7 @@ func (b *ScratchBuilder) Labels() Labels {
}

// Write the newly-built Labels out to ls, reusing an internal buffer.
// Callers must ensure that there are no other references to ls.
// Callers must ensure that there are no other references to ls, or any strings fetched from it.
func (b *ScratchBuilder) Overwrite(ls *Labels) {
size := labelsSize(b.add)
if size <= cap(b.overwriteBuffer) {
Expand Down
Loading