-
Notifications
You must be signed in to change notification settings - Fork 102
Fix telemetry header readonly error #719
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
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6466d22
Fix telemetry header readonly error
mtmk d019eae
Format
mtmk c2a0ed7
Merge branch 'main' into fix-publish-telemetry
mtmk 4c2b546
Fix missing test project in solution file
mtmk 7a528a5
Fix test
mtmk a7f7dc6
Fix missing test project in solution file
mtmk 7c1102c
Fix JetStream test flappers
mtmk 1ed762f
Merge branch 'main' into fix-publish-telemetry
mtmk 727092f
Fix test proxy signal subject
mtmk cd60b7b
Fix test connection retry
mtmk eee43aa
Fix JS Test
mtmk 60d9f9e
Fix test revert
mtmk 96db87c
Fix test
mtmk 7047d84
Fix test cancellation
mtmk e2d2e93
Merge branch 'main' into fix-publish-telemetry
mtmk 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
5 changes: 5 additions & 0 deletions
5
tests/NATS.Client.Core.Tests/resources/configs/restricted-user.conf
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,5 @@ | ||
| authorization: { | ||
| users: [ | ||
| { user:u, permissions: { publish:x } } | ||
| ] | ||
| } |
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,152 @@ | ||
| using System.Diagnostics; | ||
| using NATS.Client.Core2.Tests; | ||
| using NATS.Net; | ||
|
|
||
| namespace NATS.Client.JetStream.Tests; | ||
|
|
||
| [Collection("nats-server-restricted-user")] | ||
| public class PublishRetryTest | ||
| { | ||
| private readonly ITestOutputHelper _output; | ||
| private readonly NatsServerRestrictedUserFixture _server; | ||
|
|
||
| public PublishRetryTest(ITestOutputHelper output, NatsServerRestrictedUserFixture server) | ||
| { | ||
| _output = output; | ||
| _server = server; | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Publish_with_or_without_telemetry() | ||
| { | ||
| var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); | ||
| await using var nats = new NatsConnection(new NatsOpts | ||
| { | ||
| Url = _server.Url, | ||
| AuthOpts = new NatsAuthOpts { Username = "u" }, | ||
| }); | ||
| var prefix = _server.GetNextId(); | ||
|
|
||
| // With telemetry | ||
| { | ||
| using var activityListener = new ActivityListener { ShouldListenTo = _ => true, Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData, }; | ||
| ActivitySource.AddActivityListener(activityListener); | ||
| var headers = new NatsHeaders(); | ||
| await nats.PublishAsync(prefix, headers: headers, cancellationToken: cts.Token); | ||
| Assert.Contains("traceparent", headers); | ||
| } | ||
|
|
||
| // Without telemetry | ||
| { | ||
| var headers = new NatsHeaders(); | ||
| await nats.PublishAsync(prefix, headers: headers, cancellationToken: cts.Token); | ||
| Assert.DoesNotContain("traceparent", headers); | ||
| Assert.Empty(headers); | ||
| } | ||
|
|
||
| // With telemetry and data | ||
| { | ||
| using var activityListener = new ActivityListener { ShouldListenTo = _ => true, Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData, }; | ||
| ActivitySource.AddActivityListener(activityListener); | ||
| var headers = new NatsHeaders(); | ||
| await nats.PublishAsync(prefix, data: 1, headers: headers, cancellationToken: cts.Token); | ||
| Assert.Contains("traceparent", headers); | ||
| } | ||
|
|
||
| // Without telemetry and data | ||
| { | ||
| var headers = new NatsHeaders(); | ||
| await nats.PublishAsync(prefix, data: 1, headers: headers, cancellationToken: cts.Token); | ||
| Assert.DoesNotContain("traceparent", headers); | ||
| Assert.Empty(headers); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Multiple_publish_with_same_headers_when_telemetry_on_should_not_throw_header_readonly_exception() | ||
| { | ||
| using var activityListener = new ActivityListener | ||
| { | ||
| ShouldListenTo = _ => true, | ||
| Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData, | ||
| }; | ||
| ActivitySource.AddActivityListener(activityListener); | ||
|
|
||
| var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); | ||
| await using var nats = new NatsConnection(new NatsOpts | ||
| { | ||
| Url = _server.Url, | ||
| AuthOpts = new NatsAuthOpts { Username = "u" }, | ||
| }); | ||
| var prefix = _server.GetNextId(); | ||
|
|
||
| // Publish with no data implemented in a different method | ||
| { | ||
| var headers = new NatsHeaders(); | ||
| await nats.PublishAsync(prefix, headers: headers, cancellationToken: cts.Token); | ||
| await nats.PublishAsync(prefix, headers: headers, cancellationToken: cts.Token); | ||
| Assert.Contains("traceparent", headers); | ||
| } | ||
|
|
||
| // Also try publish-with-data method | ||
| { | ||
| var headers = new NatsHeaders(); | ||
| await nats.PublishAsync(prefix, data: 1, headers: headers, cancellationToken: cts.Token); | ||
| await nats.PublishAsync(prefix, data: 1, headers: headers, cancellationToken: cts.Token); | ||
| Assert.Contains("traceparent", headers); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Retry_with_telemetry_on_should_not_throw_header_readonly_exception() | ||
| { | ||
| using var activityListener = new ActivityListener | ||
| { | ||
| ShouldListenTo = _ => true, | ||
| Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData, | ||
| }; | ||
| ActivitySource.AddActivityListener(activityListener); | ||
|
|
||
| var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); | ||
| await using var nats = new NatsConnection(new NatsOpts | ||
| { | ||
| Url = _server.Url, | ||
| AuthOpts = new NatsAuthOpts { Username = "u" }, | ||
| RequestTimeout = TimeSpan.FromSeconds(.5), | ||
| }); | ||
| var prefix = _server.GetNextId(); | ||
| var js = nats.CreateJetStreamContext(); | ||
|
|
||
| // Because of the permission error (associated user should only have permission to subject 'x' to publish) | ||
| // JetStream publish internally retry publishing again which in turn would use the same header. | ||
| // Telemetry altering headers should not cause 'readonly' exceptions. | ||
| { | ||
| var headers = new NatsHeaders(); | ||
| var exception = await Assert.ThrowsAnyAsync<Exception>(async () => | ||
| { | ||
| await js.PublishAsync( | ||
| subject: $"{prefix}.foo", | ||
| data: "order 1", | ||
| headers: headers, | ||
| cancellationToken: cts.Token); | ||
| }); | ||
| Assert.IsNotType<InvalidOperationException>(exception); | ||
| Assert.DoesNotMatch("response headers cannot be modified", exception.Message); | ||
| Assert.Contains("traceparent", headers); | ||
| } | ||
|
|
||
| // Also check for specific exception | ||
| { | ||
| var headers = new NatsHeaders(); | ||
| await Assert.ThrowsAsync<NatsJSPublishNoResponseException>(async () => | ||
| { | ||
| await js.PublishAsync( | ||
| subject: $"{prefix}.foo", | ||
| data: "order 1", | ||
| headers: headers, | ||
| cancellationToken: cts.Token); | ||
| }); | ||
| Assert.Contains("traceparent", headers); | ||
| } | ||
| } | ||
| } |
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.
this was a bug fix