public async Task HandleKinesisEventAsync(KinesisEvent kinesisEvent, ILambdaContext context)
        {
            context.Logger.LogLine($"Processing {kinesisEvent.Records.Count} records...");
            var batch = this.dynamo.CreateBatchWrite <EmailAudit>();

            foreach (var record in kinesisEvent.Records)
            {
                var payload  = record.Kinesis.Data.ToArray();
                var contents = Encoding.UTF8.GetString(payload);

                // Send through SES....
                await this.emailService.SendEmailAsync(new SendEmailRequest
                {
                    Destination = new Destination(new List <string> {
                        Environment.GetEnvironmentVariable("EMAIL_TO")
                    }),
                    Message = new Message()
                    {
                        Subject = new Content("My Mail"),
                        Body    = new Body(new Content(contents))
                    }
                });

                // Make a record in Dynamo for auditing purposes...
                batch.AddPutItem(new EmailAudit
                {
                    PartitionKey = DateTime.UtcNow.ToString("yyyy-MM-dd"),
                    SortKey      = record.EventId,
                    Contents     = contents
                });
            }

            // Record in the audit table...
            await batch.ExecuteAsync();
        }
Exemplo n.º 2
0
        private static async Task <IEnumerable <LogData> > ParseLogs(KinesisEvent @event)
        {
            var logs = new List <LogData>();

            foreach (var record in @event.Records)
            {
                var    kinesisRecord = record.Kinesis;
                string payload;
                using (var decompressionStream = new GZipStream(kinesisRecord.Data, CompressionMode.Decompress))
                {
                    using (var streamReader = new StreamReader(decompressionStream))
                        payload = await streamReader.ReadToEndAsync();
                }

                var data = JsonConvert.DeserializeObject <Payload>(payload);
                if (data.MessageType == "CONTROL_MESSAGE")
                {
                    continue;
                }

                var functionName    = GetLambdaName(data.LogGroup);
                var functionVersion = GetLambdaVersion(data.LogStream);
                var awsRegion       = record.AwsRegion;

                logs.AddRange(data.LogEvents.Select(logEvent => ParseLog(functionName, functionVersion, logEvent.Message, awsRegion))
                              .Where(log => log != null));
            }

            return(logs);
        }
Exemplo n.º 3
0
        public async Task AspectKinesisHandler(KinesisEvent kinesisEvent, ILambdaContext context)
        {
            if (_logger == null)
            {
                _logger = new AspectAwsLambdaLogger.AspectAwsLambdaLogger(context.Logger);  //NOTE: Needed for Test or if using CloudWatch and not using NLog->CloudWatch setup in constructor
            }
            if (_sqsFacade == null)
            {
                _sqsFacade = new AwsSqsFacade(_logger); //NOTE: Needed for Test or if using CloudWatch and not using NLog->CloudWatch setup in constructor
            }
            _logger.Debug($"DynamoDbTableName: {_dynamoDbTableName}");
            _logger.Trace("Beginning AspectKinesisHandler");

            _logger.Info($"Record Count: {kinesisEvent.Records.Count}");

            bool writeEventsToQueue = Convert.ToBoolean(Environment.GetEnvironmentVariable(WRITE_EVENTS_TO_QUEUE_ENVIRONMENT_VARIABLE_LOOKUP));

            _logger.Debug($"WriteEventsToQueue: {writeEventsToQueue}");

            foreach (var record in kinesisEvent.Records)
            {
                await ProcessEventRecord(record, writeEventsToQueue);
            }

            _logger.Trace("Ending AspectKinesisHandler");
        }
Exemplo n.º 4
0
        public void TestFunction()
        {
            KinesisEvent evnt = new KinesisEvent
            {
                Records = new List <KinesisEvent.KinesisEventRecord>
                {
                    new KinesisEvent.KinesisEventRecord
                    {
                        AwsRegion = "us-west-2",
                        Kinesis   = new KinesisEvent.Record
                        {
                            ApproximateArrivalTimestamp = DateTime.Now,
                            Data = new MemoryStream(Encoding.UTF8.GetBytes("Hello World Kinesis Record"))
                        }
                    }
                }
            };


            var context  = new TestLambdaContext();
            var function = new Function();

            function.FunctionHandler(evnt, context);

            var testLogger = context.Logger as TestLambdaLogger;

            Assert.Contains("Stream processing complete", testLogger.Buffer.ToString());
        }
        /// <summary>
        /// A simple function that takes a string and does a ToUpper
        /// </summary>
        /// <param name="input"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public void FunctionHandler(KinesisEvent kinesisEvent, ILambdaContext context)
        {
            // Insert / Delete Site table

            SqlConnection oSqlConnection = null;
            int           intSiteID;
            tInput        oInput;

            context.Logger.LogLine("FunctionHandler 1 1725");

            try
            {
                oSqlConnection = new SqlConnection(ecoCommon.GetSecret("CloudEcoPlus", context)); oSqlConnection.Open();
                context.Logger.LogLine("FunctionHandler 2");
            }
            catch (Exception ex)
            {
                context.Logger.LogLine("WriteRecord Ex  1" + ex.Message);
            }

            context.Logger.LogLine("FunctionHandler 3");

            context.Logger.LogLine($"Beginning to process {kinesisEvent.Records.Count} records...");

            foreach (var record in kinesisEvent.Records)
            {
                string recordData = GetRecordContents(record.Kinesis);

                context.Logger.LogLine("FunctionHandler rd  >" + recordData);

                oInput = JsonSerializer.Deserialize <tInput>(recordData);

                if (oInput.MessageName.ToLower() != "site")
                {
                    continue;
                }

                if (oInput.Action == tInput.tAction.UPSERT)
                {
                    intSiteID = oInput.SiteID;

                    if (SiteRecordExists(intSiteID, context, ref oSqlConnection) == true)
                    {
                        UpdateSite(oInput, context, ref oSqlConnection);
                    }
                    else
                    {
                        InsertSite(oInput, context, ref oSqlConnection);
                    }
                }

                if (oInput.Action == tInput.tAction.DELETE)
                {
                    DeleteSite(oInput, context, ref oSqlConnection);
                }
                ;

                context.Logger.LogLine("Stream processing complete.");
            }
        }
Exemplo n.º 6
0
        //--- Methods ---
        public void FunctionHandler(KinesisEvent kinesisEvent, ILambdaContext context)
        {
            context.Logger.LogLine($"Beginning to process {kinesisEvent.Records.Count} records...");
            var streamName = "YOUR STREAM HERE";
            var rows       = new List <string>();

            foreach (var record in kinesisEvent.Records)
            {
                string recordData = GetRecordContents(record.Kinesis);
                var    doc        = XElement.Parse(recordData);
                var    row        = string.Join(",", new [] {
                    doc.Element("method-name")?.Value,
                    doc.Element("elapsed-ms")?.Value,
                    doc.Element("timestamp")?.Value,
                    doc.Element("customer-id")?.Value
                });
                rows.Add(row);
            }
            var skip       = 0;
            var CHUNK_SIZE = 25;

            while (true)
            {
                var chunk = rows.Skip(skip).Take(CHUNK_SIZE).ToArray();
                if (!chunk.Any())
                {
                    break;
                }
                var t = _klient.PutRecordsAsync(new PutRecordsRequest {
                    Records = chunk.Select(row => {
                        using (var m = new MemoryStream(Encoding.UTF8.GetBytes(row + "|"))) {
                            return(new PutRecordsRequestEntry {
                                Data = m,
                                PartitionKey = Guid.NewGuid().ToString()
                            });
                        }
                    }).ToList(),
                    StreamName = streamName
                }, CancellationToken.None);
                Task.WaitAll(new [] { t });
                skip += CHUNK_SIZE;
                var response = t.Result;
                if (response.FailedRecordCount > 0)
                {
                    foreach (var record in response.Records)
                    {
                        if (!string.IsNullOrEmpty(record.ErrorCode) || !string.IsNullOrEmpty(record.ErrorMessage))
                        {
                            context.Logger.LogLine($"errorCode={record.ErrorCode}, errorMessage={record.ErrorMessage}");
                        }
                    }
                }
                else
                {
                    context.Logger.LogLine($"awsRequestId={context.AwsRequestId}, successfully flushed all the messages");
                }
                Thread.Sleep(1000);
            }
            context.Logger.LogLine("Stream processing complete.");
        }
        public async Task AspectKinesisHandler(KinesisEvent kinesisEvent, ILambdaContext context)
        {
            if (_logger == null)
            {
                _logger = new AspectAwsLambdaLogger.AspectAwsLambdaLogger(context.Logger);  //NOTE: Needed for Test or if using CloudWatch and not using NLog->CloudWatch setup in constructor
            }
            if (_sqsFacade == null)
            {
                _sqsFacade = new AwsSqsFacade(_logger); //NOTE: Needed for Test or if using CloudWatch and not using NLog->CloudWatch setup in constructor
            }
            _logger.Debug($"DynamoDbTableName: {_dynamoDbTableName}");
            _logger.Trace("Beginning AspectKinesisHandler");

            _logger.Info($"Record Count: {kinesisEvent.Records.Count}");

            var writeEventsToQueue = await ReadWriteEventsToQueueFlag();

            _logger.Debug($"WriteEventsToQueue: {writeEventsToQueue}");

            foreach (var record in kinesisEvent.Records)
            {
                await ProcessEventRecord(record, writeEventsToQueue);
            }

            _logger.Trace("Ending AspectKinesisHandler");
        }
Exemplo n.º 8
0
        public void FunctionHandler(KinesisEvent kinesisEvent, ILambdaContext context)
        {
            tRevertCommand oRevertCommand;
            DateTime       datRevert;
            diags          oNewDiag = new diags();
            tCommand       oCommandSend;

            context.Logger.LogLine("FunctionHandler 1 1145");

            foreach (var record in kinesisEvent.Records)
            {
                string recordData = GetRecordContents(record.Kinesis);

                oRevertCommand = JsonSerializer.Deserialize <tRevertCommand>(recordData);

                if (oRevertCommand.MessageName.ToLower() != "diagintervalrevert")
                {
                    continue;
                }

                context.Logger.LogLine("FunctionHandler rd  >" + recordData);

                datRevert = ecoCommon.DateJson(oRevertCommand.RevertAfter); // encoded as dd/mm/yyyy hh:mm:ss
                if (DateTime.Compare(DateTime.Now, datRevert) < 0)
                {
                    context.Logger.LogLine("FunctionHandler Not my time " + datRevert.ToString() + " " + DateTime.Now.ToString());

                    context.Logger.LogLine(oRevertCommand.MakeItFail.ToString()); // Property does not exists, deliberately let it fail (cludge) and leave the message on the queue

                    continue;                                                     // Not my time, let the stream represent until ready
                }


                // --Send New values diags to the Eco+ -----------------------------------------------------------------



                oCommandSend = new tCommand();

                oNewDiag.controlInterval = oRevertCommand.SensorInterval;
                oNewDiag.sensorInterval  = oRevertCommand.SensorInterval;

                context.Logger.LogLine("FunctionHandler This is my time, Reverting " + datRevert.ToString() + " " + DateTime.Now.ToString() + " To Interval " + oRevertCommand.SensorInterval.ToString());

                oCommandSend.CommandName = "diags";
                oCommandSend.UrlPath     = "diags/{devid}";
                oCommandSend.CommandJson = JsonSerializer.Serialize(oNewDiag);
                oCommandSend.ToEco       = true;

                tCommand oControlreportSetReply = new tCommand();

                oControlreportSetReply = PostApi(oCommandSend, oRevertCommand.IMEI);

                // -- End Command -----------------------------------------------------------------
            }
        }
Exemplo n.º 9
0
        public async Task FunctionHandler(KinesisEvent kinesisEvent, ILambdaContext context)
        {
            foreach (var record in kinesisEvent.Records)
            {
                string recordData = GetRecordContents(record.Kinesis);

                await SaveObjectToMongoDb(recordData).ConfigureAwait(false);
                await SaveImageToMongoDb(recordData).ConfigureAwait(false);
            }
        }
Exemplo n.º 10
0
        public async Task FunctionHandler(KinesisEvent kinesisEvent, ILambdaContext context)
        {
            context.Logger.LogLine($"Beginning to process {kinesisEvent.Records.Count} records...");

            var table    = Table.LoadTable(_dynamoDb, Constants.TableName);
            var scanResp = await _dynamoDb.ScanAsync(new ScanRequest()
            {
                TableName            = Constants.TableName,
                ProjectionExpression = Constants.ConnectionIdField
            });

            foreach (var record in kinesisEvent.Records)
            {
                context.Logger.LogLine($"Event ID: {record.EventId}");
                context.Logger.LogLine($"Event Name: {record.EventName}");

                string recordData = GetRecordContents(record.Kinesis);
                context.Logger.LogLine($"Record Data:");
                context.Logger.LogLine(recordData);

                var stream = new MemoryStream(UTF8Encoding.UTF8.GetBytes(recordData));

                foreach (var item in scanResp.Items)
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    stream.Position = 0;

                    var connectionId          = item[Constants.ConnectionIdField].S;
                    var postConnectionRequest = new PostToConnectionRequest()
                    {
                        ConnectionId = connectionId,
                        Data         = stream
                    };

                    try
                    {
                        await _apiClient.PostToConnectionAsync(postConnectionRequest);
                    }
                    catch (AmazonServiceException e)
                    {
                        if (e.StatusCode == HttpStatusCode.Gone)
                        {
                            await table.DeleteItemAsync(connectionId);
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }

            context.Logger.LogLine("Stream processing complete.");
        }
Exemplo n.º 11
0
 private void Handle(KinesisEvent kinesisEvent)
 {
     foreach (var record in kinesisEvent.Records)
     {
         var kinesisRecord = record.Kinesis;
         var dataBytes     = kinesisRecord.Data.ToArray();
         var dataText      = Encoding.UTF8.GetString(dataBytes);
         Assert.Equal("Hello World", dataText);
         Console.WriteLine($"[{record.EventName}] Data = '{dataText}'.");
     }
 }
Exemplo n.º 12
0
        public async Task <string> FunctionHandler(KinesisEvent kinesisEvent)
        {
            foreach (var record in kinesisEvent.Records)
            {
                LambdaLogger.Log(GetRecordContents(record.Kinesis));
            }

            var builder = PooledQldbDriver.Builder();
            var driver  = builder.WithLedger("demo-ledger-1").Build();
            var session = driver.GetSession();

            return(kinesisEvent.ToString());
        }
Exemplo n.º 13
0
        protected override async Task Handle(KinesisEvent kinesisEvent, ILambdaContext context)
        {
            Logger.Info($"Processing Kinesis Event With {kinesisEvent.Records?.Count} records.");

            if (kinesisEvent?.Records == null)
            {
                return;
            }
            if (kinesisEvent.Records.Count == 0)
            {
                return;
            }

            foreach (var record in kinesisEvent.Records)
            {
                Logger.Info($"Processing Kinesis Event message ID {record.EventId}.");

                try
                {
                    Logger.Info("Reading Stream Content.");

                    using var reader = new StreamReader(record.Kinesis.Data, Encoding.UTF8);

                    var content = await reader.ReadToEndAsync();

                    Logger.Info("Stream Content Read.");

                    Logger.Info("Creating DataStream Message.");

                    var message = DeserializeBody(content, record.Kinesis.PartitionKey);

                    if (message != null)
                    {
                        message.EventId = record.EventId;
                        message.ApproximateArrivalTimestamp = record.Kinesis.ApproximateArrivalTimestamp;
                    }

                    Logger.Info("Invoking ProcessMessage.");

                    await ProcessMessage(message);
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, "Error Processing Message from Kinesis Event. Developer, you should take care of it!. Message: Id={EventId}, PartitionKey= {PartitionKey}",
                                 record.EventId, record.Kinesis.PartitionKey);
                    throw;
                }
            }
        }
        //--- Methods ---
        public void FunctionHandler(KinesisEvent kinesisEvent, ILambdaContext context)
        {
            context.Logger.LogLine($"Beginning to process {kinesisEvent.Records.Count} records...");
            var streamName = "YOUR STREAM NAME HERE";

            foreach (var record in kinesisEvent.Records)
            {
                context.Logger.LogLine($"Event ID: {record.EventId}");
                context.Logger.LogLine($"Event Name: {record.EventName}");

                string recordData = GetRecordContents(record.Kinesis);
                context.Logger.LogLine($"Record Data:");
                context.Logger.LogLine(recordData);
            }
        }
Exemplo n.º 15
0
 protected virtual async Task Ship(KinesisEvent @event)
 {
     try
     {
         var logs = (await ParseLogs(@event)).ToArray();
         if (logs.Any())
         {
             await Task.WhenAll(logs.Select(log => _logger(log)));
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(JsonConvert.SerializeObject(ex));
     }
 }
        public async Task FunctionHandlerAsync(KinesisEvent kinesisEvent, ILambdaContext lambdaContext)
        {
            var context = new StreamRequestContext
            {
                AppName           = lambdaContext.FunctionName,
                Version           = lambdaContext.FunctionVersion,
                SystemInformation = new
                {
                    lambdaRequestId = lambdaContext.AwsRequestId,
                    sourceType      = "LAMBDA_FUNCTION"
                }
            };

            _kinesisEventProcessor.ProcessKinesisEvent(kinesisEvent, context);
        }
Exemplo n.º 17
0
        public void FunctionHandler(KinesisEvent kinesisEvent, ILambdaContext context)
        {
            context.Logger.LogLine($"Beginning to process {kinesisEvent.Records.Count} records...");

            foreach (var record in kinesisEvent.Records)
            {
                context.Logger.LogLine($"Event ID: {record.EventId}");
                context.Logger.LogLine($"Event Name: {record.EventName}");

                string recordData = GetRecordContents(record.Kinesis);
                context.Logger.LogLine($"Record Data:");
                context.Logger.LogLine(recordData);
            }

            context.Logger.LogLine("Stream processing complete.");
        }
        public async Task FunctionHandler(KinesisEvent kinesisEvent, ILambdaContext context)
        {
            context.Logger.LogLine($"Beginning to process {kinesisEvent.Records.Count} records...");

            foreach (var record in kinesisEvent.Records)
            {
                string recordContents = GetRecordContents(record.Kinesis);
                var    recordData     = JsonConvert.DeserializeObject <Event>(recordContents);
                if (IsGoodParaglidingWeather(recordData))
                {
                    context.Logger.LogLine($"Found excellent paragliding weather!: {recordContents}");
                    await _snsClient.PublishAsync(SNS_ARN, $"Holy shit we are good to fly!! Travel to {recordData.location.areaDescription} at approximately {recordData.timestamp}");
                }
            }
            context.Logger.LogLine("Stream processing complete.");
        }
Exemplo n.º 19
0
        public async Task Execute(KinesisEvent kinesisEvent)
        {
            _logger.Information($"Processing {kinesisEvent.Records.Count} records.");

            var tollAmountMessages = GetRecordContents <TollAmountMessage>(kinesisEvent.Records);
            var customerIds        = tollAmountMessages.Select(t => t.CustomerId);

            try
            {
                var billingAlerts = (await _billingAlertStore.Get(customerIds))?.ToList();

                if (billingAlerts == null)
                {
                    billingAlerts = new List <BillingAlertItem>();
                }

                var alertsToPublish = new List <AlertMessage>();

                foreach (var billingAlert in billingAlerts)
                {
                    var tollAmountMessage = tollAmountMessages.FirstOrDefault(t => t.CustomerId == billingAlert.CustomerId);
                    billingAlert.TotalBillAmount      += tollAmountMessage.TollAmount;
                    billingAlert.BillAmountLastUpdated = DateTime.UtcNow;

                    if (ShouldAlert(billingAlert))
                    {
                        alertsToPublish.Add(new AlertMessage
                        {
                            CustomerId           = billingAlert.CustomerId,
                            TotalBillAmount      = billingAlert.TotalBillAmount,
                            AlertAmountThreshold = billingAlert.AlertAmountThreshold,
                            Message = DefaultAlertMessage(billingAlert)
                        });
                    }
                }

                await _messagePublisher.Publish(alertsToPublish);

                billingAlerts.ForEach(b => { b.IsAlerted = b.IsAlerted || alertsToPublish.Any(a => a.CustomerId == b.CustomerId); });
                await _billingAlertStore.Put(billingAlerts);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, ex.Message);
            }
        }
Exemplo n.º 20
0
 public void ProcessKinesisEvent(KinesisEvent kinesisEvent, StreamRequestContext context)
 {
     try
     {
         foreach (var record in kinesisEvent.Records)
         {
             ProcessRecord(context, record);
         }
     }
     catch (BatchProcessingException batchEx)
     {
         throw;
     }
     catch (Exception ex)
     {
         throw;
     }
 }
        public void FunctionHandler(KinesisEvent kinesisEvent, ILambdaContext context)
        {
            var totalReceived = kinesisEvent.Records.Count;

            context.Logger.LogLine($"===Init===, received{totalReceived}");

            if (kinesisEvent != null && kinesisEvent.Records != null && kinesisEvent.Records.Any())
            {
                var records = kinesisEvent.Records.Select(r => new Record(r.Kinesis.Data, new Dictionary <string, object> {
                    ["functionName"] = context.FunctionName
                }));
                var bisectDecorator = new BisectExtensionConcreteDecorator(new ConcreteCompMyProcessing(), new BisectConfig(), totalReceived, new CloudWatchShardMetrics());
                bisectDecorator.Handle(records);
            }
            else
            {
                context.Logger.LogLine("===No KinesisEvent records available");
            }
        }
Exemplo n.º 22
0
        private static KinesisEvent GenerateKinesisEvent(string recordData)
        {
            KinesisEvent evnt = new KinesisEvent
            {
                Records = new List <KinesisEvent.KinesisEventRecord>
                {
                    new KinesisEvent.KinesisEventRecord
                    {
                        AwsRegion = "us-east-1",
                        Kinesis   = new KinesisEvent.Record
                        {
                            ApproximateArrivalTimestamp = DateTime.Now,
                            Data = new MemoryStream(Encoding.UTF8.GetBytes(recordData))
                        }
                    }
                }
            };

            return(evnt);
        }
Exemplo n.º 23
0
        protected override async Task Handle(KinesisEvent message, ILambdaContext context)
        {
            Logger.Info($"Processing Kinesis Event With {message?.Records?.Count} records.");

            if (message?.Records == null)
            {
                return;
            }
            if (message.Records.Count == 0)
            {
                return;
            }

            using var activity = EventProcessorActivitySource.StartActivity(nameof(Handle));
            foreach (var record in message.Records)
            {
                Logger.Info($"Processing Kinesis Event message ID {record.EventId}.");

                var body = await base.ParseRecord <DataStream <TBody> >(record).ConfigureAwait(false);

                if (body?.TraceId != null && activity != null)
                {
                    activity.SetParentId(body.TraceId);
                }

                Logger.Info("Invoking ProcessMessage.");

                using var processActivity = EventProcessorActivitySource.StartActivity(nameof(ProcessMessage));
                processActivity?.SetTag("KinesisEventId", record?.EventId);
                processActivity?.SetTag("KinesisEventName", record?.EventName);
                processActivity?.SetTag("KinesisPartitionKey", record?.Kinesis?.PartitionKey);
                processActivity?.SetTag("KinesisApproximateArrivalTimestamp", record?.Kinesis?.ApproximateArrivalTimestamp);
                processActivity?.SetTag("BodyEventId", body?.EventId);
                processActivity?.SetTag("BodyPartition", body?.Partition);
                processActivity?.SetTag("BodyVersion", body?.Version);
                processActivity?.SetTag("BodyVersion", body?.TraceId);

                await ProcessMessage(body).ConfigureAwait(false);
            }
        }
Exemplo n.º 24
0
        protected override async Task Handle(KinesisEvent message, ILambdaContext context)
        {
            Logger.Info($"Processing Kinesis Event With {message?.Records?.Count} records.");

            if (message?.Records == null)
            {
                return;
            }
            if (message.Records.Count == 0)
            {
                return;
            }

            using var activity = EventProcessorActivitySource.StartActivity(nameof(Handle));
            foreach (var record in message.Records)
            {
                Logger.Info($"Processing Kinesis Event message ID {record.EventId}.");

                try
                {
                    Logger.Info("Reading Stream Content.");

                    var body = await ParseRecord <TBody>(record).ConfigureAwait(false);

                    if (body?.TraceId != null && activity != null)
                    {
                        activity.SetParentId(body.TraceId);
                    }

                    await ProcessMessage(body).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, "Error Processing Message from Kinesis Event. Developer, you should take care of it!. Message: Id={EventId}, PartitionKey= {PartitionKey}",
                                 record.EventId, record.Kinesis.PartitionKey);
                    throw;
                }
            }
        }
Exemplo n.º 25
0
 public void FunctionHandler(KinesisEvent kinesisEvent, ILambdaContext context)
 {
     try
     {
         context.Logger.LogLine(string.Format("Beginning to process {0} records...", (object)kinesisEvent.Records.Count));
         foreach (KinesisEvent.KinesisEventRecord record in (IEnumerable <KinesisEvent.KinesisEventRecord>)kinesisEvent.Records)
         {
             context.Logger.LogLine(string.Format("Event ID: {0}", (object)record.EventId));
             context.Logger.LogLine(string.Format("Event Name: {0}", (object)record.EventName));
             string recordContents = this.GetRecordContents(record.Kinesis);
             context.Logger.LogLine("Record Data:");
             context.Logger.LogLine(recordContents);
             this.AddRecord(JsonConvert.DeserializeObject <Student>(recordContents));
             context.Logger.LogLine(this.databaseResponse);
         }
         context.Logger.LogLine("Stream processing complete.");
     }
     catch (Exception ex)
     {
         context.Logger.LogLine(ex.Message);
     }
 }
        private KinesisEvent GetKinesisEvent(List <TollAmountMessage> tollAmountMessages)
        {
            KinesisEvent kinesisEvent = null;

            foreach (var message in tollAmountMessages)
            {
                var jsonStringMessage = JsonSerializer.Serialize(message);
                kinesisEvent = new KinesisEvent
                {
                    Records = new List <KinesisEventRecord>
                    {
                        new KinesisEventRecord {
                            Kinesis = new KinesisEvent.Record {
                                Data = new MemoryStream(Encoding.ASCII.GetBytes(jsonStringMessage))
                            }
                        }
                    }
                };
            }

            return(kinesisEvent);
        }
        public async Task Process(KinesisEvent kinesisEvent)
        {
            foreach (var record in kinesisEvent.Records)
            {
                using (var sr = new StreamReader(record.Kinesis.Data))
                {
                    var message = sr.ReadToEnd();

                    Event @event = null;
                    try
                    {
                        @event = JsonConvert.DeserializeObject <Event>(message);

                        if (@event?.Message == null)
                        {
                            await MarkAsDeadLetter(@event, "EventStreamProcessor", new Exception("Invalid Message in Event"));

                            continue;
                        }
                    }
                    catch (JsonException jsonException)
                    {
                        await MarkAsDeadLetter(@event, "EventStreamProcessor", jsonException);

                        continue;
                    }

                    try
                    {
                        await _businessEventStore.PutEvent(@event);
                    }
                    catch (Exception e)
                    {
                        await MarkAsDeadLetter(@event, "EventStreamProcessor", e);
                    }
                }
            }
            ;
        }
        protected override async Task Handle(KinesisEvent message, ILambdaContext context)
        {
            Logger.Info($"Processing Kinesis Event With {message?.Records?.Count} records.");

            if (message?.Records == null)
            {
                return;
            }
            if (message.Records.Count == 0)
            {
                return;
            }

            Logger.Info($"Processing Kinesis Event With {message?.Records?.Count} records.");
            var batchMessages = await CreateBatchMessages(message.Records).ConfigureAwait(false);

            Logger.Info($"Processing Kinesis Event With {message?.Records?.Count} records.");

            using var activity = EventProcessorActivitySource.StartActivity(nameof(ProcessMessage));
            activity?.SetTag("BatchMessagesCount", batchMessages.Count);

            await ProcessMessage(batchMessages).ConfigureAwait(false);
        }
        public async Task Process(KinesisEvent ev, ILambdaContext context)
        {
            try
            {
                await PreparePubSubMessage(ev);

                var rawData = await _messageExtractor.ExtractEventBody(ev);

                await HandleEvent(rawData, context);
                await HandleRawEvent(ev, context);
            }
            catch (Exception exception)
            {
                LogError(ev, exception, context);

                var result = await HandleError(ev, context, exception);

                if (result == HandleErrorResult.Throw)
                {
                    throw;
                }
            }
        }
Exemplo n.º 30
0
        /// <summary>
        /// Kinesis Stream からイベントを受け取り、Kinesis Streamへデータを書き込む関数です
        /// ローカル開発環境として Kinesalite を使用しています
        /// </summary>
        /// <param name="kinesisEvent"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task FunctionHandler(KinesisEvent kinesisEvent, ILambdaContext context)
        {
            context.Logger.LogLine($"Beginning to process {kinesisEvent.Records.Count} records...");

            // Kinesis Eventからレコードを取得し、ループ処理を行う
            foreach (var record in kinesisEvent.Records)
            {
                context.Logger.LogLine($"Event ID: {record.EventId}");
                context.Logger.LogLine($"Event Name: {record.EventName}");

                var recordData = GetRecordContents(record.Kinesis);
                context.Logger.LogLine($"Record Data:");
                context.Logger.LogLine(recordData);
            }

            #region ローカル開発用のコード
            // Kinesis Stream の作成, 通常は AWSコンソール より作成する
            var request = new CreateStreamRequest
            {
                ShardCount = 1,
                StreamName = _streamName
            };

            var client = new AmazonKinesisClient(new AmazonKinesisConfig
            {
                ServiceURL = _serviceURL
            });

            var response = await client.ListStreamsAsync();

            if (!response.StreamNames.Any(_ => _ == _streamName))
            {
                await client.CreateStreamAsync(request);
            }
            #endregion

            // Kinesis Stream に対してデータを書き込む
            foreach (var i in Enumerable.Range(1, 10))
            {
                using (var memory = new MemoryStream(Encoding.UTF8.GetBytes($"Put Data:{i}")))
                {
                    try
                    {
                        var req = new PutRecordRequest
                        {
                            StreamName   = _streamName,
                            PartitionKey = "url-response-times",
                            Data         = memory
                        };

                        var res = await client.PutRecordAsync(req);

                        context.Logger.LogLine($"Successfully sent record to Kinesis. Sequence number: {res.SequenceNumber}.");
                    }
                    catch (Exception ex)
                    {
                        context.Logger.LogLine($"Failed to send record to Kinesis. Exception: {ex.Message}.");
                    }
                }
                context.Logger.LogLine("Stream processing complete.");
            }
        }