Skip to content

Commit 4b1e860

Browse files
committed
Use different database lib
1 parent 97aa39b commit 4b1e860

File tree

5 files changed

+994
-60
lines changed

5 files changed

+994
-60
lines changed

.env.example

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
INFLUX_URL=http://localhost:8086
1+
INFLUX_HOST=http://localhost:8086
22
INFLUX_TOKEN=example
3-
INFLUX_ORG=example
4-
INFLUX_BUCKET=example
3+
INFLUX_DATABASE=example

index.js

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,44 @@
1-
import { InfluxDB, Point } from '@influxdata/influxdb-client';
1+
import { InfluxDBClient, Point } from '@influxdata/influxdb3-client';
22

3-
const INFLUX_URL = process.env.INFLUX_URL || 'http://localhost:8086';
3+
const INFLUX_HOST = process.env.INFLUX_HOST || 'http://localhost:8086';
44
const INFLUX_TOKEN = process.env.INFLUX_TOKEN || '';
5-
const INFLUX_ORG = process.env.INFLUX_ORG || '';
6-
const INFLUX_BUCKET = process.env.INFLUX_BUCKET || '';
5+
const INFLUX_DATABASE = process.env.INFLUX_DATABASE || '';
76

8-
const client = new InfluxDB({ url: INFLUX_URL, token: INFLUX_TOKEN });
9-
10-
// global vars
117
async function handleRequest(request, env, ctx) {
12-
// TODO: Replace
13-
return await fetch('http://httpbin.org/status/200');
8+
return await fetch(request);
149
}
1510

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

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

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

4320
const point = new Point('request')
4421
.tag('url', url.toString())
4522
.tag('hostname', url.hostname)
4623
.tag('pathname', url.pathname)
4724
.tag('method', request.method)
4825
.tag('cf_cache', cache)
49-
.tag('country', country)
5026
.tag('service', service)
51-
.tag('visitor', visitor)
5227
.timestamp(today);
5328

5429
return point;
5530
}
5631

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

6237
console.log(point)
63-
writeApi.writePoint(point);
64-
38+
await client.write(INFLUX_DATABASE, point);
6539
}
6640

67-
ctx.waitUntil(
68-
writeApi.close()
69-
)
41+
await writeApi.close()
7042
}
7143

7244
export default {

0 commit comments

Comments
 (0)