diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md index 8bf55cc7a87..9da8e9b416a 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +* Improve perf by avoiding boxing of common status codes values. + ([#4360](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4360)) + ## 1.0.0-rc9.14 Released 2023-Feb-24 diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs index 15bb59e0cb2..42bd14b3801 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs @@ -240,7 +240,7 @@ public void OnStopActivity(Activity activity, object payload) var response = context.Response; - activity.SetTag(SemanticConventions.AttributeHttpStatusCode, response.StatusCode); + activity.SetTag(SemanticConventions.AttributeHttpStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode)); #if !NETSTANDARD2_0 if (this.options.EnableGrpcAspNetCoreSupport && TryGetGrpcMethod(activity, out var grpcMethod)) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs index cd9ec3c7ba0..8e688b03432 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs @@ -80,7 +80,7 @@ public override void OnEventWritten(string name, object payload) tags.Add(new KeyValuePair(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocol(context.Request.Protocol))); tags.Add(new KeyValuePair(SemanticConventions.AttributeHttpScheme, context.Request.Scheme)); tags.Add(new KeyValuePair(SemanticConventions.AttributeHttpMethod, context.Request.Method)); - tags.Add(new KeyValuePair(SemanticConventions.AttributeHttpStatusCode, context.Response.StatusCode)); + tags.Add(new KeyValuePair(SemanticConventions.AttributeHttpStatusCode, TelemetryHelper.GetBoxedStatusCode(context.Response.StatusCode))); if (context.Request.Host.HasValue) { diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/TelemetryHelper.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/TelemetryHelper.cs new file mode 100644 index 00000000000..00157b118b0 --- /dev/null +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/TelemetryHelper.cs @@ -0,0 +1,104 @@ +// +// 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. +// + +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 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 }, + + { 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 + + public static object GetBoxedStatusCode(int statusCode) + { + if (BoxedStatusCodes.TryGetValue(statusCode, out var result)) + { + return result; + } + + return statusCode; + } +}