-
Notifications
You must be signed in to change notification settings - Fork 293
feat: Add support to set partition key and headers via Request Context #3678
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
Merged
lillo42
merged 7 commits into
BrighterCommand:master
from
lillo42:allow-set-partition-key
Jul 28, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f6a98eb
feat: Allow flow PartitionKey/Headers/TraceParent/TraceState/Baggage …
lillo42 9b718e4
Merge branch 'master' into allow-set-partition-key
lillo42 0927af6
feat: Change MassTransit and JustSaying to flow partition key, trace …
lillo42 f0d52c8
Merge branch 'master' into allow-set-partition-key
lillo42 a09656c
fix build
lillo42 8687305
Merge branch 'master' into allow-set-partition-key
lillo42 3715933
feat: Remove Trace Parent/State and Baggage, also add DefaultHeaders …
lillo42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Paramore.Brighter.Extensions; | ||
|
|
||
| /// <summary> | ||
| /// Provides extension methods for dictionaries. | ||
| /// </summary> | ||
| public static class DictionaryExtensions | ||
| { | ||
| /// <summary> | ||
| /// Merges two dictionaries into a new dictionary, with values from the second dictionary overwriting those from the first. | ||
| /// </summary> | ||
| /// <param name="dict1">The primary dictionary to merge (will be copied first)</param> | ||
| /// <param name="dict2">The secondary dictionary to merge (may be null)</param> | ||
| /// <returns> | ||
| /// A new dictionary containing the combined key-value pairs from both dictionaries. | ||
| /// When keys exist in both dictionaries, the value from <paramref name="dict2"/> takes precedence. | ||
| /// </returns> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// This method performs a shallow merge where: | ||
| /// <list type="number"> | ||
| /// <item><description>All entries from <paramref name="dict1"/> are copied to the new dictionary</description></item> | ||
| /// <item><description>Entries from <paramref name="dict2"/> are then added or overwrite existing keys</description></item> | ||
| /// <item><description>Null values in <paramref name="dict2"/> will overwrite existing keys with null</description></item> | ||
| /// </list> | ||
| /// </para> | ||
| /// <para> | ||
| /// Special cases: | ||
| /// <list type="bullet"> | ||
| /// <item><description>If <paramref name="dict2"/> is null, returns a copy of <paramref name="dict1"/></description></item> | ||
| /// <item><description>Original dictionaries remain unmodified</description></item> | ||
| /// <item><description>Performs a shallow copy (values are not cloned)</description></item> | ||
| /// </list> | ||
| /// </para> | ||
| /// <example> | ||
| /// Basic merge with key override: | ||
| /// <code> | ||
| /// var dict1 = new Dictionary<string, object> { ["a"] = 1, ["b"] = 2 }; | ||
| /// var dict2 = new Dictionary<string, object> { ["b"] = 99, ["c"] = 3 }; | ||
| /// | ||
| /// var merged = dict1.Merge(dict2); | ||
| /// // Result: { "a": 1, "b": 99, "c": 3 } | ||
| /// </code> | ||
| /// | ||
| /// Handling null merge: | ||
| /// <code> | ||
| /// var merged = dict1.Merge(null); | ||
| /// // Returns copy of dict1 | ||
| /// </code> | ||
| /// </example> | ||
| /// </remarks> | ||
| public static Dictionary<string, object> Merge(this IDictionary<string, object> dict1, | ||
| IDictionary<string, object>? dict2) | ||
| { | ||
| var result = new Dictionary<string, object>(dict1); | ||
| if(dict2 != null) | ||
| { | ||
| foreach (var val in dict2) | ||
| { | ||
| result[val.Key] = val.Value; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } |
152 changes: 152 additions & 0 deletions
152
src/Paramore.Brighter/Extensions/RequestContextExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Paramore.Brighter.Extensions; | ||
|
|
||
| /// <summary> | ||
| /// Provides extension methods for <see cref="IRequestContext"/> to simplify access to common context bag values. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// These extensions offer type-safe access to Brighter's reserved context bag values, | ||
| /// handling type conversion and null checks internally. They are safe to call with null contexts. | ||
| /// </remarks> | ||
| public static class RequestContextExtensions | ||
| { | ||
| /// <summary> | ||
| /// Retrieves the partition key from the request context bag. | ||
| /// </summary> | ||
| /// <param name="context">The request context (may be null)</param> | ||
| /// <returns> | ||
| /// The partition key if present and valid, otherwise <see cref="PartitionKey.Empty"/>. | ||
| /// </returns> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Handles two valid value types in the context bag: | ||
| /// <list type="bullet"> | ||
| /// <item><description><see cref="string"/>: Converted to a <see cref="PartitionKey"/> instance</description></item> | ||
| /// <item><description><see cref="PartitionKey"/>: Returned directly</description></item> | ||
| /// </list> | ||
| /// </para> | ||
| /// <para> | ||
| /// Returns <see cref="PartitionKey.Empty"/> for: | ||
| /// <list type="bullet"> | ||
| /// <item><description>Null context</description></item> | ||
| /// <item><description>Missing partition key entry</description></item> | ||
| /// <item><description>Unsupported value types</description></item> | ||
| /// </list> | ||
| /// </para> | ||
| /// <example> | ||
| /// Usage: | ||
| /// <code> | ||
| /// var partitionKey = requestContext.GetPartitionKey(); | ||
| /// if (!partitionKey.IsEmpty) | ||
| /// { | ||
| /// // Use partition key | ||
| /// } | ||
| /// </code> | ||
| /// </example> | ||
| /// </remarks> | ||
| public static PartitionKey GetPartitionKey(this IRequestContext? context) | ||
| { | ||
| if (context == null || !context.Bag.TryGetValue(RequestContextBagNames.PartitionKey, out var tmp)) | ||
| { | ||
| return PartitionKey.Empty; | ||
| } | ||
|
|
||
| return tmp switch | ||
| { | ||
| string partitionKeyAsString => new PartitionKey(partitionKeyAsString), | ||
| PartitionKey partitionKey => partitionKey, | ||
| _ => PartitionKey.Empty | ||
| }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Retrieves the dynamic headers dictionary from the request context bag. | ||
| /// </summary> | ||
| /// <param name="context">The request context (may be null)</param> | ||
| /// <returns> | ||
| /// The headers dictionary if present and valid, otherwise null. | ||
| /// </returns> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// The value must be stored in the context bag as a <see cref="Dictionary{TKey, TValue}"/> | ||
| /// where TKey is <see cref="string"/> and TValue is <see cref="object"/>. | ||
| /// </para> | ||
| /// <para> | ||
| /// Returns null for: | ||
| /// <list type="bullet"> | ||
| /// <item><description>Null context</description></item> | ||
| /// <item><description>Missing headers entry</description></item> | ||
| /// <item><description>Type mismatch (not Dictionary<string, object>)</description></item> | ||
| /// </list> | ||
| /// </para> | ||
| /// <example> | ||
| /// Usage: | ||
| /// <code> | ||
| /// var headers = requestContext.GetHeaders(); | ||
| /// if (headers != null) | ||
| /// { | ||
| /// foreach (var header in headers) | ||
| /// { | ||
| /// // Process headers | ||
| /// } | ||
| /// } | ||
| /// </code> | ||
| /// </example> | ||
| /// </remarks> | ||
| public static Dictionary<string, object>? GetHeaders(this IRequestContext? context) | ||
| { | ||
| if (context != null | ||
| && context.Bag.TryGetValue(RequestContextBagNames.Headers, out var tmp) | ||
| && tmp is Dictionary<string, object> headers) | ||
| { | ||
| return headers; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Retrieves CloudEvent additional properties from the request context bag. | ||
| /// </summary> | ||
| /// <param name="context">The request context (may be null)</param> | ||
| /// <returns> | ||
| /// The CloudEvent extensions dictionary if present and valid, otherwise null. | ||
| /// </returns> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// The value must be stored in the context bag as a <see cref="Dictionary{TKey, TValue}"/> | ||
| /// where TKey is <see cref="string"/> and TValue is <see cref="object"/>. | ||
| /// </para> | ||
| /// <para> | ||
| /// Returns null for: | ||
| /// <list type="bullet"> | ||
| /// <item><description>Null context</description></item> | ||
| /// <item><description>Missing CloudEventsAdditionalProperties entry</description></item> | ||
| /// <item><description>Type mismatch (not Dictionary<string, object>)</description></item> | ||
| /// </list> | ||
| /// </para> | ||
| /// <example> | ||
| /// Usage: | ||
| /// <code> | ||
| /// var cloudEventProps = requestContext.GetCloudEventAdditionalProperties(); | ||
| /// if (cloudEventProps != null) | ||
| /// { | ||
| /// // Add to CloudEvent extensions | ||
| /// } | ||
| /// </code> | ||
| /// </example> | ||
| /// <seealso cref="RequestContextBagNames.CloudEventsAdditionalProperties"/> | ||
| /// </remarks> | ||
| public static Dictionary<string, object>? GetCloudEventAdditionalProperties(this IRequestContext? context) | ||
| { | ||
| if (context != null | ||
| && context.Bag.TryGetValue(RequestContextBagNames.CloudEventsAdditionalProperties, out var tmp) | ||
| && tmp is Dictionary<string, object> cloudEventAdditionalProperties) | ||
|
Comment on lines
+143
to
+145
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ New issue: Complex Conditional |
||
| { | ||
| return cloudEventAdditionalProperties; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❌ New issue: Complex Conditional
GetHeaders has 1 complex conditionals with 2 branches, threshold = 2
Suppress