Skip to content

Commit

Permalink
Fix build against GopherJS (#897)
Browse files Browse the repository at this point in the history
* 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>

* Fix formatting

Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>

* Fix linter issue

Move build tags for licence header checks

Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>

Co-authored-by: Kemal Akkoyun <kakkoyun@gmail.com>
  • Loading branch information
paralin and kakkoyun committed Aug 5, 2022
1 parent 1638da9 commit d44fbbe
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 6 deletions.
26 changes: 26 additions & 0 deletions prometheus/get_pid.go
@@ -0,0 +1,26 @@
// 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.

//go:build !js || wasm
// +build !js wasm

package prometheus

import "os"

func getPIDFn() func() (int, error) {
pid := os.Getpid()
return func() (int, error) {
return pid, nil
}
}
23 changes: 23 additions & 0 deletions prometheus/get_pid_gopherjs.go
@@ -0,0 +1,23 @@
// 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.

//go:build js && !wasm
// +build js,!wasm

package prometheus

func getPIDFn() func() (int, error) {
return func() (int, error) {
return 1, nil
}
}
5 changes: 3 additions & 2 deletions prometheus/go_collector.go
Expand Up @@ -246,8 +246,9 @@ 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)
ch <- MustNewConstMetric(c.threadsDesc, GaugeValue, float64(n))

n := getRuntimeNumThreads()
ch <- MustNewConstMetric(c.threadsDesc, GaugeValue, n)

var stats debug.GCStats
stats.PauseQuantiles = make([]time.Duration, 5)
Expand Down
25 changes: 25 additions & 0 deletions prometheus/num_threads.go
@@ -0,0 +1,25 @@
// 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.

//go:build !js || wasm
// +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)
}
22 changes: 22 additions & 0 deletions prometheus/num_threads_gopherjs.go
@@ -0,0 +1,22 @@
// 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.

//go:build js && !wasm
// +build js,!wasm

package prometheus

// getRuntimeNumThreads returns the number of open OS threads.
func getRuntimeNumThreads() float64 {
return 1
}
3 changes: 1 addition & 2 deletions prometheus/process_collector.go
Expand Up @@ -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
}
Expand Down
26 changes: 26 additions & 0 deletions prometheus/process_collector_js.go
@@ -0,0 +1,26 @@
// 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.

//go:build js
// +build js

package prometheus

func canCollectProcess() bool {
return false
}

func (c *processCollector) processCollect(ch chan<- Metric) {
// noop on this platform
return
}
4 changes: 2 additions & 2 deletions prometheus/process_collector_other.go
Expand Up @@ -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

Expand Down

0 comments on commit d44fbbe

Please sign in to comment.