コード例 #1
0
        public override void OnException(HttpActionExecutedContext context)
        {
            var request       = context.Request;
            var routeTemplate = GetRouteTemplate(request);

            ApiMetrics.GetMetrics().Measure.Counter.Increment(new App.Metrics.Counter.CounterOptions
            {
                Name = "Exception Count",
                Tags = new MetricTags(
                    new string[] { "method", "route", "exception" },
                    new string[] { request.Method.Method, routeTemplate, context.Exception.GetType().FullName }
                    )
            });
        }
コード例 #2
0
        public HttpResponseMessage GetMetrics()
        {
            var formatter = new App.Metrics.Formatters.InfluxDB.MetricsInfluxDbLineProtocolOutputFormatter();
            var snapshot  = ApiMetrics.GetMetrics().Snapshot.Get();

            using (var ms = new MemoryStream())
            {
                formatter.WriteAsync(ms, snapshot).GetAwaiter();
                var result = Encoding.UTF8.GetString(ms.ToArray());

                var response = Request.CreateResponse(HttpStatusCode.OK);
                response.Content = new StringContent(result, Encoding.UTF8, formatter.MediaType.ContentType);

                return(response);
            }
        }
コード例 #3
0
        public async Task <HttpResponseMessage> GetMetricsAsync()
        {
            var formatter = new App.Metrics.Formatters.Prometheus.MetricsPrometheusTextOutputFormatter();
            var snapshot  = ApiMetrics.GetMetrics().Snapshot.Get();

            using (var ms = new MemoryStream())
            {
                await formatter.WriteAsync(ms, snapshot);

                var result = Encoding.UTF8.GetString(ms.ToArray());

                var response = Request.CreateResponse(HttpStatusCode.OK);
                response.Content = new StringContent(result, Encoding.UTF8, formatter.MediaType.ContentType);

                return(response);
            }
        }
コード例 #4
0
        /// <summary>
        /// 停止记录响应时间
        /// </summary>
        /// <param name="response"></param>
        private void EndRecordingResponseTime(string routeTemplate, HttpRequestMessage request, HttpResponseMessage response)
        {
            var stopwatch = response.RequestMessage.Properties[API_METRICS_RESPONSE_TIME_KEY] as Stopwatch;

            ApiMetrics.GetMetrics().Provider.Timer.Instance(new TimerOptions
            {
                Name = "Response Time",
                Tags = new MetricTags(
                    new string[] { "method", "route", "status" },
                    new string[] { request.Method.Method, routeTemplate, ((int)response.StatusCode).ToString() }
                    ),
                DurationUnit    = TimeUnit.Milliseconds,
                RateUnit        = TimeUnit.Milliseconds,
                MeasurementUnit = Unit.Requests
            }).Record(stopwatch.ElapsedMilliseconds, TimeUnit.Milliseconds);

            response.RequestMessage.Properties.Remove(API_METRICS_RESPONSE_TIME_KEY);
        }