HttpStatusCode as int instead of string (#998)

* HttpStatusCode to use int

* foramt

* format

* Cops arrested me
This commit is contained in:
Cijo Thomas 2020-08-04 09:17:03 -07:00 committed by GitHub
parent bde64f2b6a
commit 80edb939e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 28 additions and 35 deletions

View File

@ -2,16 +2,20 @@
## Unreleased
* Introduced `RuntimeContext` API.
([#948](https://github.com/open-telemetry/opentelemetry-dotnet/pull/948)).
Link constructor changed to accept ActivityTagsCollection instead of
IDictionary<string, object> attributes. TelemetrySpan adds more overloads for
SetAttribute with value of type bool, int, double. (string already existed).
TelemetrySpan's SetAttribute behavior changed to match the
[spec](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/api.md#set-attributes).
Setting an attribute with an existing key now results in overwriting it.
Setting null value has no impact except if null is set to an existing key,
it gets removed.
* Introduced `RuntimeContext` API
([#948](https://github.com/open-telemetry/opentelemetry-dotnet/pull/948)).
* Link constructor changed to accept ActivityTagsCollection instead of
IDictionary<string, object> attributes.
* TelemetrySpan adds more overloads for SetAttribute with value of type bool,
int, double (string already existed).
* TelemetrySpan's SetAttribute behavior
changed to match the
[spec](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/api.md#set-attributes).
* Setting an attribute with an existing key now results in overwriting it.
* Setting null value has no impact except if null is set to an existing key, it
gets removed.
([#954](https://github.com/open-telemetry/opentelemetry-dotnet/pull/954)).
* HttpStatusCode in all spans attribute (http.status_code) to use int value.
## 0.4.0-beta.2

View File

@ -140,7 +140,7 @@ namespace OpenTelemetry.Instrumentation.AspNet.Implementation
var response = context.Response;
activityToEnrich.SetTag(SemanticConventions.AttributeHttpStatusCode, response.StatusCode.ToString());
activityToEnrich.SetTag(SemanticConventions.AttributeHttpStatusCode, response.StatusCode);
activityToEnrich.SetStatus(
SpanHelper

View File

@ -129,7 +129,7 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation
}
var response = context.Response;
activity.SetTag(SemanticConventions.AttributeHttpStatusCode, response.StatusCode.ToString());
activity.SetTag(SemanticConventions.AttributeHttpStatusCode, response.StatusCode);
Status status = SpanHelper.ResolveSpanStatusForHttpStatusCode(response.StatusCode);
activity.SetStatus(status.WithDescription(response.HttpContext.Features.Get<IHttpResponseFeature>()?.ReasonPhrase));

View File

@ -136,7 +136,7 @@ namespace OpenTelemetry.Instrumentation.Http.Implementation
if (this.stopResponseFetcher.Fetch(payload) is HttpResponseMessage response)
{
// response could be null for DNS issues, timeouts, etc...
activity.SetTag(SemanticConventions.AttributeHttpStatusCode, HttpTagHelper.GetStatusCodeTagValueFromHttpStatusCode(response.StatusCode));
activity.SetTag(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode);
activity.SetStatus(
SpanHelper

View File

@ -30,13 +30,11 @@ namespace OpenTelemetry.Instrumentation.Http.Implementation
private static readonly ConcurrentDictionary<HttpMethod, string> HttpMethodNameCache = new ConcurrentDictionary<HttpMethod, string>();
private static readonly ConcurrentDictionary<string, ConcurrentDictionary<int, string>> HostAndPortToStringCache = new ConcurrentDictionary<string, ConcurrentDictionary<int, string>>();
private static readonly ConcurrentDictionary<Version, string> ProtocolVersionToStringCache = new ConcurrentDictionary<Version, string>();
private static readonly ConcurrentDictionary<HttpStatusCode, string> StatusCodeToStringCache = new ConcurrentDictionary<HttpStatusCode, string>();
private static readonly Func<string, string> ConvertMethodToOperationNameRef = ConvertMethodToOperationName;
private static readonly Func<HttpMethod, string> ConvertHttpMethodToOperationNameRef = ConvertHttpMethodToOperationName;
private static readonly Func<HttpMethod, string> ConvertHttpMethodToNameRef = ConvertHttpMethodToName;
private static readonly Func<Version, string> ConvertProtocolVersionToStringRef = ConvertProtocolVersionToString;
private static readonly Func<HttpStatusCode, string> ConvertHttpStatusCodeToStringRef = ConvertHttpStatusCodeToString;
/// <summary>
/// Gets the OpenTelemetry standard name for an activity based on its Http method.
@ -66,13 +64,6 @@ namespace OpenTelemetry.Instrumentation.Http.Implementation
/// <returns>Span flavor value.</returns>
public static string GetFlavorTagValueFromProtocolVersion(Version protocolVersion) => ProtocolVersionToStringCache.GetOrAdd(protocolVersion, ConvertProtocolVersionToStringRef);
/// <summary>
/// Gets the OpenTelemetry standard status code tag value for a span based on its protocol <see cref="HttpStatusCode"/>.
/// </summary>
/// <param name="statusCode"><see cref="HttpStatusCode"/>.</param>
/// <returns>Span status code value.</returns>
public static string GetStatusCodeTagValueFromHttpStatusCode(HttpStatusCode statusCode) => StatusCodeToStringCache.GetOrAdd(statusCode, ConvertHttpStatusCodeToStringRef);
/// <summary>
/// Gets the OpenTelemetry standard host tag value for a span based on its request <see cref="Uri"/>.
/// </summary>
@ -110,8 +101,6 @@ namespace OpenTelemetry.Instrumentation.Http.Implementation
private static string ConvertHttpMethodToName(HttpMethod method) => method.ToString();
private static string ConvertHttpStatusCodeToString(HttpStatusCode statusCode) => ((int)statusCode).ToString();
private static string ConvertProtocolVersionToString(Version protocolVersion) => protocolVersion.ToString();
}
}

View File

@ -119,7 +119,7 @@ namespace OpenTelemetry.Instrumentation.Http.Implementation
if (activity.IsAllDataRequested)
{
activity.SetTag(SemanticConventions.AttributeHttpStatusCode, HttpTagHelper.GetStatusCodeTagValueFromHttpStatusCode(response.StatusCode));
activity.SetTag(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode);
activity.SetStatus(
SpanHelper
@ -143,7 +143,7 @@ namespace OpenTelemetry.Instrumentation.Http.Implementation
{
if (wexc.Response is HttpWebResponse response)
{
activity.SetTag(SemanticConventions.AttributeHttpStatusCode, HttpTagHelper.GetStatusCodeTagValueFromHttpStatusCode(response.StatusCode));
activity.SetTag(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode);
status = SpanHelper.ResolveSpanStatusForHttpStatusCode((int)response.StatusCode).WithDescription(response.StatusDescription);
}

View File

@ -202,8 +202,8 @@ namespace OpenTelemetry.Instrumentation.AspNet.Tests
Assert.True(span.Duration != TimeSpan.Zero);
Assert.Equal(
"200",
span.Tags.FirstOrDefault(i => i.Key == SemanticConventions.AttributeHttpStatusCode).Value);
200,
span.TagObjects.FirstOrDefault(i => i.Key == SemanticConventions.AttributeHttpStatusCode).Value);
Assert.Equal(
"Ok",

View File

@ -103,7 +103,7 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Tests
Assert.Equal("GET", span.Tags.FirstOrDefault(i => i.Key == SemanticConventions.AttributeHttpMethod).Value);
Assert.Equal(urlPath, span.Tags.FirstOrDefault(i => i.Key == SpanAttributeConstants.HttpPathKey).Value);
Assert.Equal($"http://localhost{urlPath}", span.Tags.FirstOrDefault(i => i.Key == SemanticConventions.AttributeHttpUrl).Value);
Assert.Equal(statusCode.ToString(), span.Tags.FirstOrDefault(i => i.Key == SemanticConventions.AttributeHttpStatusCode).Value);
Assert.Equal(statusCode, span.TagObjects.FirstOrDefault(i => i.Key == SemanticConventions.AttributeHttpStatusCode).Value);
Status status = SpanHelper.ResolveSpanStatusForHttpStatusCode(statusCode);
Assert.Equal(SpanHelper.GetCachedCanonicalCodeString(status.CanonicalCode), span.Tags.FirstOrDefault(i => i.Key == SpanAttributeConstants.StatusCodeKey).Value);

View File

@ -123,7 +123,7 @@ namespace OpenTelemetry.Instrumentation.Http.Tests
Assert.Equal(tc.SpanStatusHasDescription.Value, !string.IsNullOrEmpty(desc));
}
var normalizedAttributes = span.Tags.Where(kv => !kv.Key.StartsWith("ot")).ToImmutableSortedDictionary(x => x.Key, x => x.Value.ToString());
var normalizedAttributes = span.TagObjects.Where(kv => !kv.Key.StartsWith("ot")).ToImmutableSortedDictionary(x => x.Key, x => x.Value.ToString());
var normalizedAttributesTestCase = tc.SpanAttributes.ToDictionary(x => x.Key, x => HttpTestData.NormalizeValues(x.Value, host, port));
Assert.Equal(normalizedAttributesTestCase.Count, normalizedAttributes.Count);

View File

@ -188,7 +188,7 @@ namespace OpenTelemetry.Instrumentation.Http.Tests
HttpWebResponse response = (HttpWebResponse)stopEvent.Value.GetCustomProperty("HttpWebRequest.Response");
Assert.NotNull(response);
VerifyActivityStopTags("200", "OK", activity);
VerifyActivityStopTags(200, "OK", activity);
}
[Theory]
@ -404,7 +404,7 @@ namespace OpenTelemetry.Instrumentation.Http.Tests
HttpWebResponse response = (HttpWebResponse)stopEvent.Value.GetCustomProperty("HttpWebRequest.Response");
Assert.NotNull(response);
VerifyActivityStopTags("200", "OK", activity);
VerifyActivityStopTags(200, "OK", activity);
}
[Fact]
@ -523,7 +523,7 @@ namespace OpenTelemetry.Instrumentation.Http.Tests
HttpWebResponse stopResponse = (HttpWebResponse)stopEvent.Value.GetCustomProperty("HttpWebRequest.Response");
Assert.NotNull(stopResponse);
VerifyActivityStopTags("204", "No Content", activity);
VerifyActivityStopTags(204, "No Content", activity);
}
/// <summary>
@ -896,9 +896,9 @@ namespace OpenTelemetry.Instrumentation.Http.Tests
Assert.Equal(url, activity.Tags.FirstOrDefault(i => i.Key == SemanticConventions.AttributeHttpUrl).Value);
}
private static void VerifyActivityStopTags(string statusCode, string statusText, Activity activity)
private static void VerifyActivityStopTags(int statusCode, string statusText, Activity activity)
{
Assert.Equal(statusCode, activity.Tags.FirstOrDefault(i => i.Key == SemanticConventions.AttributeHttpStatusCode).Value);
Assert.Equal(statusCode, activity.TagObjects.FirstOrDefault(i => i.Key == SemanticConventions.AttributeHttpStatusCode).Value);
Assert.Equal(statusText, activity.Tags.FirstOrDefault(i => i.Key == SpanAttributeConstants.StatusDescriptionKey).Value);
}