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

Resource Attributes allow Primitive Arrays #1852

Merged
merged 13 commits into from
Mar 4, 2021
3 changes: 3 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ please check the latest changes
[self diagnostic configuration file](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry/README.md#troubleshooting).
([#1865](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1865))

* Resource Attributes now accept primitive arrays as values.
([#1852](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1852))

## 1.0.1

Released 2021-Feb-10
Expand Down
33 changes: 32 additions & 1 deletion src/OpenTelemetry/Resources/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ private static object SanitizeValue(object value, string keyName)
{
if (value != null)
{
if (value is string || value is bool || value is long || value is double)
if (value is string || value is bool || value is double || value is long)
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
{
return value;
}

if (value is string[] || value is bool[] || value is double[] || value is long[])
Austin-Tan marked this conversation as resolved.
Show resolved Hide resolved
{
return value;
}
Expand All @@ -124,6 +129,32 @@ private static object SanitizeValue(object value, string keyName)
return System.Convert.ToDouble(value, System.Globalization.CultureInfo.InvariantCulture);
}

if (value is int[] || value is short[])
{
long[] convertedArr = new long[((System.Array)value).Length];
int i = 0;
foreach (var val in (System.Array)value)
{
convertedArr[i] = System.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] = System.Convert.ToDouble(val, System.Globalization.CultureInfo.InvariantCulture);
i++;
}

return convertedArr;
}

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

Expand Down
57 changes: 55 additions & 2 deletions test/OpenTelemetry.Tests/Resources/ResourceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ public void CreateResource_EmptyAttributeValue()
Assert.Contains(new KeyValuePair<string, object>("EmptyValue", string.Empty), resource.Attributes);
}

[Fact]
public void CreateResource_EmptyArray()
{
// Arrange
var attributes = new Dictionary<string, object> { { "EmptyArray", new string[0] } };

// does not throw
var resource = new Resource(attributes);

// Assert
Assert.Single(resource.Attributes);
Assert.Equal(new string[0], resource.Attributes.Where(x => x.Key == "EmptyArray").FirstOrDefault().Value);
}

[Fact]
public void CreateResource_EmptyAttribute()
{
Expand Down Expand Up @@ -126,33 +140,72 @@ public void CreateResource_MultipleAttribute()
[Fact]
public void CreateResource_SupportedAttributeTypes()
{
// Arrange
var attributes = new Dictionary<string, object>
{
{ "string", "stringValue" },
{ "long", 1L },
{ "bool", true },
{ "double", 0.1d },
{ "long", 1L },

// int and float supported by conversion to long and double
{ "int", 1 },
Austin-Tan marked this conversation as resolved.
Show resolved Hide resolved
{ "short", (short)1 },
{ "float", 0.1f },
};

// Act
var resource = new Resource(attributes);

// Assert
Assert.Equal(7, resource.Attributes.Count());
Assert.Contains(new KeyValuePair<string, object>("string", "stringValue"), resource.Attributes);
Assert.Contains(new KeyValuePair<string, object>("long", 1L), resource.Attributes);
Assert.Contains(new KeyValuePair<string, object>("bool", true), resource.Attributes);
Assert.Contains(new KeyValuePair<string, object>("double", 0.1d), resource.Attributes);
Assert.Contains(new KeyValuePair<string, object>("long", 1L), resource.Attributes);

Assert.Contains(new KeyValuePair<string, object>("int", 1L), resource.Attributes);
Assert.Contains(new KeyValuePair<string, object>("short", 1L), resource.Attributes);

double convertedFloat = Convert.ToDouble(0.1f, System.Globalization.CultureInfo.InvariantCulture);
Assert.Contains(new KeyValuePair<string, object>("float", convertedFloat), resource.Attributes);
}

[Fact]
public void CreateResource_SupportedAttributeArrayTypes()
{
// Arrange
var attributes = new Dictionary<string, object>
{
// natively supported array types
{ "string arr", new string[] { "stringValue" } },
{ "bool arr", new bool[] { true } },
{ "double arr", new double[] { 0.1d } },
{ "long arr", new long[] { 1L } },

// have to convert to other primitive array types
{ "int arr", new int[] { 1 } },
{ "short arr", new short[] { (short)1 } },
{ "float arr", new float[] { 0.1f } },
};

// Act
var resource = new Resource(attributes);

// Assert
Assert.Equal(7, resource.Attributes.Count());
Assert.Equal(new string[] { "stringValue" }, resource.Attributes.Where(x => x.Key == "string arr").FirstOrDefault().Value);
Assert.Equal(new bool[] { true }, resource.Attributes.Where(x => x.Key == "bool arr").FirstOrDefault().Value);
Assert.Equal(new double[] { 0.1d }, resource.Attributes.Where(x => x.Key == "double arr").FirstOrDefault().Value);
Assert.Equal(new long[] { 1L }, resource.Attributes.Where(x => x.Key == "long arr").FirstOrDefault().Value);

var longArr = new long[] { 1 };
var doubleArr = new double[] { Convert.ToDouble(0.1f, System.Globalization.CultureInfo.InvariantCulture) };
Assert.Equal(longArr, resource.Attributes.Where(x => x.Key == "int arr").FirstOrDefault().Value);
Assert.Equal(longArr, resource.Attributes.Where(x => x.Key == "short arr").FirstOrDefault().Value);
Assert.Equal(doubleArr, resource.Attributes.Where(x => x.Key == "float arr").FirstOrDefault().Value);
}

[Fact]
public void CreateResource_NotSupportedAttributeTypes()
{
Expand Down