当前位置: 首页 > news >正文

netcore 集成Prometheus

一、安装包

<ItemGroup><PackageReference Include="prometheus-net" Version="8.2.1" /><PackageReference Include="prometheus-net.AspNetCore" Version="8.2.1" />
</ItemGroup>

二、添加代码

#region Prometheus ... endregion 相关代码

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Prometheus;
using Prometheus.HttpMetrics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;namespace prometheusweb
{public class Startup{public Startup(IConfiguration configuration){Configuration = configuration;}public IConfiguration Configuration { get; }// This method gets called by the runtime. Use this method to add services to the container.public void ConfigureServices(IServiceCollection services){#region Prometheus// Let's suppress the default metrics that are built-in, to more easily see the changing metrics output.Metrics.SuppressDefaultMetrics();#endregionservices.AddControllers();services.AddSwaggerGen(c =>{c.SwaggerDoc("v1", new OpenApiInfo { Title = "prometheusweb", Version = "v1" });});}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IWebHostEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();app.UseSwagger();app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "prometheusweb v1"));}#region Prometheusvar expiringMetricFactory = Metrics.WithManagedLifetime(expiresAfter: TimeSpan.FromSeconds(60));// OPTION 1: metric lifetime can be managed by leases, to ensure they do not go away during potentially// long-running operations but go away quickly when the operation is not running anymore (e.g. "in progress" type metrics)._ = Task.Run(async delegate{var inProgress = expiringMetricFactory.CreateGauge("long_running_operations_in_progress", "Number of long running operations in progress.", labelNames: new[] { "operation_type" });// The metric will not be deleted as long as this lease is kept.await inProgress.WithLeaseAsync(async inProgressInstance =>{// Long-running operation, which we track via the "in progress" gauge.using (inProgressInstance.TrackInProgress())await Task.Delay(TimeSpan.FromSeconds(30));}, "VeryLongOperation");// Just to let you know when to look at it.Console.WriteLine("Long-running operation has finished.");// Done! Now the metric lease will be released and soon, the metric will expire and be removed.});// OPTION 2: metrics can auto-extend lifetime whenever their values are updated.app.UseHttpMetrics(options =>{// Here we do something that is typically a no-no in terms of best practices (and GDPR?): we record every unique URL!// We use metric expiration to keep the set of metrics in-memory limited to only recently used URLs, which limits the likelihood// of our web server getting DoSed. We will still need a very very beefy metrics database to actually handle so much data,// so this is not a good idea even if we manage to bypass the most obvious stumbling block of running out of memory!options.AddCustomLabel("url", context => context.Request.GetDisplayUrl());options.InProgress.Gauge = expiringMetricFactory.CreateGauge("http_requests_in_progress","The number of requests currently in progress in the ASP.NET Core pipeline. One series without controller/action label values counts all in-progress requests, with separate series existing for each controller-action pair.",// Let's say that we have all the labels present, as automatic label set selection does not work if we use a custom metric.labelNames: HttpRequestLabelNames.All// ... except for "Code" which is only possible to identify after the request is already finished..Except(new[] { "code" })// ... plus the custom "url" label that we defined above..Concat(new[] { "url" }).ToArray()).WithExtendLifetimeOnUse();options.RequestCount.Counter = expiringMetricFactory.CreateCounter("http_requests_received_total","Provides the count of HTTP requests that have been processed by the ASP.NET Core pipeline.",// Let's say that we have all the labels present, as automatic label set selection does not work if we use a custom metric.labelNames: HttpRequestLabelNames.All// ... plus the custom "url" label that we defined above..Concat(new[] { "url" }).ToArray()).WithExtendLifetimeOnUse();options.RequestDuration.Histogram = expiringMetricFactory.CreateHistogram("http_request_duration_seconds","The duration of HTTP requests processed by an ASP.NET Core application.",// Let's say that we have all the labels present, as automatic label set selection does not work if we use a custom metric.labelNames: HttpRequestLabelNames.All// ... plus the custom "url" label that we defined above..Concat(new[] { "url" }).ToArray(),new HistogramConfiguration{// 1 ms to 32K ms bucketsBuckets = Histogram.ExponentialBuckets(0.001, 2, 16)}).WithExtendLifetimeOnUse();});#endregionapp.UseRouting();app.UseAuthorization();app.UseEndpoints(endpoints =>{#region Prometheusendpoints.MapMetrics();#endregionendpoints.MapControllers();});}}
}

运行访问:

http://192.168.31.68:5000/metrics

三、添加到Prometheus采集job

nano /root/apisix-docker/example/prometheus_conf/prometheus.yml

集成到Grafana

查看数据

http://www.lryc.cn/news/507671.html

相关文章:

  • 同城外卖系统源码扩展指南:搭建海外外卖APP平台详解
  • JavaScript 中常见内置对象的知识点及示例总结
  • CSSmodule的作用是什么
  • python\shell\c++语法对比
  • 优先队列【东北大学oj数据结构9-3】C++
  • 圣诞快乐(h5 css js(圣诞树))
  • 基于MATLAB的图像增强
  • 大数据之Hbase环境安装
  • javaEE--计算机是如何工作的-1
  • vue.js 指令的修饰符
  • 基于java web在线商城购物系统源码+论文
  • Autosar入门_架构(Architecture)
  • Altair: 轻松创建交互式数据可视化
  • APM32F411使用IIS外设驱动es8388实现自录自播
  • RabbitMQ消息队列的笔记
  • JAVA没有搞头了吗?
  • 【线性代数】理解矩阵乘法的意义(点乘)
  • 游戏开发技能系统常用概念
  • 【案例80】麒麟操作系统无法使用Uclient访问NC65
  • 【AIGC-ChatGPT进阶副业提示词】育儿锦囊:化解日常育儿难题的实用指南
  • 使用Docker部署一个Node.js项目
  • 数据科学与SQL:如何利用本福特法则识别财务数据造假?
  • 文心一言对接FreeSWITCH实现大模型呼叫中心
  • LSTM实现天气模型训练与预测
  • TCL发布万象分区,再造Mini LED技术天花板
  • 2024广东省职业技能大赛云计算——私有云(OpenStack)平台搭建
  • 简单了解图注意力机制
  • UI Automator Viewer操作
  • SpringBoot的创建方式
  • Vue3之性能优化