public Task SendPayloadAsync(EventsPayload payload) { var str = $"c:\\temp\\file-{Guid.NewGuid().ToString("n")}"; var msgPackBytes = payload.ToArray(); File.WriteAllBytes(str + ".mpack", msgPackBytes); var json = Vendors.MessagePack.MessagePackSerializer.ToJson(msgPackBytes); File.WriteAllText(str + ".json", json); return(Task.CompletedTask); }
public async Task SendPayloadAsync(EventsPayload payload) { var numberOfTraces = payload.Count; var tracesEndpoint = payload.Url; // retry up to 5 times with exponential back-off const int retryLimit = 5; var retryCount = 1; var sleepDuration = 100; // in milliseconds var payloadMimeType = MimeTypes.MsgPack; var payloadBytes = payload.ToArray(); Log.Information($"Sending ({numberOfTraces} events) {payloadBytes.Length.ToString("N0")} bytes..."); while (true) { IApiRequest request; try { request = _apiRequestFactory.Create(tracesEndpoint); request.AddHeader(ApiKeyHeader, CIVisibility.Settings.ApiKey); } catch (Exception ex) { Log.Error(ex, "An error occurred while generating http request to send events to {AgentEndpoint}", _apiRequestFactory.Info(tracesEndpoint)); return; } bool success = false; Exception exception = null; bool isFinalTry = retryCount >= retryLimit; try { success = await SendPayloadAsync(new ArraySegment <byte>(payloadBytes), payloadMimeType, request, isFinalTry).ConfigureAwait(false); } catch (Exception ex) { exception = ex; if (_globalSettings.DebugEnabled) { if (ex.InnerException is InvalidOperationException ioe) { Log.Error <int, string>(ex, "An error occurred while sending {Count} events to {AgentEndpoint}", numberOfTraces, _apiRequestFactory.Info(tracesEndpoint)); return; } } } // Error handling block if (!success) { if (isFinalTry) { // stop retrying Log.Error <int, string>(exception, "An error occurred while sending {Count} events to {AgentEndpoint}", numberOfTraces, _apiRequestFactory.Info(tracesEndpoint)); return; } // Before retry delay bool isSocketException = false; Exception innerException = exception; while (innerException != null) { if (innerException is SocketException) { isSocketException = true; break; } innerException = innerException.InnerException; } if (isSocketException) { Log.Debug(exception, "Unable to communicate with {AgentEndpoint}", _apiRequestFactory.Info(tracesEndpoint)); } // Execute retry delay await Task.Delay(sleepDuration).ConfigureAwait(false); retryCount++; sleepDuration *= 2; continue; } Log.Debug <int, string>("Successfully sent {Count} events to {AgentEndpoint}", numberOfTraces, _apiRequestFactory.Info(tracesEndpoint)); return; } }