|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Diagnostics.Metrics; |
| 3 | +using Servus.Core.Diagnostics; |
| 4 | + |
| 5 | +namespace Servus.Akka.Transport; |
| 6 | + |
| 7 | +internal static class ServusExtensions |
| 8 | +{ |
| 9 | + private static Histogram<double>? _dnsLookupDuration; |
| 10 | + private static Histogram<double>? _socketConnectDuration; |
| 11 | + |
| 12 | + public static Histogram<double> DnsLookupDuration(this ServusMetrics metrics) |
| 13 | + { |
| 14 | + return _dnsLookupDuration ??= metrics.Meter.CreateHistogram<double>( |
| 15 | + "dns.lookup.duration", |
| 16 | + unit: "s", |
| 17 | + description: "Duration of DNS lookups in seconds"); |
| 18 | + } |
| 19 | + |
| 20 | + public static Histogram<double> SocketConnectDuration(this ServusMetrics metrics) |
| 21 | + { |
| 22 | + return _socketConnectDuration ??= metrics.Meter.CreateHistogram<double>( |
| 23 | + "network.socket.connect.duration", |
| 24 | + unit: "s", |
| 25 | + description: "Duration of socket connect operations in seconds"); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +internal static class ServusTraceExtensions |
| 30 | +{ |
| 31 | + public static Activity? StartDnsLookup(this ServusTrace trace, string hostname) |
| 32 | + { |
| 33 | + if (!trace.Source.HasListeners()) |
| 34 | + { |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + var activity = trace.Source.StartActivity("dns.lookup", ActivityKind.Client); |
| 39 | + activity?.SetTag("dns.question.name", hostname); |
| 40 | + return activity; |
| 41 | + } |
| 42 | + |
| 43 | + public static void SetDnsAnswers(this ServusTrace _, Activity activity, string[] answers) |
| 44 | + { |
| 45 | + activity.SetTag("dns.answers", string.Join(",", answers)); |
| 46 | + activity.SetTag("dns.answer.count", answers.Length); |
| 47 | + } |
| 48 | + |
| 49 | + public static Activity? StartSocketConnect(this ServusTrace trace, string address, int port, string transport, string networkType) |
| 50 | + { |
| 51 | + if (!trace.Source.HasListeners()) |
| 52 | + { |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + var activity = trace.Source.StartActivity("network.socket.connect", ActivityKind.Client); |
| 57 | + if (activity is null) |
| 58 | + { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + activity.SetTag("network.peer.address", address); |
| 63 | + activity.SetTag("network.peer.port", port); |
| 64 | + activity.SetTag("network.transport", transport); |
| 65 | + activity.SetTag("network.type", networkType); |
| 66 | + return activity; |
| 67 | + } |
| 68 | + |
| 69 | + public static void SetError(this ServusTrace _, Activity activity, Exception exception) |
| 70 | + { |
| 71 | + activity.SetStatus(ActivityStatusCode.Error, exception.Message); |
| 72 | + activity.SetTag("error.type", exception.GetType().FullName); |
| 73 | + activity.SetTag("exception.message", exception.Message); |
| 74 | + } |
| 75 | +} |
0 commit comments