Skip to content

feat: add array support for custom attributes in .NET Agent API#3456

Merged
tippmar-nr merged 14 commits into
mainfrom
feature/json-serializer-array-support
Feb 24, 2026
Merged

feat: add array support for custom attributes in .NET Agent API#3456
tippmar-nr merged 14 commits into
mainfrom
feature/json-serializer-array-support

Conversation

@tippmar-nr

@tippmar-nr tippmar-nr commented Feb 19, 2026

Copy link
Copy Markdown
Member

Summary

This PR adds comprehensive array support for custom attributes in the New Relic .NET Agent API, allowing developers to pass array values to AddCustomAttribute() that are properly serialized as JSON arrays.

Changes Made

Core Implementation

  • Enhanced JSON serialization in JsonSerializerHelpers.cs:
    • Added support for IEnumerable types (arrays, lists, etc.)
    • Proper null filtering - null elements are excluded from serialized arrays
    • Empty arrays and null-only arrays are treated as null (not serialized)
    • Maintains existing conversion logic for individual array elements
    • Refactored to use single WriteValue method for all value types

Comprehensive Testing

Unit Tests (Core.UnitTest)

  • Added JsonSerializerHelpersTests covering all array scenarios
  • Tests for string arrays, int arrays, bool arrays, and Lists
  • Validates null filtering and empty array handling
  • Ensures mixed null arrays properly exclude null elements

Integration Tests

  • Complete end-to-end testing through New Relic .NET Agent API
  • Dual platform support: Both .NET Framework (WebAPI) and .NET Core applications
  • Test applications with array attribute endpoints:
    • CustomArrayAttributes - Tests string, int, bool arrays
    • CustomEmptyArrayAttributes - Tests empty/null-only array handling
    • CustomArrayWithNulls - Tests null filtering within arrays
  • Comprehensive validation: Arrays appear correctly in both transaction traces and transaction events
  • Reliable test execution: Uses transaction events for multi-endpoint test scenarios

API Usage

Developers can now use array values with the New Relic API:

// All of these now work correctly
NewRelic.Api.Agent.NewRelic.GetAgent().CurrentTransaction.AddCustomAttribute("colors", new[] { "red", "green", "blue" });
NewRelic.Api.Agent.NewRelic.GetAgent().CurrentTransaction.AddCustomAttribute("numbers", new[] { 1, 2, 3, 4, 5 });  
NewRelic.Api.Agent.NewRelic.GetAgent().CurrentTransaction.AddCustomAttribute("flags", new[] { true, false, true });
NewRelic.Api.Agent.NewRelic.GetAgent().CurrentTransaction.AddCustomAttribute("items", new List<string> { "item1", "item2" });

// Null handling
NewRelic.Api.Agent.NewRelic.GetAgent().CurrentTransaction.AddCustomAttribute("filtered", new object[] { "first", null, "third" });
// Results in: ["first", "third"] - nulls are filtered out

// Empty arrays are not serialized (treated as null)
NewRelic.Api.Agent.NewRelic.GetAgent().CurrentTransaction.AddCustomAttribute("empty", new string[] { }); // Not written

Behavior

  • Arrays serialize as JSON arrays: ["red", "green", "blue"]
  • Null elements filtered: ["first", null, "third"]["first", "third"]
  • Empty/null-only arrays skipped: Not included in serialized output
  • Type preservation: Uses existing element conversion logic
  • Framework support: Works with arrays, Lists, and any IEnumerable

Testing Results

  • 8/8 integration tests passing (both .NET Framework and .NET Core)
  • All unit tests passing for JSON serialization
  • End-to-end validation through actual New Relic agent instrumentation
  • Transaction traces and events contain properly formatted JSON arrays

Verified correct behavior in the New Relic UI:
image

Backward Compatibility

  • No breaking changes - existing functionality unchanged
  • Pure addition - only adds new array handling capability
  • Same API surface - no changes to public interfaces

This feature enables developers to provide richer telemetry data by including array-based custom attributes that are properly structured and queryable in New Relic.

tippmar-nr and others added 10 commits February 19, 2026 13:42
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add comprehensive integration test classes for both .NET Framework and .NET Core
- Extend test applications with new array attribute endpoints
- Add RemoteServiceFixture helper methods for array testing
- Update Assertions.cs to support array validation in tests
- Verify arrays work in transaction traces, transaction events, and error telemetry
- Test empty array filtering and null element removal

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add support for string[], int[], bool[] typed arrays in test assertions
- Extract array validation logic into ValidateArrayAttribute helper method
- Handle JArray deserialization from JSON properly
- Improve error messages with type information
- Add regular attributes to empty array test endpoints to ensure transaction capture

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
… support

- Add array attribute endpoints to both .NET Framework and .NET Core test applications
- Create integration test classes for testing array serialization end-to-end
- Test string, int, and bool arrays in transaction traces and events
- Verify empty arrays and null-only arrays are properly skipped
- Test null filtering within arrays (nulls are excluded from serialized arrays)
- Support both ASP.NET Framework (WebAPI) and ASP.NET Core applications
- Add remote service fixture methods for exercising array endpoints
- Fix test reliability by using transaction events for multi-endpoint scenarios

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add comprehensive integration test classes for both .NET Framework and .NET Core
- Extend test applications with new array attribute endpoints
- Add RemoteServiceFixture helper methods for array testing
- Update Assertions.cs to support array validation in tests
- Verify arrays work in transaction traces, transaction events, and error telemetry
- Test empty array filtering and null element removal

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add support for string[], int[], bool[] typed arrays in test assertions
- Extract array validation logic into ValidateArrayAttribute helper method
- Handle JArray deserialization from JSON properly
- Improve error messages with type information
- Add regular attributes to empty array test endpoints to ensure transaction capture

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
… support

- Add array attribute endpoints to both .NET Framework and .NET Core test applications
- Create integration test classes for testing array serialization end-to-end
- Test string, int, and bool arrays in transaction traces and events
- Verify empty arrays and null-only arrays are properly skipped
- Test null filtering within arrays (nulls are excluded from serialized arrays)
- Support both ASP.NET Framework (WebAPI) and ASP.NET Core applications
- Add remote service fixture methods for exercising array endpoints
- Fix test reliability by using transaction events for multi-endpoint scenarios

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@tippmar-nr tippmar-nr force-pushed the feature/json-serializer-array-support branch from d46e9e4 to 32a626f Compare February 19, 2026 22:22
@tippmar-nr tippmar-nr marked this pull request as ready for review February 20, 2026 15:31
@tippmar-nr tippmar-nr requested a review from a team as a code owner February 20, 2026 15:31
tippmar-nr and others added 2 commits February 20, 2026 09:40
…ath coverage

- Add tests for all numeric types: double, float, decimal, long, char
- Add tests for unsigned types: ushort, uint, ulong
- Add tests for signed types: short, sbyte, byte
- Add tests for character and boolean arrays
- Add tests for non-serializable objects to cover exception handling path
- Add tests for mixed numeric types in arrays
- Achieve 100% branch coverage for JsonSerializerHelpers.WriteValue() method
- All 27 unit tests passing with comprehensive validation of all execution paths

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…est controller

- Remove leftover CustomArrayErrorAttributes method that attempted to pass arrays to NoticeError()
- Error attributes do not support arrays and this functionality will not be implemented
- Ensures test applications only contain valid array attribute endpoints
- Maintains clean separation between AddCustomAttribute (supports arrays) and NoticeError (does not)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment thread src/Agent/NewRelic/Agent/Core/JsonConverters/JsonSerializerHelpers.cs Outdated
Comment thread src/Agent/NewRelic/Agent/Core/JsonConverters/JsonSerializerHelpers.cs Outdated
…lizerHelpers

- Revert unnecessary ReSharper refactor that introduced inconsistent variable naming
- Use discard pattern (_) consistently across all switch cases
- Maintain original value parameter usage for cleaner, more consistent code
- Addresses review feedback about variable naming inconsistencies

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@tippmar-nr

Copy link
Copy Markdown
Member Author

Review comments addressed: Reverted JsonSerializerHelpers switch cases to use discard pattern (_) as originally implemented, fixing the inconsistent variable naming from the ReSharper refactor.

@tippmar-nr tippmar-nr merged commit 4fb9405 into main Feb 24, 2026
115 checks passed
@tippmar-nr tippmar-nr deleted the feature/json-serializer-array-support branch February 24, 2026 18:34
jaffinito pushed a commit that referenced this pull request Mar 2, 2026
* feat: add array support to JsonSerializerHelpers with null filtering

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* test: add integration tests for array custom attributes

- Add comprehensive integration test classes for both .NET Framework and .NET Core
- Extend test applications with new array attribute endpoints
- Add RemoteServiceFixture helper methods for array testing
- Update Assertions.cs to support array validation in tests
- Verify arrays work in transaction traces, transaction events, and error telemetry
- Test empty array filtering and null element removal

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* chore: Minor refactoring and cleanup

* fix: improve array validation in integration test assertions

- Add support for string[], int[], bool[] typed arrays in test assertions
- Extract array validation logic into ValidateArrayAttribute helper method
- Handle JArray deserialization from JSON properly
- Improve error messages with type information
- Add regular attributes to empty array test endpoints to ensure transaction capture

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* test: add comprehensive integration tests for custom attributes array support

- Add array attribute endpoints to both .NET Framework and .NET Core test applications
- Create integration test classes for testing array serialization end-to-end
- Test string, int, and bool arrays in transaction traces and events
- Verify empty arrays and null-only arrays are properly skipped
- Test null filtering within arrays (nulls are excluded from serialized arrays)
- Support both ASP.NET Framework (WebAPI) and ASP.NET Core applications
- Add remote service fixture methods for exercising array endpoints
- Fix test reliability by using transaction events for multi-endpoint scenarios

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: add array support to JsonSerializerHelpers with null filtering

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* test: add integration tests for array custom attributes

- Add comprehensive integration test classes for both .NET Framework and .NET Core
- Extend test applications with new array attribute endpoints
- Add RemoteServiceFixture helper methods for array testing
- Update Assertions.cs to support array validation in tests
- Verify arrays work in transaction traces, transaction events, and error telemetry
- Test empty array filtering and null element removal

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* chore: Minor refactoring and cleanup

* fix: improve array validation in integration test assertions

- Add support for string[], int[], bool[] typed arrays in test assertions
- Extract array validation logic into ValidateArrayAttribute helper method
- Handle JArray deserialization from JSON properly
- Improve error messages with type information
- Add regular attributes to empty array test endpoints to ensure transaction capture

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* test: add comprehensive integration tests for custom attributes array support

- Add array attribute endpoints to both .NET Framework and .NET Core test applications
- Create integration test classes for testing array serialization end-to-end
- Test string, int, and bool arrays in transaction traces and events
- Verify empty arrays and null-only arrays are properly skipped
- Test null filtering within arrays (nulls are excluded from serialized arrays)
- Support both ASP.NET Framework (WebAPI) and ASP.NET Core applications
- Add remote service fixture methods for exercising array endpoints
- Fix test reliability by using transaction events for multi-endpoint scenarios

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* test: enhance JsonSerializerHelpers test coverage for complete code path coverage

- Add tests for all numeric types: double, float, decimal, long, char
- Add tests for unsigned types: ushort, uint, ulong
- Add tests for signed types: short, sbyte, byte
- Add tests for character and boolean arrays
- Add tests for non-serializable objects to cover exception handling path
- Add tests for mixed numeric types in arrays
- Achieve 100% branch coverage for JsonSerializerHelpers.WriteValue() method
- All 27 unit tests passing with comprehensive validation of all execution paths

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* cleanup: remove CustomArrayErrorAttributes method from ASP.NET Core test controller

- Remove leftover CustomArrayErrorAttributes method that attempted to pass arrays to NoticeError()
- Error attributes do not support arrays and this functionality will not be implemented
- Ensures test applications only contain valid array attribute endpoints
- Maintains clean separation between AddCustomAttribute (supports arrays) and NoticeError (does not)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: revert to discard pattern for switch case variables in JsonSerializerHelpers

- Revert unnecessary ReSharper refactor that introduced inconsistent variable naming
- Use discard pattern (_) consistently across all switch cases
- Maintain original value parameter usage for cleaner, more consistent code
- Addresses review feedback about variable naming inconsistencies

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.68%. Comparing base (1ccc871) to head (69c27de).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3456      +/-   ##
==========================================
+ Coverage   81.64%   81.68%   +0.04%     
==========================================
  Files         508      508              
  Lines       33944    33963      +19     
  Branches     3999     4003       +4     
==========================================
+ Hits        27712    27743      +31     
+ Misses       5273     5264       -9     
+ Partials      959      956       -3     
Flag Coverage Δ
Agent 82.67% <100.00%> (+0.04%) ⬆️
Profiler 71.75% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...Agent/Core/JsonConverters/JsonSerializerHelpers.cs 100.00% <100.00%> (+31.34%) ⬆️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

tippmar-nr added a commit that referenced this pull request Apr 9, 2026
PR #3456 added array serialization in JsonSerializerHelpers but missed two code paths:

1. GenericConverter in AttributeDefinition.cs - arrays fell through to ToString(), producing garbage strings like 'System.Int32[]' in transaction traces and events.

2. AttributeValue.SetValue in Segments/AttributeValue.cs - the protobuf-backed AttributeValue used by SpanAttributeValueCollection had no IEnumerable handling, so arrays fell through to TypeCode.Object -> ToString() in span events.

Fixes both converters and adds unit tests (GenericConverter, SpanEventWireModel) and integration tests (transaction trace, transaction event, and span event assertions).

Co-authored-by: GitHub Copilot <copilot@github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants