func()

in pkg/systemstatsmonitor/disk_collector.go [167:235]


func (dc *diskCollector) recordIOCounters(ioCountersStats map[string]disk.IOCountersStat, sampleTime time.Time) {
	for deviceName, ioCountersStat := range ioCountersStats {
		// Attach label {"device_name": deviceName} to the following metrics.
		tags := map[string]string{deviceNameLabel: deviceName}

		// Calculate average IO queue length since last measurement.
		lastIOTime, historyExist := dc.lastIOTime[deviceName]
		lastWeightedIO := dc.lastWeightedIO[deviceName]
		dc.lastIOTime[deviceName] = ioCountersStat.IoTime
		dc.lastWeightedIO[deviceName] = ioCountersStat.WeightedIO

		if dc.mIOTime != nil {
			dc.mIOTime.Record(tags, int64(ioCountersStat.IoTime-lastIOTime))
		}
		if dc.mWeightedIO != nil {
			dc.mWeightedIO.Record(tags, int64(ioCountersStat.WeightedIO-lastWeightedIO))
		}
		if historyExist {
			avgQueueLen := float64(0.0)
			if lastWeightedIO != ioCountersStat.WeightedIO {
				diffSampleTimeMs := sampleTime.Sub(dc.lastSampleTime).Seconds() * 1000
				avgQueueLen = float64(ioCountersStat.WeightedIO-lastWeightedIO) / diffSampleTimeMs
			}
			if dc.mAvgQueueLen != nil {
				dc.mAvgQueueLen.Record(tags, avgQueueLen)
			}
		}

		// Attach label {"device_name": deviceName, "direction": "read"} to the following metrics.
		tags = map[string]string{deviceNameLabel: deviceName, directionLabel: "read"}

		if dc.mOpsCount != nil {
			dc.mOpsCount.Record(tags, int64(ioCountersStat.ReadCount-dc.lastReadCount[deviceName]))
			dc.lastReadCount[deviceName] = ioCountersStat.ReadCount
		}
		if dc.mMergedOpsCount != nil {
			dc.mMergedOpsCount.Record(tags, int64(ioCountersStat.MergedReadCount-dc.lastMergedReadCount[deviceName]))
			dc.lastMergedReadCount[deviceName] = ioCountersStat.MergedReadCount
		}
		if dc.mOpsBytes != nil {
			dc.mOpsBytes.Record(tags, int64(ioCountersStat.ReadBytes-dc.lastReadBytes[deviceName]))
			dc.lastReadBytes[deviceName] = ioCountersStat.ReadBytes
		}
		if dc.mOpsTime != nil {
			dc.mOpsTime.Record(tags, int64(ioCountersStat.ReadTime-dc.lastReadTime[deviceName]))
			dc.lastReadTime[deviceName] = ioCountersStat.ReadTime
		}

		// Attach label {"device_name": deviceName, "direction": "write"} to the following metrics.
		tags = map[string]string{deviceNameLabel: deviceName, directionLabel: "write"}

		if dc.mOpsCount != nil {
			dc.mOpsCount.Record(tags, int64(ioCountersStat.WriteCount-dc.lastWriteCount[deviceName]))
			dc.lastWriteCount[deviceName] = ioCountersStat.WriteCount
		}
		if dc.mMergedOpsCount != nil {
			dc.mMergedOpsCount.Record(tags, int64(ioCountersStat.MergedWriteCount-dc.lastMergedWriteCount[deviceName]))
			dc.lastMergedWriteCount[deviceName] = ioCountersStat.MergedWriteCount
		}
		if dc.mOpsBytes != nil {
			dc.mOpsBytes.Record(tags, int64(ioCountersStat.WriteBytes-dc.lastWriteBytes[deviceName]))
			dc.lastWriteBytes[deviceName] = ioCountersStat.WriteBytes
		}
		if dc.mOpsTime != nil {
			dc.mOpsTime.Record(tags, int64(ioCountersStat.WriteTime-dc.lastWriteTime[deviceName]))
			dc.lastWriteTime[deviceName] = ioCountersStat.WriteTime
		}
	}
}