public async ValueTask <IActionResult> Post(DataPostRequest request)
        {
            Log.LogInformation("Post data. id=[{id}]", request.Id);
            ApiMetrics.IncrementDataPost();

            if (!await DataService.InsertDataAsync(Mapper.Map <DataEntity>(request)))
            {
                return(Conflict());
            }

            return(Ok());
        }
Beispiel #2
0
        protected void Application_Start()
        {
            var metrics = ApiMetrics.SetMetrics(new InitAppMetricsModel()
            {
                BaseUri  = "http://192.168.137.253:8086",
                UserName = "******",
                Password = "******",
                Database = "test"
            });

            GlobalConfiguration.Configure(WebApiConfig.Register);
        }
        public async ValueTask <IActionResult> Get(int id)
        {
            Log.LogInformation("Get data. id=[{id}]", id);
            ApiMetrics.IncrementDataGet();

            var entity = await DataService.QueryDataAsync(id);

            if (entity is null)
            {
                return(NotFound());
            }

            return(Ok(Mapper.Map <DataGetResponse>(entity)));
        }
Beispiel #4
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 }
                    )
            });
        }
        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);
            }
        }
        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);
            }
        }
        /// <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);
        }