Skip to content

Commit

Permalink
Remove influxdb lib
Browse files Browse the repository at this point in the history
  • Loading branch information
pyropy committed Dec 9, 2024
1 parent 91c9737 commit 05d725f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1,152 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ node_modules/
.wrangler
data/
config/
wranger.toml
wrangler.toml
77 changes: 49 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,59 @@
import { InfluxDBClient, Point } from '@influxdata/influxdb3-client';
import zlib from 'node:zlib';
import { promisify } from 'node:util';

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

async function formatMetricPoint(request) {
function getRequestData(request, response, startTime, endTime) {
const timestamp = Date.now();
const originResponse = response || {};
const url = new URL(request.url);
const today = new Date();
const origin = request.headers.get("origin") ?? "";
const cache = request.headers.get("cf-cache-status") ?? "unknown";
const service = new URL(origin).hostname.replaceAll(".", "-");
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('service', service)
.timestamp(today);

return point;
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');

return {
'timestamp': timestamp,
'url': request.url,
'method': request.method,
'status': originResponse.status,
'originTime': (endTime - startTime),
'cfCache': (originResponse) ? (response.headers.get('CF-Cache-Status') || 'miss') : 'miss',
'apiKey': apiKey,
};
}

function formatRequestData(data, env) {
const url = new URL(data.url);
// We're setting field value to 1 to count the number of requests
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}`
}

async function reportMetric(request) {
const client = new InfluxDBClient({ host: INFLUX_HOST, token: INFLUX_TOKEN });
const point = formatMetricPoint(request);
await client.write(INFLUX_DATABASE, point);
await client.close()
async function reportMetric(request, response, startTime, endTime, env) {
const compress = promisify(zlib.gzip);
const reqData = getRequestData(request, response, startTime, endTime);
const line = formatRequestData(reqData, env);
console.log(line);

// Define API endpoint and headers
const url = `${env.INFLUX_URL}/api/v2/write?&bucket=${env.INFLUX_DATABASE}&precision=ms`;

// Compress the string using gzip
const compressedData = await compress(line);

// Make the POST request
return fetch(url, {
method: 'POST',
headers: {
'Authorization': `Token ${env.INFLUX_TOKEN}`,
'Content-Encoding': 'gzip',
'Content-Type': 'text/plain; charset=utf-8',
},
body: compressedData,
})
}

export default {
async fetch(request, env, ctx) {
const resp = await fetch(request);
ctx.waitUntil(reportMetric(request));
return resp;
const reqStartTime = Date.now();
const response = await fetch('https://example.com');
const reqEndTime = Date.now();
ctx.waitUntil(reportMetric(request, response, reqStartTime, reqEndTime, env));
return response;
}
}
Loading

0 comments on commit 05d725f

Please sign in to comment.