百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

adonisjs-prometheus

> 云原生
开源

AdonisJS 的 Prometheus 提供程序,其中包含一些用于监控应用程序的内置指标。

100 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

AdonisJS 的 Prometheus 提供程序,其中包含一些用于监控应用程序的内置指标。

adonisjs-prometheus

Prometheus package for AdonisJS

[!NOTE] This is the version for AdonisJS 7. For AdonisJS 6, see the 1.x branch and use @julr/adonisjs-prometheus@^1.

Installation

bash
node ace add @julr/adonisjs-prometheus

Usage

After installing the package, a configuration file is added to config/prometheus.ts in your application.

…

The available options are:

  • endpoint: The URL of the endpoint where metrics will be exposed. Defaults to /metrics.
  • metricsPrefix: A prefix that will be added to all metric names. Defaults to the app name.
  • ipsWhitelist: A list of IP addresses or CIDR ranges allowed to access the metrics endpoint. Supports both exact IPs ('10.0.0.5') and CIDR notation ('192.168.1.0/24'). If empty, everyone can access it. Defaults to an empty array.
  • collectors: The list of collectors that will be registered and expose new metrics. You can remove collectors you don't want to use.
  • enableExemplars: Enable OpenMetrics exemplars support. When enabled, metrics will include trace context (traceId and spanId) from OTEL. Defaults to false.

Collectors

Each collector accepts options to customize the metrics it exposes. Be sure to explore these options using your editor's auto-completion to learn more.

HTTP Collector

Adds metrics to monitor HTTP requests:

Exposed Metrics

  • http_requests_total: Counter for the total number of HTTP requests.
  • http_request_duration_seconds: Histogram of HTTP request durations.

Options

  • shouldGroupStatusCode: Groups HTTP status codes into 1xx, 2xx, 3xx, 4xx, and 5xx. Defaults to false.
  • excludedRoutes: A list of routes to exclude from metrics. Defaults to an empty array. You can pass a list of string or a function (ctx: HttpContext) => boolean.
  • requestDuration.buckets: The buckets for the histogram of HTTP request durations.

System Collector

Adds metrics to monitor the host system's performance. See Default Metrics for more information. The collector accepts the same options as the prom-client collectDefaultMetrics function.

Lucid Collector

Adds metrics to monitor database queries made through @adonisjs/lucid.

[!IMPORTANT] To use the Lucid collector, you must set debug: true in your config/database.ts file.

Exposed Metrics

  • lucid_query_duration_seconds: Histogram of Lucid query durations. Labels include connection, model, and method.

Options

  • queryDuration.buckets: The buckets for the histogram of Lucid query durations.

Cache Collector

Adds metrics to monitor @adonisjs/cache operations.

The Cache Collector has two implementations:

New implementation (Recommended)

The recommended way to use the Cache Collector is now with useNewCollector: true, which leverages BentoCache's native Prometheus plugin. This provides richer metrics and better integration with BentoCache.

typescript
cacheCollector({
  useNewCollector: true, // Enable the new implementation
  keyGroups: [[/^users:(\d+)$/, 'users:*']],
})

Exposed Metrics:

  • cache_hits: Counter for the total number of cache hits
  • cache_misses: Counter for the total number of cache misses
  • cache_writes: Counter for the total number of cache writes
  • cache_deletes: Counter for the total number of cache deletes
  • cache_clears: Counter for the total number of cache clears
  • cache_graced_hits: Counter for the total number of graced hits
  • cache_bus_messages_published: Counter for bus messages published
  • cache_bus_messages_received: Counter for bus messages received

Options:

  • useNewCollector: Set to true to use BentoCache's native plugin (recommended)
  • keyGroups: An array of [RegExp, ((match: RegExpMatchArray) => string) | string] tuples for grouping cache keys. See BentoCache documentation for more details.

[!TIP] The modern implementation will become the default in a future major release, so we recommend migrating to it now.

Legacy Implementation

The legacy implementation uses event emitters to track cache operations. This is the default for backward compatibility but will be removed in future versions.

typescript
cacheCollector({
  keyGroups: [[/^users:(\d+)$/, 'users:*']],
})

Exposed Metrics:

  • cache_hits_total: Counter for the total number of cache hits
  • cache_misses_total: Counter for the total number of cache misses
  • cache_writes_total: Counter for the total number of cache writes

Options:

  • keyGroups: An array of [RegExp, ((match: RegExpMatchArray) => string) | string] tuples for grouping cache keys together. This helps avoid high cardinality issues when you have many unique cache keys.

Example:

typescript
cacheCollector({
  keyGroups: [
    [/^users:(\d+)$/, 'users:*'],
    [/^posts:(\d+)$/, 'posts:*'],
  ],
})

Mail Collector

Adds metrics to monitor emails sent through @adonisjs/mail.

Exposed Metrics

  • mails_sent_total: Counter for the total number of emails sent.

Exemplars

Exemplars are a way to link metrics to traces. When enabled, this package will automatically attach traceId and spanId labels from the active OpenTelemetry span to your Counter and Histogram metrics.

Setup

  1. Install @opentelemetry/api as a dependency:
bash
pnpm add @opentelemetry/api
  1. Enable exemplars in your configuration:
typescript
export default defineConfig({
  enableExemplars: true,
  // ...
})

When enableExemplars is set to true, the metrics registry will automatically be set to use the OpenMetrics format, which is required for exemplars support.

How it works

When exemplars are enabled, the API for incrementing counters and observing histograms changes. Instead of simple inc() and observe() calls, you need to pass an object with labels and exemplarLabels:

typescript
// Without exemplars
counter.inc({ method: 'GET', status: '200' })
histogram.observe({ route: '/users' }, 0.5)

// With exemplars
counter.inc({
  labels: { method: 'GET', status: '200' },
  exemplarLabels: { traceId: '...', spanId: '...' },
})
histogram.observe(
  { labels: { route: '/users' }, exemplarLabels: { traceId: '...', spanId: '...' } },
  0.5,
)

This package handles this automatically: when enableExemplars is true in the global config and an active OpenTelemetry span exists, the traceId and spanId will be attached to metrics from the built-in collectors.

[!NOTE] The API change only applies to metrics created with enableExemplars: true on the metric itself. If you create custom metrics using prom-client without setting enableExemplars, you can use the standard API (counter.inc(labels)) even when the global config has enableExemplars: true. However, if you enable exemplars on your custom metric, you'll need to use the exemplar API (counter.inc({ labels, exemplarLabels, value })).

Known TypeScript issue

There's currently a typing bug where exemplarLabels is incorrectly typed. This causes TypeScript errors when using exemplars. The issue should be fixed in a future release. In the meantime, feel free to use type assertions (as any) when calling inc() or observe() with exemplarLabels.

Requirements

  • Your application must be instrumented with OpenTelemetry to have active spans
  • The @opentelemetry/api package must be installed
  • Your Prometheus server must support OpenMetrics format to scrape exemplars

Custom metrics

To add your own metrics, you have two options:

Use prom-client

You can directly use prom-client with the same registry :

typescript
import { Counter } from 'prom-client'

export const orderMetrics = new Counter({
  name: 'sent_orders',
  help: 'Total Orders Sent',
})

export default class OrderController {
  public async store({ request }: HttpContext) {
    // ...
    OrderMetric.inc()
    // ...
  }
}

Create a custom collector

You can also create a custom collector to expose your metrics:

…

Then, add your collector to the config/prometheus.ts configuration file:

typescript
export default defineConfig({
  // ...
  collectors: [
    // ...
    appOrdersCollector(),
  ],
})

Sponsors

If you like this project, please consider supporting it by sponsoring it. It will help a lot to maintain and improve it. Thanks a lot !

Issues· 1 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptadonisjsadonisjs5prometheusprometheus-exporter

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类云原生
定价开源

> 相关工具

K
Kubernetes
容器编排事实标准
H
Helm
Kubernetes 包管理器
S
seaweedfs
SeaweedFS is a distributed storage system for object storage (S3), file systems, and Iceberg tables,