fn: prometheus collector concurrent map access (#510)

* fn: prometheus collector concurrent map access

*) Added mutex to guard against concurrent access to maps

* fn: prometheus collector method receivers should be ptr

* fn: prometheus collector concurrent map access

*) Moved the mutex into getHistogramVec()
This commit is contained in:
Tolga Ceylan
2017-11-17 12:46:53 -08:00
committed by Travis Reeder
parent 5693f7dc53
commit 57b24d63c3

View File

@@ -6,13 +6,14 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
) )
// PrometheusCollector is a custom Collector // PrometheusCollector is a custom Collector
// which sends ZipKin traces to Prometheus // which sends ZipKin traces to Prometheus
type PrometheusCollector struct { type PrometheusCollector struct {
lock sync.Mutex
// Each span name is published as a separate Histogram metric // Each span name is published as a separate Histogram metric
// Using metric names of the form fn_span_<span-name>_duration_seconds // Using metric names of the form fn_span_<span-name>_duration_seconds
@@ -27,12 +28,15 @@ type PrometheusCollector struct {
// NewPrometheusCollector returns a new PrometheusCollector // NewPrometheusCollector returns a new PrometheusCollector
func NewPrometheusCollector() (zipkintracer.Collector, error) { func NewPrometheusCollector() (zipkintracer.Collector, error) {
pc := &PrometheusCollector{make(map[string]*prometheus.HistogramVec), make(map[string][]string)} pc := &PrometheusCollector{
histogramVecMap: make(map[string]*prometheus.HistogramVec),
registeredLabelKeysMap: make(map[string][]string),
}
return pc, nil return pc, nil
} }
// PrometheusCollector implements Collector. // PrometheusCollector implements Collector.
func (pc PrometheusCollector) Collect(span *zipkincore.Span) error { func (pc *PrometheusCollector) Collect(span *zipkincore.Span) error {
spanName := span.GetName() spanName := span.GetName()
@@ -61,12 +65,15 @@ func (pc PrometheusCollector) Collect(span *zipkincore.Span) error {
} }
// Return (and create, if necessary) a HistogramVec for the specified Prometheus metric // Return (and create, if necessary) a HistogramVec for the specified Prometheus metric
func (pc PrometheusCollector) getHistogramVec( func (pc *PrometheusCollector) getHistogramVec(
metricName string, metricHelp string, labelKeysFromSpan []string, labelValuesFromSpan map[string]string) ( metricName string, metricHelp string, labelKeysFromSpan []string, labelValuesFromSpan map[string]string) (
*prometheus.HistogramVec, map[string]string) { *prometheus.HistogramVec, map[string]string) {
var labelValuesToUse map[string]string var labelValuesToUse map[string]string
pc.lock.Lock()
defer pc.lock.Unlock()
histogramVec, found := pc.histogramVecMap[metricName] histogramVec, found := pc.histogramVecMap[metricName]
if !found { if !found {
// create a new HistogramVec // create a new HistogramVec
@@ -143,4 +150,4 @@ func getLoggedMetrics(span *zipkincore.Span) map[string]uint64 {
} }
// PrometheusCollector implements Collector. // PrometheusCollector implements Collector.
func (PrometheusCollector) Close() error { return nil } func (*PrometheusCollector) Close() error { return nil }