-
Notifications
You must be signed in to change notification settings - Fork 0
Better exception management for Azure ServiceBus #1131
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
7 commits
Select commit
Hold shift + click to select a range
92ebf85
Check result from azure monitor better
andreasohlund 2ba0fe7
Better output for azure service bus results
andreasohlund ac2bfc6
Formatting
andreasohlund 5fcd356
Better output
andreasohlund c8c7d24
Include dates
andreasohlund d83c8e2
Output dates for not found
andreasohlund 43814ad
Better message
andreasohlund 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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,13 +20,16 @@ public class AzureClient | |||||
|
|
||||||
| Queue<AuthenticatedClientSet> connectionQueue; | ||||||
| AuthenticatedClientSet currentClients; | ||||||
| const string CompleteMessageMetricName = "CompleteMessage"; | ||||||
| const string MicrosoftServicebusNamespacesMetricsNamespace = "Microsoft.ServiceBus/Namespaces"; | ||||||
|
|
||||||
| public string FullyQualifiedNamespace { get; } | ||||||
|
|
||||||
| public AzureClient(string resourceId, string serviceBusDomain, string region, string metricsDomain, Action<string> log = null) | ||||||
| { | ||||||
| this.resourceId = ResourceIdentifier.Parse(resourceId); | ||||||
|
|
||||||
| this.log = log ?? new(msg => { }); | ||||||
| this.log = log ?? (_ => { }); | ||||||
|
|
||||||
| FullyQualifiedNamespace = $"{this.resourceId.Name}.{serviceBusDomain}"; | ||||||
|
|
||||||
|
|
@@ -43,9 +46,14 @@ IEnumerable<TokenCredential> CreateCredentials() | |||||
| yield return new VisualStudioCredential(); | ||||||
|
|
||||||
| // Don't really need this one to take 100s * 4 tries to finally time out | ||||||
| var opts = new TokenCredentialOptions(); | ||||||
| opts.Retry.MaxRetries = 1; | ||||||
| opts.Retry.NetworkTimeout = TimeSpan.FromSeconds(10); | ||||||
| var opts = new TokenCredentialOptions | ||||||
| { | ||||||
| Retry = | ||||||
| { | ||||||
| MaxRetries = 1, | ||||||
| NetworkTimeout = TimeSpan.FromSeconds(10) | ||||||
| } | ||||||
| }; | ||||||
| yield return new ManagedIdentityCredential(FullyQualifiedNamespace, opts); | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -108,8 +116,8 @@ public Task<IList<MetricValue>> GetMetrics(string queueName, DateOnly startTime, | |||||
| { | ||||||
| var response = await currentClients.Metrics.QueryResourcesAsync( | ||||||
| [resourceId], | ||||||
| ["CompleteMessage"], | ||||||
| "Microsoft.ServiceBus/Namespaces", | ||||||
| [CompleteMessageMetricName], | ||||||
| MicrosoftServicebusNamespacesMetricsNamespace, | ||||||
| new MetricsQueryResourcesOptions | ||||||
| { | ||||||
| Filter = $"EntityName eq '{queueName}'", | ||||||
|
|
@@ -119,8 +127,28 @@ public Task<IList<MetricValue>> GetMetrics(string queueName, DateOnly startTime, | |||||
| }, | ||||||
| token).ConfigureAwait(false); | ||||||
|
|
||||||
| // Yeah, it's buried deep | ||||||
| return response.Value.Values.FirstOrDefault()?.Metrics.FirstOrDefault()?.TimeSeries.FirstOrDefault()?.Values ?? []; | ||||||
| var metricQueryResult = response.Value.Values.SingleOrDefault(mr => mr.Namespace == MicrosoftServicebusNamespacesMetricsNamespace); | ||||||
|
|
||||||
| if (metricQueryResult is null) | ||||||
| { | ||||||
| throw new Exception("No metrics query results returned for Microsoft.ServiceBus/Namespace"); | ||||||
|
Member
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.
Suggested change
Member
Author
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. oops, I had it to automerge, will apply it to master |
||||||
| } | ||||||
|
|
||||||
| var metricResult = metricQueryResult.GetMetricByName(CompleteMessageMetricName); | ||||||
|
|
||||||
| if (metricResult.Error.Message is not null) | ||||||
| { | ||||||
| throw new Exception($"Metrics query result for '{metricResult.Name}' failed: {metricResult.Error.Message}"); | ||||||
| } | ||||||
|
|
||||||
| var timeSeries = metricResult.TimeSeries.SingleOrDefault(); | ||||||
|
|
||||||
| if (timeSeries is null) | ||||||
| { | ||||||
| throw new Exception($"Metrics query result for '{metricResult.Name}' contained no time series"); | ||||||
| } | ||||||
|
|
||||||
| return timeSeries.Values; | ||||||
| } | ||||||
| catch (Azure.RequestFailedException reqFailed) when (reqFailed.Message.Contains("ResourceGroupNotFound")) | ||||||
| { | ||||||
|
|
@@ -136,7 +164,7 @@ public Task<string[]> GetQueueNames(CancellationToken cancellationToken = defaul | |||||
| return GetDataWithCurrentCredentials(async token => | ||||||
| { | ||||||
| var queueList = new List<string>(); | ||||||
| await foreach (var queue in currentClients.ServiceBus.GetQueuesAsync(cancellationToken).WithCancellation(cancellationToken)) | ||||||
| await foreach (var queue in currentClients.ServiceBus.GetQueuesAsync(token)) | ||||||
| { | ||||||
| queueList.Add(queue.Name); | ||||||
| } | ||||||
|
|
||||||
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.
Uh oh!
There was an error while loading. Please reload this page.