Skip to content

Commit 1eb64bc

Browse files
committed
Batch metrics sending and add CI
1 parent bb02f04 commit 1eb64bc

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

.github/workflows/ci.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
ci:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- uses: actions/setup-node@v4
15+
with:
16+
node-version: 24
17+
cache: npm
18+
- run: npm ci
19+
- run: npm run lint
20+
- run: npm run typecheck
21+
- run: npm test

src/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Split an array into chunks of a specified size.
3+
*/
4+
export function chunk<T>(array: T[], size: number): T[][] {
5+
const chunks: T[][] = [];
6+
for (let i = 0; i < array.length; i += size) {
7+
chunks.push(array.slice(i, i + size));
8+
}
9+
return chunks;
10+
}

src/workflow.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import {
33
type WorkflowEvent,
44
type WorkflowStep,
55
} from "cloudflare:workers";
6+
import pAll from "p-all";
67
import { createCloudflareApi } from "./api/cloudflare";
78
import { createDatadogApi } from "./api/datadog";
89
import { formatHealthMetrics, formatMetricsForContainer } from "./metrics";
10+
import { chunk } from "./utils";
911

1012
/**
1113
* Calculate the metrics time window.
@@ -40,6 +42,11 @@ export class MetricsExporterWorkflow extends WorkflowEntrypoint<Env> {
4042
this.env.CLOUDFLARE_API_TOKEN,
4143
);
4244

45+
const datadog = createDatadogApi(
46+
this.env.DATADOG_API_KEY,
47+
this.env.DATADOG_SITE,
48+
);
49+
4350
const { start, end } = getMetricsTimeWindow();
4451
console.log("Workflow started", {
4552
start: start.toISOString(),
@@ -57,10 +64,6 @@ export class MetricsExporterWorkflow extends WorkflowEntrypoint<Env> {
5764
this.env.CLOUDFLARE_ACCOUNT_ID,
5865
result,
5966
);
60-
const datadog = createDatadogApi(
61-
this.env.DATADOG_API_KEY,
62-
this.env.DATADOG_SITE,
63-
);
6467
await datadog.sendMetrics(healthMetrics);
6568

6669
return result.map((c) => ({ id: c.id, name: c.name }));
@@ -86,18 +89,25 @@ export class MetricsExporterWorkflow extends WorkflowEntrypoint<Env> {
8689
metricsGroups,
8790
);
8891

89-
if (metrics.length > 0) {
90-
const datadog = createDatadogApi(
91-
this.env.DATADOG_API_KEY,
92-
this.env.DATADOG_SITE,
93-
);
94-
await datadog.sendMetrics(metrics);
95-
}
92+
const batches = chunk(metrics, 25000);
93+
94+
await pAll(
95+
batches.map(
96+
(batch, i) => () =>
97+
step.do(
98+
`send batch ${i + 1}/${batches.length} (${batch.length} metrics)`,
99+
STEP_CONFIG,
100+
async () => {
101+
await datadog.sendMetrics(batch);
102+
},
103+
),
104+
),
105+
{ concurrency: 6 },
106+
);
96107

97108
return metrics.length;
98109
},
99110
);
100-
101111
totalMetrics += count;
102112
}
103113

0 commit comments

Comments
 (0)