@@ -10,10 +10,10 @@ func encodeToStatsd(flushableBuckets: [BucketTimestamp: [Metric]]) -> Data {
1010 let buckets = bucket. value
1111 for metric in buckets {
1212
13- statsdString. append ( sanitize ( key : metric. key) )
13+ statsdString. append ( sanitize ( metricKey : metric. key) )
1414 statsdString. append ( " @ " )
1515
16- statsdString. append ( metric. unit. unit)
16+ statsdString. append ( sanitize ( metricUnit : metric. unit. unit) )
1717
1818 for serializedValue in metric. serialize ( ) {
1919 statsdString. append ( " : \( serializedValue) " )
@@ -24,7 +24,7 @@ func encodeToStatsd(flushableBuckets: [BucketTimestamp: [Metric]]) -> Data {
2424
2525 var firstTag = true
2626 for (tagKey, tagValue) in metric. tags {
27- let sanitizedTagKey = sanitize ( key : tagKey)
27+ let sanitizedTagKey = sanitize ( tagKey : tagKey)
2828
2929 if firstTag {
3030 statsdString. append ( " |# " )
@@ -34,7 +34,7 @@ func encodeToStatsd(flushableBuckets: [BucketTimestamp: [Metric]]) -> Data {
3434 }
3535
3636 statsdString. append ( " \( sanitizedTagKey) : " )
37- statsdString. append ( sanitize ( value : tagValue) )
37+ statsdString. append ( replaceTagValueCharacters ( tagValue : tagValue) )
3838 }
3939
4040 statsdString. append ( " |T " )
@@ -46,10 +46,27 @@ func encodeToStatsd(flushableBuckets: [BucketTimestamp: [Metric]]) -> Data {
4646 return statsdString. data ( using: . utf8) ?? Data ( )
4747}
4848
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)
5152}
5253
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+
5572}
0 commit comments