Skip to content

Commit d44fbbe

Browse files
paralinkakkoyun
andauthored
Fix build against GopherJS (#897)
* 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 <[email protected]> * Fix formatting Signed-off-by: Kemal Akkoyun <[email protected]> * Fix linter issue Move build tags for licence header checks Signed-off-by: Kemal Akkoyun <[email protected]> Co-authored-by: Kemal Akkoyun <[email protected]>
1 parent 1638da9 commit d44fbbe

File tree

8 files changed

+128
-6
lines changed

8 files changed

+128
-6
lines changed

prometheus/get_pid.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2015 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build !js || wasm
15+
// +build !js wasm
16+
17+
package prometheus
18+
19+
import "os"
20+
21+
func getPIDFn() func() (int, error) {
22+
pid := os.Getpid()
23+
return func() (int, error) {
24+
return pid, nil
25+
}
26+
}

prometheus/get_pid_gopherjs.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2015 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build js && !wasm
15+
// +build js,!wasm
16+
17+
package prometheus
18+
19+
func getPIDFn() func() (int, error) {
20+
return func() (int, error) {
21+
return 1, nil
22+
}
23+
}

prometheus/go_collector.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,9 @@ func (c *baseGoCollector) Describe(ch chan<- *Desc) {
246246
// Collect returns the current state of all metrics of the collector.
247247
func (c *baseGoCollector) Collect(ch chan<- Metric) {
248248
ch <- MustNewConstMetric(c.goroutinesDesc, GaugeValue, float64(runtime.NumGoroutine()))
249-
n, _ := runtime.ThreadCreateProfile(nil)
250-
ch <- MustNewConstMetric(c.threadsDesc, GaugeValue, float64(n))
249+
250+
n := getRuntimeNumThreads()
251+
ch <- MustNewConstMetric(c.threadsDesc, GaugeValue, n)
251252

252253
var stats debug.GCStats
253254
stats.PauseQuantiles = make([]time.Duration, 5)

prometheus/num_threads.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2018 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build !js || wasm
15+
// +build !js wasm
16+
17+
package prometheus
18+
19+
import "runtime"
20+
21+
// getRuntimeNumThreads returns the number of open OS threads.
22+
func getRuntimeNumThreads() float64 {
23+
n, _ := runtime.ThreadCreateProfile(nil)
24+
return float64(n)
25+
}

prometheus/num_threads_gopherjs.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2018 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build js && !wasm
15+
// +build js,!wasm
16+
17+
package prometheus
18+
19+
// getRuntimeNumThreads returns the number of open OS threads.
20+
func getRuntimeNumThreads() float64 {
21+
return 1
22+
}

prometheus/process_collector.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ func NewProcessCollector(opts ProcessCollectorOpts) Collector {
103103
}
104104

105105
if opts.PidFn == nil {
106-
pid := os.Getpid()
107-
c.pidFn = func() (int, error) { return pid, nil }
106+
c.pidFn = getPIDFn()
108107
} else {
109108
c.pidFn = opts.PidFn
110109
}

prometheus/process_collector_js.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2019 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build js
15+
// +build js
16+
17+
package prometheus
18+
19+
func canCollectProcess() bool {
20+
return false
21+
}
22+
23+
func (c *processCollector) processCollect(ch chan<- Metric) {
24+
// noop on this platform
25+
return
26+
}

prometheus/process_collector_other.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
//go:build !windows
15-
// +build !windows
14+
//go:build !windows && !js
15+
// +build !windows,!js
1616

1717
package prometheus
1818

0 commit comments

Comments
 (0)