Skip to content

Commit

Permalink
Merge pull request #651 from grafana/prometheus-main-64a9abb8be
Browse files Browse the repository at this point in the history
Update to upstream 64a9abb (`LabelValuesFor` signature change)
  • Loading branch information
colega authored Jun 17, 2024
2 parents 171cafc + 6af02ae commit 26c8bac
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 87 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<h1 align="center" style="border-bottom: none">
<a href="//prometheus.io" target="_blank"><img alt="Prometheus" src="/documentation/images/prometheus-logo.svg"></a><br>Prometheus
<a href="https://prometheus.io" target="_blank"><img alt="Prometheus" src="/documentation/images/prometheus-logo.svg"></a><br>Prometheus
</h1>

<p align="center">Visit <a href="//prometheus.io" target="_blank">prometheus.io</a> for the full documentation,
<p align="center">Visit <a href="https://prometheus.io" target="_blank">prometheus.io</a> for the full documentation,
examples and guides.</p>

<div align="center">
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ var (

DefaultRuntimeConfig = RuntimeConfig{
// Go runtime tuning.
GoGC: 50,
GoGC: 75,
}

// DefaultScrapeConfig is the default scrape configuration.
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ runtime:
# Configure the Go garbage collector GOGC parameter
# See: https://tip.golang.org/doc/gc-guide#GOGC
# Lowering this number increases CPU usage.
[ gogc: <int> | default = 50 ]
[ gogc: <int> | default = 75 ]

# Rule files specifies a list of globs. Rules and alerts are read from
# all matching files.
Expand Down
12 changes: 10 additions & 2 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ production deployments it is highly recommended to use a
[named volume](https://docs.docker.com/storage/volumes/)
to ease managing the data on Prometheus upgrades.

To provide your own configuration, there are several options. Here are
two examples.
### Setting command line parameters

The Docker image is started with a number of default command line parameters, which
can be found in the [Dockerfile](https://github.com/prometheus/prometheus/blob/main/Dockerfile) (adjust the link to correspond with the version in use).

If you want to add extra command line parameters to the `docker run` command,
you will need to re-add these yourself as they will be overwritten.

### Volumes & bind-mount

To provide your own configuration, there are several options. Here are
two examples.

Bind-mount your `prometheus.yml` from the host by running:

```bash
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ require (
golang.org/x/oauth2 v0.21.0
golang.org/x/sync v0.7.0
golang.org/x/sys v0.21.0
golang.org/x/text v0.16.0
golang.org/x/time v0.5.0
golang.org/x/tools v0.22.0
google.golang.org/api v0.183.0
Expand Down Expand Up @@ -194,7 +195,6 @@ require (
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
44 changes: 42 additions & 2 deletions model/labels/regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import (
"slices"
"strings"
"time"
"unicode"
"unicode/utf8"

"github.com/DmitriyVTitov/size"
"github.com/dgraph-io/ristretto"
"github.com/grafana/regexp"
"github.com/grafana/regexp/syntax"
"golang.org/x/text/unicode/norm"
)

const (
Expand Down Expand Up @@ -812,7 +814,7 @@ type equalMultiStringMapMatcher struct {

func (m *equalMultiStringMapMatcher) add(s string) {
if !m.caseSensitive {
s = strings.ToLower(s)
s = toNormalisedLower(s)
}

m.values[s] = struct{}{}
Expand All @@ -832,13 +834,51 @@ func (m *equalMultiStringMapMatcher) setMatches() []string {

func (m *equalMultiStringMapMatcher) Matches(s string) bool {
if !m.caseSensitive {
s = strings.ToLower(s)
s = toNormalisedLower(s)
}

_, ok := m.values[s]
return ok
}

// toNormalisedLower normalise the input string using "Unicode Normalization Form D" and then convert
// it to lower case.
func toNormalisedLower(s string) string {
// Check if the string is all ASCII chars and convert any upper case character to lower case character.
isASCII := true
var (
b strings.Builder
pos int
)
b.Grow(len(s))
for i := 0; i < len(s); i++ {
c := s[i]
if isASCII && c >= utf8.RuneSelf {
isASCII = false
break
}
if 'A' <= c && c <= 'Z' {
c += 'a' - 'A'
if pos < i {
b.WriteString(s[pos:i])
}
b.WriteByte(c)
pos = i + 1
}
}
if pos < len(s) {
b.WriteString(s[pos:])
}

// Optimize for ASCII-only strings. In this case we don't have to do any normalization.
if isASCII {
return b.String()
}

// Normalise and convert to lower.
return strings.Map(unicode.ToLower, norm.NFKD.String(b.String()))
}

// anyStringWithoutNewlineMatcher is a stringMatcher which matches any string
// (including an empty one) as far as it doesn't contain any newline character.
type anyStringWithoutNewlineMatcher struct{}
Expand Down
63 changes: 62 additions & 1 deletion model/labels/regexp_test.go

Large diffs are not rendered by default.

48 changes: 3 additions & 45 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"

common_templates "github.com/prometheus/common/helpers/templates"

"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/util/strutil"
)
Expand Down Expand Up @@ -263,51 +265,7 @@ func NewTemplateExpander(
}
return fmt.Sprintf("%.4g%s", v, prefix), nil
},
"humanizeDuration": func(i interface{}) (string, error) {
v, err := convertToFloat(i)
if err != nil {
return "", err
}
if math.IsNaN(v) || math.IsInf(v, 0) {
return fmt.Sprintf("%.4g", v), nil
}
if v == 0 {
return fmt.Sprintf("%.4gs", v), nil
}
if math.Abs(v) >= 1 {
sign := ""
if v < 0 {
sign = "-"
v = -v
}
duration := int64(v)
seconds := duration % 60
minutes := (duration / 60) % 60
hours := (duration / 60 / 60) % 24
days := duration / 60 / 60 / 24
// For days to minutes, we display seconds as an integer.
if days != 0 {
return fmt.Sprintf("%s%dd %dh %dm %ds", sign, days, hours, minutes, seconds), nil
}
if hours != 0 {
return fmt.Sprintf("%s%dh %dm %ds", sign, hours, minutes, seconds), nil
}
if minutes != 0 {
return fmt.Sprintf("%s%dm %ds", sign, minutes, seconds), nil
}
// For seconds, we display 4 significant digits.
return fmt.Sprintf("%s%.4gs", sign, v), nil
}
prefix := ""
for _, p := range []string{"m", "u", "n", "p", "f", "a", "z", "y"} {
if math.Abs(v) >= 1 {
break
}
prefix = p
v *= 1000
}
return fmt.Sprintf("%.4g%ss", v, prefix), nil
},
"humanizeDuration": common_templates.HumanizeDuration,
"humanizePercentage": func(i interface{}) (string, error) {
v, err := convertToFloat(i)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions tsdb/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ type IndexReader interface {
// This is useful for obtaining label values for other postings than the ones you wish to exclude.
LabelValuesExcluding(p index.Postings, name string) storage.LabelValues

// LabelNamesFor returns all the label names for the series referred to by IDs.
// LabelNamesFor returns all the label names for the series referred to by the postings.
// The names returned are sorted.
LabelNamesFor(ctx context.Context, ids ...storage.SeriesRef) ([]string, error)
LabelNamesFor(ctx context.Context, postings index.Postings) ([]string, error)

// Close releases the underlying resources of the reader.
Close() error
Expand Down Expand Up @@ -590,10 +590,10 @@ func (r blockIndexReader) LabelValueFor(ctx context.Context, id storage.SeriesRe
return r.ir.LabelValueFor(ctx, id, label)
}

// LabelNamesFor returns all the label names for the series referred to by IDs.
// LabelNamesFor returns all the label names for the series referred to by the postings.
// The names returned are sorted.
func (r blockIndexReader) LabelNamesFor(ctx context.Context, ids ...storage.SeriesRef) ([]string, error) {
return r.ir.LabelNamesFor(ctx, ids...)
func (r blockIndexReader) LabelNamesFor(ctx context.Context, postings index.Postings) ([]string, error) {
return r.ir.LabelNamesFor(ctx, postings)
}

type blockTombstoneReader struct {
Expand Down
19 changes: 13 additions & 6 deletions tsdb/head_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,22 +282,29 @@ func (h *headIndexReader) LabelValueFor(_ context.Context, id storage.SeriesRef,
return value, nil
}

// LabelNamesFor returns all the label names for the series referred to by IDs.
// LabelNamesFor returns all the label names for the series referred to by the postings.
// The names returned are sorted.
func (h *headIndexReader) LabelNamesFor(ctx context.Context, ids ...storage.SeriesRef) ([]string, error) {
func (h *headIndexReader) LabelNamesFor(ctx context.Context, series index.Postings) ([]string, error) {
namesMap := make(map[string]struct{})
for _, id := range ids {
if ctx.Err() != nil {
i := 0
for series.Next() {
i++
if i%checkContextEveryNIterations == 0 && ctx.Err() != nil {
return nil, ctx.Err()
}
memSeries := h.head.series.getByID(chunks.HeadSeriesRef(id))
memSeries := h.head.series.getByID(chunks.HeadSeriesRef(series.At()))
if memSeries == nil {
return nil, storage.ErrNotFound
// Series not found, this happens during compaction,
// when series was garbage collected after the caller got the series IDs.
continue
}
memSeries.lset.Range(func(lbl labels.Label) {
namesMap[lbl.Name] = struct{}{}
})
}
if err := series.Err(); err != nil {
return nil, err
}
names := make([]string, 0, len(namesMap))
for name := range namesMap {
names = append(names, name)
Expand Down
9 changes: 6 additions & 3 deletions tsdb/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -1570,11 +1570,14 @@ func (r *Reader) LabelValues(ctx context.Context, name string, matchers ...*labe

// LabelNamesFor returns all the label names for the series referred to by IDs.
// The names returned are sorted.
func (r *Reader) LabelNamesFor(ctx context.Context, ids ...storage.SeriesRef) ([]string, error) {
func (r *Reader) LabelNamesFor(ctx context.Context, postings Postings) ([]string, error) {
// Gather offsetsMap the name offsetsMap in the symbol table first
offsetsMap := make(map[uint32]struct{})
for _, id := range ids {
if ctx.Err() != nil {
i := 0
for postings.Next() {
id := postings.At()

if i%checkContextEveryNIterations == 0 && ctx.Err() != nil {
return nil, ctx.Err()
}

Expand Down
2 changes: 1 addition & 1 deletion tsdb/ooo_head_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ func (ir *OOOCompactionHeadIndexReader) LabelValueFor(context.Context, storage.S
return "", errors.New("not implemented")
}

func (ir *OOOCompactionHeadIndexReader) LabelNamesFor(ctx context.Context, ids ...storage.SeriesRef) ([]string, error) {
func (ir *OOOCompactionHeadIndexReader) LabelNamesFor(ctx context.Context, postings index.Postings) ([]string, error) {
return nil, errors.New("not implemented")
}

Expand Down
11 changes: 1 addition & 10 deletions tsdb/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,16 +557,7 @@ func labelNamesWithMatchers(ctx context.Context, r IndexReader, matchers ...*lab
if err != nil {
return nil, err
}

var postings []storage.SeriesRef
for p.Next() {
postings = append(postings, p.At())
}
if err := p.Err(); err != nil {
return nil, fmt.Errorf("postings for label names with matchers: %w", err)
}

return r.LabelNamesFor(ctx, postings...)
return r.LabelNamesFor(ctx, p)
}

// seriesData, used inside other iterators, are updated when we move from one series to another.
Expand Down
13 changes: 8 additions & 5 deletions tsdb/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2332,13 +2332,16 @@ func (m mockIndex) LabelValueFor(_ context.Context, id storage.SeriesRef, label
return m.series[id].l.Get(label), nil
}

func (m mockIndex) LabelNamesFor(ctx context.Context, ids ...storage.SeriesRef) ([]string, error) {
func (m mockIndex) LabelNamesFor(_ context.Context, postings index.Postings) ([]string, error) {
namesMap := make(map[string]bool)
for _, id := range ids {
m.series[id].l.Range(func(lbl labels.Label) {
for postings.Next() {
m.series[postings.At()].l.Range(func(lbl labels.Label) {
namesMap[lbl.Name] = true
})
}
if err := postings.Err(); err != nil {
return nil, err
}
names := make([]string, 0, len(namesMap))
for name := range namesMap {
names = append(names, name)
Expand Down Expand Up @@ -3301,7 +3304,7 @@ func (m mockMatcherIndex) LabelValueFor(context.Context, storage.SeriesRef, stri
return "", errors.New("label value for called")
}

func (m mockMatcherIndex) LabelNamesFor(ctx context.Context, ids ...storage.SeriesRef) ([]string, error) {
func (m mockMatcherIndex) LabelNamesFor(ctx context.Context, postings index.Postings) ([]string, error) {
return nil, errors.New("label names for for called")
}

Expand Down Expand Up @@ -3896,7 +3899,7 @@ func (m mockReaderOfLabels) LabelNames(context.Context, ...*labels.Matcher) ([]s
panic("LabelNames called")
}

func (m mockReaderOfLabels) LabelNamesFor(context.Context, ...storage.SeriesRef) ([]string, error) {
func (m mockReaderOfLabels) LabelNamesFor(context.Context, index.Postings) ([]string, error) {
panic("LabelNamesFor called")
}

Expand Down
4 changes: 2 additions & 2 deletions web/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1761,15 +1761,15 @@ func (api *API) respond(w http.ResponseWriter, req *http.Request, data interface

b, err := codec.Encode(resp)
if err != nil {
level.Error(api.logger).Log("msg", "error marshaling response", "err", err)
level.Error(api.logger).Log("msg", "error marshaling response", "url", req.URL, "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", codec.ContentType().String())
w.WriteHeader(http.StatusOK)
if n, err := w.Write(b); err != nil {
level.Error(api.logger).Log("msg", "error writing response", "bytesWritten", n, "err", err)
level.Error(api.logger).Log("msg", "error writing response", "url", req.URL, "bytesWritten", n, "err", err)
}
}

Expand Down

0 comments on commit 26c8bac

Please sign in to comment.