Skip to content

invalid pattern on singleton GetMonitor() #27

Open
@jerome-laforge

Description

@jerome-laforge

In order to avoid data race on GetMonitor(), gin-metrics has to use sync.Once or sync.Mutex.

For example with sync.Once:

var (
	defaultDuration = []float64{0.1, 0.3, 1.2, 5, 10}
	monitor         *Monitor
	onceMonitor     sync.Once

	promTypeHandler = map[MetricType]func(metric *Metric) error{
		Counter:   counterHandler,
		Gauge:     gaugeHandler,
		Histogram: histogramHandler,
		Summary:   summaryHandler,
	}
)

// GetMonitor used to get global Monitor object,
// this function returns a singleton object.
func GetMonitor() *Monitor {
        onceMonitor.Do(
            func() {
		monitor = &Monitor{
			metricPath:  defaultMetricPath,
			slowTime:    defaultSlowTime,
			reqDuration: defaultDuration,
			metrics:     make(map[string]*Metric),
		}
	}

	return monitor
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions