Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into reyang/exception
Browse files Browse the repository at this point in the history
cijothomas authored Mar 4, 2021
2 parents b54d0ba + a071d4d commit 1332c8d
Showing 3 changed files with 90 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -26,6 +26,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
33 changes: 32 additions & 1 deletion src/OpenTelemetry/Resources/Resource.cs
Original file line number Diff line number Diff line change
@@ -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)
{
return value;
}

if (value is string[] || value is bool[] || value is double[] || value is long[])
{
return value;
}
@@ -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);
}

57 changes: 55 additions & 2 deletions test/OpenTelemetry.Tests/Resources/ResourceTest.cs
Original file line number Diff line number Diff line change
@@ -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()
{
@@ -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 },
{ "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()
{

0 comments on commit 1332c8d

Please sign in to comment.