-
Notifications
You must be signed in to change notification settings - Fork 2
/
text.go
80 lines (63 loc) · 1.83 KB
/
text.go
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
// Copyright (c) 2021 Circonus, Inc. <[email protected]>
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
package trapmetrics
import (
"fmt"
"strings"
"time"
"unicode"
)
// TextSet sets a sample with a given timestamp for a text to the passed value.
func (tm *TrapMetrics) TextSet(name string, tags Tags, val string, ts *time.Time) error {
mt := mtText
metricID, err := generateMetricID(name, mt, tags)
if err != nil {
return err
}
sampleKey := generateSampleKey(ts)
tm.metricsmu.Lock()
defer tm.metricsmu.Unlock()
value := tm.cleanTextValue(val)
if m, ok := tm.metrics[metricID]; ok {
if m.Mtype != mtText {
return fmt.Errorf("(%s %s) exists with different type (text) vs (%s)", name, tags.String(), m.Mtype)
}
m.Samples[sampleKey] = value
return nil
}
m, err := tm.newMetric(name, mt, tags)
if err != nil {
return fmt.Errorf("(%s %s) failed to initialize (text): %w", name, tags.String(), err)
}
m.Rtype = rtString
m.Samples[sampleKey] = value
tm.metrics[m.ID] = m
return nil
}
// TextFetch will return the metric identified by name and tags.
func (tm *TrapMetrics) TextFetch(name string, tags Tags) (*Metric, error) {
metricID, err := generateMetricID(name, mtText, tags)
if err != nil {
return nil, err
}
tm.metricsmu.Lock()
defer tm.metricsmu.Unlock()
if m, ok := tm.metrics[metricID]; ok {
return m, nil
}
return nil, fmt.Errorf("text %d (%s %s) not found", metricID, name, tags.String())
}
func (tm *TrapMetrics) cleanTextValue(val string) string {
// remove leading and trailing spaces
clean := strings.TrimSpace(val)
// replace any non-printable characters with configured tm.nonPrintCharReplace
clean = strings.Map(func(r rune) rune {
if unicode.IsPrint(r) {
return r
}
return tm.nonPrintCharReplace
}, clean)
return clean
}