@@ -10,10 +10,10 @@ func encodeToStatsd(flushableBuckets: [BucketTimestamp: [Metric]]) -> Data {
10
10
let buckets = bucket. value
11
11
for metric in buckets {
12
12
13
- statsdString. append ( sanitize ( key : metric. key) )
13
+ statsdString. append ( sanitize ( metricKey : metric. key) )
14
14
statsdString. append ( " @ " )
15
15
16
- statsdString. append ( metric. unit. unit)
16
+ statsdString. append ( sanitize ( metricUnit : metric. unit. unit) )
17
17
18
18
for serializedValue in metric. serialize ( ) {
19
19
statsdString. append ( " : \( serializedValue) " )
@@ -24,7 +24,7 @@ func encodeToStatsd(flushableBuckets: [BucketTimestamp: [Metric]]) -> Data {
24
24
25
25
var firstTag = true
26
26
for (tagKey, tagValue) in metric. tags {
27
- let sanitizedTagKey = sanitize ( key : tagKey)
27
+ let sanitizedTagKey = sanitize ( tagKey : tagKey)
28
28
29
29
if firstTag {
30
30
statsdString. append ( " |# " )
@@ -34,7 +34,7 @@ func encodeToStatsd(flushableBuckets: [BucketTimestamp: [Metric]]) -> Data {
34
34
}
35
35
36
36
statsdString. append ( " \( sanitizedTagKey) : " )
37
- statsdString. append ( sanitize ( value : tagValue) )
37
+ statsdString. append ( replaceTagValueCharacters ( tagValue : tagValue) )
38
38
}
39
39
40
40
statsdString. append ( " |T " )
@@ -46,10 +46,27 @@ func encodeToStatsd(flushableBuckets: [BucketTimestamp: [Metric]]) -> Data {
46
46
return statsdString. data ( using: . utf8) ?? Data ( )
47
47
}
48
48
49
- private func sanitize( key: String ) -> String {
50
- return key. replacingOccurrences ( of: " [^a-zA-Z0-9_/.-]+ " , with: " _ " , options: . regularExpression)
49
+ private func sanitize( metricUnit: String ) -> String {
50
+ // We can't use \w because it includes chars like ä on Swift
51
+ return metricUnit. replacingOccurrences ( of: " [^a-zA-Z0-9_] " , with: " " , options: . regularExpression)
51
52
}
52
53
53
- private func sanitize( value: String ) -> String {
54
- return value. replacingOccurrences ( of: " [^ \\ w \\ d \\ s_:/@ \\ . \\ { \\ } \\ [ \\ ]$-]+ " , with: " " , options: . regularExpression)
54
+ private func sanitize( metricKey: String ) -> String {
55
+ // We can't use \w because it includes chars like ä on Swift
56
+ return metricKey. replacingOccurrences ( of: " [^a-zA-Z0-9_.-]+ " , with: " _ " , options: . regularExpression)
57
+ }
58
+
59
+ private func sanitize( tagKey: String ) -> String {
60
+ // We can't use \w because it includes chars like ä on Swift
61
+ return tagKey. replacingOccurrences ( of: " [^a-zA-Z0-9_/.-]+ " , with: " " , options: . regularExpression)
62
+ }
63
+
64
+ private func replaceTagValueCharacters( tagValue: String ) -> String {
65
+ var result = tagValue. replacingOccurrences ( of: " \\ " , with: #"\\\\"# )
66
+ result = result. replacingOccurrences ( of: " \n " , with: #"\\n"# )
67
+ result = result. replacingOccurrences ( of: " \r " , with: #"\\r"# )
68
+ result = result. replacingOccurrences ( of: " \t " , with: #"\\t"# )
69
+ result = result. replacingOccurrences ( of: " | " , with: #"\\u{7c}"# )
70
+ return result. replacingOccurrences ( of: " , " , with: #"\\u{2c}"# )
71
+
55
72
}
0 commit comments