Skip to content

Commit

Permalink
refactor Resource::SanitizeValue method to be more readable (#2761)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiapanahi authored Jan 11, 2022
1 parent 66ad39d commit 4bf8ed4
Showing 1 changed file with 19 additions and 47 deletions.
66 changes: 19 additions & 47 deletions src/OpenTelemetry/Resources/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using OpenTelemetry.Internal;

Expand Down Expand Up @@ -102,61 +103,32 @@ 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;
}

if (value is int || value is short)
{
return Convert.ToInt64(value);
}

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

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

return convertedArr;
}

if (value is float[])
{
double[] convertedArr = new double[((float[])value).Length];
int i = 0;
foreach (float val in (float[])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);
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, CultureInfo.InvariantCulture),
int[] v => Array.ConvertAll(v, Convert.ToInt64),
short[] v => Array.ConvertAll(v, Convert.ToInt64),
float[] v => Array.ConvertAll(v, f => Convert.ToDouble(f, CultureInfo.InvariantCulture)),
_ => throw new ArgumentException("Attribute value type is not an accepted primitive", keyName),
};
}
}
}

0 comments on commit 4bf8ed4

Please sign in to comment.