Resource Attributes allow Primitive Arrays (#1852)

* Primitive arrays allowed in Resource Attributes + Tests

* Changelog and spacing for build error

* Fix Empty Value Test

* fixing comprehensive supported types test

* Remove check for null key

* Moving one test case to separate test method

* add support for converted array types

* Remove null on key check

* Changelog merged fix

* Adjusting test
This commit is contained in:
Austin Tan 2021-03-04 11:31:15 -08:00 committed by GitHub
parent 7da935e237
commit a071d4d1d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 3 deletions

View File

@ -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

View File

@ -109,7 +109,12 @@ namespace OpenTelemetry.Resources
{
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)
{
return value;
}
if (value is string[] || value is bool[] || value is double[] || value is long[])
{
return value;
}
@ -124,6 +129,32 @@ namespace OpenTelemetry.Resources
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);
}

View File

@ -81,6 +81,20 @@ namespace OpenTelemetry.Resources.Tests
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()
{
@ -126,12 +140,13 @@ namespace OpenTelemetry.Resources.Tests
[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 },
@ -139,13 +154,16 @@ namespace OpenTelemetry.Resources.Tests
{ "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);
@ -153,6 +171,41 @@ namespace OpenTelemetry.Resources.Tests
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()
{