-
Notifications
You must be signed in to change notification settings - Fork 21
/
setup.go
126 lines (102 loc) · 4.09 KB
/
setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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 (darwin && cgo) || (freebsd && cgo) || linux || windows
// +build darwin,cgo freebsd,cgo linux windows
package report
import (
"fmt"
"runtime"
"time"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/monitoring"
"github.com/elastic/elastic-agent-system-metrics/metric/system/process"
)
var (
systemMetrics *monitoring.Registry
processStats *process.Stats
)
func init() {
systemMetrics = monitoring.Default.NewRegistry("system")
}
// monitoringCgroupsHierarchyOverride is an undocumented environment variable which
// overrides the cgroups path under /sys/fs/cgroup, which should be set to "/" when running
// Elastic Agent under Docker.
const monitoringCgroupsHierarchyOverride = "LIBBEAT_MONITORING_CGROUPS_HIERARCHY_OVERRIDE"
// SetupMetrics creates a basic suite of metrics handlers for monitoring, including build info and system resources
func SetupMetrics(logger *logp.Logger, name, version string) error {
monitoring.NewFunc(systemMetrics, "cpu", ReportSystemCPUUsage, monitoring.Report)
name = processName(name)
processStats = &process.Stats{
Procs: []string{name},
EnvWhitelist: nil,
CPUTicks: true,
CacheCmdLine: true,
IncludeTop: process.IncludeTopConfig{},
}
err := processStats.Init()
if err != nil {
return fmt.Errorf("failed to init process stats for agent: %w", err)
}
monitoring.NewFunc(processMetrics, "memstats", MemStatsReporter(logger, processStats), monitoring.Report)
monitoring.NewFunc(processMetrics, "cpu", InstanceCPUReporter(logger, processStats), monitoring.Report)
monitoring.NewFunc(processMetrics, "runtime", ReportRuntime, monitoring.Report)
monitoring.NewFunc(processMetrics, "info", infoReporter(name, version), monitoring.Report)
setupPlatformSpecificMetrics(logger, processStats)
return nil
}
// processName truncates the name if it is longer than 15 characters, so we don't fail process checks later on
// On *nix, the process name comes from /proc/PID/stat, which uses a comm value of 16 bytes, plus the null byte
func processName(name string) string {
if (isLinux() || isDarwin()) && len(name) > 15 {
name = name[:15]
}
return name
}
func isDarwin() bool {
return runtime.GOOS == "darwin"
}
func isLinux() bool {
return runtime.GOOS == "linux"
}
func isWindows() bool {
return runtime.GOOS == "windows"
}
func infoReporter(serviceName, version string) func(_ monitoring.Mode, V monitoring.Visitor) {
return func(_ monitoring.Mode, V monitoring.Visitor) {
V.OnRegistryStart()
defer V.OnRegistryFinished()
delta := time.Since(startTime)
uptime := int64(delta / time.Millisecond)
monitoring.ReportNamespace(V, "uptime", func() {
monitoring.ReportInt(V, "ms", uptime)
})
monitoring.ReportString(V, "ephemeral_id", ephemeralID.String())
monitoring.ReportString(V, "name", serviceName)
monitoring.ReportString(V, "version", version)
}
}
func setupPlatformSpecificMetrics(logger *logp.Logger, processStats *process.Stats) {
if isLinux() {
monitoring.NewFunc(processMetrics, "cgroup", InstanceCroupsReporter(logger, monitoringCgroupsHierarchyOverride), monitoring.Report)
}
if isWindows() {
SetupWindowsHandlesMetrics(logger, systemMetrics)
} else {
monitoring.NewFunc(systemMetrics, "load", ReportSystemLoadAverage, monitoring.Report)
}
SetupLinuxBSDFDMetrics(logger, processMetrics, processStats)
}