Skip to content

Commit 3dbdb6c

Browse files
committed
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 <christian@paral.in>
1 parent 618194d commit 3dbdb6c

File tree

8 files changed

+122
-5
lines changed

8 files changed

+122
-5
lines changed

prometheus/get_pid.go

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

prometheus/get_pid_gopherjs.go

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

prometheus/go_collector.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ 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)
249+
250+
n := getRuntimeNumThreads()
250251
ch <- MustNewConstMetric(c.threadsDesc, GaugeValue, float64(n))
251252

252253
var stats debug.GCStats

prometheus/num_threads.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
// +build !js wasm
15+
16+
package prometheus
17+
18+
import "runtime"
19+
20+
// getRuntimeNumThreads returns the number of open OS threads.
21+
func getRuntimeNumThreads() float64 {
22+
n, _ := runtime.ThreadCreateProfile(nil)
23+
return float64(n)
24+
}

prometheus/num_threads_gopherjs.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
// +build js,!wasm
15+
16+
package prometheus
17+
18+
// getRuntimeNumThreads returns the number of open OS threads.
19+
func getRuntimeNumThreads() float64 {
20+
return 1
21+
}

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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
// +build js
15+
16+
package prometheus
17+
18+
func canCollectProcess() bool {
19+
return false
20+
}
21+
22+
func (c *processCollector) processCollect(ch chan<- Metric) {
23+
// noop on this platform
24+
return
25+
}

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)