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

[Instrumentation.AspNetCore, Instrumentation.Http] Cache activity names #1854

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions opentelemetry-dotnet-contrib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{1FCC8E
src\Shared\PropertyFetcher.AOT.cs = src\Shared\PropertyFetcher.AOT.cs
src\Shared\PropertyFetcher.cs = src\Shared\PropertyFetcher.cs
src\Shared\RedactionHelper.cs = src\Shared\RedactionHelper.cs
src\Shared\RequestDataHelper.cs = src\Shared\RequestDataHelper.cs
src\Shared\ResourceSemanticConventions.cs = src\Shared\ResourceSemanticConventions.cs
src\Shared\SemanticConventions.cs = src\Shared\SemanticConventions.cs
src\Shared\ServerCertificateValidationHandler.cs = src\Shared\ServerCertificateValidationHandler.cs
Expand Down
25 changes: 24 additions & 1 deletion src/Shared/RequestDataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#pragma warning disable IDE0005 // Using directive is unnecessary.
using System;
using System.Collections.Concurrent;
#if NET8_0_OR_GREATER
using System.Collections.Frozen;
#endif
Expand All @@ -20,12 +21,14 @@ namespace OpenTelemetry.Internal;
internal sealed class RequestDataHelper
{
private const string KnownHttpMethodsEnvironmentVariable = "OTEL_INSTRUMENTATION_HTTP_KNOWN_METHODS";
private const int DisplayNameCacheSize = 1000;

// The value "_OTHER" is used for non-standard HTTP methods.
// https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md#common-attributes
private const string OtherHttpMethod = "_OTHER";

private static readonly char[] SplitChars = new[] { ',' };
private static readonly ConcurrentDictionary<string, string> DisplayNameCache = new ConcurrentDictionary<string, string>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be the size limit for this collection?

Copy link
Member

Choose a reason for hiding this comment

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

@joegoldman2 - What happens if a service receives huge number of requests with invalid routes?

Copy link
Contributor Author

@joegoldman2 joegoldman2 Jun 3, 2024

Choose a reason for hiding this comment

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

You're right, the dictionary will grow without limit. Let me try to think about a solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My understanding is that there's no built-in "bounded" collection in .NET. While it's possible to implement a custom one, probably by wrapping ConcurrentDictionary with additional checks, I'm not entirely enthusiastic about it. I'm open to suggestions.

Copy link
Member

Choose a reason for hiding this comment

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

@joegoldman2 - One possible option is to check the Count of items within the dictionary before adding a new item. The Count could be some pre-defined number. If the Count exceeds that number, then we could simply skip adding items to the Dictionary.


#if NET8_0_OR_GREATER
private readonly FrozenDictionary<string, string> knownHttpMethods;
Expand Down Expand Up @@ -84,7 +87,27 @@ public void SetActivityDisplayName(Activity activity, string originalHttpMethod,
var normalizedHttpMethod = this.GetNormalizedHttpMethod(originalHttpMethod);
var namePrefix = normalizedHttpMethod == "_OTHER" ? "HTTP" : normalizedHttpMethod;

activity.DisplayName = string.IsNullOrEmpty(httpRoute) ? namePrefix : $"{namePrefix} {httpRoute}";
activity.DisplayName = GetDisplayName();
Copy link
Contributor

Choose a reason for hiding this comment

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

How much performance would it gain? The new method seems to have enough if checks to counter the benefit. Plus the additional memory usage.


string GetDisplayName()
{
if (string.IsNullOrEmpty(httpRoute))
{
return namePrefix;
}

if (DisplayNameCache.TryGetValue(httpRoute!, out var displayName))
{
return displayName;
}

if (DisplayNameCache.Count < DisplayNameCacheSize)

Choose a reason for hiding this comment

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

.Count gets a lock on all buckets, so this won't be efficient on the hot path

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's what the benchmark above shows.

{
return DisplayNameCache.GetOrAdd(httpRoute!, $"{namePrefix} {httpRoute}");
Copy link
Member

@vishweshbankwar vishweshbankwar Jun 17, 2024

Choose a reason for hiding this comment

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

Re-checking - Can two http methods have the same route values?. if that happens, we will incorrectly set the display name here.

e.g.
Put /api/document/{id}
Get api/document/{id}

}

return $"{namePrefix} {httpRoute}";
}
}

internal static string GetHttpProtocolVersion(Version httpVersion)
Expand Down
Loading