当前位置: 首页 > 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

查看数据

相关文章:

netcore 集成Prometheus

一、安装包 <ItemGroup><PackageReference Include"prometheus-net" Version"8.2.1" /><PackageReference Include"prometheus-net.AspNetCore" Version"8.2.1" /> </ItemGroup> 二、添加代码 #region Pro…...

同城外卖系统源码扩展指南:搭建海外外卖APP平台详解

本篇文章&#xff0c;笔者将探讨如何基于同城外卖系统源码&#xff0c;搭建适合不同国家的海外外卖APP平台&#xff0c;涵盖多语言支持、支付接口对接、本地化适配等方面的实践经验和技术要点。 一、确定目标市场与用户需求 在开发海外外卖APP平台之前&#xff0c;首先需要深…...

JavaScript 中常见内置对象的知识点及示例总结

一、String&#xff08;字符串&#xff09;对象 知识点&#xff1a; 用于处理文本数据&#xff0c;它有许多内置的属性和方法来操作字符串&#xff0c;比如获取字符串长度、提取子字符串、替换字符等。字符串在 JavaScript 中是不可变的&#xff0c;即一旦创建&#xff0c;就不…...

CSSmodule的作用是什么

CSS Modules的作用主要体现在以下几个方面&#xff1a; 1. 解决全局样式污染问题 在传统的CSS管理方式中&#xff0c;样式定义通常是全局的&#xff0c;这很容易导致全局样式污染。当多个组件或页面共享同一个样式时&#xff0c;可能会出现样式冲突和覆盖的情况&#xff0c;从…...

python\shell\c++语法对比

语法区别举例&#xff1a; itempythonshellc变量定义a 10a10int a 10数组定义arr[1, add, 3]arr(1 a hello) declare -A arr([a]1 [b]2)int arr[] {1, 2, 3}if条件判断 if xxx: xxx elif xxx: xxx else: xxx if [ expressions ];then xxx e…...

优先队列【东北大学oj数据结构9-3】C++

优先队列 优先级队列是一种数据结构&#xff0c;其中保存了一组数据 S&#xff0c;其中每个元素都有一个键&#xff0c;并执行以下操作&#xff1a; insert(S, k)&#xff1a;将元素k插入集合S extractMax(S)&#xff1a;从S中取出S中key最大的元素并返回其值 创建一个程序&am…...

圣诞快乐(h5 css js(圣诞树))

一&#xff0c;整体设计思路 圣诞树h5&#xff08;简易&#xff09; 1.页面布局与样式&#xff1a; 页面使用了全屏的黑色背景&#xff0c;中央显示圣诞树&#xff0c;树形由三层绿色的三角形组成&#xff0c;每一层的大小逐渐变小。树干是一个棕色的矩形&#xff0c;位于三角…...

基于MATLAB的图像增强

目录 一、背景及意义介绍背景图像采集过程中的局限性 意义 二、概述三、代码结构及说明&#xff08;一&#xff09;整体结构&#xff08;二&#xff09;亮度增强部分&#xff08;三&#xff09;对比度增强部分&#xff08;四&#xff09;锐度增强部分 四、复现步骤&#xff08;…...

大数据之Hbase环境安装

Hbase软件版本下载地址&#xff1a; http://mirror.bit.edu.cn/apache/hbase/ 1. 集群环境 Master 172.16.11.97 Slave1 172.16.11.98 Slave2 172.16.11.99 2. 下载软件包 #Master wget http://archive.apache.org/dist/hbase/0.98.24/hbase-0.98.24-hadoop1-bin.tar.gz…...

javaEE--计算机是如何工作的-1

目录 一.计算机的组成: 各组件的功能: 衡量cpu好坏的标准: 二.指令(instruction) 三.操作系统Operating System 四.进程/任务process/tesk 五.进程在系统中如何管理 1.进程在系统中的管理,从两个角度来分类: 2.进程控制块PCB&#xff08;Process Control Block)) 3.P…...

vue.js 指令的修饰符

Vue.js 提供了一些指令修饰符&#xff0c;用于在指令的行为上添加额外的功能。下面详细解析一些常用的指令修饰符&#xff0c;并提供相应的代码实例。 .prevent&#xff1a;阻止默认事件 通过添加 .prevent 修饰符&#xff0c;可以阻止指令绑定的元素触发默认事件。 代码实例&a…...

基于java web在线商城购物系统源码+论文

一、环境信息 开发语言&#xff1a;JAVA JDK版本&#xff1a;JDK8及以上 数据库&#xff1a;MySql5.6及以上 Maven版本&#xff1a;任意版本 操作系统&#xff1a;Windows、macOS 开发工具&#xff1a;Idea、Eclipse、MyEclipse 开发框架&#xff1a;SpringbootHTMLjQueryMysq…...

Autosar入门_架构(Architecture)

上一篇 | 返回主目录 | 下一篇 架构(Architecture) 1 Autosar架构分层概述2 MCAL3 ECU抽象层4 复杂设备驱动5 服务层6 RTE7 应用软件层1 Autosar架构分层概述 整体架构分为三层:应用软件(APP)、实时运行环境(RTE)、基础软件(BSW)以下架构对BSW进行了细化,主要包含四…...

Altair: 轻松创建交互式数据可视化

Altair: 轻松创建交互式数据可视化 Altair 是一个基于 Vega-Lite 的 Python 数据可视化库&#xff0c;它旨在简化数据可视化的创建过程&#xff0c;尤其适用于统计图表的生成。Altair 强调声明式编码方式&#xff0c;通过简单的语法&#xff0c;用户能够快速创建复杂的交互式图…...

APM32F411使用IIS外设驱动es8388实现自录自播

前言&#xff1a; 从零开始学习I2s外设&#xff0c;配置Es8288寄存器实现录音播放。本文章使用主控芯片是APM32F411系类。音频相关的概念比较多&#xff0c;就不再次做过多的介绍&#xff0c;本文章只是简单实现边录边播功能。APM系类兼容st的芯片&#xff0c;所以用st的hal库来…...

RabbitMQ消息队列的笔记

Rabbit与Java相结合 引入依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId> </dependency> 在配置文件中编写关于rabbitmq的配置 rabbitmq:host: 192.168.190.132 /…...

JAVA没有搞头了吗?

前言 今年的Java程序员群体似乎承受着前所未有的焦虑。投递简历无人问津&#xff0c;难得的面试机会也难以把握&#xff0c;即便成功入职&#xff0c;也往往难以长久。于是&#xff0c;不少程序员感叹&#xff1a;互联网的寒冬似乎又一次卷土重来&#xff0c;环境如此恶劣&…...

【线性代数】理解矩阵乘法的意义(点乘)

刚接触线性代数时&#xff0c;很不理解矩阵乘法的计算规则&#xff0c;为什么规则定义的看起来那么有规律却又莫名其妙&#xff0c;现在参考了一些资料&#xff0c;回过头重新总结下个人对矩阵乘法的理解&#xff08;严格来说是点乘&#xff09;。 理解矩阵和矩阵的乘法&#x…...

游戏开发技能系统常用概念

一个角色同一时间可能存在多个Skill&#xff0c;一个当前播放的主动技能&#xff0c;还有好几个不在播放中&#xff0c;但是也没有结束的。 技能事件&#xff1a; 实现具体的技能功能&#xff0c;技能动作的执行都是通过触发事件来触发的&#xff0c;比如&#xff08;时间帧&am…...

【案例80】麒麟操作系统无法使用Uclient访问NC65

问题现象 麒麟操作系统&#xff0c;安装Uclient&#xff0c;添加应用后无法看到登录界面&#xff0c;一直在转圈。 问题分析 进入到Uclient的工作目录 发现在工作目录下&#xff0c;无相关app.log生成。 查看Uclient的main.log发现&#xff0c;有大量的报错与Uclient下的sha…...

URP Scriptable Renderer Feature实战:从原理到自定义后处理

1. URP Scriptable Renderer Feature基础认知 第一次接触URP的Scriptable Renderer Feature时&#xff0c;我完全被各种专业术语搞晕了。后来在实际项目中反复折腾才发现&#xff0c;这东西本质上就是个"特效插件系统"。想象你正在玩一款射击游戏&#xff0c;当角色受…...

Lingbot-Depth-Pretrain-ViTL-14 模型压缩与加速:面向边缘设备的部署优化教程

Lingbot-Depth-Pretrain-ViTL-14 模型压缩与加速&#xff1a;面向边缘设备的部署优化教程 想让一个像 Lingbot-Depth-Pretrain-ViTL-14 这样的大模型在树莓派、Jetson 这类小设备上跑起来&#xff0c;是不是感觉像让一头大象挤进小轿车&#xff1f;直接部署&#xff0c;设备可…...

面试官最爱问的Java集合+多线程,详解+示例

文章目录一、开篇&#xff1a;为什么面试官揪着这俩不放&#xff1f;二、Java集合&#xff1a;别只会用ArrayList了2.1 List三兄弟&#xff1a;ArrayList、LinkedList、Vector2.2 Set家族&#xff1a;HashSet、LinkedHashSet、TreeSet2.3 Map三巨头&#xff1a;HashMap、Concur…...

Gemma-3-12B-IT WebUI保姆级教程:含Supervisord进程守护与开机自启

Gemma-3-12B-IT WebUI保姆级教程&#xff1a;含Supervisord进程守护与开机自启 1. 前言&#xff1a;为什么选择Gemma-3-12B-IT&#xff1f; 如果你正在寻找一个性能强劲、部署友好&#xff0c;而且完全免费开源的大语言模型&#xff0c;那么Google的Gemma-3-12B-IT绝对值得你…...

Serverless时代Java开发者必学的3种函数封装范式:POJO/Function/Consumer,第2种正在被淘汰!

第一章&#xff1a;Serverless时代Java函数计算的演进与定位Serverless 架构正深刻重塑 Java 应用的部署范式。传统 Java 应用依赖长生命周期的 JVM 进程与复杂中间件栈&#xff0c;而函数计算&#xff08;Function-as-a-Service, FaaS&#xff09;将执行单元收敛为无状态、事件…...

Unity性能优化实战:用Job System并行处理海量数据,告别主线程卡顿

Unity性能优化实战&#xff1a;用Job System并行处理海量数据&#xff0c;告别主线程卡顿 当你的游戏场景中出现成千上万的粒子在飞舞&#xff0c;或是数百个NPC同时进行复杂的AI决策时&#xff0c;是否经常遇到帧率骤降的困扰&#xff1f;作为Unity开发者&#xff0c;我们每天…...

【限时开源】Polars 2.0清洗模板库V1.0发布:含金融时序对齐、电商ID映射、日志正则归一化等9大高复用Pipeline

第一章&#xff1a;Polars 2.0大规模数据清洗技巧入门到精通教程 Polars 2.0 是专为高性能、内存安全与并行计算设计的 DataFrame 库&#xff0c;其惰性执行引擎与零拷贝语义使其在处理 GB 级别结构化数据时显著优于 Pandas。本章聚焦真实场景下的数据清洗实践&#xff0c;涵盖…...

多层PCB内部结构与HDI技术深度解析

1. 多层PCB内部结构全解析作为一名硬件工程师&#xff0c;第一次拆解十层PCB板时&#xff0c;那种震撼感至今难忘。密密麻麻的过孔像微型城市的地下管网&#xff0c;精密排布的走线如同错综复杂的立体交通网。今天我就用最直观的立体解剖图&#xff0c;带你看透各种叠层结构的P…...

Vue-Super-Flow隐藏玩法:不画图,只填空!手把手教你打造可配置的流程图答题组件

Vue-Super-Flow隐藏玩法&#xff1a;不画图&#xff0c;只填空&#xff01;手把手教你打造可配置的流程图答题组件 在Vue生态中&#xff0c;流程图工具通常被用来构建复杂的可视化编辑界面。但你是否想过&#xff0c;这些工具还能用来做些什么&#xff1f;本文将带你探索一个全…...

GLM-4.1V-9B-Base实战教程:跨境电商A+页面图像卖点自动提炼

GLM-4.1V-9B-Base实战教程&#xff1a;跨境电商A页面图像卖点自动提炼 1. 为什么需要自动提炼图像卖点 跨境电商卖家每天需要处理大量商品图片&#xff0c;传统人工标注方式存在三个痛点&#xff1a; 效率低下&#xff1a;一个运营人员每天最多处理50-100张图片成本高昂&…...