-
Notifications
You must be signed in to change notification settings - Fork 5
/
TelemetryBuilder.cs
208 lines (174 loc) · 8.57 KB
/
TelemetryBuilder.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
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Fabric;
using System.Linq;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Serilog.Events;
using ServiceFabric.Logging.PropertyMap;
namespace ServiceFabric.Logging.ApplicationInsights
{
internal class TelemetryBuilder
{
private readonly ServiceContext context;
private readonly LogEvent logEvent;
public TelemetryBuilder(ServiceContext context, LogEvent logEvent)
{
this.context = context;
this.logEvent = logEvent;
}
public ITelemetry LogEventToTelemetryConverter()
{
int serviceFabricEvent = ServiceFabricEvent.Undefined;
if (logEvent.Properties.TryGetValue(SharedProperties.EventId, out LogEventPropertyValue eventId))
{
int.TryParse(((StructureValue)eventId).Properties[0].Value.ToString(), out serviceFabricEvent);
}
ITelemetry telemetry;
switch (serviceFabricEvent)
{
case ServiceFabricEvent.Exception:
telemetry = CreateExceptionTelemetry();
break;
case ServiceFabricEvent.ApiRequest:
telemetry = CreateRequestTelemetry();
break;
case ServiceFabricEvent.Metric:
telemetry = CreateMetricTelemetry();
break;
case ServiceFabricEvent.ServiceRequest:
case ServiceFabricEvent.Dependency:
telemetry = CreateDependencyTelemetry();
break;
default:
telemetry = CreateTraceTelemetry();
break;
}
SetContextProperties(telemetry);
return telemetry;
}
private ITelemetry CreateRequestTelemetry()
{
var requestTelemetry = new RequestTelemetry
{
ResponseCode = TryGetStringValue(ApiRequestProperties.StatusCode),
Url = new Uri($"{TryGetStringValue(ApiRequestProperties.Scheme)}://{TryGetStringValue(ApiRequestProperties.Host)}{TryGetStringValue(ApiRequestProperties.Path)}"),
Name = $"{TryGetStringValue(ApiRequestProperties.Method)} {TryGetStringValue(ApiRequestProperties.Path)}",
Timestamp = DateTime.Parse(TryGetStringValue(ApiRequestProperties.StartTime)),
Duration = TimeSpan.FromMilliseconds(double.Parse(TryGetStringValue(ApiRequestProperties.DurationInMs))),
Success = bool.Parse(TryGetStringValue(ApiRequestProperties.Success)),
Properties =
{
{ ApiRequestProperties.Method, TryGetStringValue(ApiRequestProperties.Method) },
{ ApiRequestProperties.Headers, TryGetStringValue(ApiRequestProperties.Headers) },
{ ApiRequestProperties.Body, TryGetStringValue(ApiRequestProperties.Body) }
}
};
requestTelemetry.Context.Operation.Name = requestTelemetry.Name;
requestTelemetry.Id = TryGetStringValue(SharedProperties.TraceId);
AddLogEventProperties(requestTelemetry, typeof(ApiRequestProperties).GetFields().Select(f => f.GetRawConstantValue().ToString()));
return requestTelemetry;
}
private ITelemetry CreateDependencyTelemetry()
{
var dependencyTelemetry = new DependencyTelemetry
{
Name = TryGetStringValue(DependencyProperties.DependencyTypeName),
Duration = TimeSpan.FromMilliseconds(double.Parse(TryGetStringValue(DependencyProperties.DurationInMs))),
Data = TryGetStringValue(DependencyProperties.Name),
Success = bool.Parse(TryGetStringValue(DependencyProperties.Success)),
Type = TryGetStringValue(DependencyProperties.Type),
Timestamp = DateTime.Parse(TryGetStringValue(DependencyProperties.StartTime)),
};
dependencyTelemetry.Id = dependencyTelemetry.Data;
dependencyTelemetry.Context.Operation.Name = dependencyTelemetry.Name;
AddLogEventProperties(dependencyTelemetry, typeof(DependencyProperties).GetFields().Select(f => f.GetRawConstantValue().ToString()));
return dependencyTelemetry;
}
private ITelemetry CreateMetricTelemetry()
{
var metricTelemetry = new MetricTelemetry
{
Name = TryGetStringValue(MetricProperties.Name),
Sum = double.Parse(TryGetStringValue(MetricProperties.Value)),
Timestamp = logEvent.Timestamp
};
if (logEvent.Properties.TryGetValue(MetricProperties.MinValue, out LogEventPropertyValue min))
metricTelemetry.Min = double.Parse(min.ToString());
if (logEvent.Properties.TryGetValue(MetricProperties.MaxValue, out LogEventPropertyValue max))
metricTelemetry.Max = double.Parse(max.ToString());
AddLogEventProperties(metricTelemetry, typeof(MetricProperties).GetFields().Select(f => f.GetRawConstantValue().ToString()));
return metricTelemetry;
}
private ITelemetry CreateExceptionTelemetry()
{
var exceptionTelemetry = new ExceptionTelemetry(logEvent.Exception)
{
SeverityLevel = logEvent.Level.ToSeverityLevel(),
Timestamp = logEvent.Timestamp
};
AddLogEventProperties(exceptionTelemetry);
return exceptionTelemetry;
}
private ITelemetry CreateTraceTelemetry()
{
var traceTelemetry = new TraceTelemetry(logEvent.RenderMessage())
{
SeverityLevel = logEvent.Level.ToSeverityLevel(),
Timestamp = logEvent.Timestamp
};
AddLogEventProperties(traceTelemetry);
return traceTelemetry;
}
private void SetContextProperties(ITelemetry telemetry)
{
telemetry.Context.Cloud.RoleName = FabricEnvironmentVariable.ServicePackageName;
telemetry.Context.Cloud.RoleInstance = FabricEnvironmentVariable.ServicePackageActivationId ?? FabricEnvironmentVariable.ServicePackageInstanceId;
telemetry.Context.Component.Version = context.CodePackageActivationContext.CodePackageVersion;
if (!telemetry.Context.Properties.ContainsKey(ServiceContextProperties.NodeName))
{
if (!string.IsNullOrEmpty(FabricEnvironmentVariable.NodeName))
{
telemetry.Context.Properties.Add(ServiceContextProperties.NodeName, FabricEnvironmentVariable.NodeName);
}
}
#if Debug
telemetry.Context.Operation.SyntheticSource = "DebugSession";
#else
if (Debugger.IsAttached)
{
telemetry.Context.Operation.SyntheticSource = "DebuggerAttached";
}
#endif
if (logEvent.Properties.TryGetValue(SharedProperties.TraceId, out LogEventPropertyValue value))
{
var id = ((ScalarValue) value).Value.ToString();
telemetry.Context.Operation.ParentId = id;
telemetry.Context.Operation.Id = id;
}
}
private void AddLogEventProperties(ISupportProperties telemetry, IEnumerable<string> excludePropertyKeys = null)
{
var excludedPropertyKeys = new List<string>
{
ServiceContextProperties.NodeName,
ServiceContextProperties.ServicePackageVersion
};
if(excludePropertyKeys != null)
excludedPropertyKeys.AddRange(excludePropertyKeys);
foreach (var property in logEvent
.Properties
.Where(property => property.Value != null && !excludedPropertyKeys.Contains(property.Key) && !telemetry.Properties.ContainsKey(property.Key)))
{
ApplicationInsightsPropertyFormatter.WriteValue(property.Key, property.Value, telemetry.Properties);
}
}
private string TryGetStringValue(string propertyName)
{
if (!logEvent.Properties.TryGetValue(propertyName, out LogEventPropertyValue value))
throw new ArgumentException($"LogEvent does not contain required property {propertyName} for EventId {logEvent.Properties[SharedProperties.EventId]}", propertyName);
return ((ScalarValue)value).Value.ToString();
}
}
}