Skip to content

Commit d27848d

Browse files
committed
Move all influx db related code to lib/influx.js
1 parent 882792d commit d27848d

File tree

8 files changed

+74
-73
lines changed

8 files changed

+74
-73
lines changed

bin/worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { reportMetric } from "../lib/metrics";
1+
import { reportMetric } from "../lib/influx.js";
22

33
export const handleRequest = async (request, env) => {
44
const url = new URL(request.url);

lib/influx.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import zlib from 'node:zlib';
2+
import { promisify } from 'node:util';
3+
4+
/*
5+
* Sends compressed request metrics to InfluxDB
6+
* https://docs.influxdata.com/enterprise_influxdb/v1/guides/write_data/
7+
*/
8+
export const reportMetric = async (request, response, env) => {
9+
const compress = promisify(zlib.gzip);
10+
11+
// Define API endpoint and headers
12+
const url = `${env.INFLUX_URL}/api/v2/write?&bucket=${env.INFLUX_DATABASE}&precision=ms`;
13+
14+
return fetch(url, {
15+
method: 'POST',
16+
headers: {
17+
'Authorization': `Token ${env.INFLUX_TOKEN}`,
18+
'Content-Encoding': 'gzip',
19+
'Content-Type': 'text/plain; charset=utf-8',
20+
},
21+
body: await compress(createMetricsFromRequest(request, response, env)),
22+
})
23+
}
24+
25+
/*
26+
* Returns request metrics in InfluxDB line protocol format
27+
* https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/
28+
*/
29+
export const createMetricsFromRequest = (request, response = {}, env) => {
30+
const url = new URL(request.url);
31+
const timestamp = Date.now();
32+
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');
33+
const cfCacheStatus = response?.headers.get('CF-Cache-Status') ?? 'miss'
34+
const formattedUrl = url.toString().replaceAll('=', '\\=');
35+
36+
// We're setting field value to 1 to count the number of requests
37+
return `${env.INFLUX_METRIC_NAME},status_code=${response.status},url=${formattedUrl},hostname=${url.hostname},pathname="${url.pathname}",method=${request.method},cf_cache_status=${cfCacheStatus},api_key=${apiKey} value=1 ${timestamp}`
38+
}

lib/metrics.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

lib/request.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/metrics.test.js renamed to test/influx.test.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect, vi } from 'vitest';
2-
import { reportMetric } from '../lib/metrics.js';
2+
import { reportMetric, createMetricsFromRequest } from '../lib/influx.js';
33

44
describe('reportMetric', () => {
55
it('should report metrics correctly', async () => {
@@ -40,4 +40,36 @@ describe('reportMetric', () => {
4040
})
4141
);
4242
});
43+
});
44+
45+
describe('createMetricsFromRequest', () => {
46+
it('should format request data correctly', () => {
47+
const request = {
48+
url: 'https://example.com/path?api-key=test-key',
49+
method: 'GET',
50+
headers: new Map([
51+
['api-key', 'test-key'],
52+
['Authorization', 'Bearer test-token']
53+
])
54+
};
55+
const response = {
56+
status: 200,
57+
headers: new Map([
58+
['CF-Cache-Status', 'HIT']
59+
])
60+
};
61+
const env = {
62+
INFLUX_METRIC: 'test_metric'
63+
};
64+
65+
const result = createMetricsFromRequest(request, response, env);
66+
expect(result).toContain('test_metric');
67+
expect(result).toContain('status_code=200');
68+
expect(result).toContain('url=https://example.com/path?api-key\\=test-key');
69+
expect(result).toContain('hostname=example.com');
70+
expect(result).toContain('pathname="/path"');
71+
expect(result).toContain('method=GET');
72+
expect(result).toContain('cf_cache_status=HIT');
73+
expect(result).toContain('api_key=test-key');
74+
});
4375
});

test/request.test.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

test/worker.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, it, expect, vi } from 'vitest';
22
import worker, { handleRequest } from '../bin/worker.js';
3-
import { reportMetric } from '../lib/metrics.js';
3+
import { reportMetric } from '../lib/influx.js';
44

55
describe('handleRequest', () => {
66
it('should handle request correctly', async () => {

wrangler.toml.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ compatibility_flags = ["nodejs_compat"]
55

66
[vars]
77
REQUEST_URL = "https://example.com"
8-
INFLUX_METRIC = "example"
8+
INFLUX_METRIC_NAME = "example"
99
INFLUX_URL = "http://localhost:8086"
1010
INFLUX_TOKEN = "example"
1111
INFLUX_DATABASE = "example"

0 commit comments

Comments
 (0)