Beispiel #1
0
        /// <summary>
        /// LogTrafficWatch gets HttpContext, elapsed time and database name
        /// </summary>
        /// <param name="context"></param>
        /// <param name="elapsedMilliseconds"></param>
        /// <param name="database"></param>
        private void LogTrafficWatch(HttpContext context, long elapsedMilliseconds, string database)
        {
            var requestId   = Interlocked.Increment(ref _requestId);
            var contextItem = context.Items["TrafficWatch"];

            (string CustomInfo, TrafficWatchChangeType Type)twTuple =
                ((string, TrafficWatchChangeType)?)contextItem ?? ("N/A", TrafficWatchChangeType.None);

            var twn = new TrafficWatchChange
            {
                TimeStamp           = DateTime.UtcNow,
                RequestId           = requestId,                       // counted only for traffic watch
                HttpMethod          = context.Request.Method ?? "N/A", // N/A ?
                ElapsedMilliseconds = elapsedMilliseconds,
                ResponseStatusCode  = context.Response.StatusCode,
                RequestUri          = context.Request.GetEncodedUrl(),
                AbsoluteUri         = $"{context.Request.Scheme}://{context.Request.Host}",
                DatabaseName        = database ?? "N/A",
                CustomInfo          = twTuple.CustomInfo,
                Type = twTuple.Type,
                InnerRequestsCount = 0,
                QueryTimings       = null
            };

            TrafficWatchManager.DispatchMessage(twn);
        }
Beispiel #2
0
        private void LogTrafficWatch(HttpContext context, long elapsedMilliseconds, string database)
        {
            var requestId = Interlocked.Increment(ref _requestId);

            var twn = new TrafficWatchChange
            {
                TimeStamp           = DateTime.UtcNow,
                RequestId           = requestId,                       // counted only for traffic watch
                HttpMethod          = context.Request.Method ?? "N/A", // N/A ?
                ElapsedMilliseconds = elapsedMilliseconds,
                ResponseStatusCode  = context.Response.StatusCode,
                RequestUri          = context.Request.GetEncodedUrl(),
                AbsoluteUri         = $@"{context.Request.Scheme}://{context.Request.Host}",
                DatabaseName        = database ?? "N/A",
                CustomInfo          = "",  // TODO: Implement
                InnerRequestsCount  = 0,   // TODO: Implement
                QueryTimings        = null // TODO: Implement
            };

            TrafficWatchManager.DispatchMessage(twn);
        }
Beispiel #3
0
        private async Task RequestHandler(HttpContext context)
        {
            try
            {
                context.Response.StatusCode = 200;
                var sp     = Stopwatch.StartNew();
                var tenant = await _router.HandlePath(context, context.Request.Method, context.Request.Path.Value);

                sp.Stop();

                if (_logger.IsInfoEnabled)
                {
                    _logger.Info($"{context.Request.Method} {context.Request.Path.Value}?{context.Request.QueryString.Value} - {context.Response.StatusCode} - {sp.ElapsedMilliseconds:#,#;;0} ms");
                }

                if (TrafficWatchManager.HasRegisteredClients)
                {
                    var requestId = Interlocked.Increment(ref _requestId);

                    var twn = new TrafficWatchNotification
                    {
                        TimeStamp           = DateTime.UtcNow,
                        RequestId           = requestId,                       // counted only for traffic watch
                        HttpMethod          = context.Request.Method ?? "N/A", // N/A ?
                        ElapsedMilliseconds = sp.ElapsedMilliseconds,
                        ResponseStatusCode  = context.Response.StatusCode,
                        RequestUri          = context.Request.GetEncodedUrl(),
                        AbsoluteUri         = $@"{context.Request.Scheme}://{context.Request.Host}",
                        TenantName          = tenant ?? "N/A",
                        CustomInfo          = "",   // TODO: Implement
                        InnerRequestsCount  = 0,    // TODO: Implement
                        QueryTimings        = null, // TODO: Implement
                    };

                    TrafficWatchManager.DispatchMessage(twn);
                }
            }
            catch (Exception e)
            {
                if (context.RequestAborted.IsCancellationRequested)
                {
                    return;
                }
                //TODO: special handling for argument exception (400 bad request)
                //TODO: database not found (503)
                //TODO: operaton cancelled (timeout)
                //TODO: Invalid data exception 422


                //TODO: Proper json output, not like this
                var response = context.Response;

                var documentConflictException = e as DocumentConflictException;
                if (response.HasStarted == false)
                {
                    if (documentConflictException != null)
                    {
                        response.StatusCode = 409;
                    }
                    else if (response.StatusCode < 400)
                    {
                        response.StatusCode = 500;
                    }
                }


                JsonOperationContext ctx;
                using (_server.ServerStore.ContextPool.AllocateOperationContext(out ctx))
                {
                    var djv = new DynamicJsonValue
                    {
                        ["Url"]     = $"{context.Request.Path}?{context.Request.QueryString}",
                        ["Type"]    = e.GetType().FullName,
                        ["Message"] = e.Message
                    };

                    string errorString;

                    try
                    {
                        errorString = e.ToAsyncString();
                    }
                    catch (Exception)
                    {
                        errorString = e.ToString();
                    }

                    djv["Error"] = errorString;

                    var indexCompilationException = e as IndexCompilationException;
                    if (indexCompilationException != null)
                    {
                        djv[nameof(IndexCompilationException.IndexDefinitionProperty)] =
                            indexCompilationException.IndexDefinitionProperty;
                        djv[nameof(IndexCompilationException.ProblematicText)] =
                            indexCompilationException.ProblematicText;
                    }

                    if (documentConflictException != null)
                    {
                        djv["ConflictInfo"] = ReplicationUtils.GetJsonForConflicts(
                            documentConflictException.DocId,
                            documentConflictException.Conflicts);
                    }

                    using (var writer = new BlittableJsonTextWriter(ctx, response.Body))
                    {
                        var json = ctx.ReadObject(djv, "exception");
                        writer.WriteObject(json);
                    }
                }
            }
        }
        private async Task RequestHandler(HttpContext context)
        {
            try
            {
                context.Response.StatusCode = (int)HttpStatusCode.OK;
                context.Response.Headers["Content-Type"] = "application/json; charset=utf-8";

                var sp       = Stopwatch.StartNew();
                var database = await _router.HandlePath(context, context.Request.Method, context.Request.Path.Value);

                sp.Stop();

                if (_logger.IsInfoEnabled && SkipHttpLogging == false)
                {
                    _logger.Info($"{context.Request.Method} {context.Request.Path.Value}?{context.Request.QueryString.Value} - {context.Response.StatusCode} - {sp.ElapsedMilliseconds:#,#;;0} ms");
                }

                if (TrafficWatchManager.HasRegisteredClients)
                {
                    var requestId = Interlocked.Increment(ref _requestId);

                    var twn = new TrafficWatchChange
                    {
                        TimeStamp           = DateTime.UtcNow,
                        RequestId           = requestId,                       // counted only for traffic watch
                        HttpMethod          = context.Request.Method ?? "N/A", // N/A ?
                        ElapsedMilliseconds = sp.ElapsedMilliseconds,
                        ResponseStatusCode  = context.Response.StatusCode,
                        RequestUri          = context.Request.GetEncodedUrl(),
                        AbsoluteUri         = $@"{context.Request.Scheme}://{context.Request.Host}",
                        DatabaseName        = database ?? "N/A",
                        CustomInfo          = "",  // TODO: Implement
                        InnerRequestsCount  = 0,   // TODO: Implement
                        QueryTimings        = null // TODO: Implement
                    };

                    TrafficWatchManager.DispatchMessage(twn);
                }
            }
            catch (Exception e)
            {
                if (context.RequestAborted.IsCancellationRequested)
                {
                    return;
                }

                //TODO: special handling for argument exception (400 bad request)
                //TODO: operation canceled (timeout)
                //TODO: Invalid data exception 422


                //TODO: Proper json output, not like this
                var response = context.Response;

                MaybeSetExceptionStatusCode(response, e);

                using (_server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext ctx))
                {
                    var djv = new DynamicJsonValue
                    {
                        [nameof(ExceptionDispatcher.ExceptionSchema.Url)]     = $"{context.Request.Path}{context.Request.QueryString}",
                        [nameof(ExceptionDispatcher.ExceptionSchema.Type)]    = e.GetType().FullName,
                        [nameof(ExceptionDispatcher.ExceptionSchema.Message)] = e.Message
                    };


#if EXCEPTION_ERROR_HUNT
                    var f = Guid.NewGuid() + ".error";
                    File.WriteAllText(f,
                                      $"{context.Request.Path}{context.Request.QueryString}" + Environment.NewLine + errorString);
#endif
                    djv[nameof(ExceptionDispatcher.ExceptionSchema.Error)] = e.ToString();

                    MaybeAddAdditionalExceptionData(djv, e);

                    using (var writer = new BlittableJsonTextWriter(ctx, response.Body))
                    {
                        var json = ctx.ReadObject(djv, "exception");
                        writer.WriteObject(json);
                    }

#if EXCEPTION_ERROR_HUNT
                    File.Delete(f);
#endif
                }
            }
        }