Skip to content

Commit

Permalink
fix nullables in AzMonList (Azure#35082)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMothra authored Mar 23, 2023
1 parent 3d1a464 commit 14896e4
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 207 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable disable // TODO: remove and fix errors

using System;
using System.Buffers;
using System.Collections;
Expand All @@ -14,45 +12,40 @@ namespace Azure.Monitor.OpenTelemetry.Exporter.Internals
{
private static int s_allocationSize = 8;

private readonly KeyValuePair<string, object>[] data;
private readonly KeyValuePair<string, object?>[] data;

private AzMonList(KeyValuePair<string, object>[] data, int length)
private AzMonList(KeyValuePair<string, object?>[] data, int length)
{
this.data = data;
this.Length = length;
}

public int Length { get; }

public ref KeyValuePair<string, object> this[int index]
public ref KeyValuePair<string, object?> this[int index]
{
get => ref this.data[index];
}

public static AzMonList Initialize()
{
return new AzMonList(ArrayPool<KeyValuePair<string, object>>.Shared.Rent(s_allocationSize), 0);
return new AzMonList(ArrayPool<KeyValuePair<string, object?>>.Shared.Rent(s_allocationSize), 0);
}

public static void Add(ref AzMonList list, KeyValuePair<string, object> keyValuePair)
public static void Add(ref AzMonList list, KeyValuePair<string, object?> keyValuePair)
{
var data = list.data;

if (data == null)
{
throw new InvalidOperationException("AzMonList instance is not initialized.");
}
var data = list.data ?? throw new InvalidOperationException("AzMonList instance is not initialized.");

if (list.Length >= data.Length)
{
s_allocationSize = data.Length * 2;
var previousData = data;

data = ArrayPool<KeyValuePair<string, object>>.Shared.Rent(s_allocationSize);
data = ArrayPool<KeyValuePair<string, object?>>.Shared.Rent(s_allocationSize);

var span = previousData.AsSpan();
span.CopyTo(data);
ArrayPool<KeyValuePair<string, object>>.Shared.Return(previousData);
ArrayPool<KeyValuePair<string, object?>>.Shared.Return(previousData);
}

data[list.Length] = keyValuePair;
Expand All @@ -64,7 +57,7 @@ public static void Clear(ref AzMonList list)
list = new AzMonList(list.data, 0);
}

public static object GetTagValue(ref AzMonList list, string tagName)
public static object? GetTagValue(ref AzMonList list, string tagName)
{
int length = list.Length;

Expand All @@ -79,11 +72,11 @@ public static object GetTagValue(ref AzMonList list, string tagName)
return null;
}

internal static object[] GetTagValues(ref AzMonList list, params string[] tagNames)
internal static object?[] GetTagValues(ref AzMonList list, params string[] tagNames)
{
int lengthTagNames = tagNames.Length;
int lengthList = list.Length;
object[] values = new object[lengthTagNames];
object?[] values = new object[lengthTagNames];

for (int i = 0; i < lengthList; i++)
{
Expand All @@ -108,7 +101,7 @@ public void Return()
var data = this.data;
if (data != null)
{
ArrayPool<KeyValuePair<string, object>>.Shared.Return(data);
ArrayPool<KeyValuePair<string, object?>>.Shared.Return(data);
}
}

Expand All @@ -119,10 +112,10 @@ IEnumerator IEnumerable.GetEnumerator()

public struct Enumerator : IEnumerator
{
private readonly KeyValuePair<string, object>[] data;
private readonly KeyValuePair<string, object?>[] data;
private readonly int length;
private int index;
private KeyValuePair<string, object> current;
private KeyValuePair<string, object?> current;

public Enumerator(in AzMonList list)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable disable // TODO: remove and fix errors

using System;
using System.Runtime.CompilerServices;
using Azure.Monitor.OpenTelemetry.Exporter.Models;
Expand All @@ -14,15 +12,15 @@ internal static class AzMonListExtensions
///<summary>
/// Gets http request url from activity tag objects.
///</summary>
internal static string GetRequestUrl(this AzMonList tagObjects)
internal static string? GetRequestUrl(this AzMonList tagObjects)
{
// From spec: one of the following combinations is required in case of server spans:
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-server-semantic-conventions
// http.url
// http.scheme, http.host, http.target
// http.scheme, http.server_name, net.host.port, http.target
// http.scheme, net.host.name, net.host.port, http.target
string url = null;
string? url = null;
url = tagObjects.GetUrlUsingHttpUrl();
if (url != null)
{
Expand All @@ -41,14 +39,14 @@ internal static string GetRequestUrl(this AzMonList tagObjects)
}

string defaultPort = GetDefaultHttpPort(httpScheme);
url = tagObjects.GetUrlUsingHttpHost(httpScheme, defaultPort, httpTarget);
url = tagObjects.GetUrlUsingHttpHost(httpScheme!, defaultPort, httpTarget!);
if (url != null)
{
return url;
}

var httpServerName = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeHttpServerName)?.ToString();
string host;
string? host;
if (!string.IsNullOrWhiteSpace(httpServerName))
{
host = httpServerName;
Expand All @@ -62,7 +60,7 @@ internal static string GetRequestUrl(this AzMonList tagObjects)
var netHostPort = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeNetHostPort)?.ToString();
if (!string.IsNullOrWhiteSpace(netHostPort))
{
url = tagObjects.GetUrlUsingHostAndPort(httpScheme, host, netHostPort, defaultPort, httpTarget);
url = tagObjects.GetUrlUsingHostAndPort(httpScheme!, host!, netHostPort!, defaultPort, httpTarget!);
return url;
}
}
Expand All @@ -74,15 +72,15 @@ internal static string GetRequestUrl(this AzMonList tagObjects)
///<summary>
/// Gets http dependency url from activity tag objects.
///</summary>
internal static string GetDependencyUrl(this AzMonList tagObjects)
internal static string? GetDependencyUrl(this AzMonList tagObjects)
{
// From spec: one of the following combinations is required in case of client spans:
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-client
// http.url
// http.scheme, http.host, http.target
// http.scheme, net.peer.name, net.peer.port, http.target
// http.scheme, net.peer.ip, net.peer.port, http.target
string url = null;
string? url = null;
url = tagObjects.GetUrlUsingHttpUrl();
if (url != null)
{
Expand All @@ -101,7 +99,7 @@ internal static string GetDependencyUrl(this AzMonList tagObjects)
}

string defaultPort = GetDefaultHttpPort(httpScheme);
url = tagObjects.GetUrlUsingHttpHost(httpScheme, defaultPort, httpTarget);
url = tagObjects.GetUrlUsingHttpHost(httpScheme!, defaultPort, httpTarget!);
if (url != null)
{
return url;
Expand All @@ -113,7 +111,7 @@ internal static string GetDependencyUrl(this AzMonList tagObjects)
var netPeerPort = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeNetPeerPort)?.ToString();
if (!string.IsNullOrWhiteSpace(netPeerPort))
{
url = tagObjects.GetUrlUsingHostAndPort(httpScheme, host, netPeerPort, defaultPort, httpTarget);
url = tagObjects.GetUrlUsingHostAndPort(httpScheme!, host!, netPeerPort!, defaultPort, httpTarget!);
return url;
}
}
Expand All @@ -123,7 +121,7 @@ internal static string GetDependencyUrl(this AzMonList tagObjects)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static string GetDefaultHttpPort(string httpScheme)
internal static string GetDefaultHttpPort(string? httpScheme)
{
if (string.Equals(httpScheme, "http", StringComparison.OrdinalIgnoreCase))
{
Expand All @@ -140,9 +138,13 @@ internal static string GetDefaultHttpPort(string httpScheme)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static string GetDefaultDbPort(string dbSystem)
internal static string GetDefaultDbPort(string? dbSystem)
{
if (string.Equals(dbSystem, "mssql", StringComparison.OrdinalIgnoreCase))
if (dbSystem == null)
{
return "0";
}
else if (string.Equals(dbSystem, "mssql", StringComparison.OrdinalIgnoreCase))
{
return "1433";
}
Expand All @@ -157,9 +159,9 @@ internal static string GetDefaultDbPort(string dbSystem)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static string GetUrlUsingHttpUrl(this AzMonList tagObjects)
internal static string? GetUrlUsingHttpUrl(this AzMonList tagObjects)
{
string url = null;
string? url = null;
var httpUrl = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeHttpUrl)?.ToString();
if (!string.IsNullOrWhiteSpace(httpUrl))
{
Expand All @@ -169,14 +171,14 @@ internal static string GetUrlUsingHttpUrl(this AzMonList tagObjects)
return url;
}

internal static string GetUrlUsingHttpHost(this AzMonList tagObjects, string httpScheme, string defaultPort, string httpTarget)
internal static string? GetUrlUsingHttpHost(this AzMonList tagObjects, string httpScheme, string defaultPort, string httpTarget)
{
string url = null;
string? url = null;
var httpHost = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeHttpHost)?.ToString();
if (!string.IsNullOrWhiteSpace(httpHost))
{
string portSection = $":{defaultPort}";
if (httpHost.EndsWith(portSection, StringComparison.OrdinalIgnoreCase))
if (httpHost!.EndsWith(portSection, StringComparison.OrdinalIgnoreCase))
{
var truncatedHost = httpHost.Substring(0, httpHost.IndexOf(portSection, StringComparison.OrdinalIgnoreCase));
url = $"{httpScheme}://{truncatedHost}{httpTarget}";
Expand All @@ -190,9 +192,9 @@ internal static string GetUrlUsingHttpHost(this AzMonList tagObjects, string htt
return url;
}

internal static string GetUrlUsingHostAndPort(this AzMonList tagObjects, string httpScheme, string host, string port, string defaultPort, string httpTarget)
internal static string? GetUrlUsingHostAndPort(this AzMonList tagObjects, string httpScheme, string host, string port, string defaultPort, string httpTarget)
{
string url = null;
string? url = null;
if (port == defaultPort)
{
url = $"{httpScheme}://{host}{httpTarget}";
Expand All @@ -205,10 +207,10 @@ internal static string GetUrlUsingHostAndPort(this AzMonList tagObjects, string
return url;
}

internal static string GetHostUsingNetPeerAttributes(this AzMonList tagObjects)
internal static string? GetHostUsingNetPeerAttributes(this AzMonList tagObjects)
{
var netPeerName = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeNetPeerName)?.ToString();
string host;
string? host;
if (!string.IsNullOrWhiteSpace(netPeerName))
{
host = netPeerName;
Expand All @@ -222,9 +224,9 @@ internal static string GetHostUsingNetPeerAttributes(this AzMonList tagObjects)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static string GetTargetUsingNetPeerAttributes(this AzMonList tagObjects, string defaultPort)
internal static string? GetTargetUsingNetPeerAttributes(this AzMonList tagObjects, string defaultPort)
{
string target = tagObjects.GetHostUsingNetPeerAttributes();
string? target = tagObjects.GetHostUsingNetPeerAttributes();
if (!string.IsNullOrWhiteSpace(target))
{
var netPeerPort = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeNetPeerPort)?.ToString();
Expand All @@ -240,9 +242,9 @@ internal static string GetTargetUsingNetPeerAttributes(this AzMonList tagObjects
///<summary>
/// Gets Http dependency target from activity tag objects.
///</summary>
internal static string GetHttpDependencyTarget(this AzMonList tagObjects)
internal static string? GetHttpDependencyTarget(this AzMonList tagObjects)
{
string target;
string? target;
string defaultPort = GetDefaultHttpPort(AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeHttpScheme)?.ToString());
var peerService = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributePeerService)?.ToString();
if (!string.IsNullOrWhiteSpace(peerService))
Expand All @@ -255,7 +257,7 @@ internal static string GetHttpDependencyTarget(this AzMonList tagObjects)
if (!string.IsNullOrWhiteSpace(httpHost))
{
string portSection = $":{defaultPort}";
if (httpHost.EndsWith(portSection, StringComparison.OrdinalIgnoreCase))
if (httpHost!.EndsWith(portSection, StringComparison.OrdinalIgnoreCase))
{
var truncatedHost = httpHost.Substring(0, httpHost.IndexOf(portSection, StringComparison.OrdinalIgnoreCase));
target = truncatedHost;
Expand All @@ -268,7 +270,9 @@ internal static string GetHttpDependencyTarget(this AzMonList tagObjects)
}

var httpUrl = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeHttpUrl)?.ToString();
if (!string.IsNullOrWhiteSpace(httpUrl) && Uri.TryCreate(httpUrl.ToString(), UriKind.RelativeOrAbsolute, out var uri) && uri.IsAbsoluteUri)
if (!string.IsNullOrWhiteSpace(httpUrl)
&& Uri.TryCreate(httpUrl!.ToString(), UriKind.RelativeOrAbsolute, out var uri)
&& uri.IsAbsoluteUri)
{
target = uri.Authority;
if (!string.IsNullOrWhiteSpace(target))
Expand All @@ -285,9 +289,9 @@ internal static string GetHttpDependencyTarget(this AzMonList tagObjects)
///<summary>
/// Gets Database dependency target from activity tag objects.
///</summary>
internal static string GetDbDependencyTarget(this AzMonList tagObjects)
internal static string? GetDbDependencyTarget(this AzMonList tagObjects)
{
string target = null;
string? target = null;
string defaultPort = GetDefaultDbPort(AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeDbSystem)?.ToString());
var peerService = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributePeerService)?.ToString();
if (!string.IsNullOrWhiteSpace(peerService))
Expand All @@ -299,7 +303,7 @@ internal static string GetDbDependencyTarget(this AzMonList tagObjects)
target = tagObjects.GetTargetUsingNetPeerAttributes(defaultPort);
}

string dbName = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeDbName)?.ToString();
string? dbName = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeDbName)?.ToString();
bool isTargetEmpty = string.IsNullOrWhiteSpace(target);
bool isDbNameEmpty = string.IsNullOrWhiteSpace(dbName);
if (!isTargetEmpty && !isDbNameEmpty)
Expand All @@ -321,7 +325,7 @@ internal static string GetDbDependencyTarget(this AzMonList tagObjects)
///<summary>
/// Gets dependency target from activity tag objects.
///</summary>
internal static string GetDependencyTarget(this AzMonList tagObjects, OperationType type)
internal static string? GetDependencyTarget(this AzMonList tagObjects, OperationType type)
{
switch (type)
{
Expand All @@ -334,7 +338,7 @@ internal static string GetDependencyTarget(this AzMonList tagObjects, OperationT
}
}

internal static string GetHttpDependencyName(this AzMonList tagObjects, string httpUrl)
internal static string? GetHttpDependencyName(this AzMonList tagObjects, string? httpUrl)
{
if (string.IsNullOrWhiteSpace(httpUrl))
{
Expand All @@ -344,7 +348,7 @@ internal static string GetHttpDependencyName(this AzMonList tagObjects, string h
var httpMethod = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeHttpMethod)?.ToString();
if (!string.IsNullOrWhiteSpace(httpMethod))
{
if (Uri.TryCreate(httpUrl.ToString(), UriKind.RelativeOrAbsolute, out var uri) && uri.IsAbsoluteUri)
if (Uri.TryCreate(httpUrl!.ToString(), UriKind.RelativeOrAbsolute, out var uri) && uri.IsAbsoluteUri)
{
return $"{httpMethod} {uri.AbsolutePath}";
}
Expand All @@ -353,7 +357,7 @@ internal static string GetHttpDependencyName(this AzMonList tagObjects, string h
return null;
}

internal static string GetDependencyType(this AzMonList tagObjects, OperationType operationType)
internal static string? GetDependencyType(this AzMonList tagObjects, OperationType operationType)
{
switch (operationType)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void ForEach(IEnumerable<KeyValuePair<string, object?>> activityTags)

if (activityTag.Value is Array array)
{
AzMonList.Add(ref UnMappedTags, new KeyValuePair<string, object>(activityTag.Key, array.ToCommaDelimitedString()));
AzMonList.Add(ref UnMappedTags, new KeyValuePair<string, object?>(activityTag.Key, array.ToCommaDelimitedString()));
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ internal static void AddActivityLinksToProperties(Activity activity, ref AzMonLi

linksJson.Append(']');

AzMonList.Add(ref UnMappedTags, new KeyValuePair<string, object>(msLinks, linksJson.ToString()));
AzMonList.Add(ref UnMappedTags, new KeyValuePair<string, object?>(msLinks, linksJson.ToString()));
}
}

Expand Down
Loading

0 comments on commit 14896e4

Please sign in to comment.