summaryrefslogtreecommitdiff
path: root/util/metrics/tagcounter.go
blob: c2b5fcb9bb5ca21b0ce0eb596c8e87ebab725a41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package metrics

import (
	"strconv"
	"strings"
	"sync/atomic"

	"github.com/algorand/go-deadlock"
	"golang.org/x/exp/maps"
)

// NewTagCounterFiltered makes a set of metrics under rootName for tagged counting.
// "{TAG}" in rootName is replaced by the tag, otherwise "_{TAG}" is appended.
// Tags not in allowedTags will be filtered out and ignored.
// unknownTag may be "" or a value that will be counted for tags not in allowedTags.
func NewTagCounterFiltered(rootName, desc string, allowedTags []string, unknownTag string) *TagCounter {
	tc := &TagCounter{Name: rootName, Description: desc, UnknownTag: unknownTag}
	if len(allowedTags) != 0 {
		tc.AllowedTags = make(map[string]bool, len(allowedTags))
		for _, tag := range allowedTags {
			tc.AllowedTags[tag] = true
		}
	}
	DefaultRegistry().Register(tc)
	return tc
}

// NewTagCounter makes a set of metrics under rootName for tagged counting.
// "{TAG}" in rootName is replaced by the tag, otherwise "_{TAG}" is appended.
// Optionally provided declaredTags counters for these names up front (making them easier to discover).
func NewTagCounter(rootName, desc string, declaredTags ...string) *TagCounter {
	tc := &TagCounter{Name: rootName, Description: desc}
	for _, tag := range declaredTags {
		tc.Add(tag, 0)
	}
	DefaultRegistry().Register(tc)
	return tc
}

// TagCounter holds a set of counters
type TagCounter struct {
	Name        string
	Description string

	AllowedTags map[string]bool

	UnknownTag string

	// a read only race-free reference to tags
	tagptr atomic.Value

	tags map[string]*uint64

	storage    [][]uint64
	storagePos int

	tagLock deadlock.Mutex
}

// Add t[tag] += val, fast and multithread safe
func (tc *TagCounter) Add(tag string, val uint64) {
	if (tc.AllowedTags != nil) && (!tc.AllowedTags[tag]) {
		if len(tc.UnknownTag) != 0 {
			tag = tc.UnknownTag
		} else {
			return
		}
	}
	for {
		var tags map[string]*uint64
		tagptr := tc.tagptr.Load()
		if tagptr != nil {
			tags = tagptr.(map[string]*uint64)
		}

		count, ok := tags[tag]
		if ok {
			atomic.AddUint64(count, val)
			return
		}
		tc.tagLock.Lock()
		if _, ok = tc.tags[tag]; !ok {
			// Still need to add a new tag.
			// Make a new map so there's never any race.
			newtags := make(map[string]*uint64, len(tc.tags)+1)
			maps.Copy(newtags, tc.tags)
			var st []uint64
			if len(tc.storage) > 0 {
				st = tc.storage[len(tc.storage)-1]
			}
			if tc.storagePos > (len(st) - 1) {
				st = make([]uint64, 16)
				tc.storagePos = 0
				tc.storage = append(tc.storage, st)
			}
			newtags[tag] = &(st[tc.storagePos])
			tc.storagePos++
			tc.tags = newtags
			tc.tagptr.Store(newtags)
		}
		tc.tagLock.Unlock()
	}
}

// WriteMetric is part of the Metric interface
func (tc *TagCounter) WriteMetric(buf *strings.Builder, parentLabels string) {
	tagptr := tc.tagptr.Load()
	if tagptr == nil {
		// no values, nothing to say.
		return
	}
	isTemplate := strings.Contains(tc.Name, "{TAG}")
	tags := tagptr.(map[string]*uint64)
	for tag, tagcount := range tags {
		if tagcount == nil {
			continue
		}
		var name string
		if isTemplate {
			name = strings.ReplaceAll(tc.Name, "{TAG}", tag)
		} else {
			name = tc.Name + "_" + tag
		}
		buf.WriteString("# HELP ")
		buf.WriteString(name)
		buf.WriteRune(' ')
		buf.WriteString(strings.ReplaceAll(tc.Description, "{TAG}", tag))
		buf.WriteString("\n# TYPE ")
		buf.WriteString(name)
		buf.WriteString(" counter\n")
		buf.WriteString(name)
		if len(parentLabels) > 0 {
			buf.WriteRune('{')
			buf.WriteString(parentLabels)
			buf.WriteRune('}')
		}
		buf.WriteRune(' ')
		count := atomic.LoadUint64(tagcount)
		buf.WriteString(strconv.FormatUint(count, 10))
		buf.WriteRune('\n')
	}
}

// AddMetric is part of the Metric interface
// Copy the values in this TagCounter out into the string-string map.
func (tc *TagCounter) AddMetric(values map[string]float64) {
	tagp := tc.tagptr.Load()
	if tagp == nil {
		return
	}
	isTemplate := strings.Contains(tc.Name, "{TAG}")
	tags := tagp.(map[string]*uint64)
	for tag, tagcount := range tags {
		if tagcount == nil {
			continue
		}
		var name string
		if isTemplate {
			name = strings.ReplaceAll(tc.Name, "{TAG}", tag)
		} else {
			name = tc.Name + "_" + tag
		}
		count := atomic.LoadUint64(tagcount)
		values[sanitizeTelemetryName(name)] = float64(count)
	}
}