-
Notifications
You must be signed in to change notification settings - Fork 784
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
[ASP.NET Core] Set http.request.method
as per spec
#5001
Changes from all commits
8a991e1
20c3b09
4519884
f70c4fd
06e0c5d
f1f330f
a11a481
bf22886
b1f168c
2cd0715
a387d1e
fe96c9f
6feef4b
c4f79da
6cb1267
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,77 @@ | ||
// <copyright file="RequestMethodHelper.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
#if NET8_0_OR_GREATER | ||
using System.Collections.Frozen; | ||
#endif | ||
|
||
namespace OpenTelemetry.Internal; | ||
|
||
internal static class RequestMethodHelper | ||
{ | ||
#if NET8_0_OR_GREATER | ||
internal static readonly FrozenDictionary<string, string> KnownMethods; | ||
#else | ||
internal static readonly Dictionary<string, string> KnownMethods; | ||
#endif | ||
|
||
static RequestMethodHelper() | ||
{ | ||
#if NET8_0_OR_GREATER | ||
KnownMethods = FrozenDictionary.ToFrozenDictionary( | ||
utpilla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
new[] | ||
vishweshbankwar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
KeyValuePair.Create("GET", "GET"), | ||
KeyValuePair.Create("PUT", "PUT"), | ||
KeyValuePair.Create("POST", "POST"), | ||
KeyValuePair.Create("DELETE", "DELETE"), | ||
KeyValuePair.Create("HEAD", "HEAD"), | ||
KeyValuePair.Create("OPTIONS", "OPTIONS"), | ||
KeyValuePair.Create("TRACE", "TRACE"), | ||
KeyValuePair.Create("PATCH", "PATCH"), | ||
KeyValuePair.Create("CONNECT", "CONNECT"), | ||
}, | ||
StringComparer.OrdinalIgnoreCase); | ||
#else | ||
KnownMethods = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) | ||
utpilla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
{ "GET", "GET" }, | ||
{ "PUT", "PUT" }, | ||
{ "POST", "POST" }, | ||
{ "DELETE", "DELETE" }, | ||
{ "HEAD", "HEAD" }, | ||
{ "OPTIONS", "OPTIONS" }, | ||
{ "TRACE", "TRACE" }, | ||
{ "PATCH", "PATCH" }, | ||
{ "CONNECT", "CONNECT" }, | ||
}; | ||
#endif | ||
} | ||
|
||
public static bool TryResolveHttpMethod(string method, out string resolvedMethod) | ||
{ | ||
if (KnownMethods.TryGetValue(method, out resolvedMethod)) | ||
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. @vishweshbankwar Would you do a bit of benchmarking here? I think a Check out: https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-8/#frozen-collections If you scroll down from that anchor to the "IsMostPopular" example, the compiler generates really smart logic for a string switch. I'm guessing it will be faster because it is customized for the known values and done inline, plus it also doesn't need the processing at startup FrozenDictionary needs. 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. @CodeBlanch We need a case insensitive search here. I think we would end up doing either 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. @CodeBlanch We need case-insensitive look up. https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-8/#frozen-collections:~:text=such%20that%20if%20you%20know%20all%20of%20your%20string%20keys%20at%20compile%2Dtime%2C%20and%20you%20just%20need%20an%20ordinal%2C%20case%2Dsensitive%20lookup%2C%20you%20might%20be%20best%20off%20simply%20writing%20a%20switch%20statement%20or%20expression 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. Makes sense however I still feel like we could solve it in a way that would be faster. Given there are only a handful of values and we know them all upfront. Perf challenge for anyone willing to take it on 😄 |
||
{ | ||
// KnownMethods ignores case. Use the value returned by the dictionary to have a consistent case. | ||
return true; | ||
} | ||
|
||
// Set to default "_OTHER" as per spec. | ||
// https://github.com/open-telemetry/semantic-conventions/blob/v1.22.0/docs/http/http-spans.md#common-attributes | ||
resolvedMethod = "_OTHER"; | ||
return false; | ||
} | ||
} |
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.
Nice! We should identify other places in the repo that can use Frozen collections.