예제 #1
0
        void MapMetadata(TraceMetadata trace, HttpContext httpContext)
        {
            trace.RequestContentType = httpContext.Request.ContentType;
            trace.ResponseTimestamp  = DateTime.Now;
            trace.ResponseTimeMs     = _timeKeeper.Elapsed.TotalMilliseconds;
            trace.ResponseStatusCode = httpContext.Response.StatusCode;
            trace.RequestMethod      = httpContext.Request.Method;
            trace.RequestUri         = httpContext.Request.Path;
            trace.RequestHost        = httpContext.Request.Host.ToString();

            var type = (int)httpContext.Response.StatusCode / 100;

            trace.Type = type.ToString() + "xx";
        }
예제 #2
0
        public IHttpActionResult Trace([FromBody] TraceMetadata traceMetadata)
        {
            if (traceMetadata == null)
            {
                return(BadRequest("No trace metadata was specified"));
            }
            if (string.IsNullOrWhiteSpace(traceMetadata.Message))
            {
                return(BadRequest("No message was specified"));
            }

            try
            {
                var applicationInsightsTelemetry = new ApplicationInsightsTelemetry(traceMetadata.InstrumentationKey);
                applicationInsightsTelemetry.TrackTrace(traceMetadata.Message, traceMetadata.SeverityLevel, traceMetadata.CustomProperties);

                return(StatusCode(HttpStatusCode.NoContent));
            }
            catch (InstrumentationKeyNotSpecifiedException)
            {
                return(Content(HttpStatusCode.InternalServerError, Constants.Errors.MissingInstrumentationKey));
            }
        }
예제 #3
0
 public NamedController(IHttpClientFactory clientFactory, TraceMetadata traces)
 {
     _client = clientFactory.CreateClient("named");
 }
예제 #4
0
 public BasicController(IHttpClientFactory clientFactory, TraceMetadata traces)
 {
     _client = clientFactory.CreateClient();
 }
예제 #5
0
 public GrpcService(TraceMetadata traces)
 {
     _traces = traces;
 }
예제 #6
0
        public async Task Invoke(HttpContext httpContext)
        {
            TraceMetadata _trace = new TraceMetadata();

            if (httpContext.Request.Path != "/trace" && httpContext.Request.Path != "/ui" && httpContext.Request.Path != "/analysis" &&
                httpContext.Request.Path != "/traceanalysis")
            {
                _trace.RequestContentType  = httpContext.Request.ContentType;
                _trace.ResponseContentType = httpContext.Response.ContentType;
                _trace.RequestTimestamp    = DateTime.Now;
                _timeKeeper = Stopwatch.StartNew();

                #region Retrieve Request-Responce Content
                _trace.RequestContent = await FormatRequest(httpContext.Request);

                var originalBodyStream = httpContext.Response.Body;

                using (var responseBody = new MemoryStream())
                {
                    httpContext.Response.Body = responseBody;
                    try
                    {
                        await _next(httpContext);

                        _timeKeeper.Stop();

                        if (httpContext.Response.StatusCode != 204 && (httpContext.Response.ContentType == "application/json; charset=utf-8"))
                        {
                            _trace.ResponseContent = await FormatResponse(httpContext.Response);

                            _trace.ResponseContentType = httpContext.Response.ContentType;
                            await responseBody.CopyToAsync(originalBodyStream);
                        }
                        else
                        {
                            _trace.ResponseContent     = "N/A";
                            _trace.ResponseContentType = "N/A";
                        }

                        MapMetadata(_trace, httpContext);
                        await _repository.SaveAsync(_trace);
                    }

                    catch (Exception ex)
                    {
                        _timeKeeper.Stop();
                        MapMetadata(_trace, httpContext);
                        _trace.ResponseContent     = ex.ToString();
                        _trace.ResponseContentType = "N/A";
                        _trace.ResponseStatusCode  = 500;
                        _trace.Type = "5xx";
                        await _repository.SaveAsync(_trace);

                        httpContext.Response.Body = originalBodyStream;
                        throw;
                    }
                }
                #endregion
            }

            // Configurable Data Endpoint.
            else if (httpContext.Request.Path == "/trace")
            {
                await  Trace(httpContext);
            }
            // Configurable Data Analysis Endpoint.
            else if (httpContext.Request.Path == "/traceanalysis")
            {
                await TraceAnalysisAsync(httpContext);
            }
            // Configurable UI and Analysis End Points.
            else if (httpContext.Request.Path == "/ui" || httpContext.Request.Path == "/analysis")
            {
                await RenderFrontEnd(httpContext);
            }
        }
예제 #7
0
 public Controller(TraceMetadata traces)
 {
     _traces = traces;
 }
예제 #8
0
 public TypedController(TypedHttpClient httpClient, TraceMetadata traces)
 {
     _client = httpClient;
 }