@@ -789,6 +789,110 @@ test('display_host facts', async (t) => {
789789 )
790790} )
791791
792+ test ( 'host facts' , async ( t ) => {
793+ t . beforeEach ( ( ctx ) => {
794+ ctx . nr = { }
795+
796+ // Mock fetchSystemInfo to return a specific value
797+ const mockSysInfo = async ( agent , callback ) => {
798+ const systemInfo = { }
799+
800+ // Mock utilization.getVendors
801+ const vendorStats = {
802+ gcp : {
803+ id : 'mock-gcp-instance-id' ,
804+ zone : 'us-central1-a'
805+ }
806+ }
807+ systemInfo . vendors = vendorStats
808+
809+ callback ( null , systemInfo )
810+ }
811+
812+ const systemInfoPath = require . resolve ( '../../../lib/system-info' )
813+ ctx . originalSystemInfoModule = require . cache [ systemInfoPath ]
814+ ctx . systemInfoPath = systemInfoPath
815+ require . cache [ systemInfoPath ] = {
816+ id : systemInfoPath ,
817+ filename : systemInfoPath ,
818+ loaded : true ,
819+ exports : mockSysInfo
820+ }
821+
822+ const factsPath = require . resolve ( '../../../lib/collector/facts' )
823+ ctx . originalFactsModule = require . cache [ factsPath ]
824+ ctx . factsPath = factsPath
825+ delete require . cache [ factsPath ]
826+
827+ const facts = require ( '../../../lib/collector/facts' )
828+ ctx . nr . facts = function ( agent , callback ) {
829+ return facts ( agent , callback , { logger : ctx . nr . logger } )
830+ }
831+
832+ ctx . nr . agent = helper . loadMockedAgent ( structuredClone ( DISABLE_ALL_DETECTIONS ) )
833+ ctx . nr . agent . config . utilization = null
834+ ctx . nr . agent . config . getHostnameSafe = ( ) => 'localhost'
835+ } )
836+
837+ t . afterEach ( ( ctx ) => {
838+ helper . unloadAgent ( ctx . nr . agent )
839+ delete process . env . K_SERVICE
840+
841+ // Restore system-info cache
842+ if ( ctx . systemInfoPath ) {
843+ if ( ctx . originalSystemInfoModule === undefined ) {
844+ delete require . cache [ ctx . systemInfoPath ]
845+ } else {
846+ require . cache [ ctx . systemInfoPath ] = ctx . originalSystemInfoModule
847+ }
848+ }
849+
850+ // Restore facts cache
851+ if ( ctx . factsPath ) {
852+ if ( ctx . originalFactsModule === undefined ) {
853+ delete require . cache [ ctx . factsPath ]
854+ } else {
855+ require . cache [ ctx . factsPath ] = ctx . originalFactsModule
856+ }
857+ }
858+ } )
859+
860+ await t . test ( 'should use GCP instance ID as hostname when K_SERVICE is set' , ( t , end ) => {
861+ const { agent, facts } = t . nr
862+
863+ agent . config . gcp_cloud_run = { use_instance_as_host : true }
864+ process . env . K_SERVICE = 'mock-service'
865+
866+ facts ( agent , ( result ) => {
867+ assert . equal ( result . host , 'mock-gcp-instance-id' , 'Hostname should be set to GCP instance ID' )
868+ end ( )
869+ } )
870+ } )
871+
872+ await t . test ( 'should use getHostnameSafe as hostname when K_SERVICE is not present' , ( t , end ) => {
873+ const { agent, facts } = t . nr
874+
875+ agent . config . gcp_cloud_run = { use_instance_as_host : true }
876+
877+ facts ( agent , ( result ) => {
878+ assert . equal ( result . host , 'localhost' , 'Hostname should be set to GCP instance ID' )
879+ end ( )
880+ } )
881+ } )
882+
883+ await t . test ( 'should use getHostnameSafe when K_SERVICE is set but gcp_cloud_run.use_instance_as_host is false' , ( t , end ) => {
884+ const { agent, facts } = t . nr
885+
886+ agent . config . gcp_cloud_run = { use_instance_as_host : false }
887+ process . env . K_SERVICE = 'mock-service'
888+
889+ facts ( agent , ( result ) => {
890+ assert . equal ( result . host , 'localhost' , 'Hostname should be set to GCP instance ID' )
891+ end ( )
892+ } )
893+ } )
894+ } )
895+
792896function mockIpAddresses ( values ) {
793897 os . networkInterfaces = ( ) => {
794898 return {
0 commit comments