mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-06 09:47:54 +00:00
Safeguard against int64 Overflow and default to MaxInt64
This commit is contained in:
parent
718cb2400e
commit
dce5b81642
1 changed files with 11 additions and 1 deletions
|
|
@ -16,6 +16,7 @@ package provider
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
|
|
||||||
prom "github.com/directxman12/k8s-prometheus-adapter/pkg/client"
|
prom "github.com/directxman12/k8s-prometheus-adapter/pkg/client"
|
||||||
"github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider"
|
"github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider"
|
||||||
|
|
@ -55,12 +56,21 @@ func (c *metricConverter) Convert(info provider.ExternalMetricInfo, queryResult
|
||||||
func (c *metricConverter) convertSample(info provider.ExternalMetricInfo, sample *model.Sample) (*external_metrics.ExternalMetricValue, error) {
|
func (c *metricConverter) convertSample(info provider.ExternalMetricInfo, sample *model.Sample) (*external_metrics.ExternalMetricValue, error) {
|
||||||
labels := c.convertLabels(sample.Metric)
|
labels := c.convertLabels(sample.Metric)
|
||||||
|
|
||||||
|
intQuantity := int64(0)
|
||||||
|
if float64(sample.Value) == math.Inf(+1) {
|
||||||
|
intQuantity = math.MaxInt64
|
||||||
|
} else if float64(sample.Value) == math.Inf(-1) {
|
||||||
|
intQuantity = math.MinInt64
|
||||||
|
} else {
|
||||||
|
intQuantity = int64(sample.Value * 1000.0)
|
||||||
|
}
|
||||||
|
|
||||||
singleMetric := external_metrics.ExternalMetricValue{
|
singleMetric := external_metrics.ExternalMetricValue{
|
||||||
MetricName: info.Metric,
|
MetricName: info.Metric,
|
||||||
Timestamp: metav1.Time{
|
Timestamp: metav1.Time{
|
||||||
sample.Timestamp.Time(),
|
sample.Timestamp.Time(),
|
||||||
},
|
},
|
||||||
Value: *resource.NewMilliQuantity(int64(sample.Value*1000.0), resource.DecimalSI),
|
Value: *resource.NewMilliQuantity(intQuantity, resource.DecimalSI),
|
||||||
MetricLabels: labels,
|
MetricLabels: labels,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue