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
Show file tree
Hide file tree
Changes from 2 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
7 changes: 5 additions & 2 deletions src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
## Unreleased

* Fixed an issue of missing `http.client.duration` metric data in case of
network failures (when response is not available).
([#4098](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4098))
network failures (when response is not available).
([#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))

## 1.0.0-rc9.14

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public void OnStopActivity(Activity activity, object payload)

if (this.stopResponseFetcher.TryFetch(payload, out HttpResponseMessage response) && response != null)
{
activity.SetTag(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode);
activity.SetTag(SemanticConventions.AttributeHttpStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode));
utpilla marked this conversation as resolved.
Show resolved Hide resolved

if (currentStatusCode == ActivityStatusCode.Unset)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public override void OnEventWritten(string name, object payload)

if (this.stopResponseFetcher.TryFetch(payload, out HttpResponseMessage response) && response != null)
{
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode));
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode)));
}

// We are relying here on HttpClient library to set duration before writing the stop event.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private static void AddResponseTags(HttpWebResponse response, Activity activity)
{
if (activity.IsAllDataRequested)
{
activity.SetTag(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode);
activity.SetTag(SemanticConventions.AttributeHttpStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode));

activity.SetStatus(SpanHelper.ResolveSpanStatusForHttpStatusCode(activity.Kind, (int)response.StatusCode));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// <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>

using System.Net;

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()
reyang marked this conversation as resolved.
Show resolved Hide resolved
{
{ 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 },*/

{ 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

public static object GetBoxedStatusCode(HttpStatusCode statusCode)
{
if (BoxedStatusCodes.TryGetValue(statusCode, out var result))
{
return result;
}

return (int)statusCode;
}
}