Skip to content

Commit

Permalink
Merge branch 'main' into split-describe-by-platform
Browse files Browse the repository at this point in the history
  • Loading branch information
ying-jeanne authored Sep 29, 2024
2 parents 0bfbc37 + 25bda7c commit aecb101
Show file tree
Hide file tree
Showing 15 changed files with 405 additions and 129 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Unreleased

* [BUGFIX] histograms: Fix possible data race when appending exemplars vs metrics gather. #1623

## 1.20.3 / 2024-09-05

* [BUGFIX] histograms: Fix possible data race when appending exemplars. #1608

## 1.20.2 / 2024-08-23

* [BUGFIX] promhttp: Unset Content-Encoding header when data is uncompressed. #1596
Expand Down
6 changes: 6 additions & 0 deletions Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,9 @@ $(1)_precheck:
exit 1; \
fi
endef

govulncheck: install-govulncheck
govulncheck ./...

install-govulncheck:
command -v govulncheck > /dev/null || go install golang.org/x/vuln/cmd/govulncheck@latest
44 changes: 44 additions & 0 deletions examples/customlabels/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2022 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// A simple example of how to add fixed custom labels to all metrics exported by the origin collector.
// For more details, see the documentation: https://pkg.go.dev/github.com/prometheus/client_golang/prometheus#WrapRegistererWith

package main

import (
"flag"
"log"
"net/http"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")

func main() {
flag.Parse()

// Create a new registry.
reg := prometheus.NewRegistry()
prometheus.WrapRegistererWith(prometheus.Labels{"serviceName": "my-service-name"}, reg).MustRegister(
collectors.NewGoCollector(),
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
)

// Expose the registered metrics via HTTP.
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
log.Fatal(http.ListenAndServe(*addr, nil))
}
63 changes: 56 additions & 7 deletions prometheus/collectors/gen_go_collector_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,48 @@ func main() {
v := goVersion(gv.Segments()[1])
log.Printf("generating metrics for Go version %q", v)

descriptions := computeMetricsList()
descriptions := computeMetricsList(metrics.All())
groupedMetrics := groupMetrics(descriptions)

// Find default metrics.
var defaultRuntimeDesc []metrics.Description
for _, d := range metrics.All() {
if !internal.GoCollectorDefaultRuntimeMetrics.MatchString(d.Name) {
continue
}
defaultRuntimeDesc = append(defaultRuntimeDesc, d)
}

defaultRuntimeMetricsList := computeMetricsList(defaultRuntimeDesc)

onlyGCDefRuntimeMetricsList := []string{}
onlySchedDefRuntimeMetricsList := []string{}

for _, m := range defaultRuntimeMetricsList {
if strings.HasPrefix(m, "go_gc") {
onlyGCDefRuntimeMetricsList = append(onlyGCDefRuntimeMetricsList, m)
}
if strings.HasPrefix(m, "go_sched") {
onlySchedDefRuntimeMetricsList = append(onlySchedDefRuntimeMetricsList, m)
} else {
continue
}
}

// Generate code.
var buf bytes.Buffer
err = testFile.Execute(&buf, struct {
GoVersion goVersion
Groups []metricGroup
GoVersion goVersion
Groups []metricGroup
DefaultRuntimeMetricsList []string
OnlyGCDefRuntimeMetricsList []string
OnlySchedDefRuntimeMetricsList []string
}{
GoVersion: v,
Groups: groupedMetrics,
GoVersion: v,
Groups: groupedMetrics,
DefaultRuntimeMetricsList: defaultRuntimeMetricsList,
OnlyGCDefRuntimeMetricsList: onlyGCDefRuntimeMetricsList,
OnlySchedDefRuntimeMetricsList: onlySchedDefRuntimeMetricsList,
})
if err != nil {
log.Fatalf("executing template: %v", err)
Expand All @@ -107,9 +138,9 @@ func main() {
}
}

func computeMetricsList() []string {
func computeMetricsList(descs []metrics.Description) []string {
var metricsList []string
for _, d := range metrics.All() {
for _, d := range descs {
if trans := rm2prom(d); trans != "" {
metricsList = append(metricsList, trans)
}
Expand Down Expand Up @@ -186,4 +217,22 @@ func {{ .Name }}() []string {
})
}
{{ end }}
var (
defaultRuntimeMetrics = []string{
{{- range $metric := .DefaultRuntimeMetricsList }}
{{ $metric | printf "%q"}},
{{- end }}
}
onlyGCDefRuntimeMetrics = []string{
{{- range $metric := .OnlyGCDefRuntimeMetricsList }}
{{ $metric | printf "%q"}},
{{- end }}
}
onlySchedDefRuntimeMetrics = []string{
{{- range $metric := .OnlySchedDefRuntimeMetricsList }}
{{ $metric | printf "%q"}},
{{- end }}
}
)
`))
23 changes: 8 additions & 15 deletions prometheus/collectors/go_collector_go120_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package collectors

import "sort"

func withAllMetrics() []string {
return withBaseMetrics([]string{
"go_cgo_go_to_c_calls_calls_total",
Expand Down Expand Up @@ -119,17 +117,12 @@ func withDebugMetrics() []string {
return withBaseMetrics([]string{})
}

var defaultRuntimeMetrics = []string{
"go_sched_gomaxprocs_threads",
}

func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string {
// If withoutSched is true, exclude "go_sched_gomaxprocs_threads".
if withoutSched {
return metricNames
var (
defaultRuntimeMetrics = []string{
"go_sched_gomaxprocs_threads",
}
metricNames = append(metricNames, defaultRuntimeMetrics...)
// sorting is required
sort.Strings(metricNames)
return metricNames
}
onlyGCDefRuntimeMetrics = []string{}
onlySchedDefRuntimeMetrics = []string{
"go_sched_gomaxprocs_threads",
}
)
38 changes: 13 additions & 25 deletions prometheus/collectors/go_collector_go121_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package collectors

import "sort"

func withAllMetrics() []string {
return withBaseMetrics([]string{
"go_cgo_go_to_c_calls_calls_total",
Expand Down Expand Up @@ -172,27 +170,17 @@ func withDebugMetrics() []string {
})
}

var defaultRuntimeMetrics = []string{
"go_gc_gogc_percent",
"go_gc_gomemlimit_bytes",
"go_sched_gomaxprocs_threads",
}

func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string {
if withoutGC && withoutSched {
// If both flags are true, return the metricNames as is.
return metricNames
} else if withoutGC && !withoutSched {
// If only withoutGC is true, include "go_sched_gomaxprocs_threads" only.
metricNames = append(metricNames, []string{"go_sched_gomaxprocs_threads"}...)
} else if withoutSched && !withoutGC {
// If only withoutSched is true, exclude "go_sched_gomaxprocs_threads".
metricNames = append(metricNames, []string{"go_gc_gogc_percent", "go_gc_gomemlimit_bytes"}...)
} else {
// If neither flag is true, use the default metrics.
metricNames = append(metricNames, defaultRuntimeMetrics...)
var (
defaultRuntimeMetrics = []string{
"go_gc_gogc_percent",
"go_gc_gomemlimit_bytes",
"go_sched_gomaxprocs_threads",
}
// sorting is required
sort.Strings(metricNames)
return metricNames
}
onlyGCDefRuntimeMetrics = []string{
"go_gc_gogc_percent",
"go_gc_gomemlimit_bytes",
}
onlySchedDefRuntimeMetrics = []string{
"go_sched_gomaxprocs_threads",
}
)
38 changes: 13 additions & 25 deletions prometheus/collectors/go_collector_go122_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package collectors

import "sort"

func withAllMetrics() []string {
return withBaseMetrics([]string{
"go_cgo_go_to_c_calls_calls_total",
Expand Down Expand Up @@ -194,27 +192,17 @@ func withDebugMetrics() []string {
})
}

var defaultRuntimeMetrics = []string{
"go_gc_gogc_percent",
"go_gc_gomemlimit_bytes",
"go_sched_gomaxprocs_threads",
}

func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string {
if withoutGC && withoutSched {
// If both flags are true, return the metricNames as is.
return metricNames
} else if withoutGC && !withoutSched {
// If only withoutGC is true, include "go_sched_gomaxprocs_threads" only.
metricNames = append(metricNames, []string{"go_sched_gomaxprocs_threads"}...)
} else if withoutSched && !withoutGC {
// If only withoutSched is true, exclude "go_sched_gomaxprocs_threads".
metricNames = append(metricNames, []string{"go_gc_gogc_percent", "go_gc_gomemlimit_bytes"}...)
} else {
// If neither flag is true, use the default metrics.
metricNames = append(metricNames, defaultRuntimeMetrics...)
var (
defaultRuntimeMetrics = []string{
"go_gc_gogc_percent",
"go_gc_gomemlimit_bytes",
"go_sched_gomaxprocs_threads",
}
// sorting is required
sort.Strings(metricNames)
return metricNames
}
onlyGCDefRuntimeMetrics = []string{
"go_gc_gogc_percent",
"go_gc_gomemlimit_bytes",
}
onlySchedDefRuntimeMetrics = []string{
"go_sched_gomaxprocs_threads",
}
)
38 changes: 13 additions & 25 deletions prometheus/collectors/go_collector_go123_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package collectors

import "sort"

func withAllMetrics() []string {
return withBaseMetrics([]string{
"go_cgo_go_to_c_calls_calls_total",
Expand Down Expand Up @@ -206,27 +204,17 @@ func withDebugMetrics() []string {
})
}

var defaultRuntimeMetrics = []string{
"go_gc_gogc_percent",
"go_gc_gomemlimit_bytes",
"go_sched_gomaxprocs_threads",
}

func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string {
if withoutGC && withoutSched {
// If both flags are true, return the metricNames as is.
return metricNames
} else if withoutGC && !withoutSched {
// If only withoutGC is true, include "go_sched_gomaxprocs_threads" only.
metricNames = append(metricNames, []string{"go_sched_gomaxprocs_threads"}...)
} else if withoutSched && !withoutGC {
// If only withoutSched is true, exclude "go_sched_gomaxprocs_threads".
metricNames = append(metricNames, []string{"go_gc_gogc_percent", "go_gc_gomemlimit_bytes"}...)
} else {
// If neither flag is true, use the default metrics.
metricNames = append(metricNames, defaultRuntimeMetrics...)
var (
defaultRuntimeMetrics = []string{
"go_gc_gogc_percent",
"go_gc_gomemlimit_bytes",
"go_sched_gomaxprocs_threads",
}
// sorting is required
sort.Strings(metricNames)
return metricNames
}
onlyGCDefRuntimeMetrics = []string{
"go_gc_gogc_percent",
"go_gc_gomemlimit_bytes",
}
onlySchedDefRuntimeMetrics = []string{
"go_sched_gomaxprocs_threads",
}
)
17 changes: 17 additions & 0 deletions prometheus/collectors/go_collector_latest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ var memstatMetrics = []string{
"go_memstats_sys_bytes",
}

func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string {
switch {
case withoutGC && !withoutSched:
// If only withoutGC is true, exclude "go_gc_*" metrics.
metricNames = append(metricNames, onlySchedDefRuntimeMetrics...)
case withoutSched && !withoutGC:
// If only withoutSched is true, exclude "go_sched_*" metrics.
metricNames = append(metricNames, onlyGCDefRuntimeMetrics...)
default:
// In any other case, use the default metrics.
metricNames = append(metricNames, defaultRuntimeMetrics...)
}
// sorting is required
sort.Strings(metricNames)
return metricNames
}

func TestGoCollectorMarshalling(t *testing.T) {
reg := prometheus.NewPedanticRegistry()
reg.MustRegister(NewGoCollector(
Expand Down
Loading

0 comments on commit aecb101

Please sign in to comment.