func remapProcessMetrics()

in remappers/hostmetrics/process.go [28:270]


func remapProcessMetrics(
	src, out pmetric.MetricSlice,
	resource pcommon.Resource,
	mutator func(pmetric.NumberDataPoint),
) error {
	var timestamp, startTimestamp pcommon.Timestamp
	var threads, memUsage, memVirtual, fdOpen, ioReadBytes, ioWriteBytes, ioReadOperations, ioWriteOperations int64
	var memUtil, total, systemCpuTime, userCpuTime float64

	for i := 0; i < src.Len(); i++ {
		metric := src.At(i)
		switch metric.Name() {
		case "process.threads":
			dp := metric.Sum().DataPoints().At(0)
			if timestamp == 0 {
				timestamp = dp.Timestamp()
			}
			if startTimestamp == 0 {
				startTimestamp = dp.StartTimestamp()
			}
			threads = dp.IntValue()
		case "process.memory.utilization":
			dp := metric.Gauge().DataPoints().At(0)
			if timestamp == 0 {
				timestamp = dp.Timestamp()
			}
			if startTimestamp == 0 {
				startTimestamp = dp.StartTimestamp()
			}
			memUtil = dp.DoubleValue()
		case "process.memory.usage":
			dp := metric.Sum().DataPoints().At(0)
			if timestamp == 0 {
				timestamp = dp.Timestamp()
			}
			if startTimestamp == 0 {
				startTimestamp = dp.StartTimestamp()
			}
			memUsage = dp.IntValue()
		case "process.memory.virtual":
			dp := metric.Sum().DataPoints().At(0)
			if timestamp == 0 {
				timestamp = dp.Timestamp()
			}
			if startTimestamp == 0 {
				startTimestamp = dp.StartTimestamp()
			}
			memVirtual = dp.IntValue()
		case "process.open_file_descriptors":
			dp := metric.Sum().DataPoints().At(0)
			if timestamp == 0 {
				timestamp = dp.Timestamp()
			}
			if startTimestamp == 0 {
				startTimestamp = dp.StartTimestamp()
			}
			fdOpen = dp.IntValue()
		case "process.cpu.time":
			dataPoints := metric.Sum().DataPoints()
			for j := 0; j < dataPoints.Len(); j++ {
				dp := dataPoints.At(j)
				if timestamp == 0 {
					timestamp = dp.Timestamp()
				}
				if startTimestamp == 0 {
					startTimestamp = dp.StartTimestamp()
				}
				value := dp.DoubleValue()
				if state, ok := dp.Attributes().Get("state"); ok {
					switch state.Str() {
					case "system":
						systemCpuTime = value
						total += value
					case "user":
						userCpuTime = value
						total += value
					case "wait":
						total += value

					}
				}
			}
		case "process.disk.io":
			dataPoints := metric.Sum().DataPoints()
			for j := 0; j < dataPoints.Len(); j++ {
				dp := dataPoints.At(j)
				if timestamp == 0 {
					timestamp = dp.Timestamp()
				}
				if startTimestamp == 0 {
					startTimestamp = dp.StartTimestamp()
				}
				value := dp.IntValue()
				if direction, ok := dp.Attributes().Get("direction"); ok {
					switch direction.Str() {
					case "read":
						ioReadBytes = value
					case "write":
						ioWriteBytes = value
					}
				}
			}
		case "process.disk.operations":
			dataPoints := metric.Sum().DataPoints()
			for j := 0; j < dataPoints.Len(); j++ {
				dp := dataPoints.At(j)
				if timestamp == 0 {
					timestamp = dp.Timestamp()
				}
				if startTimestamp == 0 {
					startTimestamp = dp.StartTimestamp()
				}
				value := dp.IntValue()
				if direction, ok := dp.Attributes().Get("direction"); ok {
					switch direction.Str() {
					case "read":
						ioReadOperations = value
					case "write":
						ioWriteOperations = value
					}
				}
			}
		}
	}

	systemCpuTime = systemCpuTime * 1000
	userCpuTime = userCpuTime * 1000

	startTime := startTimestamp.AsTime()
	startTimeMillis := startTime.UnixMilli()
	memUtilPct := memUtil / 100
	cpuTimeValue := total * 1000
	processRuntime := timestamp.AsTime().UnixMilli() - startTimeMillis
	cpuPct := cpuTimeValue / float64(processRuntime)

	finalMutator := remappedmetric.ChainedMutator(
		mutator,
		addProcessResources(resource, startTime.UTC()),
	)
	remappedmetric.Add(out, finalMutator,
		// The timestamp metrics get converted from Int to Timestamp in Kibana
		// since these are mapped to timestamp datatype
		remappedmetric.Metric{
			DataType:  pmetric.MetricTypeSum,
			Name:      "process.cpu.start_time",
			Timestamp: timestamp,
			IntValue:  &startTimeMillis,
		},
		remappedmetric.Metric{
			DataType:  pmetric.MetricTypeSum,
			Name:      "system.process.num_threads",
			Timestamp: timestamp,
			IntValue:  &threads,
		},
		remappedmetric.Metric{
			DataType:    pmetric.MetricTypeGauge,
			Name:        "system.process.memory.rss.pct",
			Timestamp:   timestamp,
			DoubleValue: &memUtilPct,
		},
		// The process rss bytes have been found to be equal to the memory usage reported by OTEL
		remappedmetric.Metric{
			DataType:  pmetric.MetricTypeSum,
			Name:      "system.process.memory.rss.bytes",
			Timestamp: timestamp,
			IntValue:  &memUsage,
		},
		remappedmetric.Metric{
			DataType:  pmetric.MetricTypeSum,
			Name:      "system.process.memory.size",
			Timestamp: timestamp,
			IntValue:  &memVirtual,
		},
		remappedmetric.Metric{
			DataType:  pmetric.MetricTypeSum,
			Name:      "system.process.fd.open",
			Timestamp: timestamp,
			IntValue:  &fdOpen,
		},
		remappedmetric.Metric{
			DataType:    pmetric.MetricTypeGauge,
			Name:        "process.memory.pct",
			Timestamp:   timestamp,
			DoubleValue: &memUtilPct,
		},
		remappedmetric.Metric{
			DataType:    pmetric.MetricTypeSum,
			Name:        "system.process.cpu.total.value",
			Timestamp:   timestamp,
			DoubleValue: &cpuTimeValue,
		},
		remappedmetric.Metric{
			DataType:    pmetric.MetricTypeSum,
			Name:        "system.process.cpu.system.ticks",
			Timestamp:   timestamp,
			DoubleValue: &systemCpuTime,
		},
		remappedmetric.Metric{
			DataType:    pmetric.MetricTypeSum,
			Name:        "system.process.cpu.user.ticks",
			Timestamp:   timestamp,
			DoubleValue: &userCpuTime,
		},
		remappedmetric.Metric{
			DataType:    pmetric.MetricTypeSum,
			Name:        "system.process.cpu.total.ticks",
			Timestamp:   timestamp,
			DoubleValue: &cpuTimeValue,
		},
		remappedmetric.Metric{
			DataType:  pmetric.MetricTypeSum,
			Name:      "system.process.io.read_bytes",
			Timestamp: timestamp,
			IntValue:  &ioReadBytes,
		},
		remappedmetric.Metric{
			DataType:  pmetric.MetricTypeSum,
			Name:      "system.process.io.write_bytes",
			Timestamp: timestamp,
			IntValue:  &ioWriteBytes,
		},
		remappedmetric.Metric{
			DataType:  pmetric.MetricTypeSum,
			Name:      "system.process.io.read_ops",
			Timestamp: timestamp,
			IntValue:  &ioReadOperations,
		},
		remappedmetric.Metric{
			DataType:  pmetric.MetricTypeSum,
			Name:      "system.process.io.write_ops",
			Timestamp: timestamp,
			IntValue:  &ioWriteOperations,
		},
		remappedmetric.Metric{
			DataType:    pmetric.MetricTypeGauge,
			Name:        "system.process.cpu.total.pct",
			Timestamp:   timestamp,
			DoubleValue: &cpuPct,
		},
	)

	return nil
}