Skip to content

Instantly share code, notes, and snippets.

@cdeutsch
Last active February 22, 2023 15:47
Show Gist options
  • Save cdeutsch/9a012489b0941cb711e0825b851f32db to your computer and use it in GitHub Desktop.
Save cdeutsch/9a012489b0941cb711e0825b851f32db to your computer and use it in GitHub Desktop.
Axiom Ingest Splitter - Send logs to multiple datasets
/* eslint-disable import/no-default-export */
/**
* Axiom Ingest Splitter
*
* - Run `npx wrangler dev src/index.ts` in your terminal to start a development server
* - Open a browser tab at http://localhost:8787/ to see your worker in action
* - Run `npx wrangler publish src/index.ts --name my-worker` to publish your worker
*
* Learn more at https://developers.cloudflare.com/workers/
*
*/
// These initial Types are based on bindings that don't exist in the project yet,
// you can follow the links to learn how to implement them.
export interface Env {
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
// MY_KV_NAMESPACE: KVNamespace
//
// Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/
// MY_DURABLE_OBJECT: DurableObjectNamespace
//
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
// MY_BUCKET: R2Bucket
}
interface Config {
[key: string]: string[];
}
// POST your Axiom logs to:
// https://YOUR-CLOUDFLARE-WORKER.workers.dev/MAKE_A_UNIQUE_ID_USING_A_GUID_OR_SIMILAR
const CONFIG: Config = {
'MAKE_A_UNIQUE_ID_USING_A_GUID_OR_SIMILAR': [
'https://:AXIOM_API_TOKEN@api.axiom.co/v1/datasets/YOUR_DATASET/ingest',
'https://:AXIOM_API_TOKEN@api-axiom-co-bucketid.curlhub.io/v1/datasets/ANOTHER_DATASET/ingest',
],
};
export default {
fetch: async function (request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const url = new URL(request.url);
const key = url.pathname.substring(1);
const body = await request.blob();
const endpoints = CONFIG[key];
if (endpoints) {
const fetches = [];
for (const endpoint of endpoints) {
const endpointUrl = new URL(endpoint);
console.info(`⏳️ ${endpointUrl.hostname}`, endpointUrl.password);
try {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
fetches.push(
fetch(endpoint, {
method: 'POST',
body: body,
headers: {
authorization: `Basic ${btoa(`:${endpointUrl.password}`)}`,
'content-type': 'application/json',
},
}).then((resp) => {
if (resp.status === 200) {
console.info(`✅️ ${endpointUrl.hostname}`, JSON.stringify(resp.body));
} else {
console.error(`🚩️ ${endpointUrl.hostname}`, 'status', resp.status, JSON.stringify(resp.body));
}
})
);
} catch (error) {
console.error('🚩️ ERROR:', error);
}
}
await Promise.all(fetches);
}
return new Response(`Hello from the Ingest Splitter ${request.url}`);
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment