Skip to content

Commit

Permalink
Initialize logging worker
Browse files Browse the repository at this point in the history
  • Loading branch information
krasun committed Jan 12, 2024
1 parent 1f1c6e2 commit 19e3ef7
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 75 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Deploy Worker

on:
push:
branches:
- main

jobs:
deploy:
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- uses: actions/checkout@v3
- name: Build & Deploy Worker
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CF_API_TOKEN }}
accountId: ${{ secrets.CF_ACCOUNT_ID }}
vars: LOKI_PUSH_URL
env:
LOKI_PUSH_URL: ${{ secrets.LOKI_PUSH_URL }}
90 changes: 64 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,70 @@
/**
* Welcome to Cloudflare Workers! This is your first worker.
*
* - Run `npm run dev` in your terminal to start a development server
* - Open a browser tab at http://localhost:8787/ to see your worker in action
* - Run `npm run deploy` to publish your worker
*
* Learn more at https://developers.cloudflare.com/workers/
*/

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;
//
// Example binding to a Service. Learn more at https://developers.cloudflare.com/workers/runtime-apis/service-bindings/
// MY_SERVICE: Fetcher;
//
// Example binding to a Queue. Learn more at https://developers.cloudflare.com/queues/javascript-apis/
// MY_QUEUE: Queue;
LOKI_PUSH_URL: string;
}

export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
return new Response('Hello World!');
async tail(events: TraceItem[], env: Env) {
const data = this.transformEvents(events);
if (data.streams.length == 0) {
return;
}

fetch(env.LOKI_PUSH_URL, {
method: 'POST',
body: JSON.stringify(data),
});
},

transformEvents(events: TraceItem[]) {
const streams: { stream: Record<string, string>; values: [number, string][] }[] = [];
for (const event of events) {
this.transformEvent(event).forEach((stream) => streams.push(stream));
}

return { streams };
},

transformEvent(event: TraceItem) {
if (!(event.outcome == 'ok' || event.outcome == 'exception') || !event.scriptName) {
return [];
}

const streams: { stream: Record<string, string>; values: [number, string][] }[] = [];

const logsByLevel: Record<string, [number, string][]> = {};
for (const log of event.logs) {
if (!(log.level in logsByLevel)) {
logsByLevel[log.level] = [];
}
logsByLevel[log.level].push([log.timestamp * 100000, log.message]);
}

for (const [level, logs] of Object.entries(logsByLevel)) {
if (level == 'debug') {
continue;
}

streams.push({
stream: {
level,
outcome: event.outcome,
app: event.scriptName,
},
values: logs,
});
}

if (event.exceptions.length) {
streams.push({
stream: {
level: 'error',
outcome: event.outcome,
app: event.scriptName,
},
values: event.exceptions.map((e) => [e.timestamp * 100000, `${e.name}: ${e.message}`]),
});
}

return streams;
},
};
51 changes: 2 additions & 49 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,51 +1,4 @@
name = "cflokiworker"
name = "logger"
main = "src/index.ts"
compatibility_date = "2023-12-18"

# Variable bindings. These are arbitrary, plaintext strings (similar to environment variables)
# Note: Use secrets to store sensitive data.
# Docs: https://developers.cloudflare.com/workers/platform/environment-variables
# [vars]
# MY_VARIABLE = "production_value"

# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs.
# Docs: https://developers.cloudflare.com/workers/runtime-apis/kv
# [[kv_namespaces]]
# binding = "MY_KV_NAMESPACE"
# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# Bind an R2 Bucket. Use R2 to store arbitrarily large blobs of data, such as files.
# Docs: https://developers.cloudflare.com/r2/api/workers/workers-api-usage/
# [[r2_buckets]]
# binding = "MY_BUCKET"
# bucket_name = "my-bucket"

# Bind a Queue producer. Use this binding to schedule an arbitrary task that may be processed later by a Queue consumer.
# Docs: https://developers.cloudflare.com/queues/get-started
# [[queues.producers]]
# binding = "MY_QUEUE"
# queue = "my-queue"

# Bind a Queue consumer. Queue Consumers can retrieve tasks scheduled by Producers to act on them.
# Docs: https://developers.cloudflare.com/queues/get-started
# [[queues.consumers]]
# queue = "my-queue"

# Bind another Worker service. Use this binding to call another Worker without network overhead.
# Docs: https://developers.cloudflare.com/workers/platform/services
# [[services]]
# binding = "MY_SERVICE"
# service = "my-service"

# Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model.
# Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps.
# Docs: https://developers.cloudflare.com/workers/runtime-apis/durable-objects
# [[durable_objects.bindings]]
# name = "MY_DURABLE_OBJECT"
# class_name = "MyDurableObject"

# Durable Object migrations.
# Docs: https://developers.cloudflare.com/workers/learning/using-durable-objects#configure-durable-object-classes-with-migrations
# [[migrations]]
# tag = "v1"
# new_classes = ["MyDurableObject"]
workers_dev = false

0 comments on commit 19e3ef7

Please sign in to comment.