사용 사례
API Gateway Blueprint에서 지원하는 일반적인 시나리오를 살펴보세요:
API-as-a-Service
API 플랫폼에서 고객별 사용량을 추적하고 호출 수를 기준으로 요금 청구.
Rate Limiting
API 사용 패턴을 모니터링하고 사용량 기반 속도 제한을 구현.
Performance Monitoring
청구 데이터와 함께 응답 시간 및 오류율을 추적.
Multi-Tenant SaaS
다양한 엔드포인트에서 API 소비를 기준으로 고객에게 청구.
API 엔드포인트 사용량 추적, 속도 제한, 사용량 기반 API 청구 구현에 적합.
빠른 시작
고용량 시나리오를 위한 자동 배치로 게이트웨이 수준에서 API 호출을 추적합니다:
Install the SDK
npm install @dodopayments/ingestion-blueprints
Create a Meter
다음에서 미터를 생성하세요: Dodo Payments Dashboard
- 이벤트 이름:
api_call (또는 선호하는 이름)
- 집계 유형:
count (호출 수 추적용)
- 응답 시간, 상태 코드 등과 같은 메타데이터를 추적하는 경우 추가 속성을 구성합니다.
Track API Calls
import { Ingestion, trackAPICall } from '@dodopayments/ingestion-blueprints';
const ingestion = new Ingestion({
apiKey: process.env.DODO_PAYMENTS_API_KEY,
environment: 'test_mode',
eventName: 'api_call'
});
// Track a single API call
await trackAPICall(ingestion, {
customerId: 'customer_123',
metadata: {
endpoint: '/api/v1/users',
method: 'GET',
status_code: 200,
response_time_ms: 45
}
});
import { Ingestion, createBatch } from '@dodopayments/ingestion-blueprints';
const ingestion = new Ingestion({
apiKey: process.env.DODO_PAYMENTS_API_KEY,
environment: 'live_mode',
eventName: 'api_call'
});
// Create batch for high-volume tracking
const batch = createBatch(ingestion, {
maxSize: 100, // Flush after 100 events
flushInterval: 5000 // Or flush every 5 seconds
});
// Add API calls to batch
batch.add({
customerId: 'customer_123',
metadata: {
endpoint: '/api/v1/products',
method: 'GET',
status_code: 200
}
});
// Clean up when done
await batch.cleanup();
import express from 'express';
import { Ingestion, createBatch } from '@dodopayments/ingestion-blueprints';
const app = express();
const ingestion = new Ingestion({
apiKey: process.env.DODO_PAYMENTS_API_KEY,
environment: 'live_mode',
eventName: 'api_call'
});
const batch = createBatch(ingestion, {
maxSize: 50,
flushInterval: 10000
});
// Middleware to track all API calls
app.use((req, res, next) => {
const startTime = Date.now();
res.on('finish', () => {
const responseTime = Date.now() - startTime;
batch.add({
customerId: req.user?.id || 'anonymous',
metadata: {
endpoint: req.path,
method: req.method,
status_code: res.statusCode,
response_time_ms: responseTime
}
});
});
next();
});
// Cleanup on shutdown
process.on('SIGTERM', async () => {
await batch.cleanup();
process.exit(0);
});
수집 구성
대시보드에서 가져온 Dodo Payments API 키입니다.
환경 모드: test_mode 또는 live_mode.
API 호출 추적 옵션
API 호출에 대한 선택적 메타데이터(엔드포인트, 메서드, 상태 코드, 응답 시간 등).
배치 구성
자동 플러시 전 최대 이벤트 수. 기본값: 100.
자동 플러시 간격(밀리초). 기본값: 5000 (5초).
모범 사례
대용량에는 배칭 사용: 초당 10건 이상의 요청을 처리하는 애플리케이션은 createBatch()를 사용하여 오버헤드를 줄이고 성능을 개선하세요.
항상 배치 정리: 애플리케이션 종료 시 batch.cleanup()를 호출하여 대기 중인 이벤트를 플러시하고 데이터 손실을 방지하세요.