Skip to content

Commit

Permalink
Do not build internal metrics without CGO (#6501)
Browse files Browse the repository at this point in the history
* libbeat: build internal metrics on windows and linux without cgo

* libbeat: add ephemeral id even if no internal memory metrics is reported

* fix incorrect build tags && create common file for all platforms
  • Loading branch information
kvch authored and andrewkroh committed Mar 13, 2018
1 parent 3bb5cb6 commit 11be2ad
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ https://github.com/elastic/beats/compare/v6.0.0-beta2...master[Check the HEAD di
- Remove double slashes in Windows service script. {pull}6491[6491]
- Fix infinite failure on Kubernetes watch {pull}6504[6504]
- Ensure Kubernetes labels/annotations don't break mapping {pull}6490[6490]
- Report ephemeral ID and uptime in monitoring events on all platforms {pull}6501[6501]

*Auditbeat*

Expand Down
33 changes: 6 additions & 27 deletions libbeat/cmd/instance/metrics.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
// +build darwin linux windows
// +build cgo
// +build darwin,cgo freebsd,cgo linux windows

package instance

import (
"fmt"
"os"
"runtime"
"time"

"github.com/satori/go.uuid"

"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/metric/system/cpu"
"github.com/elastic/beats/libbeat/metric/system/process"
"github.com/elastic/beats/libbeat/monitoring"
"github.com/elastic/beats/libbeat/monitoring/report/log"
)

var (
beatProcessStats *process.Stats
ephemeralID uuid.UUID
systemMetrics *monitoring.Registry
)

func init() {
beatMetrics := monitoring.Default.NewRegistry("beat")
systemMetrics = monitoring.Default.NewRegistry("system")
}

func setupMetrics(name string) error {
monitoring.NewFunc(beatMetrics, "memstats", reportMemStats, monitoring.Report)
monitoring.NewFunc(beatMetrics, "cpu", reportBeatCPU, monitoring.Report)
monitoring.NewFunc(beatMetrics, "info", reportInfo, monitoring.Report)

systemMetrics := monitoring.Default.NewRegistry("system")
monitoring.NewFunc(systemMetrics, "cpu", reportSystemCPUUsage, monitoring.Report)
if runtime.GOOS != "windows" {
monitoring.NewFunc(systemMetrics, "load", reportSystemLoadAverage, monitoring.Report)
}

ephemeralID = uuid.NewV4()
}

func setupMetrics(name string) error {
beatProcessStats = &process.Stats{
Procs: []string{name},
EnvWhitelist: nil,
Expand Down Expand Up @@ -91,19 +83,6 @@ func getRSSSize() (uint64, error) {
return rss, nil
}

func reportInfo(_ monitoring.Mode, V monitoring.Visitor) {
V.OnRegistryStart()
defer V.OnRegistryFinished()

delta := time.Since(log.StartTime)
uptime := int64(delta / time.Millisecond)
monitoring.ReportNamespace(V, "uptime", func() {
monitoring.ReportInt(V, "ms", uptime)
})

monitoring.ReportString(V, "ephemeral_id", ephemeralID.String())
}

func reportBeatCPU(_ monitoring.Mode, V monitoring.Visitor) {
V.OnRegistryStart()
defer V.OnRegistryFinished()
Expand Down
35 changes: 35 additions & 0 deletions libbeat/cmd/instance/metrics_common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package instance

import (
"time"

"github.com/satori/go.uuid"

"github.com/elastic/beats/libbeat/monitoring"
"github.com/elastic/beats/libbeat/monitoring/report/log"
)

var (
ephemeralID uuid.UUID
beatMetrics *monitoring.Registry
)

func init() {
beatMetrics = monitoring.Default.NewRegistry("beat")
monitoring.NewFunc(beatMetrics, "info", reportInfo, monitoring.Report)

ephemeralID = uuid.NewV4()
}

func reportInfo(_ monitoring.Mode, V monitoring.Visitor) {
V.OnRegistryStart()
defer V.OnRegistryFinished()

delta := time.Since(log.StartTime)
uptime := int64(delta / time.Millisecond)
monitoring.ReportNamespace(V, "uptime", func() {
monitoring.ReportInt(V, "ms", uptime)
})

monitoring.ReportString(V, "ephemeral_id", ephemeralID.String())
}
8 changes: 6 additions & 2 deletions libbeat/cmd/instance/metrics_other.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// +build !darwin,!linux,!windows darwin,!cgo linux,!cgo windows,!cgo
// +build !darwin !cgo
// +build !freebsd !cgo
// +build !linux,!windows

package instance

import "github.com/elastic/beats/libbeat/logp"
import (
"github.com/elastic/beats/libbeat/logp"
)

func setupMetrics(name string) error {
logp.Warn("Metrics not implemented for this OS.")
Expand Down

0 comments on commit 11be2ad

Please sign in to comment.