跳转到主要内容

用例

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

Serverless Functions

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

Container Runtime

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

Compute Instances

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

Background Jobs

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

快速开始

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

Install the SDK

npm install @dodopayments/ingestion-blueprints
2

Get Your API Keys

3

Create a Meter

Dodo Payments Dashboard 中创建一个计量器:
  • Event Nametime_range_usage(或您喜欢的名称)
  • Aggregation Typesum 以跟踪总时长
  • Over PropertydurationSecondsdurationMinutesdurationMs
4

Track Time Usage

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
必填
来自仪表板的 Dodo Payments API 密钥。
environment
string
必填
环境模式:test_modelive_mode
eventName
string
必填
与您的计量器配置匹配的事件名称。

跟踪时间范围选项

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

最佳实践

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