From 3dbdb6ca672575e7faed93c3e85976ee20349cae Mon Sep 17 00:00:00 2001 From: Christian Stewart Date: Tue, 3 Aug 2021 18:54:50 -0700 Subject: [PATCH 1/3] 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 --- prometheus/get_pid.go | 25 +++++++++++++++++++++++++ prometheus/get_pid_gopherjs.go | 22 ++++++++++++++++++++++ prometheus/go_collector.go | 3 ++- prometheus/num_threads.go | 24 ++++++++++++++++++++++++ prometheus/num_threads_gopherjs.go | 21 +++++++++++++++++++++ prometheus/process_collector.go | 3 +-- prometheus/process_collector_js.go | 25 +++++++++++++++++++++++++ prometheus/process_collector_other.go | 4 ++-- 8 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 prometheus/get_pid.go create mode 100644 prometheus/get_pid_gopherjs.go create mode 100644 prometheus/num_threads.go create mode 100644 prometheus/num_threads_gopherjs.go create mode 100644 prometheus/process_collector_js.go diff --git a/prometheus/get_pid.go b/prometheus/get_pid.go new file mode 100644 index 000000000..8036aa1ac --- /dev/null +++ b/prometheus/get_pid.go @@ -0,0 +1,25 @@ +// +build !js wasm + +// Copyright 2015 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. + +package prometheus + +import "os" + +func getPIDFn() func() (int, error) { + pid := os.Getpid() + return func() (int, error) { + return pid, nil + } +} diff --git a/prometheus/get_pid_gopherjs.go b/prometheus/get_pid_gopherjs.go new file mode 100644 index 000000000..baa7acc32 --- /dev/null +++ b/prometheus/get_pid_gopherjs.go @@ -0,0 +1,22 @@ +// +build js,!wasm + +// Copyright 2015 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. + +package prometheus + +func getPIDFn() func() (int, error) { + return func() (int, error) { + return 1, nil + } +} diff --git a/prometheus/go_collector.go b/prometheus/go_collector.go index 4d792aa29..8bf4f4011 100644 --- a/prometheus/go_collector.go +++ b/prometheus/go_collector.go @@ -246,7 +246,8 @@ func (c *baseGoCollector) Describe(ch chan<- *Desc) { // Collect returns the current state of all metrics of the collector. func (c *baseGoCollector) Collect(ch chan<- Metric) { ch <- MustNewConstMetric(c.goroutinesDesc, GaugeValue, float64(runtime.NumGoroutine())) - n, _ := runtime.ThreadCreateProfile(nil) + + n := getRuntimeNumThreads() ch <- MustNewConstMetric(c.threadsDesc, GaugeValue, float64(n)) var stats debug.GCStats diff --git a/prometheus/num_threads.go b/prometheus/num_threads.go new file mode 100644 index 000000000..361310752 --- /dev/null +++ b/prometheus/num_threads.go @@ -0,0 +1,24 @@ +// Copyright 2018 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. + +// +build !js wasm + +package prometheus + +import "runtime" + +// getRuntimeNumThreads returns the number of open OS threads. +func getRuntimeNumThreads() float64 { + n, _ := runtime.ThreadCreateProfile(nil) + return float64(n) +} diff --git a/prometheus/num_threads_gopherjs.go b/prometheus/num_threads_gopherjs.go new file mode 100644 index 000000000..771033a2f --- /dev/null +++ b/prometheus/num_threads_gopherjs.go @@ -0,0 +1,21 @@ +// Copyright 2018 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. + +// +build js,!wasm + +package prometheus + +// getRuntimeNumThreads returns the number of open OS threads. +func getRuntimeNumThreads() float64 { + return 1 +} diff --git a/prometheus/process_collector.go b/prometheus/process_collector.go index 930272579..8548dd18e 100644 --- a/prometheus/process_collector.go +++ b/prometheus/process_collector.go @@ -103,8 +103,7 @@ func NewProcessCollector(opts ProcessCollectorOpts) Collector { } if opts.PidFn == nil { - pid := os.Getpid() - c.pidFn = func() (int, error) { return pid, nil } + c.pidFn = getPIDFn() } else { c.pidFn = opts.PidFn } diff --git a/prometheus/process_collector_js.go b/prometheus/process_collector_js.go new file mode 100644 index 000000000..2e2be1209 --- /dev/null +++ b/prometheus/process_collector_js.go @@ -0,0 +1,25 @@ +// Copyright 2019 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. + +// +build js + +package prometheus + +func canCollectProcess() bool { + return false +} + +func (c *processCollector) processCollect(ch chan<- Metric) { + // noop on this platform + return +} diff --git a/prometheus/process_collector_other.go b/prometheus/process_collector_other.go index 2dc3660da..c0152cdb6 100644 --- a/prometheus/process_collector_other.go +++ b/prometheus/process_collector_other.go @@ -11,8 +11,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build !windows -// +build !windows +//go:build !windows && !js +// +build !windows,!js package prometheus From d3b824ce4658d585ec1a2fbbf74a6abcee36a329 Mon Sep 17 00:00:00 2001 From: Kemal Akkoyun Date: Fri, 5 Aug 2022 15:47:28 +0200 Subject: [PATCH 2/3] Fix formatting Signed-off-by: Kemal Akkoyun --- prometheus/get_pid.go | 1 + prometheus/get_pid_gopherjs.go | 1 + prometheus/num_threads.go | 1 + prometheus/num_threads_gopherjs.go | 1 + prometheus/process_collector_js.go | 1 + 5 files changed, 5 insertions(+) diff --git a/prometheus/get_pid.go b/prometheus/get_pid.go index 8036aa1ac..519b56d8b 100644 --- a/prometheus/get_pid.go +++ b/prometheus/get_pid.go @@ -1,3 +1,4 @@ +//go:build !js || wasm // +build !js wasm // Copyright 2015 The Prometheus Authors diff --git a/prometheus/get_pid_gopherjs.go b/prometheus/get_pid_gopherjs.go index baa7acc32..c38e20750 100644 --- a/prometheus/get_pid_gopherjs.go +++ b/prometheus/get_pid_gopherjs.go @@ -1,3 +1,4 @@ +//go:build js && !wasm // +build js,!wasm // Copyright 2015 The Prometheus Authors diff --git a/prometheus/num_threads.go b/prometheus/num_threads.go index 361310752..7c12b2108 100644 --- a/prometheus/num_threads.go +++ b/prometheus/num_threads.go @@ -11,6 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +//go:build !js || wasm // +build !js wasm package prometheus diff --git a/prometheus/num_threads_gopherjs.go b/prometheus/num_threads_gopherjs.go index 771033a2f..7348df01d 100644 --- a/prometheus/num_threads_gopherjs.go +++ b/prometheus/num_threads_gopherjs.go @@ -11,6 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +//go:build js && !wasm // +build js,!wasm package prometheus diff --git a/prometheus/process_collector_js.go b/prometheus/process_collector_js.go index 2e2be1209..b1e363d6c 100644 --- a/prometheus/process_collector_js.go +++ b/prometheus/process_collector_js.go @@ -11,6 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +//go:build js // +build js package prometheus From 4d022cc62404048921b8d5e94ecff7eeafeb22a5 Mon Sep 17 00:00:00 2001 From: Kemal Akkoyun Date: Fri, 5 Aug 2022 15:59:07 +0200 Subject: [PATCH 3/3] Fix linter issue Move build tags for licence header checks Signed-off-by: Kemal Akkoyun --- prometheus/get_pid.go | 6 +++--- prometheus/get_pid_gopherjs.go | 6 +++--- prometheus/go_collector.go | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/prometheus/get_pid.go b/prometheus/get_pid.go index 519b56d8b..614fd61be 100644 --- a/prometheus/get_pid.go +++ b/prometheus/get_pid.go @@ -1,6 +1,3 @@ -//go:build !js || wasm -// +build !js wasm - // Copyright 2015 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. @@ -14,6 +11,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +//go:build !js || wasm +// +build !js wasm + package prometheus import "os" diff --git a/prometheus/get_pid_gopherjs.go b/prometheus/get_pid_gopherjs.go index c38e20750..eaf8059ee 100644 --- a/prometheus/get_pid_gopherjs.go +++ b/prometheus/get_pid_gopherjs.go @@ -1,6 +1,3 @@ -//go:build js && !wasm -// +build js,!wasm - // Copyright 2015 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. @@ -14,6 +11,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +//go:build js && !wasm +// +build js,!wasm + package prometheus func getPIDFn() func() (int, error) { diff --git a/prometheus/go_collector.go b/prometheus/go_collector.go index 8bf4f4011..5fd730043 100644 --- a/prometheus/go_collector.go +++ b/prometheus/go_collector.go @@ -248,7 +248,7 @@ func (c *baseGoCollector) Collect(ch chan<- Metric) { ch <- MustNewConstMetric(c.goroutinesDesc, GaugeValue, float64(runtime.NumGoroutine())) n := getRuntimeNumThreads() - ch <- MustNewConstMetric(c.threadsDesc, GaugeValue, float64(n)) + ch <- MustNewConstMetric(c.threadsDesc, GaugeValue, n) var stats debug.GCStats stats.PauseQuantiles = make([]time.Duration, 5)