Example #1
0
        private string AgentArnToSqsQueueUrl(string agentArn)
        {
            _logger.Trace("Beginning AgentArnToSqsQueueUrl");
            try
            {
                if (_numSqsQueues == 1)
                {
                    return(_sqsQueueUrls[0]);
                }

                using (var algorithm = SHA256.Create())
                {
                    var hash = algorithm.ComputeHash(Encoding.UTF8.GetBytes(agentArn));
                    var i    = BitConverter.ToInt64(hash) % _numSqsQueues;
                    if (i < 0)
                    {
                        i += _numSqsQueues;
                    }
                    _logger.Debug($"AgentARN: {agentArn} SQS Queue index: {i}");
                    return(_sqsQueueUrls[i]);
                }
            }
            finally
            {
                _logger.Trace("Ending AgentArnToSqsQueueUrl");
            }
        }
Example #2
0
        public AwsSqsFacade(IAspectLogger logger)
        {
            _logger    = logger;
            _sqsClient = new AmazonSQSClient();
            var numQueuesStr = Environment.GetEnvironmentVariable(NUM_SQS_QUEUES_ENVIRONMENT_VARIABLE_LOOKUP);

            if (string.IsNullOrEmpty(numQueuesStr))
            {
                throw new Exception($"Missing Lambda Variable {NUM_SQS_QUEUES_ENVIRONMENT_VARIABLE_LOOKUP}");
            }
            if (!int.TryParse(numQueuesStr, out _numSqsQueues) || _numSqsQueues < 1)
            {
                throw new Exception($"Invalid Lambda Variable {NUM_SQS_QUEUES_ENVIRONMENT_VARIABLE_LOOKUP}: value must be a positive integer");
            }

            _sqsQueueUrls = new string[_numSqsQueues];

            for (int i = 0; i < _numSqsQueues; ++i)
            {
                _sqsQueueUrls[i] = ParseQueue(i + 1);
                _logger.Debug($"SqsQueueUrl{i+1}: {_sqsQueueUrls[i]}");
            }

            _sqsQueueMessageGroupId = Environment.GetEnvironmentVariable(SQS_QUEUE_MESSAGE_GROUP_ID_ENVIRONMENT_VARIABLE_LOOKUP);
            if (string.IsNullOrEmpty(_sqsQueueMessageGroupId))
            {
                throw new Exception($"Missing Lambda Variable {SQS_QUEUE_MESSAGE_GROUP_ID_ENVIRONMENT_VARIABLE_LOOKUP}");
            }
            _sqsQueueMessageGroupId = _sqsQueueMessageGroupId.Trim();
            _logger.Debug($"SqsQueueMessagGroupId: {_sqsQueueMessageGroupId}");
        }
Example #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");
        }
        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");
        }
        public AwsSqsFacade(IAspectLogger logger)
        {
            _logger    = logger;
            _sqsClient = new AmazonSQSClient();

            _sqsQueueUrl = Environment.GetEnvironmentVariable(SQS_QUEUE_URL_ENVIRONMENT_VARIABLE_LOOKUP);
            var sqsQueueName  = Environment.GetEnvironmentVariable(SQS_QUEUE_NAME_ENVIRONMENT_VARIABLE_LOOKUP);
            var sqsQueueOwner = Environment.GetEnvironmentVariable(SQS_QUEUE_OWNER_ENVIRONMENT_VARIABLE_LOOKUP);

            if (!string.IsNullOrEmpty(_sqsQueueUrl))
            {
                _sqsQueueUrl = _sqsQueueUrl.Trim();
            }
            if (string.IsNullOrEmpty(_sqsQueueUrl))
            {
                if (string.IsNullOrEmpty(sqsQueueName))
                {
                    throw new Exception($"Missing Lambda Variable {SQS_QUEUE_NAME_ENVIRONMENT_VARIABLE_LOOKUP} or {SQS_QUEUE_URL_ENVIRONMENT_VARIABLE_LOOKUP}");
                }
                sqsQueueName = sqsQueueName.Trim();
                _logger.Debug($"SqsQueueName: {sqsQueueName}");
                if (string.IsNullOrEmpty(sqsQueueOwner))
                {
                    throw new Exception($"Missing Lambda Variable {SQS_QUEUE_OWNER_ENVIRONMENT_VARIABLE_LOOKUP} or {SQS_QUEUE_URL_ENVIRONMENT_VARIABLE_LOOKUP}");
                }
                sqsQueueOwner = sqsQueueOwner.Trim();
                _logger.Debug($"SqsQueueOwner: {sqsQueueOwner}");

                var request = new GetQueueUrlRequest
                {
                    QueueName = sqsQueueName,
                    QueueOwnerAWSAccountId = sqsQueueOwner
                }; //NOTE: Throws error if does not exist

                var response = _sqsClient.GetQueueUrlAsync(request).Result;
                _sqsQueueUrl = response.QueueUrl;
            }
            _logger.Debug($"SqsQueueUrl: {_sqsQueueUrl}");

            _sqsQueueMessageGroupId = Environment.GetEnvironmentVariable(SQS_QUEUE_MESSAGE_GROUP_ID_ENVIRONMENT_VARIABLE_LOOKUP);
            if (string.IsNullOrEmpty(_sqsQueueMessageGroupId))
            {
                throw new Exception($"Missing Lambda Variable {SQS_QUEUE_MESSAGE_GROUP_ID_ENVIRONMENT_VARIABLE_LOOKUP}");
            }
            _sqsQueueMessageGroupId = _sqsQueueMessageGroupId.Trim();
            _logger.Debug($"SqsQueueMessagGroupId: {_sqsQueueMessageGroupId}");
        }
        public async Task SendMessageToQueue(string recordData)
        {
            _logger.Trace("Beginning SendMessageToQueue");

            SendMessageRequest sendMessageRequest = new SendMessageRequest
            {
                QueueUrl       = _sqsQueueUrl,
                MessageBody    = recordData,
                MessageGroupId = _sqsQueueMessageGroupId
            };//NOTE:  Enable ContentBasedDeduplication for particular SQSSqueue since not sending MessageDeduplicationId which could use recordData's Event Id

            var sendMessageResponse = await _sqsClient.SendMessageAsync(sendMessageRequest);

            _logger.Debug($"Message sent to queue {sendMessageResponse.MessageId}");

            _logger.Trace("Ending SendMessageToQueue");
        }