public static async Task DistributeCall(RawRequestEnvelope requestEnvelope, ILogger log) { try { InternalContract.RequireNotNull(Startup.AsyncCallerServiceConfiguration, nameof(Startup.AsyncCallerServiceConfiguration), $"Missing {nameof(Startup.AsyncCallerServiceConfiguration)}. Please check your Nexus configuration for this function app."); // Client tenant is found in the request envelope var clientTenant = new Tenant(requestEnvelope.Organization, requestEnvelope.Environment); ServiceContract.RequireValidated(clientTenant, nameof(clientTenant)); FulcrumApplication.Context.ClientTenant = clientTenant; // Setup the tenants AC configuration (cache and refresh is handled by LeverServiceConfiguration) var clientConfig = await Startup.AsyncCallerServiceConfiguration.GetConfigurationForAsync(clientTenant); FulcrumApplication.Context.LeverConfiguration = clientConfig; // Distribute the request. RequestHandler will put back on queue if necessary, and also handle callbacks var handler = new RequestHandler(HttpSender, clientTenant, FulcrumApplication.Context.LeverConfiguration, requestEnvelope); await handler.ProcessOneRequestAsync(); } catch (Exception e) { const string errorMessage = "Failed to distribute request. (Code location 5ADA0B3E-2344-4977-922B-F7BB870EA065)"; log.LogError(e, errorMessage); Log.LogError(errorMessage, e); throw; } }
public static async Task DistributeCall(RawRequestEnvelope requestEnvelope, ILogger log) { try { // Setup correlation id MaybeSetupCorrelationId(requestEnvelope, log); // Log this invocation var invocationLogMessage = $"{requestEnvelope}; prio: {requestEnvelope.RawRequest.Priority?.ToString() ?? "standard"}"; log.LogDebug(invocationLogMessage); Log.LogVerbose(invocationLogMessage); InternalContract.RequireNotNull(Startup.AsyncCallerServiceConfiguration, nameof(Startup.AsyncCallerServiceConfiguration), $"Missing {nameof(Startup.AsyncCallerServiceConfiguration)}. Please check your Nexus configuration for this function app."); // Client tenant is found in the request envelope var clientTenant = new Tenant(requestEnvelope.Organization, requestEnvelope.Environment); ServiceContract.RequireValidated(clientTenant, nameof(clientTenant)); FulcrumApplication.Context.ClientTenant = clientTenant; // Setup the tenants AC configuration (cache and refresh is handled by LeverServiceConfiguration) if (!Startup.AsyncCallerServiceConfiguration.TryGetValue(clientTenant, out var serviceConfiguration)) { throw new FulcrumUnauthorizedException($"Unable to find credentials for fetching Nexus configuration for tenant {clientTenant}"); } var clientConfig = await serviceConfiguration.GetConfigurationForAsync(clientTenant); FulcrumApplication.Context.LeverConfiguration = clientConfig; // Distribute the request. RequestHandler will put back on queue if necessary, and also handle callbacks var handler = new RequestHandler(HttpSender, clientTenant, FulcrumApplication.Context.LeverConfiguration, requestEnvelope); await handler.ProcessOneRequestAsync(); } catch (Exception e) { var errorMessage = "Failed to distribute request. (Code location 5ADA0B3E-2344-4977-922B-F7BB870EA065)" + $" | Loaded configurations: {string.Join(", ", Startup.AsyncCallerServiceConfiguration.Keys)}"; log.LogError(e, errorMessage); Log.LogError(errorMessage, e); throw; } }
public async Task Distribution_Invocation_Logged_Verbose() { var logger = new LoggerFactory().CreateLogger(nameof(LogTests)); var mockLogger = new Mock <ISyncLogger>(); FulcrumApplication.Setup.SynchronousFastLogger = mockLogger.Object; var testDone = new ManualResetEvent(false); var origionalId = Guid.NewGuid().ToString(); var url = $"https://example.org/api/v1/stuffs/{origionalId}"; mockLogger .Setup(x => x.LogSync(It.IsAny <LogRecord>())) .Callback((LogRecord record) => { Console.WriteLine(record.Message); if (record.SeverityLevel == LogSeverityLevel.Verbose && record.Message.Contains(origionalId) && record.Message.Contains(url) && record.ToLogString().Contains("corr-id")) { testDone.Set(); } }); var callOut = new HttpRequestMessage(HttpMethod.Get, url); callOut.Headers.Add("X-Correlation-ID", "corr-id"); var requestEnvelope = new RawRequestEnvelope { Organization = Tenant.Organization, Environment = Tenant.Environment, OriginalRequestId = origionalId, RawRequest = await new Request { CallOut = callOut }.ToRawAsync() }; await Distributor.DistributeCall(requestEnvelope, logger); Assert.IsTrue(testDone.WaitOne(1000)); }
private static void MaybeSetupCorrelationId(RawRequestEnvelope requestEnvelope, ILogger log) { if (!string.IsNullOrWhiteSpace(FulcrumApplication.Context.CorrelationId)) { return; } try { var callOut = Encoding.UTF8.GetString(requestEnvelope.RawRequest.CallOut); var correlationIdMatch = CorrelationIdRegex.Match(callOut); if (correlationIdMatch.Success) { FulcrumApplication.Context.CorrelationId = correlationIdMatch.Groups[1].Value; } } catch (Exception e) { const string message = "Error when finding correlation id"; log.LogWarning(message, e); Log.LogWarning(message, e); } }
public static async Task Priority10([QueueTrigger(RequestQueueHelper.DefaultQueueName + RequestQueueHelper.MultipleQueueNameInterfix + "10", Connection = "AzureWebJobsStorage")] RawRequestEnvelope rawRequestEnvelope, ILogger log) { await Distributor.DistributeCall(rawRequestEnvelope, log); }
public static async Task Standard([QueueTrigger(RequestQueueHelper.DefaultQueueName, Connection = "AzureWebJobsStorage")] RawRequestEnvelope rawRequestEnvelope, ILogger log) { await Distributor.DistributeCall(rawRequestEnvelope, log); }