Skip to content

Commit

Permalink
Use different database lib
Browse files Browse the repository at this point in the history
  • Loading branch information
pyropy committed Dec 6, 2024
1 parent 97aa39b commit 4b1e860
Show file tree
Hide file tree
Showing 5 changed files with 994 additions and 60 deletions.
5 changes: 2 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
INFLUX_URL=http://localhost:8086
INFLUX_HOST=http://localhost:8086
INFLUX_TOKEN=example
INFLUX_ORG=example
INFLUX_BUCKET=example
INFLUX_DATABASE=example
42 changes: 7 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,44 @@
import { InfluxDB, Point } from '@influxdata/influxdb-client';
import { InfluxDBClient, Point } from '@influxdata/influxdb3-client';

const INFLUX_URL = process.env.INFLUX_URL || 'http://localhost:8086';
const INFLUX_HOST = process.env.INFLUX_HOST || 'http://localhost:8086';
const INFLUX_TOKEN = process.env.INFLUX_TOKEN || '';
const INFLUX_ORG = process.env.INFLUX_ORG || '';
const INFLUX_BUCKET = process.env.INFLUX_BUCKET || '';
const INFLUX_DATABASE = process.env.INFLUX_DATABASE || '';

const client = new InfluxDB({ url: INFLUX_URL, token: INFLUX_TOKEN });

// global vars
async function handleRequest(request, env, ctx) {
// TODO: Replace
return await fetch('http://httpbin.org/status/200');
return await fetch(request);
}

async function formatMetricPoint(request) {
const url = new URL(request.url);
const today = new Date();

const origin = request.headers.get("origin") ?? "";
const ip = request.headers.get("cf-connecting-ip") ?? "";
const userAgent = request.headers.get("user-agent") ?? "";
const country = request.headers.get("cf-ipcountry") ?? "unknown";
const cache = request.headers.get("cf-cache-status") ?? "unknown";
const service = new URL(origin).hostname.replaceAll(".", "-");

/**
* For every origin that reports a page_view, visitors get a unique ID every
* day. We don't log their IPs / UserAgents, but we do use them to calculate
* their IDs. Visitor IDs let us determine uniqueness.
*
* This is also the strategy Plausible uses, and is a great balance between
* usefulness and privacy.
*/
const visitorDigest = await crypto.subtle.digest(
"SHA-256",
encoder.encode(today.toDateString() + ip + userAgent),
);
const visitor = Array.from(new Uint8Array(visitorDigest))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");

const point = new Point('request')
.tag('url', url.toString())
.tag('hostname', url.hostname)
.tag('pathname', url.pathname)
.tag('method', request.method)
.tag('cf_cache', cache)
.tag('country', country)
.tag('service', service)
.tag('visitor', visitor)
.timestamp(today);

return point;
}

async function handleMetrics(events, env, ctx) {
const writeApi = client.getWriteApi(INFLUX_ORG, INFLUX_BUCKET);
const client = new InfluxDBClient({ host: INFLUX_HOST, token: INFLUX_TOKEN });
for (const event of events) {
const point = formatMetricPoint(event.request);

console.log(point)
writeApi.writePoint(point);

await client.write(INFLUX_DATABASE, point);
}

ctx.waitUntil(
writeApi.close()
)
await writeApi.close()
}

export default {
Expand Down
Loading

0 comments on commit 4b1e860

Please sign in to comment.