Skip to content

Commit

Permalink
Expose custom process metrics (#26912)
Browse files Browse the repository at this point in the history
Expose custom process metrics  (#26912)
  • Loading branch information
michalpristas authored Jul 16, 2021
1 parent f731832 commit f141482
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 57 deletions.
103 changes: 47 additions & 56 deletions x-pack/elastic-agent/pkg/agent/operation/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,34 +461,7 @@ func (o *Operator) getMonitoringMetricbeatConfig(outputType string, output inter
},
{
"copy_fields": map[string]interface{}{
"fields": []map[string]interface{}{
// I should be able to see the CPU Usage on the running machine. Am using too much CPU?
{
"from": "http.agent.beat.cpu",
"to": "system.process.cpu",
},
// I should be able to see the Memory usage of Elastic Agent. Is the Elastic Agent using too much memory?
{
"from": "http.agent.beat.memstats.memory_sys",
"to": "system.process.memory.size",
},
// I should be able to see the system memory. Am I running out of memory?
// TODO: with APM agent: total and free

// I should be able to see Disk usage on the running machine. Am I running out of disk space?
// TODO: with APM agent

// I should be able to see fd usage. Am I keep too many files open?
{
"from": "http.agent.beat.handles",
"to": "system.process.fd",
},
// Cgroup reporting
{
"from": "http.agent.beat.cgroup",
"to": "system.process.cgroup",
},
},
"fields": normalizeHTTPCopyRules(name),
"ignore_missing": true,
},
},
Expand Down Expand Up @@ -552,34 +525,7 @@ func (o *Operator) getMonitoringMetricbeatConfig(outputType string, output inter
},
{
"copy_fields": map[string]interface{}{
"fields": []map[string]interface{}{
// I should be able to see the CPU Usage on the running machine. Am using too much CPU?
{
"from": "http.agent.beat.cpu",
"to": "system.process.cpu",
},
// I should be able to see the Memory usage of Elastic Agent. Is the Elastic Agent using too much memory?
{
"from": "http.agent.beat.memstats.memory_sys",
"to": "system.process.memory.size",
},
// I should be able to see the system memory. Am I running out of memory?
// TODO: with APM agent: total and free

// I should be able to see Disk usage on the running machine. Am I running out of disk space?
// TODO: with APM agent

// I should be able to see fd usage. Am I keep too many files open?
{
"from": "http.agent.beat.handles",
"to": "system.process.fd",
},
// Cgroup reporting
{
"from": "http.agent.beat.cgroup",
"to": "system.process.cgroup",
},
},
"fields": normalizeHTTPCopyRules(fixedAgentName),
"ignore_missing": true,
},
},
Expand Down Expand Up @@ -678,3 +624,48 @@ func (o *Operator) isMonitoringLogs() bool {
func (o *Operator) isMonitoringMetrics() bool {
return (o.isMonitoring & isMonitoringMetricsFlag) != 0
}

func normalizeHTTPCopyRules(name string) []map[string]interface{} {
fromToMap := []map[string]interface{}{
// I should be able to see the CPU Usage on the running machine. Am using too much CPU?
{
"from": "http.agent.beat.cpu",
"to": "system.process.cpu",
},
// I should be able to see the Memory usage of Elastic Agent. Is the Elastic Agent using too much memory?
{
"from": "http.agent.beat.memstats.memory_sys",
"to": "system.process.memory.size",
},
// I should be able to see the system memory. Am I running out of memory?
// TODO: with APM agent: total and free

// I should be able to see Disk usage on the running machine. Am I running out of disk space?
// TODO: with APM agent

// I should be able to see fd usage. Am I keep too many files open?
{
"from": "http.agent.beat.handles",
"to": "system.process.fd",
},
// Cgroup reporting
{
"from": "http.agent.beat.cgroup",
"to": "system.process.cgroup",
},
}

spec, found := program.SupportedMap[name]
if !found {
return fromToMap
}

for _, exportedMetric := range spec.ExprtedMetrics {
fromToMap = append(fromToMap, map[string]interface{}{
"from": fmt.Sprintf("http.agent.%s", exportedMetric),
"to": exportedMetric,
})
}

return fromToMap
}
35 changes: 35 additions & 0 deletions x-pack/elastic-agent/pkg/agent/operation/monitoring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ package operation

import (
"context"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/program"

"github.com/elastic/elastic-agent-client/v7/pkg/proto"
Expand All @@ -29,6 +32,38 @@ import (
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/status"
)

func TestExportedMetrics(t *testing.T) {
programName := "testing"
expectedMetricsName := "metric_name"
program.SupportedMap[programName] = program.Spec{ExprtedMetrics: []string{expectedMetricsName}}

exportedMetrics := normalizeHTTPCopyRules(programName)

exportedMetricFound := false
for _, kv := range exportedMetrics {
from, found := kv["from"]
if !found {
continue
}
to, found := kv["to"]
if !found {
continue
}

if to != expectedMetricsName {
continue
}
if from != fmt.Sprintf("http.agent.%s", expectedMetricsName) {
continue
}
exportedMetricFound = true
break
}

require.True(t, exportedMetricFound, "exported metric not found")
delete(program.SupportedMap, programName)
}

func TestGenerateSteps(t *testing.T) {
const sampleOutput = "sample-output"
const outputType = "logstash"
Expand Down
1 change: 1 addition & 0 deletions x-pack/elastic-agent/pkg/agent/program/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Spec struct {
When string `yaml:"when"`
Constraints string `yaml:"constraints"`
RestartOnOutputChange bool `yaml:"restart_on_output_change,omitempty"`
ExprtedMetrics []string `yaml:"exported_metrics,omitempty"`
}

// ReadSpecs reads all the specs that match the provided globbing path.
Expand Down
2 changes: 1 addition & 1 deletion x-pack/elastic-agent/pkg/agent/program/supported.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions x-pack/elastic-agent/spec/apm-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ args: [
"-E", "apm-server.data_streams.enabled=true",
"-E", "gc_percent=${APMSERVER_GOGC:100}"
]
exported_metrics: [
"apm-server",
]
rules:
- copy_to_list:
item: fleet
Expand Down

0 comments on commit f141482

Please sign in to comment.