1- import zlib from 'node:zlib' ;
2- import { promisify } from 'node:util' ;
3-
41/*
5- * Sends compressed request metrics to InfluxDB
2+ * Sends request metrics to InfluxDB
63* https://docs.influxdata.com/enterprise_influxdb/v1/guides/write_data/
74*/
8- export const reportMetric = async ( request , response , env ) => {
9- const compress = promisify ( zlib . gzip ) ;
10-
5+ export const reportMetric = async ( request , env ) => {
116 // Define API endpoint and headers
127 const url = `${ env . INFLUX_URL } /api/v2/write?&bucket=${ env . INFLUX_DATABASE } &precision=ms` ;
138
14- return fetch ( url , {
9+ await fetch ( url , {
1510 method : 'POST' ,
1611 headers : {
1712 'Authorization' : `Token ${ env . INFLUX_TOKEN } ` ,
18- 'Content-Encoding' : 'gzip' ,
19- 'Content-Type' : 'text/plain; charset=utf-8' ,
13+ 'Content-Type' : 'application/octet-stream'
2014 } ,
21- body : await compress ( createMetricsFromRequest ( request , response , env ) ) ,
15+ body : createMetricsFromRequest ( request , env ) ,
2216 } )
2317}
2418
2519/*
2620* Returns request metrics in InfluxDB line protocol format
2721* https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/
2822*/
29- export const createMetricsFromRequest = ( request , response = { } , env ) => {
23+ export const createMetricsFromRequest = ( request , env ) => {
3024 const url = new URL ( request . url ) ;
3125 const timestamp = Date . now ( ) ;
32- const apiKey = request . headers . get ( 'api-key' ) ||
26+ const apiKey = request . headers . get ( 'api-key' ) ||
3327 url . searchParams . get ( 'api-key' ) || (
34- request . headers . get ( 'Authorization' ) ?. startsWith ( 'Bearer ' )
35- ? request . headers . get ( 'Authorization' ) . substring ( 7 )
36- : 'unknown'
28+ request . headers . get ( 'Authorization' ) ?. startsWith ( 'Bearer ' )
29+ ? request . headers . get ( 'Authorization' ) . substring ( 7 )
30+ : 'unknown'
3731 ) ;
38- const cfCacheStatus = response ?. headers . get ( 'CF-Cache-Status' ) ?? 'miss'
39- const formattedUrl = url . toString ( ) . replaceAll ( '=' , '\\=' ) ;
4032
4133 // We're setting field value to 1 to count the number of requests
42- 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 } `
34+ return `${ env . INFLUX_METRIC_NAME } ,api_key= "${ apiKey } " value=1 ${ timestamp } `
4335}
0 commit comments