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.rhkb.cn/news/493438.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

CLION中运行远程的GUI程序

在CLION中运行远程GUI程序&#xff0c;很有可能会遇到下面错误 Gtk-WARNING **: cannot open display: 这是因为远程的GUI程序不能再本地机器上显示。这个问题一般有两种解决方法 通过SSH的ForwardX11的方法&#xff0c;就是将远程的GUI程序显示到本地机器上&#xff0c;一般在…

datasets 笔记:加载数据集(基本操作)

参考了huggingface的教程 1 了解数据集基本信息&#xff08; load_dataset_builder&#xff09; 在下载数据集之前&#xff0c;通常先快速了解数据集的基本信息会很有帮助。数据集的信息存储在 DatasetInfo 中&#xff0c;可能包括数据集描述、特征和数据集大小等信息。&…

Java期末复习暨学校第十三次上机课作业

Java期末复习暨学校第十三次上机课作业&#xff1a; &#xff08;1&#xff09;&#xff1a;掌握正则表达式的使用 第一题&#xff1a; 第13行代码为正则表达式&#xff0c;中国内地的手机号必须是11个数字&#xff1a; &#xff08;1&#xff09; ^1&#xff1a;是该电话号码…

aosp15 - Activity生命周期切换

本文探查的是&#xff0c;从App冷启动后到MainActivity生命周期切换的系统实现。 调试步骤 在com.android.server.wm.RootWindowContainer#attachApplication 方法下断点&#xff0c;为了attach目标进程在com.android.server.wm.ActivityTaskSupervisor#realStartActivityLock…

【libuv】Fargo信令1:client发connect消息给到server

tcp 单机测试,进行模拟 (借助copilot实现) 【Fargo】28:字节序列client发connect消息给到serverserver 收到后回复ack给到客户端程序借助copilot实现。项目构建 Console依赖于Halo.dll提供的api,Halo 依赖于 Immanuel, 运行效果 遗留问题 客户端似乎么有逻辑收到ack做处理各…

libmodbus安装使用

要配置和编译 libmodbus&#xff0c;您需要确保安装了所有必要的依赖项&#xff0c;并按照正确的步骤进行操作。以下是详细的环境配置和编译指南&#xff0c;适用于不同的操作系统。 1. Linux (Debian/Ubuntu) 安装依赖项 首先&#xff0c;确保您的包列表是最新的&#xff1…

猫咪睡眠:萌态背后的奥秘与启示

猫咪的睡眠&#xff0c;犹如一本充满趣味与奥秘的小书&#xff0c;每一页都写满了它们独特的习性与本能。 猫咪堪称 “睡眠大师”&#xff0c;睡眠时间之长令人咋舌&#xff0c;一天中大约有 12 - 16 个小时在梦乡中度过&#xff0c;幼猫和老年猫甚至能睡更久。它们似乎深谙放…

关于小程序内嵌h5打开新的小程序

关于小程序内嵌h5打开新的小程序 三种方式 https://juejin.cn/post/7055551463489011749 只依赖于h5本身的就是 https://huaweicloud.csdn.net/64f97ebb6b896f66024ca16c.html https://juejin.cn/post/7055551463489011749 navigateToMiniProgram 故小程序webview里的h5无法…

免费GIS工具箱:轻松将glb文件转换成3DTiles文件

在GIS地理信息系统领域&#xff0c;GLB文件作为GLTF文件的二进制版本&#xff0c;主要用于3D模型数据的存储和展示。然而&#xff0c;GLB文件的使用频率相对较低&#xff0c;这是因为GIS系统主要处理的是地理空间数据&#xff0c;如地图、地形、地貌、植被、水系等&#xff0c;…

音视频入门基础:MPEG2-TS专题(21)——FFmpeg源码中,获取TS流的视频信息的实现

一、引言 通过FFmpeg命令可以获取到TS文件/TS流的视频压缩编码格式、色彩格式&#xff08;像素格式&#xff09;、分辨率、帧率信息&#xff1a; ./ffmpeg -i XXX.ts 本文以H.264为例讲述FFmpeg到底是从哪个地方获取到这些视频信息的。 二、视频压缩编码格式 FFmpeg获取TS文…

BenchmarkSQL使用教程

1. TPC-C介绍 Transaction Processing Performance Council (TPC) 事务处理性能委员会&#xff0c;是一家非盈利IT组织&#xff0c;他们的目的是定义数据库基准并且向产业界推广可验证的数据库性能测试。而TPC-C最后一个C代表的是压测模型的版本&#xff0c;在这之前还有TPC-A、…

火山引擎发布数据飞轮 2.0,AI 重塑企业数据消费

12 月 18 日&#xff0c;在 2024 冬季火山引擎 FORCE 原动力大会上&#xff0c;火山引擎数智平台&#xff08;VeDI&#xff09;正式升级发布数据飞轮 2.0 模式。 延续去年 4 月发布的数据飞轮“以数据消费促资产建设&#xff0c;以数据消费助业务发展”的核心内涵&#xff0c;…

LLaMA-Factory 单卡3080*2 deepspeed zero3 微调Qwen2.5-7B-Instruct

环境安装 git clone https://gitcode.com/gh_mirrors/ll/LLaMA-Factory.git 下载模型 pip install modelscope modelscope download --model Qwen/Qwen2.5-7B-Instruct --local_dir /root/autodl-tmp/models/Qwen/Qwen2.5-7B-Instruct 微调 llamafactory-cli train \--st…

合并比对学习资料

目录 ContractComparison已开源: ContractComparison已开源: GitHub - UnstoppableCurry/ContractComparison: Comparison of General Chinese Contracts with OCR Pytorch

全速下载 50M/S,不限速下载就是香

近几年来虽说各大网盘层出不穷&#xff0c;各有乾坤&#xff0c;而这其中某些网盘对于网速限制非常严重&#xff0c;这也是很多小伙伴一直吐槽的点&#xff0c;并且某些网盘下载文件还需要安装客户端&#xff0c;并且每家的限速方式不同&#xff0c;有的限速取决于文件大小&…

回归预测 | MATLAB实现CNN-BiGRU-Attention卷积神经网络结合双向门控循环单元融合注意力机制多输入单输出回归预测

回归预测 | MATLAB实现CNN-BiGRU-Attention卷积神经网络结合双向门控循环单元融合注意力机制多输入单输出回归预测 目录 回归预测 | MATLAB实现CNN-BiGRU-Attention卷积神经网络结合双向门控循环单元融合注意力机制多输入单输出回归预测预测效果基本介绍程序设计参考资料 预测效…

RunCam WiFiLink连接手机图传测试

RunCam WiFiLink中文手册从这里下载 一、摄像头端 1.连接天线&#xff08;易忘&#xff09; 2.打开摄像头前面的盖子&#xff08;易忘&#xff09; 3.接上直流电源&#xff0c;红线为正&#xff0c;黑线为负 4.直流电源设置电压为14v&#xff0c;电流为3.15A&#xff0c; 通…

AI的进阶之路:从机器学习到深度学习的演变(二)

AI的进阶之路&#xff1a;从机器学习到深度学习的演变&#xff08;一&#xff09; 三、机器学习&#xff08;ML&#xff09;&#xff1a;AI的核心驱动力 3.1 机器学习的核心原理 机器学习&#xff08;Machine Learning, ML&#xff09;突破了传统编程的局限&#xff0c;它不再…

WordPress 去除?v= 动态后缀

Wordpress url后面带有?vxxx的参数符&#xff0c;这种现象出现在安装了Woocommerce插件的店铺类型站点上&#xff0c;参数的作用是帮助系统根据用户的geographic定位计算 tax and shipping fee。 如何删除&#xff1f; 后台进入WooCommerce Settings &#xff0c;将根据IP定…

Spring Cloud Gateway 源码

Spring Cloud Gateway 架构图 按照以上架构图&#xff0c;请求的处理流程&#xff1a; 1.客户端请求发送到网关 DispatcherHandler 2.网关通过 HandlerMapping 找到相应的 WebHandler 3.WebHandler生成FilterChain过滤器链执行所有的过滤器 4.返回Response结果 自动装配类Gat…