Skip to content

Commit

Permalink
Set attribute (#240)
Browse files Browse the repository at this point in the history
* Set attribute support

* Do not allow empty attribute key

---------

Co-authored-by: Konrad Dysput <[email protected]>
  • Loading branch information
konraddysput and Konrad Dysput authored Jan 24, 2025
1 parent db6e086 commit e1dd858
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
29 changes: 23 additions & 6 deletions Runtime/BacktraceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,7 @@ public string this[string index]
}
set
{
AttributeProvider[index] = value;
if (_nativeClient != null)
{
_nativeClient.SetAttribute(index, value);
}
SetAttribute(index, value);
}
}

Expand All @@ -170,6 +166,27 @@ public IEnumerable<string> GetAttachments()
return _clientReportAttachments;
}

/// <summary>
/// Set a client attribute that will be included in every report
/// </summary>
/// <param name="key">Attribute name</param>
/// <param name="value">Attribute value</param>
/// <returns>True, if the value was added. Otherwise false.</returns>
public bool SetAttribute(string key, string value)
{
if (string.IsNullOrEmpty(key))
{
return false;
}

AttributeProvider[key] = value;
if (_nativeClient != null)
{
_nativeClient.SetAttribute(key, value);
}
return true;
}

/// <summary>
/// Set client attributes that will be included in every report
/// </summary>
Expand All @@ -182,7 +199,7 @@ public void SetAttributes(Dictionary<string, string> attributes)
}
foreach (var attribute in attributes)
{
this[attribute.Key] = attribute.Value;
SetAttribute(attribute.Key, attribute.Value);
}
}

Expand Down
35 changes: 35 additions & 0 deletions Tests/Runtime/BacktraceAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,41 @@ public IEnumerator TesClientAttributes_ReportShouldntExtendClientAttributes_Clie
yield return null;
}

[UnityTest]
public IEnumerator TesClientAttributeMethod_BacktraceDataShouldIncludeClientAttribute_ClientAttributeAreAvailableInDiagnosticData()
{
var key = "attribute-key";
var value = "attribute-value";
BacktraceClient.SetAttribute(key, value);

BacktraceData data = null;
BacktraceClient.BeforeSend = (BacktraceData reportData) =>
{
data = reportData;
return null;
};
BacktraceClient.Send(new Exception("foo"));
yield return WaitForFrame.Wait();

Assert.IsNotNull(data);
Assert.AreEqual(data.Attributes.Attributes[key], value);
yield return null;
}

[UnityTest]
public IEnumerator TesClientAttributeMethod_ShouldNotAcceptNullableKey_AttributeIsNotAvailable()
{
Assert.IsFalse(BacktraceClient.SetAttribute(null, "attribute-value"));
yield return null;
}

[UnityTest]
public IEnumerator TesClientAttributeMethod_ShouldNotAcceptEmptyStringKey_AttributeIsNotAvailable()
{
Assert.IsFalse(BacktraceClient.SetAttribute(string.Empty, "attribute-value"));
yield return null;
}


[UnityTest]
public IEnumerator TesClientAttributesMethod_BacktraceDataShouldIncludeClientAttributes_ClientAttributesAreAvailableInDiagnosticData()
Expand Down

0 comments on commit e1dd858

Please sign in to comment.