Exemple #1
0
func (self *influxdbStorage) Percentiles(
	containerName string,
	cpuUsagePercentiles []int,
	memUsagePercentiles []int,
) (*info.ContainerStatsPercentiles, error) {
	selectedCol := make([]string, 0, len(cpuUsagePercentiles)+len(memUsagePercentiles)+1)

	selectedCol = append(selectedCol, fmt.Sprintf("max(%v)", colMemoryUsage))
	for _, p := range cpuUsagePercentiles {
		selectedCol = append(selectedCol, fmt.Sprintf("percentile(%v, %v)", colCpuInstantUsage, p))
	}
	for _, p := range memUsagePercentiles {
		selectedCol = append(selectedCol, fmt.Sprintf("percentile(%v, %v)", colMemoryUsage, p))
	}

	query := fmt.Sprintf("select %v from %v where %v='%v' and %v='%v' and time > now() - %v",
		strings.Join(selectedCol, ","),
		self.tableName,
		colContainerName,
		containerName,
		colMachineName,
		self.machineName,
		fmt.Sprintf("%vs", self.windowLen.Seconds()),
	)
	series, err := self.client.Query(query)
	if err != nil {
		return nil, err
	}
	if len(series) != 1 {
		return nil, nil
	}
	if len(series[0].Points) == 0 {
		return nil, nil
	}

	point := series[0].Points[0]

	ret := new(info.ContainerStatsPercentiles)
	ret.MaxMemoryUsage, err = convertToUint64(point[1])
	if err != nil {
		return nil, fmt.Errorf("invalid max memory usage: %v", err)
	}
	retrievedCpuPercentiles := point[2 : 2+len(cpuUsagePercentiles)]
	for i, p := range cpuUsagePercentiles {
		v, err := convertToUint64(retrievedCpuPercentiles[i])
		if err != nil {
			return nil, fmt.Errorf("invalid cpu usage: %v", err)
		}
		ret.CpuUsagePercentiles = append(
			ret.CpuUsagePercentiles,
			info.Percentile{
				Percentage: p,
				Value:      v,
			},
		)
	}
	retrievedMemoryPercentiles := point[2+len(cpuUsagePercentiles):]
	for i, p := range memUsagePercentiles {
		v, err := convertToUint64(retrievedMemoryPercentiles[i])
		if err != nil {
			return nil, fmt.Errorf("invalid memory usage: %v", err)
		}
		ret.MemoryUsagePercentiles = append(
			ret.MemoryUsagePercentiles,
			info.Percentile{
				Percentage: p,
				Value:      v,
			},
		)
	}
	return ret, nil
}
Exemple #2
0
func (self *bigqueryStorage) Percentiles(
	containerName string,
	cpuUsagePercentiles []int,
	memUsagePercentiles []int,
) (*info.ContainerStatsPercentiles, error) {
	selectedCol := make([]string, 0, len(cpuUsagePercentiles)+len(memUsagePercentiles)+1)

	selectedCol = append(selectedCol, fmt.Sprintf("max(%v)", colMemoryUsage))
	for _, p := range cpuUsagePercentiles {
		selectedCol = append(selectedCol, fmt.Sprintf("percentile(%v, %v)", colCpuInstantUsage, p))
	}
	for _, p := range memUsagePercentiles {
		selectedCol = append(selectedCol, fmt.Sprintf("percentile(%v, %v)", colMemoryUsage, p))
	}

	tableName, err := self.client.GetTableName()
	if err != nil {
		return nil, err
	}
	query := fmt.Sprintf("SELECT %v FROM %v WHERE %v='%v' AND %v='%v' AND timestamp > DATE_ADD(CURRENT_TIMESTAMP(), -%v, 'SECOND')",
		strings.Join(selectedCol, ","),
		tableName,
		colContainerName,
		containerName,
		colMachineName,
		self.machineName,
		self.windowLen.Seconds(),
	)
	_, rows, err := self.client.Query(query)
	if err != nil {
		return nil, err
	}

	if len(rows) != 1 {
		return nil, nil
	}

	point := rows[0]

	ret := new(info.ContainerStatsPercentiles)
	ret.MaxMemoryUsage, err = convertToUint64(point[0])
	if err != nil {
		return nil, fmt.Errorf("invalid max memory usage: %v", err)
	}
	retrievedCpuPercentiles := point[1 : 1+len(cpuUsagePercentiles)]
	for i, p := range cpuUsagePercentiles {
		v, err := convertToUint64(retrievedCpuPercentiles[i])
		if err != nil {
			return nil, fmt.Errorf("invalid cpu usage: %v", err)
		}
		ret.CpuUsagePercentiles = append(
			ret.CpuUsagePercentiles,
			info.Percentile{
				Percentage: p,
				Value:      v,
			},
		)
	}
	retrievedMemoryPercentiles := point[1+len(cpuUsagePercentiles):]
	for i, p := range memUsagePercentiles {
		v, err := convertToUint64(retrievedMemoryPercentiles[i])
		if err != nil {
			return nil, fmt.Errorf("invalid memory usage: %v", err)
		}
		ret.MemoryUsagePercentiles = append(
			ret.MemoryUsagePercentiles,
			info.Percentile{
				Percentage: p,
				Value:      v,
			},
		)
	}
	return ret, nil
}