diff --git a/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md index 715a06ff6dd..c9329f714e5 100644 --- a/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md @@ -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 diff --git a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs index a60049eb939..fa10137333b 100644 --- a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs +++ b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs @@ -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)); if (currentStatusCode == ActivityStatusCode.Unset) { diff --git a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs index 8a533f342cf..4fc5a4f982e 100644 --- a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs +++ b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs @@ -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(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode)); + tags.Add(new KeyValuePair(SemanticConventions.AttributeHttpStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode))); } // We are relying here on HttpClient library to set duration before writing the stop event. diff --git a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs index c1c20ac9218..7150ce8ddde 100644 --- a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs +++ b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs @@ -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)); diff --git a/src/OpenTelemetry.Instrumentation.Http/Implementation/TelemetryHelper.cs b/src/OpenTelemetry.Instrumentation.Http/Implementation/TelemetryHelper.cs new file mode 100644 index 00000000000..b0cdce4eb11 --- /dev/null +++ b/src/OpenTelemetry.Instrumentation.Http/Implementation/TelemetryHelper.cs @@ -0,0 +1,44 @@ +// +// 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. +// + +using System.Net; + +namespace OpenTelemetry.Instrumentation.Http.Implementation; + +internal static class TelemetryHelper +{ + private static readonly object[] BoxedStatusCodes; + + static TelemetryHelper() + { + BoxedStatusCodes = new object[500]; + for (int i = 0, c = 100; i < BoxedStatusCodes.Length; i++, c++) + { + BoxedStatusCodes[i] = c; + } + } + + public static object GetBoxedStatusCode(HttpStatusCode statusCode) + { + int intStatusCode = (int)statusCode; + if (intStatusCode >= 100 && intStatusCode < 600) + { + return BoxedStatusCodes[intStatusCode - 100]; + } + + return intStatusCode; + } +}