@@ -35,6 +35,7 @@ const REQUESTS_PER_MINUTE_UNIT =
3535const MINUTE_UNIT = Number ( Deno . env . get ( "MINUTE_UNIT" ) ) ;
3636const AGENT_TOKEN = Deno . env . get ( "AGENT_TOKEN" ) ;
3737const PORT = Number ( Deno . env . get ( "PORT" ) ) || 8000 ;
38+ const NAME = Deno . env . get ( "NAME" ) ?? 'no name configured'
3839if ( ! AGENT_TOKEN ) {
3940 throw new Error ( "No AGENT_TOKEN set in env variables. Exiting agent..." ) ;
4041}
@@ -64,6 +65,7 @@ console.log(
6465console . log ( `SEND_REQUESTS: ${ SEND_REQUESTS } ` ) ;
6566console . log ( `SERVER_URL: ${ SERVER_URL } ` ) ;
6667console . log ( `PORT: ${ PORT } ` ) ;
68+ console . log ( `NAME: ${ NAME } ` )
6769
6870async function getMemoryStats ( ) : Promise < MemoryInfo > {
6971 const { total, available, active } = await mem ( ) ;
@@ -196,6 +198,7 @@ async function getContainersInfo(): Promise<ContainerInfo[]> {
196198
197199async function getMetrics ( ) : Promise < Metrics > {
198200 return {
201+ name : NAME ,
199202 timestamp : Date . now ( ) ,
200203 memory : await getMemoryStats ( ) ,
201204 battery : await getBatteryStats ( ) ,
@@ -292,10 +295,11 @@ setInterval(async () => {
292295 } ) ;
293296 const parsedResponse = await response . json ( ) ;
294297 if ( parsedResponse ?. statusCode !== 201 ) {
295- throw new Error ( `Error sending metrics ` + parsedResponse ?. statusMessage ) ;
298+ throw new Error ( `Error sending metrics ` + parsedResponse ?. statusMessage + JSON . stringify ( parsedResponse ?. data , null , 2 ) ) ;
296299 }
297- } catch ( error ) {
298- console . error ( error ) ;
300+ } catch ( error :unknown ) {
301+ console . log ( error )
302+ console . log ( 'tried sending: ' , JSON . stringify ( metrics , null , 2 ) )
299303 }
300304} , requestIntervalMilliSeconds ) ;
301305
@@ -311,7 +315,7 @@ async function handler(_req: Request): Promise<Response> {
311315}
312316
313317const ws = new CommandResponderWebsocketClient ( {
314- url : `${ SERVER_URL } /api/ws?type=server` ,
318+ url : `${ SERVER_URL } /api/ws?type=server&name= ${ NAME } ` ,
315319} ) ;
316320
317321ws . connect ( ) ;
0 commit comments