Last active
February 22, 2023 15:47
-
-
Save cdeutsch/9a012489b0941cb711e0825b851f32db to your computer and use it in GitHub Desktop.
Axiom Ingest Splitter - Send logs to multiple datasets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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://:[email protected]/v1/datasets/YOUR_DATASET/ingest', | |
'https://:[email protected]/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