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] Avoid boxing of status code tags #4360

Merged
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: 3 additions & 0 deletions src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public override void OnEventWritten(string name, object payload)
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocol(context.Request.Protocol)));
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpScheme, context.Request.Scheme));
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpMethod, context.Request.Method));
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpStatusCode, context.Response.StatusCode));
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpStatusCode, TelemetryHelper.GetBoxedStatusCode(context.Response.StatusCode)));

if (context.Request.Host.HasValue)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// <copyright file="TelemetryHelper.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>

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 },

{ 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;
}
}