MCPcopy
hub / github.com/grafana/tempo / NewWordCounter

Function NewWordCounter

pkg/usagestats/stats.go:380–394  ·  view source on GitHub ↗

NewWordCounter returns a new WordCounter stats object. The WordCounter object is thread-safe and counts the number of words recorded. If a WordCounter stats object with the same name already exists it is returned.

(name string)

Source from the content-addressed store, hash-verified

378// The WordCounter object is thread-safe and counts the number of words recorded.
379// If a WordCounter stats object with the same name already exists it is returned.
380func NewWordCounter(name string) *WordCounter {
381 c := &WordCounter{
382 count: atomic.NewInt64(0),
383 words: sync.Map{},
384 }
385 existing := expvar.Get(statsPrefix + name)
386 if existing != nil {
387 if w, ok := existing.(*WordCounter); ok {
388 return w
389 }
390 panic(fmt.Sprintf("%v is set to a non-WordCounter value", name))
391 }
392 expvar.Publish(statsPrefix+name, c)
393 return c
394}
395
396func (w *WordCounter) Add(word string) {
397 if _, loaded := w.words.LoadOrStore(xxhash.Sum64String(word), struct{}{}); !loaded {

Callers 4

Test_BuildReportFunction · 0.85
Test_BuildStatsFunction · 0.85
TestWordCounterFunction · 0.85
TestPanicsFunction · 0.85

Calls 1

GetMethod · 0.65

Tested by 4

Test_BuildReportFunction · 0.68
Test_BuildStatsFunction · 0.68
TestWordCounterFunction · 0.68
TestPanicsFunction · 0.68