protected override async Task <Option <IEnumerable <ModuleLogsResponse> > > HandleRequestInternal(Option <ModuleLogsRequest> payloadOption, CancellationToken cancellationToken)
        {
            ModuleLogsRequest payload = payloadOption.Expect(() => new ArgumentException("Request payload not found"));

            if (ExpectedSchemaVersion.CompareMajorVersion(payload.SchemaVersion, "logs upload request schema") != 0)
            {
                Events.MismatchedMinorVersions(payload.SchemaVersion, ExpectedSchemaVersion);
            }

            Events.ProcessingRequest(payload);

            ILogsRequestToOptionsMapper requestToOptionsMapper = new LogsRequestToOptionsMapper(
                this.runtimeInfoProvider,
                payload.Encoding,
                payload.ContentType,
                LogOutputFraming.None,
                Option.None <LogsOutputGroupingConfig>(),
                false);

            IList <(string id, ModuleLogOptions logOptions)> logOptionsList = await requestToOptionsMapper.MapToLogOptions(payload.Items, cancellationToken);

            int messageSize = 0;
            IEnumerable <Task <ModuleLogsResponse> > uploadLogsTasks = logOptionsList.Select(
                async l =>
            {
                byte[] moduleLogs = await this.logsProvider.GetLogs(l.id, l.logOptions, cancellationToken);

                Events.ReceivedModuleLogs(moduleLogs, l.id);

                if (l.logOptions.ContentEncoding == LogsContentEncoding.Gzip)
                {
                    Interlocked.Add(ref messageSize, moduleLogs.Length);
                    return(new ModuleLogsResponse(l.id, moduleLogs));
                }
                else
                {
                    string encodedLogs = moduleLogs.FromBytes();
                    Interlocked.Add(ref messageSize, encodedLogs.Length);

                    return(new ModuleLogsResponse(l.id, encodedLogs));
                }
            });

            IEnumerable <ModuleLogsResponse> response = await Task.WhenAll(uploadLogsTasks);

            if (messageSize > MaxPayloadSize)
            {
                string message = Events.LargePayload(messageSize, logOptionsList.Select(o => o.logOptions));
                throw new ArgumentException(message);
            }

            return(Option.Some(response));
        }
        protected override async Task <Option <IEnumerable <ModuleLogsResponse> > > HandleRequestInternal(Option <ModuleLogsRequest> payloadOption, CancellationToken cancellationToken)
        {
            ModuleLogsRequest payload = payloadOption.Expect(() => new ArgumentException("Request payload not found"));

            if (ExpectedSchemaVersion.CompareMajorVersion(payload.SchemaVersion, "logs upload request schema") != 0)
            {
                Events.MismatchedMinorVersions(payload.SchemaVersion, ExpectedSchemaVersion);
            }

            Events.ProcessingRequest(payload);

            ILogsRequestToOptionsMapper requestToOptionsMapper = new LogsRequestToOptionsMapper(
                this.runtimeInfoProvider,
                payload.Encoding,
                payload.ContentType,
                LogOutputFraming.None,
                Option.None <LogsOutputGroupingConfig>(),
                false);

            IList <(string id, ModuleLogOptions logOptions)> logOptionsList = await requestToOptionsMapper.MapToLogOptions(payload.Items, cancellationToken);

            IEnumerable <Task <ModuleLogsResponse> > uploadLogsTasks = logOptionsList.Select(
                async l =>
            {
                Events.ReceivedLogOptions(l);
                ModuleLogOptions logOptions = l.logOptions.Filter.Tail
                                              .Filter(t => t < MaxTailValue)
                                              .Map(t => l.logOptions)
                                              .GetOrElse(
                    () =>
                {
                    var filter = new ModuleLogFilter(Option.Some(MaxTailValue), l.logOptions.Filter.Since, l.logOptions.Filter.Until, l.logOptions.Filter.LogLevel, l.logOptions.Filter.RegexString);
                    return(new ModuleLogOptions(l.logOptions.ContentEncoding, l.logOptions.ContentType, filter, l.logOptions.OutputFraming, l.logOptions.OutputGroupingConfig, l.logOptions.Follow));
                });

                byte[] moduleLogs = await this.logsProvider.GetLogs(l.id, logOptions, cancellationToken);

                Events.ReceivedModuleLogs(moduleLogs, l.id);
                return(logOptions.ContentEncoding == LogsContentEncoding.Gzip
                        ? new ModuleLogsResponse(l.id, moduleLogs)
                        : new ModuleLogsResponse(l.id, moduleLogs.FromBytes()));
            });
            IEnumerable <ModuleLogsResponse> response = await Task.WhenAll(uploadLogsTasks);

            return(Option.Some(response));
        }
Example #3
0
        protected override async Task <Option <object> > HandleRequestInternal(Option <LogsUploadRequest> payloadOption, CancellationToken cancellationToken)
        {
            LogsUploadRequest payload = payloadOption.Expect(() => new ArgumentException("Request payload not found"));

            ILogsRequestToOptionsMapper requestToOptionsMapper = new LogsRequestToOptionsMapper(
                this.runtimeInfoProvider,
                payload.Encoding,
                payload.ContentType,
                LogOutputFraming.None,
                Option.Some(new LogsOutputGroupingConfig(100, TimeSpan.FromSeconds(10))),
                false);
            IList <(string id, ModuleLogOptions logOptions)> logOptionsList = await requestToOptionsMapper.MapToLogOptions(payload.Items, cancellationToken);

            IEnumerable <Task> uploadLogsTasks = logOptionsList.Select(l => this.UploadLogs(payload.SasUrl, l.id, l.logOptions, cancellationToken));
            await Task.WhenAll(uploadLogsTasks);

            return(Option.None <object>());
        }
Example #4
0
        protected override async Task <Option <TaskStatusResponse> > HandleRequestInternal(Option <LogsUploadRequest> payloadOption, CancellationToken cancellationToken)
        {
            LogsUploadRequest payload = payloadOption.Expect(() => new ArgumentException("Request payload not found"));

            if (ExpectedSchemaVersion.CompareMajorVersion(payload.SchemaVersion, "logs upload request schema") != 0)
            {
                Events.MismatchedMinorVersions(payload.SchemaVersion, ExpectedSchemaVersion);
            }

            Events.ProcessingRequest(payload);
            ILogsRequestToOptionsMapper requestToOptionsMapper = new LogsRequestToOptionsMapper(
                this.runtimeInfoProvider,
                payload.Encoding,
                payload.ContentType,
                LogOutputFraming.None,
                Option.Some(new LogsOutputGroupingConfig(100, TimeSpan.FromSeconds(10))),
                false);
            IList <(string id, ModuleLogOptions logOptions)> logOptionsList = await requestToOptionsMapper.MapToLogOptions(payload.Items, cancellationToken);

            IEnumerable <Task> uploadLogsTasks = logOptionsList.Select(l => this.UploadLogs(payload.SasUrl, l.id, l.logOptions, cancellationToken));

            (string correlationId, BackgroundTaskStatus status) = BackgroundTask.Run(
                () =>
            {
                try
                {
                    return(Task.WhenAll(uploadLogsTasks));
                }
                catch (Exception e)
                {
                    Events.ErrorUploadingLogs(e);
                    throw;
                }
            },
                "upload logs",
                cancellationToken);
            return(Option.Some(TaskStatusResponse.Create(correlationId, status)));
        }
        public async Task Handle(IClientWebSocket clientWebSocket, CancellationToken cancellationToken)
        {
            try
            {
                LogsStreamRequest streamRequest = await this.ReadLogsStreamingRequest(clientWebSocket, cancellationToken);

                Events.RequestData(streamRequest);

                if (ExpectedSchemaVersion.CompareMajorVersion(streamRequest.SchemaVersion, "logs stream request schema") != 0)
                {
                    Events.MismatchedMinorVersions(streamRequest.SchemaVersion, ExpectedSchemaVersion);
                }

                var socketCancellationTokenSource = new CancellationTokenSource();

                Task ProcessLogsFrame(ArraySegment <byte> bytes)
                {
                    if (clientWebSocket.State != WebSocketState.Open)
                    {
                        Events.WebSocketNotOpen(streamRequest, clientWebSocket.State);
                        socketCancellationTokenSource.Cancel();
                        return(Task.CompletedTask);
                    }
                    else
                    {
                        return(clientWebSocket.SendAsync(bytes, WebSocketMessageType.Binary, true, cancellationToken));
                    }
                }

                ILogsRequestToOptionsMapper requestToOptionsMapper = new LogsRequestToOptionsMapper(
                    this.runtimeInfoProvider,
                    streamRequest.Encoding,
                    streamRequest.ContentType,
                    LogOutputFraming.SimpleLength,
                    Option.None <LogsOutputGroupingConfig>(),
                    true);
                IList <(string id, ModuleLogOptions logOptions)> logOptionsList = await requestToOptionsMapper.MapToLogOptions(streamRequest.Items, cancellationToken);

                using (var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, socketCancellationTokenSource.Token))
                {
                    await this.logsProvider.GetLogsStream(logOptionsList, ProcessLogsFrame, linkedCts.Token);
                }

                Events.StreamingCompleted(streamRequest);
            }
            catch (Exception e)
            {
                Events.ErrorHandlingRequest(e);
            }
        }