public async Task <string> CreateMessages([FromBody] string[] messages) { var queueUrl = await QueueUrl.Build(_client, QueueName, _sqsConfigParam.QueueAttributes); var sendMessageBatchRequest = new SendMessageBatchRequest { Entries = messages .Select((message, index) => new SendMessageBatchRequestEntry(index.ToString(), message)).ToList(), QueueUrl = queueUrl.Value }; var response = await _client.SendMessageBatchAsync(sendMessageBatchRequest); if (response.HttpStatusCode == HttpStatusCode.OK) { var result = new { StatusCode = "正常終了", Message = "SQSへメッセージの登録に成功しました。", SuccessfulResponse = response.Successful, FailedResponse = response.Failed }; return(JsonConvert.SerializeObject(result)); } else { var result = new { StatusCode = "エラー", Message = "SQSへメッセージの登録に失敗しました。" }; return(JsonConvert.SerializeObject(result)); } }
private void OnSendStart(KeyValuePair <string, object> kv) { var currentSegment = ApmAgent.GetCurrentExecutionSegment(); if (currentSegment is null) { Logger.Trace()?.Log("No current transaction or span - exiting"); return; } if (!(kv.Value is Activity activity)) { Logger.Trace()?.Log("Value is not an activity - exiting"); return; } string queueName = null; string destinationAddress = null; var urlTag = activity.Tags.FirstOrDefault(t => t.Key == "url").Value; if (!string.IsNullOrEmpty(urlTag)) { var queueUrl = new QueueUrl(urlTag); queueName = queueUrl.QueueName; destinationAddress = queueUrl.FullyQualifiedNamespace; } if (MatchesIgnoreMessageQueues(queueName)) { return; } var spanName = queueName is null ? $"{AzureQueueStorage.SpanName} SEND" : $"{AzureQueueStorage.SpanName} SEND to {queueName}"; var span = currentSegment.StartSpan(spanName, ApiConstants.TypeMessaging, AzureQueueStorage.SubType, "send"); span.Context.Destination = new Destination { Address = destinationAddress, Service = new Destination.DestinationService { Name = AzureQueueStorage.SubType, Resource = queueName is null ? AzureQueueStorage.SubType : $"{AzureQueueStorage.SubType}/{queueName}", Type = ApiConstants.TypeMessaging } }; if (!_processingSegments.TryAdd(activity.Id, span)) { Logger.Trace() ?.Log( "Could not add {Action} span {SpanId} for activity {ActivityId} to tracked spans", "SEND", span.Id, activity.Id); } }
public async Task <string> CreateMessage([FromBody] string message) { var queueUrl = await QueueUrl.Build(_client, QueueName, _sqsConfigParam.QueueAttributes); var request = new SendMessageRequest { QueueUrl = queueUrl.Value, MessageBody = message }; var response = await _client.SendMessageAsync(request); if (response.HttpStatusCode == HttpStatusCode.OK) { var result = new { StatusCode = "正常終了", Message = "SQSへメッセージの登録に成功しました。" }; return(JsonConvert.SerializeObject(result)); } else { var result = new { StatusCode = "エラー", Message = "SQSへメッセージの登録に失敗しました。" }; return(JsonConvert.SerializeObject(result)); } }
public async Task <string> ReceiveMessages() { var queueUrl = await QueueUrl.Build(_client, QueueName, _sqsConfigParam.QueueAttributes); var receiveMessageRequest = new ReceiveMessageRequest { QueueUrl = queueUrl.Value }; var response = await _client.ReceiveMessageAsync(receiveMessageRequest); if (response.HttpStatusCode == HttpStatusCode.OK) { var result = new { StatusCode = "正常終了", Message = "SQSへメッセージの受信に成功しました。", QueueMessages = response.Messages, }; return(JsonConvert.SerializeObject(result)); } else { var result = new { StatusCode = "エラー", Message = $"SQSへメッセージの受信に失敗しました。[{response.HttpStatusCode}]" }; return(JsonConvert.SerializeObject(result)); } }
private void OnSendStart(KeyValuePair <string, object> kv) { var currentSegment = ApmAgent.GetCurrentExecutionSegment(); if (currentSegment is null) { Logger.Trace()?.Log("No current transaction or span - exiting"); return; } if (!(kv.Value is Activity activity)) { Logger.Trace()?.Log("Value is not an activity - exiting"); return; } string queueName = null; string destinationAddress = null; var urlTag = activity.Tags.FirstOrDefault(t => t.Key == "url").Value; if (QueueUrl.TryCreate(urlTag, out var queueUrl)) { queueName = queueUrl.QueueName; destinationAddress = queueUrl.FullyQualifiedNamespace; } if (MatchesIgnoreMessageQueues(queueName)) { return; } var spanName = queueName is null ? $"{AzureQueueStorage.SpanName} SEND" : $"{AzureQueueStorage.SpanName} SEND to {queueName}"; var span = currentSegment.StartSpan(spanName, ApiConstants.TypeMessaging, AzureQueueStorage.SubType, "send", true); if (span is Span realSpan) { realSpan.InstrumentationFlag = InstrumentationFlag.Azure; } if (queueUrl != null) { SetDestination(span, destinationAddress, queueName); } if (!_processingSegments.TryAdd(activity.Id, span)) { Logger.Trace() ?.Log( "Could not add {Action} span {SpanId} for activity {ActivityId} to tracked spans", "SEND", span.Id, activity.Id); } }
public static bool TryCreate(string url, out QueueUrl queueUrl) { if (Uri.TryCreate(url, UriKind.Absolute, out var uri)) { queueUrl = new QueueUrl(uri); return(true); } queueUrl = null; return(false); }
public async Task Queueが存在するときBuildにより指定したQueue名のURLが取得できること() { var response = new GetQueueUrlResponse(); response.QueueUrl = "hoge"; _clientMock.Setup(c => c.GetQueueUrlAsync(It.IsAny <string>(), CancellationToken.None)) .ReturnsAsync(response); var actual = await QueueUrl.Build(_clientMock.Object, "test", new Dictionary <string, string>()); Assert.NotNull(actual); Assert.Equal(response.QueueUrl, actual.Value); }
public async Task <IActionResult> DeleteMessage() { var queueUrl = await QueueUrl.Build(_client, QueueName, _sqsConfigParam.QueueAttributes); var receiveMessageRequest = new ReceiveMessageRequest { QueueUrl = queueUrl.Value }; var response = await _client.ReceiveMessageAsync(receiveMessageRequest); var deleteResults = new List <dynamic>(); foreach (var message in response.Messages) { _logger.LogDebug($"messageId:{message.MessageId} / ReceiptHandle:{message.ReceiptHandle}"); var deleteRequest = new DeleteMessageRequest { QueueUrl = queueUrl.Value, ReceiptHandle = message.ReceiptHandle }; var deleteResponse = await _client.DeleteMessageAsync(deleteRequest); if (deleteResponse.HttpStatusCode == HttpStatusCode.OK) { deleteResults.Add(new { MessageId = message.MessageId, Status = "success" }); } else { deleteResults.Add(new { MessageId = message.MessageId, Status = "fail" }); } } if (deleteResults.All(result => result.Status == "success")) { var result = new { StatusCode = "正常終了", Message = "SQSへメッセージの削除に成功しました。", }; return(Ok(JsonConvert.SerializeObject(result))); } else { return(new StatusCodeResult(500)); } }
public async Task Queueが存在しないときBuildにより指定したQueue名のQueueが作成されURLが取得できること() { _clientMock.Setup(c => c.GetQueueUrlAsync(It.IsAny <string>(), CancellationToken.None)) .Throws(new QueueDoesNotExistException("test_message")); var createQueueResponse = new CreateQueueResponse() { QueueUrl = "fuga" }; _clientMock.Setup(c => c.CreateQueueAsync(It.IsAny <CreateQueueRequest>(), CancellationToken.None)) .ReturnsAsync(createQueueResponse); var actual = await QueueUrl.Build(_clientMock.Object, "test", new Dictionary <string, string>()); Assert.NotNull(actual); Assert.Equal(createQueueResponse.QueueUrl, actual.Value); }