Skip to content

Commit

Permalink
Fix build against GopherJS
Browse files Browse the repository at this point in the history
When building against GopherJS, ThreadCreateProfile and Getpid are not
available.

Return 1 to shim the functions.

Signed-off-by: Christian Stewart <christian@paral.in>
  • Loading branch information
paralin committed Aug 4, 2021
1 parent 20eef74 commit dc49ac2
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 5 deletions.
25 changes: 25 additions & 0 deletions prometheus/get_pid.go
Original file line number Diff line number Diff line change
@@ -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
}
}
22 changes: 22 additions & 0 deletions prometheus/get_pid_gopherjs.go
Original file line number Diff line number Diff line change
@@ -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
}
}
3 changes: 2 additions & 1 deletion prometheus/go_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ func (c *goCollector) 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
Expand Down
24 changes: 24 additions & 0 deletions prometheus/num_threads.go
Original file line number Diff line number Diff line change
@@ -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)
}
21 changes: 21 additions & 0 deletions prometheus/num_threads_gopherjs.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 1 addition & 3 deletions prometheus/process_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
Expand Down Expand Up @@ -104,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
}
Expand Down
25 changes: 25 additions & 0 deletions prometheus/process_collector_js.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion prometheus/process_collector_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// +build !windows
// +build !windows,!js

package prometheus

Expand Down

0 comments on commit dc49ac2

Please sign in to comment.