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

[AspNetCore+Http] StatusCode boxing logic improvements #4363

Merged
merged 2 commits into from
Apr 4, 2023
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
3 changes: 2 additions & 1 deletion src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
## Unreleased

* Improve perf by avoiding boxing of common status codes values.
([#4360](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4360))
([#4360](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4360),
[#4363](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4363))

## 1.0.0-rc9.14

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,85 +18,22 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.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<int, object> BoxedStatusCodes = new()
{
{ 100, 100 },
{ 101, 101 },
{ 102, 102 },

{ 200, 200 },
{ 201, 201 },
{ 202, 202 },
{ 203, 203 },
{ 204, 204 },
{ 205, 205 },
{ 206, 206 },
{ 207, 207 },
{ 208, 208 },
{ 226, 226 },

{ 300, 300 },
{ 301, 301 },
{ 302, 302 },
{ 303, 303 },
{ 304, 304 },
{ 305, 305 },
{ 306, 306 },
{ 307, 307 },
{ 308, 308 },

{ 400, 400 },
{ 401, 401 },
{ 402, 402 },
{ 403, 403 },
{ 404, 404 },
{ 405, 405 },
{ 406, 406 },
{ 407, 407 },
{ 408, 408 },
{ 409, 409 },
{ 410, 410 },
{ 411, 411 },
{ 412, 412 },
{ 413, 413 },
{ 414, 414 },
{ 415, 415 },
{ 416, 416 },
{ 417, 417 },
{ 418, 418 },
{ 419, 419 },
{ 421, 421 },
{ 422, 422 },
{ 423, 423 },
{ 424, 424 },
{ 426, 426 },
{ 428, 428 },
{ 429, 429 },
{ 431, 431 },
{ 451, 451 },
{ 499, 499 },
public static readonly object[] BoxedStatusCodes;

{ 500, 500 },
{ 501, 501 },
{ 502, 502 },
{ 503, 503 },
{ 504, 504 },
{ 505, 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];
for (int i = 0, c = 100; i < BoxedStatusCodes.Length; i++, c++)
{
BoxedStatusCodes[i] = c;
}
}

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

return statusCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenTelemetry.Instrumentation.AspNetCore;
using OpenTelemetry.Instrumentation.AspNetCore.Implementation;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Metrics
Expand Down Expand Up @@ -60,6 +61,9 @@ public static MeterProviderBuilder AddAspNetCoreInstrumentation(
{
Guard.ThrowIfNull(builder);

// Note: Warm-up the status code mapping.
_ = TelemetryHelper.BoxedStatusCodes;

name ??= Options.DefaultName;

if (configureAspNetCoreInstrumentationOptions != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public static TracerProviderBuilder AddAspNetCoreInstrumentation(
{
Guard.ThrowIfNull(builder);

// Note: Warm-up the status code mapping.
_ = TelemetryHelper.BoxedStatusCodes;

name ??= Options.DefaultName;

if (configureAspNetCoreInstrumentationOptions != null)
Expand Down
3 changes: 2 additions & 1 deletion src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
([#4098](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4098))

* Improve perf by avoiding boxing of common status codes values.
([#4361](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4361))
([#4361](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4361),
[#4363](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4363))

## 1.0.0-rc9.14

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace OpenTelemetry.Instrumentation.Http.Implementation;

internal static class TelemetryHelper
{
private static readonly object[] BoxedStatusCodes;
public static readonly object[] BoxedStatusCodes;

static TelemetryHelper()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>

using OpenTelemetry.Instrumentation.Http;
using OpenTelemetry.Instrumentation.Http.Implementation;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Metrics
Expand All @@ -34,6 +35,9 @@ public static MeterProviderBuilder AddHttpClientInstrumentation(
{
Guard.ThrowIfNull(builder);

// Note: Warm-up the status code mapping.
_ = TelemetryHelper.BoxedStatusCodes;

// TODO: Implement an IDeferredMeterProviderBuilder

// TODO: Handle HttpClientInstrumentationOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public static TracerProviderBuilder AddHttpClientInstrumentation(
{
Guard.ThrowIfNull(builder);

// Note: Warm-up the status code mapping.
_ = TelemetryHelper.BoxedStatusCodes;

name ??= Options.DefaultName;

if (configureHttpClientInstrumentationOptions != null)
Expand Down