prom-client使用

2024-04-10 17:28
文章标签 使用 client prom

本文主要是介绍prom-client使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

prom-client

metric.ts

import {Counter,Histogram,Gauge,Summary,Pushgateway,Registry,
} from 'prom-client';type MetricData = {name: string;labels: Record<string, string>;labelNames: string[];type: string;help: string;value?: number;buckets?: number[];percentiles?: number[];
};export enum MetricType {CounterType = 'counter',HistogramType = 'histogram',GaugeType = 'gauge',SummaryType = 'summary',
}const register = new Registry();// 上报到平台
export async function pushGateway() {try {const gateway = new Pushgateway('https://xxx',[],register);const { resp, body } = await gateway.push({ jobName: 'pipeline test' });console.log(`push gateway successfully, status code is ${(resp as any).statusCode}, body is ${body}`);} catch (err) {console.log('push gateway failed:', err);}
}// 创建指标类别
function createCounter(metricData: MetricData) {const { name, help, labelNames } = metricData;let counter = register.getSingleMetric(name) as Counter<string>;if (!counter) {counter = new Counter({name,help,labelNames,registers: [register],});}return counter;
}function createHistogram(metricData: MetricData) {const { name, help, labelNames, buckets } = metricData;let histogram = register.getSingleMetric(name) as Histogram<string>;if (!histogram) {histogram = new Histogram({name,help,labelNames,buckets,registers: [register],});}return histogram;
}function createSummary(metricData: MetricData) {const { name, help, labelNames, percentiles } = metricData;let summary = register.getSingleMetric(name) as Summary<string>;if (!summary) {summary = new Summary({name,help,labelNames,percentiles,registers: [register],});}return summary;
}function createGauge(metricData: MetricData) {const { name, help, labelNames } = metricData;let gauge = register.getSingleMetric(name) as Gauge<string>;if (!gauge) {gauge = new Gauge({name,help,labelNames,registers: [register],});}return gauge;
}export function handleMetric(metricData: MetricData) {const { type: metricType, value, labels } = metricData;const newValue = value || 1;if (metricType === MetricType.CounterType) {const counter = createCounter(metricData);register.registerMetric(counter);counter.inc(labels, newValue);} else if (metricType === MetricType.HistogramType) {const histogram = createHistogram(metricData);register.registerMetric(histogram);histogram.observe(labels, newValue);} else if (metricType === MetricType.SummaryType) {const summary = createSummary(metricData);register.registerMetric(summary);summary.observe(labels, newValue);} else if (metricType === MetricType.GaugeType) {const gauge = createGauge(metricData);register.registerMetric(gauge);gauge.set(labels, newValue);}
}

使用:

async function collectMetrics(name: string) {try {// 先获取指标原始数据// 比如需要掉接口拉取// await getList()...const labels = {labels: {name,env},labelNames: ['name','env',],};const counterMetric = {name: 'pipeline_run_total',help: 'pipeline run total',type: MetricType.CounterType,...labels,};const histogramMetric = {name: 'pipeline_run_duration',help: 'pipeline run finished',value: duration,buckets: [30000, 60000, 75000, 90000, 105000, 120000, 135000, 150000, 165000,180000, 195000, 210000, 225000, 240000, 270000, 300000, 330000,360000,],type: MetricType.HistogramType,...labels,};// 处理指标[counterMetric, histogramMetric].forEach((metric) =>handleMetric(metric));}} catch (err) {// err}
}(async () => {// collect metricsconst testNames=['test1','test2']await Promise.all(testNames.map((name) => collectMetrics(name)));// export to metric storeawait pushGateway();
})().catch((e) => console.log(e.message || e))

这篇关于prom-client使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/891689

相关文章

Java中的ConcurrentBitSet使用小结

《Java中的ConcurrentBitSet使用小结》本文主要介绍了Java中的ConcurrentBitSet使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、核心澄清:Java标准库无内置ConcurrentBitSet二、推荐方案:Eclipse

Go语言结构体标签(Tag)的使用小结

《Go语言结构体标签(Tag)的使用小结》结构体标签Tag是Go语言中附加在结构体字段后的元数据字符串,用于提供额外的属性信息,这些信息可以通过反射在运行时读取和解析,下面就来详细的介绍一下Tag的使... 目录什么是结构体标签?基本语法常见的标签用途1.jsON 序列化/反序列化(最常用)2.数据库操作(

Java中ScopeValue的使用小结

《Java中ScopeValue的使用小结》Java21引入的ScopedValue是一种作用域内共享不可变数据的预览API,本文就来详细介绍一下Java中ScopeValue的使用小结,感兴趣的可以... 目录一、Java ScopedValue(作用域值)详解1. 定义与背景2. 核心特性3. 使用方法

spring中Interceptor的使用小结

《spring中Interceptor的使用小结》SpringInterceptor是SpringMVC提供的一种机制,用于在请求处理的不同阶段插入自定义逻辑,通过实现HandlerIntercept... 目录一、Interceptor 的核心概念二、Interceptor 的创建与配置三、拦截器的执行顺

C#中checked关键字的使用小结

《C#中checked关键字的使用小结》本文主要介绍了C#中checked关键字的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录✅ 为什么需要checked? 问题:整数溢出是“静默China编程”的(默认)checked的三种用

C#中预处理器指令的使用小结

《C#中预处理器指令的使用小结》本文主要介绍了C#中预处理器指令的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 第 1 名:#if/#else/#elif/#endif✅用途:条件编译(绝对最常用!) 典型场景: 示例

Mysql中RelayLog中继日志的使用

《Mysql中RelayLog中继日志的使用》MySQLRelayLog中继日志是主从复制架构中的核心组件,负责将从主库获取的Binlog事件暂存并应用到从库,本文就来详细的介绍一下RelayLog中... 目录一、什么是 Relay Log(中继日志)二、Relay Log 的工作流程三、Relay Lo

使用Redis实现会话管理的示例代码

《使用Redis实现会话管理的示例代码》文章介绍了如何使用Redis实现会话管理,包括会话的创建、读取、更新和删除操作,通过设置会话超时时间并重置,可以确保会话在用户持续活动期间不会过期,此外,展示了... 目录1. 会话管理的基本概念2. 使用Redis实现会话管理2.1 引入依赖2.2 会话管理基本操作

Springboot请求和响应相关注解及使用场景分析

《Springboot请求和响应相关注解及使用场景分析》本文介绍了SpringBoot中用于处理HTTP请求和构建HTTP响应的常用注解,包括@RequestMapping、@RequestParam... 目录1. 请求处理注解@RequestMapping@GetMapping, @PostMappin

springboot3.x使用@NacosValue无法获取配置信息的解决过程

《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x