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

[http] Avoid boxing of status code tags #4361

Merged
merged 4 commits into from
Apr 4, 2023
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,92 +20,25 @@ namespace OpenTelemetry.Instrumentation.Http.Implementation;

internal static class TelemetryHelper
{
#pragma warning disable SA1509 // Opening braces should not be preceded by blank line
// Status Codes listed at http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
private static readonly Dictionary<HttpStatusCode, object> BoxedStatusCodes = new()
{
{ HttpStatusCode.Continue, 100 },
{ HttpStatusCode.SwitchingProtocols, 101 },
/*{ 102, 102 },*/

{ HttpStatusCode.OK, 200 },
{ HttpStatusCode.Created, 201 },
{ HttpStatusCode.Accepted, 202 },
{ HttpStatusCode.NonAuthoritativeInformation, 203 },
{ HttpStatusCode.NoContent, 204 },
{ HttpStatusCode.ResetContent, 205 },
{ HttpStatusCode.PartialContent, 206 },
/*{ 207, 207 },
{ 208, 208 },
{ 226, 226 },*/

{ HttpStatusCode.MultipleChoices, 300 },
/* { HttpStatusCode.Ambiguous, 300 }, */
{ HttpStatusCode.MovedPermanently, 301 },
/* { HttpStatusCode.Moved, 301 }, */
{ HttpStatusCode.Found, 302 },
/* { HttpStatusCode.Redirect, 302 }, */
{ HttpStatusCode.SeeOther, 303 },
/* { HttpStatusCode.RedirectMethod, 303 }, */
{ HttpStatusCode.NotModified, 304 },
{ HttpStatusCode.UseProxy, 305 },
{ HttpStatusCode.Unused, 306 },
{ HttpStatusCode.TemporaryRedirect, 307 },
/* { HttpStatusCode.RedirectKeepVerb, 307 }, */
/*{ 308, 308 },*/

{ HttpStatusCode.BadRequest, 400 },
{ HttpStatusCode.Unauthorized, 401 },
{ HttpStatusCode.PaymentRequired, 402 },
{ HttpStatusCode.Forbidden, 403 },
{ HttpStatusCode.NotFound, 404 },
{ HttpStatusCode.MethodNotAllowed, 405 },
{ HttpStatusCode.NotAcceptable, 406 },
{ HttpStatusCode.ProxyAuthenticationRequired, 407 },
{ HttpStatusCode.RequestTimeout, 408 },
{ HttpStatusCode.Conflict, 409 },
{ HttpStatusCode.Gone, 410 },
{ HttpStatusCode.LengthRequired, 411 },
{ HttpStatusCode.PreconditionFailed, 412 },
{ HttpStatusCode.RequestEntityTooLarge, 413 },
{ HttpStatusCode.RequestUriTooLong, 414 },
{ HttpStatusCode.UnsupportedMediaType, 415 },
{ HttpStatusCode.RequestedRangeNotSatisfiable, 416 },
{ HttpStatusCode.ExpectationFailed, 417 },
/*{ 418, 418 },
{ 419, 419 },
{ 421, 421 },
{ 422, 422 },
{ 423, 423 },
{ 424, 424 },*/
{ HttpStatusCode.UpgradeRequired, 426 },
/*{ 428, 428 },
{ 429, 429 },
{ 431, 431 },
{ 451, 451 },
{ 499, 499 },*/
private static readonly object[] BoxedStatusCodes;

{ HttpStatusCode.InternalServerError, 500 },
{ HttpStatusCode.NotImplemented, 501 },
{ HttpStatusCode.BadGateway, 502 },
{ HttpStatusCode.ServiceUnavailable, 503 },
{ HttpStatusCode.GatewayTimeout, 504 },
{ HttpStatusCode.HttpVersionNotSupported, 505 },
/*{ 506, 506 },
{ 507, 507 },
{ 508, 508 },
{ 510, 510 },
{ 511, 511 },*/
};
#pragma warning restore SA1509 // Opening braces should not be preceded by blank line
static TelemetryHelper()
{
BoxedStatusCodes = new object[500];
Copy link
Member

Choose a reason for hiding this comment

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

@JamesNK FYI.

for (int i = 0, c = 100; i < BoxedStatusCodes.Length; i++, c++)
{
BoxedStatusCodes[i] = c;
}
}

public static object GetBoxedStatusCode(HttpStatusCode statusCode)
{
if (BoxedStatusCodes.TryGetValue(statusCode, out var result))
int intStatusCode = (int)statusCode;
if (intStatusCode >= 100 && intStatusCode < 600)
{
return result;
return BoxedStatusCodes[intStatusCode - 100];
}

return (int)statusCode;
return intStatusCode;
}
}