Skip to content

Commit

Permalink
removed map in favour of single keyVals
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Boten <aboten@lightstep.com>
  • Loading branch information
Alex Boten committed Nov 30, 2023
1 parent 0237d61 commit 88d070c
Showing 1 changed file with 25 additions and 30 deletions.
55 changes: 25 additions & 30 deletions exporters/prometheus/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ type collector struct {
scopeInfos map[instrumentation.Scope]prometheus.Metric
scopeInfosInvalid map[instrumentation.Scope]struct{}
metricFamilies map[string]*dto.MetricFamily
resourceKeyVals map[*resource.Resource]keyVals
resourceKeyVals keyVals
}

// prometheus counters MUST have a _total suffix by default:
Expand Down Expand Up @@ -168,8 +168,6 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {

global.Debug("Prometheus exporter export", "Data", metrics)

var resourceKeys, resourceValues []string

// Initialize (once) targetInfo and disableTargetInfo.
func() {
c.mu.Lock()
Expand All @@ -193,7 +191,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
}

if c.resourceAttributesFilter != nil {
resourceKeys, resourceValues = c.resourceAttributes(metrics.Resource)
c.resourceAttributes(metrics.Resource)
}

for _, scopeMetrics := range metrics.ScopeMetrics {
Expand Down Expand Up @@ -234,26 +232,26 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {

switch v := m.Data.(type) {
case metricdata.Histogram[int64]:
addHistogramMetric(ch, v, m, keys, values, name, resourceKeys, resourceValues)
addHistogramMetric(ch, v, m, keys, values, name, c.resourceKeyVals)
case metricdata.Histogram[float64]:
addHistogramMetric(ch, v, m, keys, values, name, resourceKeys, resourceValues)
addHistogramMetric(ch, v, m, keys, values, name, c.resourceKeyVals)
case metricdata.Sum[int64]:
addSumMetric(ch, v, m, keys, values, name, resourceKeys, resourceValues)
addSumMetric(ch, v, m, keys, values, name, c.resourceKeyVals)
case metricdata.Sum[float64]:
addSumMetric(ch, v, m, keys, values, name, resourceKeys, resourceValues)
addSumMetric(ch, v, m, keys, values, name, c.resourceKeyVals)
case metricdata.Gauge[int64]:
addGaugeMetric(ch, v, m, keys, values, name, resourceKeys, resourceValues)
addGaugeMetric(ch, v, m, keys, values, name, c.resourceKeyVals)
case metricdata.Gauge[float64]:
addGaugeMetric(ch, v, m, keys, values, name, resourceKeys, resourceValues)
addGaugeMetric(ch, v, m, keys, values, name, c.resourceKeyVals)
}
}
}
}

func addHistogramMetric[N int64 | float64](ch chan<- prometheus.Metric, histogram metricdata.Histogram[N], m metricdata.Metrics, ks, vs [2]string, name string, resourceKeys, resourceVals []string) {
func addHistogramMetric[N int64 | float64](ch chan<- prometheus.Metric, histogram metricdata.Histogram[N], m metricdata.Metrics, ks, vs [2]string, name string, resourceKV keyVals) {
// TODO(https://github.com/open-telemetry/opentelemetry-go/issues/3163): support exemplars
for _, dp := range histogram.DataPoints {
keys, values := getAttrs(dp.Attributes, ks, vs, resourceKeys, resourceVals)
keys, values := getAttrs(dp.Attributes, ks, vs, resourceKV)

desc := prometheus.NewDesc(name, m.Description, keys, nil)
buckets := make(map[float64]uint64, len(dp.Bounds))
Expand All @@ -272,14 +270,14 @@ func addHistogramMetric[N int64 | float64](ch chan<- prometheus.Metric, histogra
}
}

func addSumMetric[N int64 | float64](ch chan<- prometheus.Metric, sum metricdata.Sum[N], m metricdata.Metrics, ks, vs [2]string, name string, resourceKeys, resourceVals []string) {
func addSumMetric[N int64 | float64](ch chan<- prometheus.Metric, sum metricdata.Sum[N], m metricdata.Metrics, ks, vs [2]string, name string, resourceKV keyVals) {
valueType := prometheus.CounterValue
if !sum.IsMonotonic {
valueType = prometheus.GaugeValue
}

for _, dp := range sum.DataPoints {
keys, values := getAttrs(dp.Attributes, ks, vs, resourceKeys, resourceVals)
keys, values := getAttrs(dp.Attributes, ks, vs, resourceKV)

desc := prometheus.NewDesc(name, m.Description, keys, nil)
m, err := prometheus.NewConstMetric(desc, valueType, float64(dp.Value), values...)
Expand All @@ -291,9 +289,9 @@ 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, resourceKeys, resourceVals []string) {
func addGaugeMetric[N int64 | float64](ch chan<- prometheus.Metric, gauge metricdata.Gauge[N], m metricdata.Metrics, ks, vs [2]string, name string, resourceKV keyVals) {
for _, dp := range gauge.DataPoints {
keys, values := getAttrs(dp.Attributes, ks, vs, resourceKeys, resourceVals)
keys, values := getAttrs(dp.Attributes, ks, vs, resourceKV)

desc := prometheus.NewDesc(name, m.Description, keys, nil)
m, err := prometheus.NewConstMetric(desc, prometheus.GaugeValue, float64(dp.Value), values...)
Expand All @@ -308,7 +306,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, ks, vs [2]string, resourceKeys, resourceVals []string) ([]string, []string) {
func getAttrs(attrs attribute.Set, ks, vs [2]string, resourceKV keyVals) ([]string, []string) {
keysMap := make(map[string][]string)
itr := attrs.Iter()
for itr.Next() {
Expand Down Expand Up @@ -337,16 +335,16 @@ func getAttrs(attrs attribute.Set, ks, vs [2]string, resourceKeys, resourceVals
values = append(values, vs[:]...)
}

for idx := range resourceKeys {
keys = append(keys, resourceKeys[idx])
values = append(values, resourceVals[idx])
for idx := range resourceKV.keys {
keys = append(keys, resourceKV.keys[idx])
values = append(values, resourceKV.vals[idx])
}

return keys, values
}

func createInfoMetric(name, description string, res *resource.Resource) (prometheus.Metric, error) {
keys, values := getAttrs(*res.Set(), [2]string{}, [2]string{}, []string{}, []string{})
keys, values := getAttrs(*res.Set(), [2]string{}, [2]string{}, keyVals{})
desc := prometheus.NewDesc(name, description, keys, nil)
return prometheus.NewConstMetric(desc, prometheus.GaugeValue, float64(1), values...)
}
Expand Down Expand Up @@ -494,20 +492,17 @@ func (c *collector) metricType(m metricdata.Metrics) *dto.MetricType {
return nil
}

func (c *collector) resourceAttributes(res *resource.Resource) ([]string, []string) {
func (c *collector) resourceAttributes(res *resource.Resource) {
c.mu.Lock()
defer c.mu.Unlock()

if _, ok := c.resourceKeyVals[res]; ok {
return c.resourceKeyVals[res].keys, c.resourceKeyVals[res].vals
if len(c.resourceKeyVals.keys) > 0 {
return
}

resourceAttrs, _ := res.Set().Filter(c.resourceAttributesFilter)
resourceKeys, resourceValues := getAttrs(resourceAttrs, [2]string{}, [2]string{}, []string{}, []string{})
if c.resourceKeyVals == nil {
c.resourceKeyVals = make(map[*resource.Resource]keyVals, 1)
}
c.resourceKeyVals[res] = keyVals{keys: resourceKeys, vals: resourceValues}
return c.resourceKeyVals[res].keys, c.resourceKeyVals[res].vals
resourceKeys, resourceValues := getAttrs(resourceAttrs, [2]string{}, [2]string{}, keyVals{})
c.resourceKeyVals = keyVals{keys: resourceKeys, vals: resourceValues}
}

func (c *collector) scopeInfo(scope instrumentation.Scope) (prometheus.Metric, error) {
Expand Down

0 comments on commit 88d070c

Please sign in to comment.