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 fix #1647

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d806644
Change to Resource IsValidValue check
Austin-Tan Dec 8, 2020
521f707
Removing a text that is deprecated with this change
Austin-Tan Dec 8, 2020
be509ac
Changelog addition
Austin-Tan Dec 8, 2020
8650fe5
Int to long, float to double
Austin-Tan Dec 8, 2020
6ee0ba0
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
Austin-Tan Dec 8, 2020
b31a64e
Expected Dict size changed
Austin-Tan Dec 8, 2020
69101e9
Resolve CHANGELOG nit
Austin-Tan Dec 9, 2020
928d302
Enforce InvariantCulture for float to double convert
Austin-Tan Dec 9, 2020
5f3401a
UT updated for new supported types
Austin-Tan Dec 9, 2020
1857d9f
Throw exception instead of logging invalid value as empty string
Austin-Tan Dec 9, 2020
49ad77c
Updating tests for new exception
Austin-Tan Dec 9, 2020
60edf24
Supporting short type as well
Austin-Tan Dec 9, 2020
56c5099
Remove Resource tag population
Austin-Tan Dec 17, 2020
b4003f7
Changelog updated
Austin-Tan Dec 17, 2020
d658f7a
Removing test case involving Resource
Austin-Tan Dec 22, 2020
a689896
removing local dictionary of tags, unused
Austin-Tan Jan 5, 2021
bd78a6c
Making @CodeBlanch 's test changes thank you!
Austin-Tan Jan 5, 2021
f462632
Merge branch 'master' into ZipkinRemoveResourceTags
cijothomas Jan 6, 2021
456f2c3
Merge branch 'ZipkinRemoveResourceTags' of https://github.com/Austin-…
Austin-Tan Jan 20, 2021
4a5d70d
Correcting changelog to include exception
Austin-Tan Jan 20, 2021
c6fe949
Adding specificity to exceptions
Austin-Tan Jan 20, 2021
28aa176
Re-adding lost changes
Austin-Tan Jan 22, 2021
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
2 changes: 2 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* Resource Attributes fixed to allow the values of key-value pairs to be of any
object type, matching the behavior of Activity's SetTag method.
Austin-Tan marked this conversation as resolved.
Show resolved Hide resolved
* The following extension methods on `ResourceBuilder` has been moved from the
`OpenTelemetry` namespace to the `OpenTelemetry.Resources` namespace:
`AddEnvironmentVariableDetector`, `AddAttributes`, `AddService`, and
Expand Down
33 changes: 19 additions & 14 deletions src/OpenTelemetry/Resources/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,28 +101,33 @@ private static KeyValuePair<string, object> SanitizeAttribute(KeyValuePair<strin
sanitizedKey = attribute.Key;
}

object sanitizedValue;
if (!IsValidValue(attribute.Value))
{
OpenTelemetrySdkEventSource.Log.InvalidArgument("Create resource", "attribute value", "Attribute value should be a non-null string, long, bool or double.");
sanitizedValue = string.Empty;
}
else
{
sanitizedValue = attribute.Value;
}
object sanitizedValue = SanitizeValue(attribute.Value);

return new KeyValuePair<string, object>(sanitizedKey, sanitizedValue);
}

private static bool IsValidValue(object value)
private static object SanitizeValue(object value)
{
if (value != null && (value is string || value is bool || value is long || value is double))
if (value != null)
{
return true;
if (value is string || value is bool || value is long || value is double)
{
return value;
}

if (value is int)
Austin-Tan marked this conversation as resolved.
Show resolved Hide resolved
{
return System.Convert.ToInt64(value);
}

if (value is float)
{
return System.Convert.ToDouble(value);
eddynaka marked this conversation as resolved.
Show resolved Hide resolved
}
}

return false;
OpenTelemetrySdkEventSource.Log.InvalidArgument("Create resource", "attribute value", "Attribute value should be a non-null primitive or homogeneous array of primitives.");
return string.Empty;
Austin-Tan marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
2 changes: 0 additions & 2 deletions test/OpenTelemetry.Tests/Resources/ResourceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ public void CreateResource_NotSupportedAttributeTypes()
{ "dynamic", new { } },
{ "array", new int[1] },
{ "complex", this },
{ "float", 0.1f },
Austin-Tan marked this conversation as resolved.
Show resolved Hide resolved
Austin-Tan marked this conversation as resolved.
Show resolved Hide resolved
};

var resource = new Resource(attributes);
Expand All @@ -167,7 +166,6 @@ public void CreateResource_NotSupportedAttributeTypes()
Assert.Contains(new KeyValuePair<string, object>("dynamic", string.Empty), resource.Attributes);
Assert.Contains(new KeyValuePair<string, object>("array", string.Empty), resource.Attributes);
Assert.Contains(new KeyValuePair<string, object>("complex", string.Empty), resource.Attributes);
Assert.Contains(new KeyValuePair<string, object>("float", string.Empty), resource.Attributes);
}

[Fact]
Expand Down