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

refactor Resource::SanitizeValue method to be more readable #2761

Merged
merged 5 commits into from
Jan 11, 2022
Merged
Changes from 1 commit
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
67 changes: 40 additions & 27 deletions src/OpenTelemetry/Resources/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,61 +102,74 @@ private static KeyValuePair<string, object> SanitizeAttribute(KeyValuePair<strin
sanitizedKey = attribute.Key;
}

object sanitizedValue = SanitizeValue(attribute.Value, sanitizedKey);
var sanitizedValue = SanitizeValue(attribute.Value, sanitizedKey);
return new KeyValuePair<string, object>(sanitizedKey, sanitizedValue);
}

private static object SanitizeValue(object value, string keyName)
{
Guard.Null(keyName, nameof(keyName));

if (value is string || value is bool || value is double || value is long)
return value switch
{
return value;
}

if (value is string[] || value is bool[] || value is double[] || value is long[])
{
return value;
}
string => value,
bool => value,
double => value,
long => value,
string[] => value,
bool[] => value,
double[] => value,
long[] => value,
int => Convert.ToInt64(value),
short => Convert.ToInt64(value),
float => Convert.ToDouble(value, System.Globalization.CultureInfo.InvariantCulture),
int[] v => SanitizationUtils.ConvertoToInt64Array(v),
short[] v => SanitizationUtils.ConvertoToInt64Array(v),
float[] v => SanitizationUtils.ConvertToDoubleArray(v),
_ => throw new ArgumentException("Attribute value type is not an accepted primitive", keyName),
};
}

if (value is int || value is short)
private static class SanitizationUtils
{
public static long[] ConvertoToInt64Array(short[] value)
{
return Convert.ToInt64(value);
}
var result = new long[value.Length];
var i = 0;
foreach (var val in value)
{
result[i] = Convert.ToInt64(val);
i++;
}

if (value is float)
{
return Convert.ToDouble(value, System.Globalization.CultureInfo.InvariantCulture);
return result;
}

if (value is int[] || value is short[])
public static long[] ConvertoToInt64Array(int[] value)
{
long[] convertedArr = new long[((Array)value).Length];
int i = 0;
foreach (var val in (Array)value)
var result = new long[value.Length];
var i = 0;
foreach (var val in value)
{
convertedArr[i] = Convert.ToInt64(val);
result[i] = Convert.ToInt64(val);
i++;
}

return convertedArr;
return result;
}

if (value is float[])
public static double[] ConvertToDoubleArray(float[] value)
{
double[] convertedArr = new double[((float[])value).Length];
int i = 0;
foreach (float val in (float[])value)
var convertedArr = new double[value.Length];
var i = 0;
foreach (var val in value)
{
convertedArr[i] = Convert.ToDouble(val, System.Globalization.CultureInfo.InvariantCulture);
i++;
}

return convertedArr;
}

throw new ArgumentException("Attribute value type is not an accepted primitive", keyName);
}
}
}