Skip to content

Commit

Permalink
[APIPUB-76] - Adds Sonar Analyzer - Log as json where appropiate. (#83)
Browse files Browse the repository at this point in the history
* Adds Sonar Analyzer - Log as json when appropiate.

* Build fix

* Remove unnecessary using

* Trying to undo program.cs
  • Loading branch information
DavidJGapCR authored Oct 8, 2024
1 parent 50559ff commit ca01994
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 361 deletions.
627 changes: 313 additions & 314 deletions src/EdFi.Tools.ApiPublisher.Cli/Program.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ public ApiSourceResourceItemProvider(ISourceEdFiApiClientProvider sourceEdFiApiC
}
else
{
_logger.Warning("GET request from source API for '{ResourceItemUrl}' reference failed with status '{StatusCode}': {ResponseContent}",
resourceItemUrl, getByIdResponse.StatusCode, responseContent);
var message = $"GET request from source API for '{resourceItemUrl}' reference failed with status '{getByIdResponse.StatusCode}': {responseContent}";
_logger.Warning(message);

return (false, null);
}
}
catch (RateLimitRejectedException ex)
{
_logger.Fatal(ex, "{DataManagementApiSegment}{ResourceItemUrl}: Rate limit exceeded. Please try again later.",
sourceEdFiApiClient.DataManagementApiSegment, resourceItemUrl);
var message = $"{sourceEdFiApiClient.DataManagementApiSegment}{resourceItemUrl}: Rate limit exceeded. Please try again later.";
_logger.Fatal(ex, message);
return (false, null);
}
//----------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public EdFiApiGraphMLDependencyMetadataProvider(IEdFiApiClientProvider edFiApiCl

if (!dependencyResponse.IsSuccessStatusCode)
{
_logger.Error("Ed-Fi ODS API request for dependencies to '{RequestUri}' returned '{StatusCode}' with content:{NewLine}{DependencyResponseContent}",
dependencyRequest.RequestUri, dependencyResponse.StatusCode, Environment.NewLine, dependencyResponseContent);
var message = $"Ed-Fi ODS API request for dependencies to '{dependencyRequest.RequestUri}' returned '{dependencyResponse.StatusCode}' with content:{Environment.NewLine}{dependencyResponseContent}";
_logger.Error(message);
throw new Exception("Resource dependencies could not be obtained.");
}

Expand All @@ -57,8 +57,8 @@ public EdFiApiGraphMLDependencyMetadataProvider(IEdFiApiClientProvider edFiApiCl
}
catch (Exception ex)
{
_logger.Error(ex, "Unable to parse dependency response as GraphML: {DependencyResponseContent}{NewLine}{Ex}",
dependencyResponseContent, Environment.NewLine, ex);
var message = $"Unable to parse dependency response as GraphML: {dependencyResponseContent}{Environment.NewLine}{ex}";
_logger.Error(ex, message);
throw new Exception("Resource dependencies could not be obtained.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
// See the LICENSE and NOTICES files in the project root for more information.

using System;
using System.Net;
using System.Threading.Tasks.Dataflow;
using EdFi.Tools.ApiPublisher.Connections.Api.ApiClientManagement;
Expand Down Expand Up @@ -116,12 +117,8 @@ CancellationToken cancellationToken

if (!apiResponse.IsSuccessStatusCode)
{
_logger.Error(
"{Url}: Count request returned {StatusCode}\r{Content}",
resourceUrl,
apiResponse.StatusCode,
responseContent
);
var messge = $"{resourceUrl}: Count request returned {apiResponse.StatusCode}\r{responseContent}";
_logger.Error(messge);

await HandleResourceCountRequestErrorAsync(resourceUrl, errorHandlingBlock, apiResponse)
.ConfigureAwait(false);
Expand Down Expand Up @@ -198,6 +195,7 @@ HttpResponseMessage apiResponse
)
{
string responseContent = await apiResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
string message = string.Empty;

// Was this an authorization failure?
var authFailure = apiResponse.StatusCode == HttpStatusCode.Forbidden;
Expand All @@ -207,18 +205,14 @@ HttpResponseMessage apiResponse
// Being denied read access to descriptors is potentially problematic, but is not considered
// to be breaking in its own right for change processing. We'll fail downstream
// POSTs if descriptors haven't been initialized correctly on the target.
_logger.Warning(
"{Url}: {StatusCode} - Unable to obtain total count for descriptor due to authorization failure. Descriptor values will not be published to the target, but processing will continue.\rResponse content: {Content}",
resourceUrl, apiResponse.StatusCode, responseContent
);
message = $"{resourceUrl}: {apiResponse.StatusCode} - Unable to obtain total count for descriptor due to authorization failure. Descriptor values will not be published to the target, but processing will continue.\rResponse content: {responseContent}";
_logger.Warning(message);

return;
}

_logger.Error(
"{Url}: {StatusCode} - Unable to obtain total count due to request failure. This resource will not be processed. Downstream failures are possible.\rResponse content: {Content}",
resourceUrl, apiResponse.StatusCode, responseContent
);
message = $"{resourceUrl}: {apiResponse.StatusCode} - Unable to obtain total count due to request failure. This resource will not be processed. Downstream failures are possible.\rResponse content: {responseContent}";
_logger.Error(message);

// Publish an error for the resource to allow processing to continue, but to force failure.
errorHandlingBlock.Post(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ private async Task<string> GetSourceSnapshotIdentifierAsync(EdFiApiClient source

string errorResponseText = await snapshotsResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

_logger.Error("Unable to get snapshot identifier from API at '{BaseAddress}{SnapshotsRelativePath}'. Request for available snapshots returned status '{StatusCode}' with message body: {ErrorResponseText}",
sourceApiClient.HttpClient.BaseAddress, snapshotsRelativePath, snapshotsResponse.StatusCode, errorResponseText);
var message = $"Unable to get snapshot identifier from API at '{sourceApiClient.HttpClient.BaseAddress}{snapshotsRelativePath}'. Request for available snapshots returned status '{snapshotsResponse.StatusCode}' with message body: {errorResponseText}";
_logger.Error(message);

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ public async Task<IEnumerable<TProcessDataMessage>> HandleStreamResourcePageAsyn
// Publish the failure
errorHandlingBlock.Post(error);

_logger.Error(ex, "{ResourceUrl}: JSON parsing of source page data failed: {Ex}{NewLine}{ResponseContent}",
message.ResourceUrl, ex, Environment.NewLine, responseContent);
var logMessage = $"{message.ResourceUrl}: JSON parsing of source page data failed: {ex}{Environment.NewLine}{responseContent}";
_logger.Error(ex, logMessage);

break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public EdFiApiSourceCurrentChangeVersionProvider(ISourceEdFiApiClientProvider so

string versionResponseText = await versionResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

_logger.Debug("Available change versions request from {BaseAddress}{AvailableChangeVersionsRelativePath} returned {StatusCode}: {VersionResponseText}",
sourceApiClient.HttpClient.BaseAddress, availableChangeVersionsRelativePath, versionResponse.StatusCode, versionResponseText);
var message = $"Available change versions request from {sourceApiClient.HttpClient.BaseAddress}{availableChangeVersionsRelativePath} returned {versionResponse.StatusCode}: {versionResponseText}";
_logger.Debug(message);

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ private TransformManyBlock<GetItemForKeyChangeMessage, ChangeKeyMessage> CreateG
// Failure
if (!apiResponse.IsSuccessStatusCode)
{
_logger.Error("{ResourceUrl} (source id: {SourceId}): GET by key returned {StatusCode}{NewLine}{ResponseContent}",
message.ResourceUrl, sourceId, apiResponse.StatusCode, Environment.NewLine, responseContent);
var logMessage = $"{message.ResourceUrl} (source id: {sourceId}): GET by key returned {apiResponse.StatusCode}{Environment.NewLine}{responseContent}";
_logger.Error(logMessage);

var error = new ErrorItemMessage
{
Expand Down Expand Up @@ -337,9 +337,8 @@ private TransformManyBlock<ChangeKeyMessage, ErrorItemMessage> CreateChangeKeyBl
{
string responseContent = await apiResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

_logger.Error(
"{ResourceUrl} (source id: {SourceId}): PUT returned {StatusCode}{NewLine}{ResponseContent}",
msg.ResourceUrl, sourceId, apiResponse.StatusCode, Environment.NewLine, responseContent);
var message = $"{msg.ResourceUrl} (source id: {sourceId}): PUT returned {apiResponse.StatusCode}{Environment.NewLine}{responseContent}";
_logger.Error(message);

// Publish the failure
var error = new ErrorItemMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ private TransformManyBlock<GetItemForDeletionMessage, DeleteItemMessage> CreateG

if (!apiResponse.IsSuccessStatusCode)
{
_logger.Error("{ResourceUrl} (source id: {Id}): GET by key returned {StatusCode}{NewLine}{ResponseContent}",
msg.ResourceUrl, id, apiResponse.StatusCode, Environment.NewLine, responseContent);
var message = $"{msg.ResourceUrl} (source id: {id}): GET by key returned {apiResponse.StatusCode}{Environment.NewLine}{responseContent}";
_logger.Error(message);

var error = new ErrorItemMessage
{
Expand Down Expand Up @@ -282,8 +282,8 @@ private TransformManyBlock<DeleteItemMessage, ErrorItemMessage> CreateDeleteReso
{
string responseContent = await apiResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

_logger.Error("{ResourceUrl} (source id: {SourceId}): DELETE returned {StatusCode}{NewLine}{ResponseContent}",
msg.ResourceUrl, sourceId, apiResponse.StatusCode, Environment.NewLine, responseContent);
var message = $"{msg.ResourceUrl} (source id: {sourceId}): DELETE returned {apiResponse.StatusCode}{Environment.NewLine}{responseContent}";
_logger.Error(message);

// Publish the failure
var error = new ErrorItemMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ private async Task<IEnumerable<ErrorItemMessage>> HandlePostItemMessage(
remediationResult.ModifiedRequestBody,
new JsonSerializerOptions { WriteIndented = true });

_logger.Debug("{ResourceUrl} (source id: {Id}): Remediation plan provided a modified request body: {ModifiedRequestBodyJson} ",
postItemMessage.ResourceUrl, id, modifiedRequestBodyJson);
var message = $"{postItemMessage.ResourceUrl} (source id: {id}): Remediation plan provided a modified request body: {modifiedRequestBodyJson}";
_logger.Debug(message);
}

ctx["ModifiedRequestBody"] = remediationResult.ModifiedRequestBody;
Expand All @@ -224,8 +224,8 @@ private async Task<IEnumerable<ErrorItemMessage>> HandlePostItemMessage(
{
string responseContent = await result.Result.Content.ReadAsStringAsync().ConfigureAwait(false);

_logger.Warning("{ResourceUrl} (source id: {Id}): POST attempt #{Attempts} failed with status '{StatusCode}'. Retrying... (retry #{RetryAttempt} of {MaxRetryAttempts} with {TotalSeconds:N1}s delay):{NewLine}{ResponseContent}",
postItemMessage.ResourceUrl, id, attempts, result.Result.StatusCode, retryAttempt, options.MaxRetryAttempts, ts.TotalSeconds, Environment.NewLine, responseContent);
var message = $"{postItemMessage.ResourceUrl} (source id: {id}): POST attempt #{attempts} failed with status '{result.Result.StatusCode}'. Retrying... (retry #{retryAttempt} of {options.MaxRetryAttempts} with {ts.TotalSeconds:N1}s delay):{Environment.NewLine}{responseContent}";
_logger.Warning(message);
}
});
IAsyncPolicy<HttpResponseMessage> policy = isRateLimitingEnabled ? Policy.WrapAsync(_rateLimiter?.GetRateLimitingPolicy(), retryPolicy) : retryPolicy;
Expand Down Expand Up @@ -352,24 +352,25 @@ await HandlePostItemMessage(
}

string responseContent = await apiResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
var message = string.Empty;

// If the failure is Forbidden, and we should treat it as a warning
if (apiResponse.StatusCode == HttpStatusCode.Forbidden
&& postItemMessage.PostAuthorizationFailureRetry == null
&& targetEdFiApiClient.ConnectionDetails?.TreatForbiddenPostAsWarning == true)
{
// Warn and ignore all future data for this resource
_logger.Warning("{ResourceUrl} (source id: {Id}): Authorization failed on POST of resource with no authorization failure handling defined. Remaining resource items will be ignored. Response status: {StatusCode}{NewLine}{ResponseContent}",
postItemMessage.ResourceUrl, id, apiResponse.StatusCode, Environment.NewLine, responseContent);
message = $"{postItemMessage.ResourceUrl} (source id: {id}): Authorization failed on POST of resource with no authorization failure handling defined. Remaining resource items will be ignored. Response status: {apiResponse.StatusCode}{Environment.NewLine}{responseContent}";
_logger.Warning(message);

ignoredResourceByUrl.TryAdd(postItemMessage.ResourceUrl, true);

return Enumerable.Empty<ErrorItemMessage>();
}

// Error is final, log it and indicate failure for processing
_logger.Error("{ResourceUrl} (source id: {Id}): POST attempt #{Attempts} failed with status '{StatusCode}':{NewLine}{ResponseContent}",
postItemMessage.ResourceUrl, id, attempts, apiResponse.StatusCode, Environment.NewLine, responseContent);
message = $"{postItemMessage.ResourceUrl} (source id: {id}): POST attempt #{attempts} failed with status '{apiResponse.StatusCode}':{Environment.NewLine}{responseContent}";
_logger.Error(message);

// Publish the failed data
var error = new ErrorItemMessage
Expand Down Expand Up @@ -677,8 +678,8 @@ private async Task<RemediationResult> TryRemediateFailureAsync(

if (_logger.IsEnabled(LogEventLevel.Debug))
{
_logger.Debug("{ResourceUrl} (source id: {SourceId}): Remediating request with POST request to '{RemediationRequestResource}' on target API: {RemediationRequestBodyJson}",
resourceUrl, sourceId, remediationRequest.resource, remediationRequestBodyJson);
var message = $"{resourceUrl} (source id: {sourceId}): Remediating request with POST request to '{remediationRequest.resource}' on target API: {remediationRequestBodyJson}";
_logger.Debug(message);
}

var remediationResponse = await RequestHelpers.SendPostRequestAsync(
Expand Down

0 comments on commit ca01994

Please sign in to comment.