Skip to content

Commit

Permalink
always add timestamp even if some metrics are disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyers-elastic committed Feb 13, 2024
1 parent 6e3ef4a commit 98e6638
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 31 deletions.
10 changes: 8 additions & 2 deletions processor/elasticprocessor/internal/hostmetrics/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ func addCPUMetrics(metrics pmetric.MetricSlice, dataset string) error {
metric := metrics.At(i)
if metric.Name() == "system.cpu.logical.count" {
dp := metric.Sum().DataPoints().At(0)
if timestamp == 0 {
timestamp = dp.Timestamp()
}

numCores = dp.IntValue()
timestamp = dp.Timestamp()
} else if metric.Name() == "system.cpu.utilization" {
dataPoints := metric.Gauge().DataPoints()
for j := 0; j < dataPoints.Len(); j++ {
dp := dataPoints.At(j)
value := dp.DoubleValue()
if timestamp == 0 {
timestamp = dp.Timestamp()
}

value := dp.DoubleValue()
if state, ok := dp.Attributes().Get("state"); ok {
switch state.Str() {
case "idle":
Expand Down
10 changes: 9 additions & 1 deletion processor/elasticprocessor/internal/hostmetrics/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ func addLoadMetrics(metrics pmetric.MetricSlice, dataset string) error {
metric := metrics.At(i)
if metric.Name() == "system.cpu.load_average.1m" {
dp := metric.Gauge().DataPoints().At(0)
timestamp = dp.Timestamp()
if timestamp == 0 {
timestamp = dp.Timestamp()
}
l1 = dp.DoubleValue()
} else if metric.Name() == "system.cpu.load_average.5m" {
dp := metric.Gauge().DataPoints().At(0)
if timestamp == 0 {
timestamp = dp.Timestamp()
}
l5 = dp.DoubleValue()
} else if metric.Name() == "system.cpu.load_average.15m" {
dp := metric.Gauge().DataPoints().At(0)
if timestamp == 0 {
timestamp = dp.Timestamp()
}
l15 = dp.DoubleValue()
}
}
Expand Down
10 changes: 7 additions & 3 deletions processor/elasticprocessor/internal/hostmetrics/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ func addMemoryMetrics(metrics pmetric.MetricSlice, dataset string) error {
dataPoints := metric.Sum().DataPoints()
for j := 0; j < dataPoints.Len(); j++ {
dp := dataPoints.At(j)

timestamp = dp.Timestamp()
if timestamp == 0 {
timestamp = dp.Timestamp()
}

value := dp.IntValue()
if state, ok := dp.Attributes().Get("state"); ok {
Expand Down Expand Up @@ -47,8 +48,11 @@ func addMemoryMetrics(metrics pmetric.MetricSlice, dataset string) error {
dataPoints := metric.Gauge().DataPoints()
for j := 0; j < dataPoints.Len(); j++ {
dp := dataPoints.At(j)
value := dp.DoubleValue()
if timestamp == 0 {
timestamp = dp.Timestamp()
}

value := dp.DoubleValue()
if state, ok := dp.Attributes().Get("state"); ok {
switch state.Str() {
case "free":
Expand Down
96 changes: 72 additions & 24 deletions processor/elasticprocessor/internal/hostmetrics/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,67 @@ import (
)

func addProcessMetrics(metrics pmetric.MetricSlice, dataset string) error {
var timestamp, startTimestamp pcommon.Timestamp
var threads, memUsage, memVirtual, fdOpen, ioReadBytes, ioWriteBytes, ioReadOperations, ioWriteOperations int64
var timestamp pcommon.Timestamp
var startTime, threads, memUsage, memVirtual, fdOpen, ioReadBytes, ioWriteBytes, ioReadOperations, ioWriteOperations int64
var memUtil, memUtilPct, total, cpuTimeValue, systemCpuTime, userCpuTime float64

for i := 0; i < metrics.Len(); i++ {
metric := metrics.At(i)
if metric.Name() == "process.threads" {
dp := metric.Sum().DataPoints().At(0)
timestamp = dp.Timestamp()
if timestamp == 0 {
timestamp = dp.Timestamp()
}
if startTime == 0 {
startTime = dp.StartTimestamp().AsTime().UnixMilli()
}
threads = dp.IntValue()
} else if metric.Name() == "process.memory.utilization" {
dp := metric.Gauge().DataPoints().At(0)
if timestamp == 0 {
timestamp = dp.Timestamp()
}
if startTime == 0 {
startTime = dp.StartTimestamp().AsTime().UnixMilli()
}
memUtil = dp.DoubleValue()
} else if metric.Name() == "process.memory.usage" {
dp := metric.Sum().DataPoints().At(0)
if timestamp == 0 {
timestamp = dp.Timestamp()
}
if startTime == 0 {
startTime = dp.StartTimestamp().AsTime().UnixMilli()
}
memUsage = dp.IntValue()
} else if metric.Name() == "process.memory.virtual" {
dp := metric.Sum().DataPoints().At(0)
if timestamp == 0 {
timestamp = dp.Timestamp()
}
if startTime == 0 {
startTime = dp.StartTimestamp().AsTime().UnixMilli()
}
memVirtual = dp.IntValue()
} else if metric.Name() == "process.open_file_descriptors" {
dp := metric.Sum().DataPoints().At(0)
if timestamp == 0 {
timestamp = dp.Timestamp()
}
if startTime == 0 {
startTime = dp.StartTimestamp().AsTime().UnixMilli()
}
fdOpen = dp.IntValue()
} else if metric.Name() == "process.cpu.time" {
dataPoints := metric.Sum().DataPoints()
for j := 0; j < dataPoints.Len(); j++ {
dp := dataPoints.At(j)
startTimestamp = dp.StartTimestamp()
if timestamp == 0 {
timestamp = dp.Timestamp()
}
if startTime == 0 {
startTime = dp.StartTimestamp().AsTime().UnixMilli()
}
value := dp.DoubleValue()
if state, ok := dp.Attributes().Get("state"); ok {
switch state.Str() {
Expand All @@ -52,6 +86,12 @@ func addProcessMetrics(metrics pmetric.MetricSlice, dataset string) error {
dataPoints := metric.Sum().DataPoints()
for j := 0; j < dataPoints.Len(); j++ {
dp := dataPoints.At(j)
if timestamp == 0 {
timestamp = dp.Timestamp()
}
if startTime == 0 {
startTime = dp.StartTimestamp().AsTime().UnixMilli()
}
value := dp.IntValue()
if direction, ok := dp.Attributes().Get("direction"); ok {
switch direction.Str() {
Expand All @@ -66,6 +106,12 @@ func addProcessMetrics(metrics pmetric.MetricSlice, dataset string) error {
dataPoints := metric.Sum().DataPoints()
for j := 0; j < dataPoints.Len(); j++ {
dp := dataPoints.At(j)
if timestamp == 0 {
timestamp = dp.Timestamp()
}
if startTime == 0 {
startTime = dp.StartTimestamp().AsTime().UnixMilli()
}
value := dp.IntValue()
if direction, ok := dp.Attributes().Get("direction"); ok {
switch direction.Str() {
Expand All @@ -85,6 +131,12 @@ func addProcessMetrics(metrics pmetric.MetricSlice, dataset string) error {
userCpuTime = userCpuTime * 1000

addMetrics(metrics, dataset,
metric{
dataType: Sum,
name: "process.cpu.start_time",
timestamp: timestamp,
intValue: &startTime,
},
metric{
dataType: Sum,
name: "system.process.num_threads",
Expand Down Expand Up @@ -123,32 +175,28 @@ func addProcessMetrics(metrics pmetric.MetricSlice, dataset string) error {
doubleValue: &memUtilPct,
},
metric{
dataType: Sum,
name: "system.process.cpu.total.value",
timestamp: timestamp,
startTimestamp: startTimestamp,
doubleValue: &cpuTimeValue,
dataType: Sum,
name: "system.process.cpu.total.value",
timestamp: timestamp,
doubleValue: &cpuTimeValue,
},
metric{
dataType: Sum,
name: "system.process.cpu.system.ticks",
timestamp: timestamp,
startTimestamp: startTimestamp,
doubleValue: &systemCpuTime,
dataType: Sum,
name: "system.process.cpu.system.ticks",
timestamp: timestamp,
doubleValue: &systemCpuTime,
},
metric{
dataType: Sum,
name: "system.process.cpu.user.ticks",
timestamp: timestamp,
startTimestamp: startTimestamp,
doubleValue: &userCpuTime,
dataType: Sum,
name: "system.process.cpu.user.ticks",
timestamp: timestamp,
doubleValue: &userCpuTime,
},
metric{
dataType: Sum,
name: "system.process.cpu.total.ticks",
timestamp: timestamp,
startTimestamp: startTimestamp,
doubleValue: &cpuTimeValue,
dataType: Sum,
name: "system.process.cpu.total.ticks",
timestamp: timestamp,
doubleValue: &cpuTimeValue,
},
metric{
dataType: Sum,
Expand Down
4 changes: 3 additions & 1 deletion processor/elasticprocessor/internal/hostmetrics/processes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ func addProcessSummaryMetrics(metrics pmetric.MetricSlice, dataset string) error
// iterate over the datapoints corresponding to different 'status' attributes
for j := 0; j < dataPoints.Len(); j++ {
dp := dataPoints.At(j)
timestamp = dp.Timestamp()
if timestamp == 0 {
timestamp = dp.Timestamp()
}
value := dp.IntValue()
if status, ok := dp.Attributes().Get("status"); ok {
switch status.Str() {
Expand Down

0 comments on commit 98e6638

Please sign in to comment.