Skip to content

Commit 05d725f

Browse files
committed
Remove influxdb lib
1 parent 91c9737 commit 05d725f

File tree

5 files changed

+63
-1152
lines changed

5 files changed

+63
-1152
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ node_modules/
1212
.wrangler
1313
data/
1414
config/
15-
wranger.toml
15+
wrangler.toml

index.js

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,59 @@
1-
import { InfluxDBClient, Point } from '@influxdata/influxdb3-client';
1+
import zlib from 'node:zlib';
2+
import { promisify } from 'node:util';
23

3-
const INFLUX_HOST = process.env.INFLUX_HOST || 'http://localhost:8086';
4-
const INFLUX_TOKEN = process.env.INFLUX_TOKEN || '';
5-
const INFLUX_DATABASE = process.env.INFLUX_DATABASE || '';
6-
7-
async function formatMetricPoint(request) {
4+
function getRequestData(request, response, startTime, endTime) {
5+
const timestamp = Date.now();
6+
const originResponse = response || {};
87
const url = new URL(request.url);
9-
const today = new Date();
10-
const origin = request.headers.get("origin") ?? "";
11-
const cache = request.headers.get("cf-cache-status") ?? "unknown";
12-
const service = new URL(origin).hostname.replaceAll(".", "-");
13-
const point = new Point('request')
14-
.tag('url', url.toString())
15-
.tag('hostname', url.hostname)
16-
.tag('pathname', url.pathname)
17-
.tag('method', request.method)
18-
.tag('cf_cache', cache)
19-
.tag('service', service)
20-
.timestamp(today);
21-
22-
return point;
8+
const apiKey = request.headers.get('api-key') || url.searchParams.get('api-key') || (request.headers.get('Authorization')?.startsWith('Bearer ') ? request.headers.get('Authorization').substring(7) : 'unknown');
9+
10+
return {
11+
'timestamp': timestamp,
12+
'url': request.url,
13+
'method': request.method,
14+
'status': originResponse.status,
15+
'originTime': (endTime - startTime),
16+
'cfCache': (originResponse) ? (response.headers.get('CF-Cache-Status') || 'miss') : 'miss',
17+
'apiKey': apiKey,
18+
};
19+
}
20+
21+
function formatRequestData(data, env) {
22+
const url = new URL(data.url);
23+
// We're setting field value to 1 to count the number of requests
24+
return `${env.INFLUX_METRIC},status_code=${data.status},url=${data.url},hostname=${url.hostname},pathname=${url.pathname},method=${data.method},cf_cache=${data.cfCache},api_key=${data.apiKey} value=1 ${data.timestamp}`
2325
}
2426

25-
async function reportMetric(request) {
26-
const client = new InfluxDBClient({ host: INFLUX_HOST, token: INFLUX_TOKEN });
27-
const point = formatMetricPoint(request);
28-
await client.write(INFLUX_DATABASE, point);
29-
await client.close()
27+
async function reportMetric(request, response, startTime, endTime, env) {
28+
const compress = promisify(zlib.gzip);
29+
const reqData = getRequestData(request, response, startTime, endTime);
30+
const line = formatRequestData(reqData, env);
31+
console.log(line);
32+
33+
// Define API endpoint and headers
34+
const url = `${env.INFLUX_URL}/api/v2/write?&bucket=${env.INFLUX_DATABASE}&precision=ms`;
35+
36+
// Compress the string using gzip
37+
const compressedData = await compress(line);
38+
39+
// Make the POST request
40+
return fetch(url, {
41+
method: 'POST',
42+
headers: {
43+
'Authorization': `Token ${env.INFLUX_TOKEN}`,
44+
'Content-Encoding': 'gzip',
45+
'Content-Type': 'text/plain; charset=utf-8',
46+
},
47+
body: compressedData,
48+
})
3049
}
3150

3251
export default {
3352
async fetch(request, env, ctx) {
34-
const resp = await fetch(request);
35-
ctx.waitUntil(reportMetric(request));
36-
return resp;
53+
const reqStartTime = Date.now();
54+
const response = await fetch('https://example.com');
55+
const reqEndTime = Date.now();
56+
ctx.waitUntil(reportMetric(request, response, reqStartTime, reqEndTime, env));
57+
return response;
3758
}
3859
}

0 commit comments

Comments
 (0)