Skip to content

Commit

Permalink
fix for commits
Browse files Browse the repository at this point in the history
Signed-off-by: Ziqi Zhao <[email protected]>
  • Loading branch information
fatsheep9146 committed Nov 1, 2022
1 parent b806238 commit dbeea34
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions exporters/prometheus/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
func addHistogramMetric(ch chan<- prometheus.Metric, histogram metricdata.Histogram, m metricdata.Metrics, ks, vs [2]string, name string) {
// TODO(https://github.com/open-telemetry/opentelemetry-go/issues/3163): support exemplars
for _, dp := range histogram.DataPoints {
keys, values := getAttrs(dp.Attributes)
if ks[0] != "" {
keys = append(keys, ks[:]...)
values = append(values, vs[:]...)
}
keys, values := getAttrs(dp.Attributes, ks, vs)

desc := prometheus.NewDesc(name, m.Description, keys, nil)
buckets := make(map[float64]uint64, len(dp.Bounds))
Expand Down Expand Up @@ -199,11 +195,7 @@ func addSumMetric[N int64 | float64](ch chan<- prometheus.Metric, sum metricdata
name += counterSuffix
}
for _, dp := range sum.DataPoints {
keys, values := getAttrs(dp.Attributes)
if ks[0] != "" {
keys = append(keys, ks[:]...)
values = append(values, vs[:]...)
}
keys, values := getAttrs(dp.Attributes, ks, vs)

desc := prometheus.NewDesc(name, m.Description, keys, nil)
m, err := prometheus.NewConstMetric(desc, valueType, float64(dp.Value), values...)
Expand All @@ -217,11 +209,7 @@ func addSumMetric[N int64 | float64](ch chan<- prometheus.Metric, sum metricdata

func addGaugeMetric[N int64 | float64](ch chan<- prometheus.Metric, gauge metricdata.Gauge[N], m metricdata.Metrics, ks, vs [2]string, name string) {
for _, dp := range gauge.DataPoints {
keys, values := getAttrs(dp.Attributes)
if ks[0] != "" {
keys = append(keys, ks[:]...)
values = append(values, vs[:]...)
}
keys, values := getAttrs(dp.Attributes, ks, vs)

desc := prometheus.NewDesc(name, m.Description, keys, nil)
m, err := prometheus.NewConstMetric(desc, prometheus.GaugeValue, float64(dp.Value), values...)
Expand All @@ -236,7 +224,7 @@ func addGaugeMetric[N int64 | float64](ch chan<- prometheus.Metric, gauge metric
// getAttrs parses the attribute.Set to two lists of matching Prometheus-style
// keys and values. It sanitizes invalid characters and handles duplicate keys
// (due to sanitization) by sorting and concatenating the values following the spec.
func getAttrs(attrs attribute.Set) ([]string, []string) {
func getAttrs(attrs attribute.Set, ks, vs [2]string) ([]string, []string) {
keysMap := make(map[string][]string)
itr := attrs.Iter()
for itr.Next() {
Expand All @@ -259,11 +247,16 @@ func getAttrs(attrs attribute.Set) ([]string, []string) {
})
values = append(values, strings.Join(vals, ";"))
}

if ks[0] != "" {
keys = append(keys, ks[:]...)
values = append(values, vs[:]...)
}
return keys, values
}

func (c *collector) createInfoMetric(name, description string, res *resource.Resource) (prometheus.Metric, error) {
keys, values := getAttrs(*res.Set())
keys, values := getAttrs(*res.Set(), [2]string{}, [2]string{})
desc := prometheus.NewDesc(name, description, keys, nil)
return prometheus.NewConstMetric(desc, prometheus.GaugeValue, float64(1), values...)
}
Expand Down

0 comments on commit dbeea34

Please sign in to comment.