跳转到主要内容

用例

探索时间范围蓝图支持的常见场景:

无服务器函数

根据函数执行时间和内存使用情况计费。

容器运行时

跟踪容器运行时间以进行基于使用的计费。

计算实例

监控虚拟机运行时间,并按分钟或小时收费。

后台作业

跟踪数据导出、报告和批处理作业的处理时间。
非常适合基于计算时间、函数执行持续时间、容器运行时或任何基于时间的使用进行计费。

快速开始

按时间持续时间跟踪资源使用:
1

安装 SDK

npm install @dodopayments/ingestion-blueprints
2

获取您的 API 密钥

3

创建计量器

在您的 Dodo Payments Dashboard 中创建计量器:
  • 事件名称time_range_usage(或您喜欢的名称)
  • 聚合类型sum 以跟踪总持续时间
  • 超属性durationSecondsdurationMinutes,或 durationMs
4

跟踪时间使用

import { Ingestion, trackTimeRange } from '@dodopayments/ingestion-blueprints';

const ingestion = new Ingestion({
  apiKey: process.env.DODO_PAYMENTS_API_KEY,
  environment: 'test_mode',
  eventName: 'function_execution'
});

// Track function execution time
const startTime = Date.now();

// Execute your function (example: image processing)
const result = await yourImageProcessingLogic();

const durationMs = Date.now() - startTime;

await trackTimeRange(ingestion, {
  customerId: 'customer_123',
  durationMs: durationMs
});

配置

数据摄取配置

apiKey
string
required
您从仪表板获取的 Dodo Payments API 密钥。
environment
string
required
环境模式:test_modelive_mode
eventName
string
required
与您的计量器配置匹配的事件名称。

跟踪时间范围选项

customerId
string
required
用于计费归属的客户 ID。
durationMs
number
持续时间(毫秒)。用于亚秒精度。
durationSeconds
number
持续时间(秒)。最常用于函数执行和短任务。
durationMinutes
number
持续时间(分钟)。适用于运行时间较长的资源,如虚拟机。
metadata
object
关于资源的可选元数据,如 CPU、内存、区域等。

最佳实践

选择正确的单位:对于短操作使用毫秒,对于函数使用秒,对于运行时间较长的资源使用分钟。
准确计时:使用 Date.now()performance.now() 进行准确的时间跟踪,特别是对于无服务器函数。