Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
MetricsEventSource.cs
Go to the documentation of this file.
6using System.Text;
7
9
10[EventSource(Name = "System.Diagnostics.Metrics")]
11internal sealed class MetricsEventSource : EventSource
12{
13 public static class Keywords
14 {
16
18
20 }
21
22 private sealed class CommandHandler
23 {
25
26 private string _sessionId = "";
27
28 private static readonly char[] s_instrumentSeperators = new char[4] { '\r', '\n', ',', ';' };
29
31 {
32 try
33 {
35 {
36 Log.Error("", "System.Diagnostics.Metrics EventSource not supported on browser");
37 return;
38 }
39 if (command.Command == EventCommand.Update || command.Command == EventCommand.Disable || command.Command == EventCommand.Enable)
40 {
41 if (_aggregationManager != null)
42 {
43 if (command.Command == EventCommand.Enable || command.Command == EventCommand.Update)
44 {
45 Log.MultipleSessionsNotSupportedError(_sessionId);
46 return;
47 }
50 Log.Message("Previous session with id " + _sessionId + " is stopped");
51 }
52 _sessionId = "";
53 }
54 if ((command.Command != 0 && command.Command != EventCommand.Enable) || command.Arguments == null)
55 {
56 return;
57 }
58 if (command.Arguments.TryGetValue("SessionId", out string value))
59 {
61 Log.Message("SessionId argument received: " + _sessionId);
62 }
63 else
64 {
65 _sessionId = Guid.NewGuid().ToString();
66 Log.Message("New session started. SessionId auto-generated: " + _sessionId);
67 }
68 double num = 1.0;
69 double result = num;
70 if (command.Arguments.TryGetValue("RefreshInterval", out string value2))
71 {
72 Log.Message("RefreshInterval argument received: " + value2);
73 if (!double.TryParse(value2, out result))
74 {
75 Log.Message($"Failed to parse RefreshInterval. Using default {num}s.");
76 result = num;
77 }
78 else if (result < 0.1)
79 {
80 Log.Message($"RefreshInterval too small. Using minimum interval {0.1} seconds.");
81 result = 0.1;
82 }
83 }
84 else
85 {
86 Log.Message($"No RefreshInterval argument received. Using default {num}s.");
87 result = num;
88 }
89 int num2 = 1000;
90 int result2;
91 if (command.Arguments.TryGetValue("MaxTimeSeries", out string value3))
92 {
93 Log.Message("MaxTimeSeries argument received: " + value3);
94 if (!int.TryParse(value3, out result2))
95 {
96 Log.Message($"Failed to parse MaxTimeSeries. Using default {num2}");
97 result2 = num2;
98 }
99 }
100 else
101 {
102 Log.Message($"No MaxTimeSeries argument received. Using default {num2}");
103 result2 = num2;
104 }
105 int num3 = 20;
106 int result3;
107 if (command.Arguments.TryGetValue("MaxHistograms", out string value4))
108 {
109 Log.Message("MaxHistograms argument received: " + value4);
110 if (!int.TryParse(value4, out result3))
111 {
112 Log.Message($"Failed to parse MaxHistograms. Using default {num3}");
113 result3 = num3;
114 }
115 }
116 else
117 {
118 Log.Message($"No MaxHistogram argument received. Using default {num3}");
119 result3 = num3;
120 }
121 string sessionId = _sessionId;
123 {
126 {
129 {
131 }, delegate(Instrument i)
132 {
133 Log.BeginInstrumentReporting(sessionId, i.Meter.Name, i.Meter.Version, i.Name, i.GetType().Name, i.Unit, i.Description);
134 }, delegate(Instrument i)
135 {
136 Log.EndInstrumentReporting(sessionId, i.Meter.Name, i.Meter.Version, i.Name, i.GetType().Name, i.Unit, i.Description);
137 }, delegate(Instrument i)
138 {
139 Log.InstrumentPublished(sessionId, i.Meter.Name, i.Meter.Version, i.Name, i.GetType().Name, i.Unit, i.Description);
140 }, delegate
141 {
142 Log.InitialInstrumentEnumerationComplete(sessionId);
143 }, delegate(Exception e)
144 {
145 Log.Error(sessionId, e.ToString());
146 }, delegate
147 {
148 Log.TimeSeriesLimitReached(sessionId);
149 }, delegate
150 {
151 Log.HistogramLimitReached(sessionId);
152 }, delegate(Exception e)
153 {
154 Log.ObservableInstrumentCallbackError(sessionId, e.ToString());
155 });
157 if (command.Arguments.TryGetValue("Metrics", out string value5))
158 {
159 Log.Message("Metrics argument received: " + value5);
161 }
162 else
163 {
164 Log.Message("No Metrics argument received");
165 }
167 }
168 catch (Exception e2) when (LogError(e2))
169 {
170 }
171 }
172
173 private bool LogError(Exception e)
174 {
175 Log.Error(_sessionId, e.ToString());
176 return false;
177 }
178
179 [UnsupportedOSPlatform("browser")]
180 private void ParseSpecs(string metricsSpecs)
181 {
182 if (metricsSpecs == null)
183 {
184 return;
185 }
186 string[] array = metricsSpecs.Split(s_instrumentSeperators, StringSplitOptions.RemoveEmptyEntries);
187 string[] array2 = array;
188 foreach (string text in array2)
189 {
191 {
192 Log.Message("Failed to parse metric spec: {specString}");
193 continue;
194 }
195 Log.Message("Parsed metric: {spec}");
196 if (spec.InstrumentName != null)
197 {
198 _aggregationManager.Include(spec.MeterName, spec.InstrumentName);
199 }
200 else
201 {
203 }
204 }
205 }
206
208 {
209 if (stats.AggregationStatistics is RateStatistics rateStatistics)
210 {
211 Log.CounterRateValuePublished(sessionId, instrument.Meter.Name, instrument.Meter.Version, instrument.Name, instrument.Unit, FormatTags(stats.Labels), rateStatistics.Delta.HasValue ? rateStatistics.Delta.Value.ToString(CultureInfo.InvariantCulture) : "");
212 }
213 else if (stats.AggregationStatistics is LastValueStatistics lastValueStatistics)
214 {
215 Log.GaugeValuePublished(sessionId, instrument.Meter.Name, instrument.Meter.Version, instrument.Name, instrument.Unit, FormatTags(stats.Labels), lastValueStatistics.LastValue.HasValue ? lastValueStatistics.LastValue.Value.ToString(CultureInfo.InvariantCulture) : "");
216 }
217 else if (stats.AggregationStatistics is HistogramStatistics histogramStatistics)
218 {
219 Log.HistogramValuePublished(sessionId, instrument.Meter.Name, instrument.Meter.Version, instrument.Name, instrument.Unit, FormatTags(stats.Labels), FormatQuantiles(histogramStatistics.Quantiles));
220 }
221 }
222
224 {
226 for (int i = 0; i < labels.Length; i++)
227 {
228 stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}={1}", labels[i].Key, labels[i].Value);
229 if (i != labels.Length - 1)
230 {
231 stringBuilder.Append(',');
232 }
233 }
234 return stringBuilder.ToString();
235 }
236
238 {
240 for (int i = 0; i < quantiles.Length; i++)
241 {
242 stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}={1}", quantiles[i].Quantile, quantiles[i].Value);
243 if (i != quantiles.Length - 1)
244 {
245 stringBuilder.Append(';');
246 }
247 }
248 return stringBuilder.ToString();
249 }
250 }
251
252 private class MetricSpec
253 {
254 public string MeterName { get; private set; }
255
256 public string InstrumentName { get; private set; }
257
258 public MetricSpec(string meterName, string instrumentName)
259 {
262 }
263
264 public static bool TryParse(string text, out MetricSpec spec)
265 {
266 int num = text.IndexOf('\\');
267 if (num == -1)
268 {
269 spec = new MetricSpec(text.Trim(), null);
270 return true;
271 }
272 string meterName = text.Substring(0, num).Trim();
273 string instrumentName = text.Substring(num + 1).Trim();
275 return true;
276 }
277
278 public override string ToString()
279 {
280 if (InstrumentName == null)
281 {
282 return MeterName;
283 }
284 return MeterName + "\\" + InstrumentName;
285 }
286 }
287
288 public static readonly MetricsEventSource Log = new MetricsEventSource();
289
291
293 {
294 _handler = new CommandHandler();
295 }
296
297 [Event(1, Keywords = (EventKeywords)1L)]
298 public void Message(string Message)
299 {
301 }
302
303 [Event(2, Keywords = (EventKeywords)2L)]
304 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "This calls WriteEvent with all primitive arguments which is safe. Primitives are always serialized properly.")]
309
310 [Event(3, Keywords = (EventKeywords)2L)]
311 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "This calls WriteEvent with all primitive arguments which is safe. Primitives are always serialized properly.")]
316
317 [Event(4, Keywords = (EventKeywords)2L)]
318 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "This calls WriteEvent with all primitive arguments which is safe. Primitives are always serialized properly.")]
319 public void CounterRateValuePublished(string sessionId, string meterName, string meterVersion, string instrumentName, string unit, string tags, string rate)
320 {
322 }
323
324 [Event(5, Keywords = (EventKeywords)2L)]
325 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "This calls WriteEvent with all primitive arguments which is safe. Primitives are always serialized properly.")]
326 public void GaugeValuePublished(string sessionId, string meterName, string meterVersion, string instrumentName, string unit, string tags, string lastValue)
327 {
329 }
330
331 [Event(6, Keywords = (EventKeywords)2L)]
332 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "This calls WriteEvent with all primitive arguments which is safe. Primitives are always serialized properly.")]
333 public void HistogramValuePublished(string sessionId, string meterName, string meterVersion, string instrumentName, string unit, string tags, string quantiles)
334 {
336 }
337
338 [Event(7, Keywords = (EventKeywords)2L)]
339 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "This calls WriteEvent with all primitive arguments which is safe. Primitives are always serialized properly.")]
340 public void BeginInstrumentReporting(string sessionId, string meterName, string meterVersion, string instrumentName, string instrumentType, string unit, string description)
341 {
342 WriteEvent(7, sessionId, meterName, meterVersion ?? "", instrumentName, instrumentType, unit ?? "", description ?? "");
343 }
344
345 [Event(8, Keywords = (EventKeywords)2L)]
346 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "This calls WriteEvent with all primitive arguments which is safe. Primitives are always serialized properly.")]
347 public void EndInstrumentReporting(string sessionId, string meterName, string meterVersion, string instrumentName, string instrumentType, string unit, string description)
348 {
349 WriteEvent(8, sessionId, meterName, meterVersion ?? "", instrumentName, instrumentType, unit ?? "", description ?? "");
350 }
351
352 [Event(9, Keywords = (EventKeywords)7L)]
353 public void Error(string sessionId, string errorMessage)
354 {
356 }
357
358 [Event(10, Keywords = (EventKeywords)6L)]
360 {
362 }
363
364 [Event(11, Keywords = (EventKeywords)4L)]
365 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "This calls WriteEvent with all primitive arguments which is safe. Primitives are always serialized properly.")]
366 public void InstrumentPublished(string sessionId, string meterName, string meterVersion, string instrumentName, string instrumentType, string unit, string description)
367 {
368 WriteEvent(11, sessionId, meterName, meterVersion ?? "", instrumentName, instrumentType, unit ?? "", description ?? "");
369 }
370
371 [Event(12, Keywords = (EventKeywords)2L)]
373 {
375 }
376
377 [Event(13, Keywords = (EventKeywords)2L)]
379 {
381 }
382
383 [Event(14, Keywords = (EventKeywords)2L)]
388
389 [Event(15, Keywords = (EventKeywords)7L)]
394
395 [NonEvent]
397 {
398 lock (this)
399 {
401 }
402 }
403}
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
AggregationManager SetCollectionPeriod(TimeSpan collectionPeriod)
string FormatTags(KeyValuePair< string, string >[] labels)
void TransmitMetricValue(Instrument instrument, LabeledAggregationStatistics stats, string sessionId)
MetricSpec(string meterName, string instrumentName)
static bool TryParse(string text, out MetricSpec spec)
override void OnEventCommand(EventCommandEventArgs command)
void CollectionStop(string sessionId, DateTime intervalStartTime, DateTime intervalEndTime)
void MultipleSessionsNotSupportedError(string runningSessionId)
void Error(string sessionId, string errorMessage)
void GaugeValuePublished(string sessionId, string meterName, string meterVersion, string instrumentName, string unit, string tags, string lastValue)
void BeginInstrumentReporting(string sessionId, string meterName, string meterVersion, string instrumentName, string instrumentType, string unit, string description)
void CollectionStart(string sessionId, DateTime intervalStartTime, DateTime intervalEndTime)
void ObservableInstrumentCallbackError(string sessionId, string errorMessage)
void InstrumentPublished(string sessionId, string meterName, string meterVersion, string instrumentName, string instrumentType, string unit, string description)
void EndInstrumentReporting(string sessionId, string meterName, string meterVersion, string instrumentName, string instrumentType, string unit, string description)
void CounterRateValuePublished(string sessionId, string meterName, string meterVersion, string instrumentName, string unit, string tags, string rate)
void HistogramValuePublished(string sessionId, string meterName, string meterVersion, string instrumentName, string unit, string tags, string quantiles)
unsafe void WriteEvent(int eventId)
override string ToString()
Definition Exception.cs:384
static CultureInfo InvariantCulture
static Guid NewGuid()
Definition Guid.cs:1283
static TimeSpan FromSeconds(double value)
Definition TimeSpan.cs:247