-
-
Notifications
You must be signed in to change notification settings - Fork 828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for telemetry metrics and tracing #1715
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// | ||
// ClientMetrics.cs | ||
// | ||
// Author: Jeffrey Stedfast <[email protected]> | ||
// | ||
// Copyright (c) 2013-2024 .NET Foundation and Contributors | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
|
||
#if NET6_0_OR_GREATER | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Globalization; | ||
using System.Diagnostics.Metrics; | ||
|
||
using MailKit.Net.Smtp; | ||
using MailKit.Security; | ||
|
||
namespace MailKit.Net { | ||
sealed class ClientMetrics | ||
{ | ||
public readonly Histogram<double> ConnectionDuration; | ||
public readonly Counter<long> OperationCounter; | ||
public readonly Histogram<double> OperationDuration; | ||
public readonly string MeterName; | ||
|
||
public ClientMetrics (Meter meter, string meterName, string an, string protocol) | ||
{ | ||
MeterName = meterName; | ||
|
||
ConnectionDuration = meter.CreateHistogram<double> ( | ||
name: $"{meterName}.client.connection.duration", | ||
unit: "s", | ||
description: $"The duration of successfully established connections to {an} {protocol} server."); | ||
|
||
OperationCounter = meter.CreateCounter<long> ( | ||
name: $"{meterName}.client.operation.count", | ||
unit: "{operation}", | ||
description: $"The number of times a client performed an operation on {an} {protocol} server."); | ||
|
||
OperationDuration = meter.CreateHistogram<double> ( | ||
name: $"{meterName}.client.operation.duration", | ||
unit: "ms", | ||
description: $"The amount of time it takes for the {protocol} server to perform an operation."); | ||
} | ||
|
||
static bool TryGetErrorType (Exception exception, out string errorType) | ||
{ | ||
if (SocketMetrics.TryGetErrorType (exception, false, out errorType)) | ||
return true; | ||
|
||
if (exception is SslHandshakeException) { | ||
// Note: The string "secure_connection_error" is used by HttpClient for SSL/TLS handshake errors. | ||
errorType = "secure_connection_error"; | ||
return true; | ||
} | ||
|
||
if (exception is ProtocolException) { | ||
// TODO: ProtocolExceptions tend to be either "Unexpectedly disconnected" or "Parse error". | ||
// If we add a property to ProtocolException to tell us this, we could report it better here. | ||
// | ||
// To mimic HttpClient error.type values, we could use "response_ended" and "invalid_response", respectively. | ||
// | ||
// Alternatively, HttpClient also uses "http_protocol_error" so we could use "smtp/pop3/imap_protocol_error". | ||
errorType = "protocol_error"; | ||
return true; | ||
} | ||
|
||
if (exception is SmtpCommandException smtp) { | ||
errorType = ((int) smtp.StatusCode).ToString (CultureInfo.InvariantCulture); | ||
return true; | ||
} | ||
|
||
if (exception is CommandException) { | ||
// FIXME: We need to add a property to CommandException to tell us the error type. | ||
errorType = "command_error"; | ||
return true; | ||
} | ||
|
||
// Fall back to using the exception type name. | ||
errorType = exception.GetType ().FullName; | ||
|
||
return true; | ||
} | ||
|
||
internal static TagList GetTags (Uri uri, Exception ex) | ||
{ | ||
var tags = new TagList { | ||
{ "url.scheme", uri.Scheme }, | ||
{ "server.address", uri.Host }, | ||
{ "server.port", uri.Port } | ||
}; | ||
|
||
if (ex is not null && TryGetErrorType (ex, out var errorType)) | ||
tags.Add ("error.type", errorType); | ||
|
||
return tags; | ||
} | ||
|
||
public void RecordClientDisconnected (long startTimestamp, Uri uri, Exception ex = null) | ||
{ | ||
if (ConnectionDuration.Enabled) { | ||
var duration = TimeSpan.FromTicks (Stopwatch.GetTimestamp () - startTimestamp).TotalSeconds; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most often than not, recording time better done in milliseconds rather than seconds. It'll also be consistent with your There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I got this from HttpClient using seconds for connection_duration and was thinking that clients would realistically be kept connected for long enough durations that For IMAP, this would realistically be the case but perhaps for something like SmtpClient, it isn't necessarily true unless the developer is trying to keep a connection alive for re-use. Not sure how people are using MailKit's SmtpClient in server applications, so this may or may not be the case. System.Net.Mail's SmtpClient was much more like HttpClient in that it kept a connection pool in the background. MailKit, however, is a 1:1 mapping of Client to connection and developers might spawn up a new SmtpClient for every batch send that they do in which case Gotta think about the units for this, I guess. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW, OpenTelemetry and Prometheus standardize on seconds unit for duration. Cf https://opentelemetry.io/docs/specs/semconv/general/metrics/#instrument-units and https://prometheus.io/docs/practices/naming/#metric-names There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it could be either, but it should be:
so either should be fine, just have to pick 1, document it and stick to it :) |
||
var tags = GetTags (uri, ex); | ||
|
||
ConnectionDuration.Record (duration, tags); | ||
} | ||
} | ||
} | ||
} | ||
|
||
#endif // NET6_0_OR_GREATER |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though
HttpClient
reports those tag values, you should also consider the best practices published by microsoft, specifically:In some use cases MailKit could be used to relay emails to many servers (think about a multi-tenant scenario).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair. I can likely make this configurable.