Skip to content

Instantly share code, notes, and snippets.

@stevejgordon

stevejgordon/Program.cs Secret

Last active Nov 4, 2020
Embed
What would you like to do?
Example telemetry listener which listens to Sockets, NameResolution, Http and Security events and counters
using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Net.Http;
using System.Threading.Tasks;
namespace TelemetryListener
{
internal class Program
{
private static async Task Main()
{
using var listener = new TelemetryListener();
var client = new HttpClient();
try
{
await client.GetAsync("https://www.stevejgordon.co.uk");
}
catch
{
// ignore
}
await Task.Delay(2000);
}
}
internal sealed class TelemetryListener : EventListener
{
protected override void OnEventSourceCreated(EventSource eventSource)
{
if (eventSource.Name.Equals("System.Net.Sockets")
|| eventSource.Name.Equals("System.Net.Http")
|| eventSource.Name.Equals("System.Net.NameResolution")
|| eventSource.Name.Equals("System.Net.Security"))
{
EnableEvents(eventSource, EventLevel.Verbose, EventKeywords.All, new Dictionary<string, string>
{
{"EventCounterIntervalSec", "2"}
});
}
}
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
if (eventData.EventName == "EventCounters")
{
if (eventData.Payload?.Count <= 0
|| eventData.Payload?[0] is not IDictionary<string, object> data
|| !data.TryGetValue("CounterType", out var ct)
|| !data.TryGetValue("Name", out var n)
|| ct is not string counterType
|| n is not string name) return;
var metricValue = counterType switch
{
"Sum" when data.TryGetValue("Increment", out var increment) => Convert.ToInt64(increment),
"Mean" when data.TryGetValue("Mean", out var mean) => Convert.ToInt64(mean),
_ => 0
};
switch (nameString)
{
case "dns-lookups-duration":
Console.WriteLine($"Event Counter = Avg Duration of Lookup: {metricValue}ms");
Console.WriteLine();
break;
case "dns-lookups-requested":
Console.WriteLine($"Event Counter = Name Resolution Lookups: {metricValue}");
Console.WriteLine();
break;
case "total-tls-handshakes":
Console.WriteLine($"Event Counter = Total TLS Handshakes: {metricValue}");
Console.WriteLine();
break;
case "requests-started":
Console.WriteLine($"Event Counter = HTTP Requests Started: {metricValue}");
Console.WriteLine();
break;
case "requests-failed":
Console.WriteLine($"Event Counter = HTTP Requests Failed: {metricValue}");
Console.WriteLine();
break;
case "outgoing-connections-established":
Console.WriteLine($"Event Counter = Outgoing Connections Established: {metricValue}");
Console.WriteLine();
break;
case "http11-connections-current-total":
Console.WriteLine($"Event Counter = HTTP1.1 Connections Established: {metricValue}");
Console.WriteLine();
break;
case "http20-connections-current-total":
Console.WriteLine($"Event Counter = HTTP2.0 Connections Established: {metricValue}");
Console.WriteLine();
break;
case "bytes-sent":
Console.WriteLine($"Event Counter = Bytes Sent: {metricValue}");
Console.WriteLine();
break;
case "bytes-received":
Console.WriteLine($"Event Counter = Bytes Received: {metricValue}");
Console.WriteLine();
break;
}
}
if (eventData.EventName != "EventCounters")
{
Console.WriteLine($"Event = {eventData.EventSource.Name} - {eventData.EventId}:{eventData.EventName}");
for (var i = 0; i < eventData.PayloadNames?.Count; i++)
{
Console.WriteLine(
$" - {eventData.PayloadNames[i]}: {eventData.Payload?[i]?.ToString() ?? string.Empty}");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment