Skip to content

Commit 27f478d

Browse files
authored
add properties to telemetry (#52)
1 parent 8c1d914 commit 27f478d

1 file changed

Lines changed: 72 additions & 35 deletions

File tree

Extensions.Microsoft.Diagnostics.HealthChecks.ApplicationInsights/src/ApplicationInsightsHealthCheckPublisher.cs

Lines changed: 72 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -47,54 +47,91 @@ private void TrackAvailability(KeyValuePair<string, HealthReportEntry> entry)
4747
var properties = new Dictionary<string, string>
4848
{
4949
[nameof(ApplicationInsightsHealthCheckPublisherOptions.EnvironmentName)] = options.EnvironmentName,
50+
[$"{nameof(HealthReportEntry)}{nameof(HealthReportEntry.Status)}"] = entry.Value.Status.ToString(),
5051
};
5152

53+
if (!string.IsNullOrEmpty(entry.Value.Description))
54+
{
55+
properties.Add($"{nameof(HealthReportEntry)}{nameof(HealthReportEntry.Description)}", entry.Value.Description);
56+
}
57+
58+
if (entry.Value.Tags.Any())
59+
{
60+
MapTagsAsProperties(entry, properties);
61+
}
62+
5263
var metrics = new Dictionary<string, double>();
5364

5465
if (entry.Value.Data is not null)
5566
{
56-
foreach (var data in entry.Value.Data)
57-
{
58-
if (data.Value is double metric)
59-
{
60-
metrics.Add(data.Key, metric);
61-
}
62-
else if (data.Value is HealthReport entryReport)
63-
{
64-
properties.Add(data.Key, entryReport.Status.ToString());
65-
66-
foreach (var dataEntry in entryReport.Entries)
67-
{
68-
properties.Add($"{data.Key}:{dataEntry.Key}", dataEntry.Value.Status.ToString());
69-
}
70-
}
71-
else if (data.Value is string value)
72-
{
73-
properties.Add(data.Key, value);
74-
}
75-
else if (data.Value is IEnumerable enumerable)
76-
{
77-
properties.Add(data.Key, JsonSerializer.Serialize(enumerable));
78-
}
79-
else
80-
{
81-
properties.Add(data.Key, data.Value?.ToString() ?? string.Empty);
82-
}
83-
}
67+
MapData(entry.Value, properties, metrics);
8468
}
8569

86-
metrics.Add($"{nameof(HealthReportEntry)}{nameof(HealthReportEntry.Duration)}", entry.Value.Duration.TotalMilliseconds);
70+
MapMetrics(entry.Value, metrics);
8771

8872
if (entry.Value.Exception is not null)
8973
{
90-
properties.TryAdd("ExceptionId", Guid.NewGuid().ToString());
91-
properties.TryAdd("ExceptionType", entry.Value.Exception.GetType().FullName!);
92-
properties.TryAdd("ExceptionMessage", entry.Value.Exception.Message);
93-
properties.TryAdd("ExceptionStackTrace", entry.Value.Exception.StackTrace!.ToString());
94-
95-
client.TrackException(entry.Value.Exception, properties, metrics);
74+
TrackException(entry.Value.Exception, properties, metrics);
9675
}
9776

9877
client.TrackAvailability($"{options.ApplicationName}:{entry.Key}", DateTimeOffset.Now, entry.Value.Duration, options.RunLocation, entry.Value.Status == HealthStatus.Healthy, entry.Value.Description, properties, metrics);
9978
}
79+
80+
private static void MapTagsAsProperties(KeyValuePair<string, HealthReportEntry> entry, Dictionary<string, string> properties)
81+
{
82+
var tags = JsonSerializer.Serialize(entry.Value.Tags);
83+
properties.Add($"{nameof(HealthReportEntry)}{nameof(HealthReportEntry.Tags)}", tags);
84+
}
85+
86+
private void TrackException(Exception exception, Dictionary<string, string> properties, Dictionary<string, double> metrics)
87+
{
88+
properties.TryAdd("ExceptionId", Guid.NewGuid().ToString());
89+
properties.TryAdd("ExceptionType", exception.GetType().FullName!);
90+
properties.TryAdd("ExceptionMessage", exception.Message);
91+
properties.TryAdd("ExceptionStackTrace", exception.StackTrace!.ToString());
92+
93+
client.TrackException(exception, properties, metrics);
94+
}
95+
96+
private static void MapMetrics(HealthReportEntry entry, Dictionary<string, double> metrics)
97+
{
98+
metrics.Add($"{nameof(HealthReportEntry)}{nameof(HealthReportEntry.Duration)}", entry.Duration.TotalMilliseconds);
99+
}
100+
101+
private static void MapData(HealthReportEntry entry, Dictionary<string, string> properties, Dictionary<string, double> metrics)
102+
{
103+
foreach (var data in entry.Data)
104+
{
105+
if (data.Value is double metric)
106+
{
107+
metrics.Add(data.Key, metric);
108+
}
109+
else if (data.Value is HealthReport entryReport)
110+
{
111+
MapHealthReportAsProperty(properties, data, entryReport);
112+
}
113+
else if (data.Value is string value)
114+
{
115+
properties.Add(data.Key, value);
116+
}
117+
else if (data.Value is IEnumerable enumerable)
118+
{
119+
properties.Add(data.Key, JsonSerializer.Serialize(enumerable));
120+
}
121+
else
122+
{
123+
properties.Add(data.Key, data.Value?.ToString() ?? string.Empty);
124+
}
125+
}
126+
}
127+
128+
private static void MapHealthReportAsProperty(Dictionary<string, string> properties, KeyValuePair<string, object> data, HealthReport entryReport)
129+
{
130+
properties.Add(data.Key, entryReport.Status.ToString());
131+
132+
foreach (var dataEntry in entryReport.Entries)
133+
{
134+
properties.Add($"{data.Key}:{dataEntry.Key}", dataEntry.Value.Status.ToString());
135+
}
136+
}
100137
}

0 commit comments

Comments
 (0)