/
Property.cs
198 lines (167 loc) · 6.48 KB
/
Property.cs
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// <copyright file="Property.cs" company="Microsoft">
// Copyright © Microsoft. All Rights Reserved.
// </copyright>
namespace Microsoft.ApplicationInsights.Extensibility.Implementation
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
/// <summary>
/// A helper class for implementing properties of telemetry and context classes.
/// </summary>
internal static class Property
{
public const int MaxDictionaryNameLength = 150;
public const int MaxDependencyTypeLength = 1024;
public const int MaxValueLength = 8 * 1024;
public const int MaxEventNameLength = 512;
public const int MaxNameLength = 1024;
public const int MaxMessageLength = 32768;
public const int MaxUrlLength = 2048;
public const int MaxCommandNameLength = 2 * 1024;
private const RegexOptions SanitizeOptions =
#if CORE_PCL
RegexOptions.None;
#else
RegexOptions.Compiled;
#endif
private static readonly Regex InvalidNameCharacters = new Regex(@"[^0-9a-zA-Z-._()\/ ]", Property.SanitizeOptions);
public static void Set<T>(ref T property, T value) where T : class
{
if (value == default(T))
{
throw new ArgumentNullException("value");
}
property = value;
}
public static void Initialize<T>(ref T? property, T? value) where T : struct
{
if (!property.HasValue)
{
property = value;
}
}
public static void Initialize(ref string property, string value)
{
if (string.IsNullOrEmpty(property))
{
property = value;
}
}
public static string SanitizeEventName(this string name)
{
return TrimAndTruncate(name, Property.MaxEventNameLength);
}
public static string SanitizeName(this string name)
{
return TrimAndTruncate(name, Property.MaxNameLength);
}
public static string SanitizeDependencyType(this string value)
{
return TrimAndTruncate(value, Property.MaxDependencyTypeLength);
}
public static string SanitizeValue(this string value)
{
return TrimAndTruncate(value, Property.MaxValueLength);
}
public static string SanitizeMessage(this string message)
{
return TrimAndTruncate(message, Property.MaxMessageLength);
}
public static string SanitizeCommandName(this string message)
{
return TrimAndTruncate(message, Property.MaxCommandNameLength);
}
public static Uri SanitizeUri(this Uri uri)
{
if (uri != null)
{
string url = uri.ToString();
if (url.Length > MaxUrlLength)
{
url = url.Substring(0, MaxUrlLength);
// in case that the truncated string is invalid
// URI we will not do nothing and let the Endpoint to drop the property
Uri temp;
if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out temp) == true)
{
uri = temp;
}
}
}
return uri;
}
public static void SanitizeProperties(this IDictionary<string, string> dictionary)
{
if (dictionary != null)
{
foreach (KeyValuePair<string, string> entry in dictionary.ToArray())
{
// remove the key from the dictionary first
dictionary.Remove(entry.Key);
string sanitizedKey = SanitizeKey(entry.Key, dictionary);
string sanitizedValue = SanitizeValue(entry.Value);
// add it back (sanitized at this point).
dictionary.Add(sanitizedKey, sanitizedValue);
}
}
}
public static void SanitizeMeasurements(this IDictionary<string, double> dictionary)
{
if (dictionary != null)
{
foreach (KeyValuePair<string, double> entry in dictionary.ToArray())
{
// remove the key from the dictionary first
dictionary.Remove(entry.Key);
string sanitizedKey = SanitizeKey(entry.Key, dictionary);
// add it back (sanitized at this point).
dictionary.Add(sanitizedKey, entry.Value);
}
}
}
private static string TrimAndTruncate(string value, int maxLength)
{
if (value != null)
{
value = value.Trim();
value = Truncate(value, maxLength);
}
return value;
}
private static string Truncate(string value, int maxLength)
{
return value.Length > maxLength ? value.Substring(0, maxLength) : value;
}
private static string SanitizeKey<TValue>(string key, IDictionary<string, TValue> dictionary)
{
string sanitizedKey = TrimAndTruncate(key, Property.MaxDictionaryNameLength);
sanitizedKey = InvalidNameCharacters.Replace(sanitizedKey, "_");
sanitizedKey = MakeKeyNonEmpty(sanitizedKey);
sanitizedKey = MakeKeyUnique(sanitizedKey, dictionary);
return sanitizedKey;
}
private static string MakeKeyNonEmpty(string key)
{
return string.IsNullOrEmpty(key) ? "(required property name is empty)" : key;
}
private static string MakeKeyUnique<TValue>(string key, IDictionary<string, TValue> dictionary)
{
if (dictionary.ContainsKey(key))
{
const int UniqueNumberLength = 3;
string truncatedKey = Truncate(key, MaxDictionaryNameLength - UniqueNumberLength);
int candidate = 1;
do
{
key = truncatedKey + candidate.ToString(CultureInfo.InvariantCulture).PadLeft(UniqueNumberLength, '0');
candidate++;
}
while (dictionary.ContainsKey(key));
}
return key;
}
}
}