Пример #1
0
        public async Task Run <T1, T2>(
            string[] messages,
            IBinder blobFaultBinder,
            Binder queueFaultBinder,
            IBinder incomingBatchBinder,
            IAsyncCollector <string> outputEvents,
            ILogger log)
        {
            var batchId = Guid.NewGuid().ToString();

            bool logIncoming = Utils.getEnvironmentVariable("logIncomingBatches").ToLower() == "true";

            log.LogInformation($"Received {messages.Length} messages");

            var           azMonMsgs  = (AzMonMessages)Activator.CreateInstance(typeof(T1), log);
            List <string> decomposed = null;

            try
            {
                if (logIncoming)
                {
                    try
                    {
                        var blobWriter = await incomingBatchBinder.BindAsync <CloudBlockBlob>(
                            new BlobAttribute($"transmission-incoming/{batchId}", FileAccess.ReadWrite));

                        await blobWriter.UploadTextAsync(String.Join(",", messages));

                        log.LogInformation($"Logged incoming to blob");
                    }
                    catch (Exception exIncomingBlob)
                    {
                        log.LogError($"Failed to log the incoming transmission blob: {batchId}. {exIncomingBlob.Message}");
                        throw exIncomingBlob;
                    }
                }

                decomposed = azMonMsgs.DecomposeIncomingBatch(messages);
            }
            catch (Exception ex)
            {
                log.LogError("General error in message decompositon:" + ex.Message);
                throw;
            }
            log.LogInformation($"Decomposed {decomposed.Count} messages");

            if (decomposed.Count > 0)
            {
                var splunkMsgs = (SplunkEventMessages)Activator.CreateInstance(typeof(T2), outputEvents, log);
                log.LogInformation($"Splunk message created");

                try
                {
                    splunkMsgs.Ingest(decomposed.ToArray());
                    log.LogInformation($"Splunk message ingested");

                    await splunkMsgs.Emit();

                    log.LogInformation($"Splunk message emmited");
                }
                catch (Exception exEmit)
                {
                    log.LogError("General error in message transmition:" + exEmit.Message);

                    try
                    {
                        var blobWriter = await blobFaultBinder.BindAsync <CloudBlockBlob>(
                            new BlobAttribute($"transmission-faults/{batchId}", FileAccess.ReadWrite));

                        string json = await Task <string> .Factory.StartNew(() => JsonConvert.SerializeObject(splunkMsgs.splunkEventMessages));

                        await blobWriter.UploadTextAsync(json);

                        log.LogInformation($"Logged transmition error to blob");
                    }
                    catch (Exception exFaultBlob)
                    {
                        log.LogError($"Failed to write the fault blob: {batchId}. {exFaultBlob.Message}");
                        throw exFaultBlob;
                    }

                    try
                    {
                        var qMsg = new TransmissionFaultMessage {
                            id = batchId, type = typeof(T2).ToString()
                        };
                        string qMsgJson = JsonConvert.SerializeObject(qMsg);

                        var queueWriter = await queueFaultBinder.BindAsync <CloudQueue>(
                            new QueueAttribute("transmission-faults"));

                        await queueWriter.AddMessageAsync(new CloudQueueMessage(qMsgJson));
                    }
                    catch (Exception exFaultQueue)
                    {
                        log.LogError($"Failed to write the fault queue: {batchId}. {exFaultQueue.Message}");
                        throw exFaultQueue;
                    }

                    log.LogError($"Error emitting messages to output binding: {exEmit.Message}. The messages were held in the fault processor queue for handling once the error is resolved.");
                    throw exEmit;
                }
            }

            log.LogInformation($"C# Event Hub trigger function processed a batch of messages: {messages.Length}");
        }
Пример #2
0
        public async Task Run <T1, T2>(
            string[] messages,
            IBinder blobFaultBinder,
            Binder queueFaultBinder,
            TraceWriter log)
        {
            var           azMonMsgs  = (AzMonMessages)Activator.CreateInstance(typeof(T1), log);
            List <string> decomposed = null;

            try
            {
                decomposed = azMonMsgs.DecomposeIncomingBatch(messages);
            }
            catch (Exception)
            {
                throw;
            }

            if (decomposed.Count > 0)
            {
                var splunkMsgs = (SplunkEventMessages)Activator.CreateInstance(typeof(T2), log);
                try
                {
                    splunkMsgs.Ingest(decomposed.ToArray());
                    await splunkMsgs.Emit();
                }
                catch (Exception exEmit)
                {
                    var id = Guid.NewGuid().ToString();

                    try
                    {
                        var blobWriter = await blobFaultBinder.BindAsync <CloudBlockBlob>(
                            new BlobAttribute($"transmission-faults/{id}", FileAccess.ReadWrite));

                        string json = await Task <string> .Factory.StartNew(() => JsonConvert.SerializeObject(splunkMsgs.splunkEventMessages));

                        await blobWriter.UploadTextAsync(json);
                    }
                    catch (Exception exFaultBlob)
                    {
                        log.Error($"Failed to write the fault blob: {id}. {exFaultBlob.Message}");
                        throw exFaultBlob;
                    }

                    try
                    {
                        var qMsg = new TransmissionFaultMessage {
                            id = id, type = typeof(T2).ToString()
                        };
                        string qMsgJson = JsonConvert.SerializeObject(qMsg);

                        var queueWriter = await queueFaultBinder.BindAsync <CloudQueue>(
                            new QueueAttribute("transmission-faults"));

                        await queueWriter.AddMessageAsync(new CloudQueueMessage(qMsgJson));
                    }
                    catch (Exception exFaultQueue)
                    {
                        log.Error($"Failed to write the fault queue: {id}. {exFaultQueue.Message}");
                        throw exFaultQueue;
                    }

                    log.Error($"Error emitting messages to Splunk HEC: {exEmit.Message}. The messages were held in the fault processor queue for handling once the error is resolved.");
                    throw exEmit;
                }
            }

            log.Info($"C# Event Hub trigger function processed a batch of messages: {messages.Length}");
        }