|
1 | | -import { InfluxDB, Point } from '@influxdata/influxdb-client'; |
| 1 | +import { InfluxDBClient, Point } from '@influxdata/influxdb3-client'; |
2 | 2 |
|
3 | | -const INFLUX_URL = process.env.INFLUX_URL || 'http://localhost:8086'; |
| 3 | +const INFLUX_HOST = process.env.INFLUX_HOST || 'http://localhost:8086'; |
4 | 4 | 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 || ''; |
7 | 6 |
|
8 | | -const client = new InfluxDB({ url: INFLUX_URL, token: INFLUX_TOKEN }); |
9 | | - |
10 | | -// global vars |
11 | 7 | async function handleRequest(request, env, ctx) { |
12 | | - // TODO: Replace |
13 | | - return await fetch('http://httpbin.org/status/200'); |
| 8 | + return await fetch(request); |
14 | 9 | } |
15 | 10 |
|
16 | 11 | async function formatMetricPoint(request) { |
17 | 12 | const url = new URL(request.url); |
18 | 13 | const today = new Date(); |
19 | 14 |
|
20 | 15 | 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"; |
24 | 16 | const cache = request.headers.get("cf-cache-status") ?? "unknown"; |
25 | 17 | const service = new URL(origin).hostname.replaceAll(".", "-"); |
26 | 18 |
|
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(""); |
42 | 19 |
|
43 | 20 | const point = new Point('request') |
44 | 21 | .tag('url', url.toString()) |
45 | 22 | .tag('hostname', url.hostname) |
46 | 23 | .tag('pathname', url.pathname) |
47 | 24 | .tag('method', request.method) |
48 | 25 | .tag('cf_cache', cache) |
49 | | - .tag('country', country) |
50 | 26 | .tag('service', service) |
51 | | - .tag('visitor', visitor) |
52 | 27 | .timestamp(today); |
53 | 28 |
|
54 | 29 | return point; |
55 | 30 | } |
56 | 31 |
|
57 | 32 | 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 }); |
59 | 34 | for (const event of events) { |
60 | 35 | const point = formatMetricPoint(event.request); |
61 | 36 |
|
62 | 37 | console.log(point) |
63 | | - writeApi.writePoint(point); |
64 | | - |
| 38 | + await client.write(INFLUX_DATABASE, point); |
65 | 39 | } |
66 | 40 |
|
67 | | - ctx.waitUntil( |
68 | | - writeApi.close() |
69 | | - ) |
| 41 | + await writeApi.close() |
70 | 42 | } |
71 | 43 |
|
72 | 44 | export default { |
|
0 commit comments