Skip to content
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

Merged
merged 1 commit into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion MailKit/MailKit.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>An Open Source cross-platform .NET mail-client library that is based on MimeKit and optimized for mobile devices.</Description>
Expand Down Expand Up @@ -122,8 +122,11 @@
<Compile Include="Net\Smtp\SmtpResponse.cs" />
<Compile Include="Net\Smtp\SmtpStatusCode.cs" />
<Compile Include="Net\Smtp\SmtpStream.cs" />
<Compile Include="Net\ClientMetrics.cs" />
<Compile Include="Net\IChannelBindingContext.cs" />
<Compile Include="Net\NetworkOperation.cs" />
<Compile Include="Net\NetworkStream.cs" />
<Compile Include="Net\SocketMetrics.cs" />
<Compile Include="Net\SocketUtils.cs" />
<Compile Include="Net\SslStream.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down Expand Up @@ -273,6 +276,7 @@
<Compile Include="StoreAction.cs" />
<Compile Include="StoreFlagsRequest.cs" />
<Compile Include="StoreLabelsRequest.cs" />
<Compile Include="Telemetry.cs" />
<Compile Include="TextEncodings.cs" />
<Compile Include="ThreadingAlgorithm.cs" />
<Compile Include="UniqueId.cs" />
Expand Down
4 changes: 4 additions & 0 deletions MailKit/MailKitLite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,11 @@
<Compile Include="Net\Smtp\SmtpResponse.cs" />
<Compile Include="Net\Smtp\SmtpStatusCode.cs" />
<Compile Include="Net\Smtp\SmtpStream.cs" />
<Compile Include="Net\ClientMetrics.cs" />
<Compile Include="Net\IChannelBindingContext.cs" />
<Compile Include="Net\NetworkOperation.cs" />
<Compile Include="Net\NetworkStream.cs" />
<Compile Include="Net\SocketMetrics.cs" />
<Compile Include="Net\SocketUtils.cs" />
<Compile Include="Net\SslStream.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down Expand Up @@ -276,6 +279,7 @@
<Compile Include="StoreAction.cs" />
<Compile Include="StoreFlagsRequest.cs" />
<Compile Include="StoreLabelsRequest.cs" />
<Compile Include="Telemetry.cs" />
<Compile Include="TextEncodings.cs" />
<Compile Include="ThreadingAlgorithm.cs" />
<Compile Include="UniqueId.cs" />
Expand Down
130 changes: 130 additions & 0 deletions MailKit/Net/ClientMetrics.cs
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 },

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:

Beware of having very large or unbounded combinations of tag values being recorded in practice...Most metric collection tools will either drop data to stay within technical limits or there can be large monetary costs to cover the data storage and processing...Most metric collection tools will either drop data to stay within technical limits or there can be large monetary costs to cover the data storage and processing.

In some use cases MailKit could be used to relay emails to many servers (think about a multi-tenant scenario).

Copy link
Owner Author

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.

{ "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;

Choose a reason for hiding this comment

The 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 OperationDuration metric units.

Copy link
Owner Author

Choose a reason for hiding this comment

The 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 ms wouldn't make sense.

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 ms would probably be a more useful metric.

Gotta think about the units for this, I guess.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it could be either, but it should be:

  1. consistent between multiple metrics from the same source, be predictable
  2. should be sensitive enough to show time for short processes and long processes while still being consistent

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
Loading
Loading