No estoy seguro de si resolvió esto, pero tuve que hacer algo similar para averiguar quién inició la instancia para que pueda molestarlos para que detengan la instancia si no la están usando. Puse una consulta de registro:
resource.type = gce_instance AND (jsonPayload.event_subtype = compute.instances.start OR jsonPayload.event_subtype = compute.instances.insert ) AND jsonPayload.event_type = GCE_OPERATION_DONE AND timestamp >= "2018-10-29T14:28:34-07:00" AND jsonPayload.actor.user!="" AND jsonPayload.resource.name=my-sweet-instance-name
Y aquí está la función nodejs que armé para obtenerla:
const Logging = require( '@google-cloud/logging' );
const moment = require( 'moment' );
const logging = new Logging( );
var getStartInfo = function( instanceName, querySince, cb ) {
var tstart = ( querySince ? querySince : moment( ).subtract( 48, 'hours' ).format( ) ); //
var theLogFilter = 'resource.type = gce_instance AND ' +
'(jsonPayload.event_subtype = compute.instances.start OR jsonPayload.event_subtype = compute.instances.insert ) AND ' +
'jsonPayload.event_type = GCE_OPERATION_DONE AND ' +
'timestamp >= "' + tstart + '" AND ' +
'jsonPayload.actor.user!="" AND ' +
'jsonPayload.resource.name=' + instanceName;
logging.getEntries( {
filter: theLogFilter,
autoPaginate: false
}, ( err, entries, nextQuery, apiResponse ) => {
if ( err ) {
console.log( "ERROR: " + err );
cb( err );
return;
}
var item, startedBy, startTime, runningTime, mostRecentStart;
//console.log( 'Entries: ' + JSON.stringify( entries ) );
// Mabye if none found, we try again with a longer querySince?
if ( entries.length == 0 ) {
console.log( "\nNo log entries found for instance '" + instanceName + "'. Filter:" );
console.log( theLogFilter );
cb( "No entries found" );
return;
}
// Are these sorted by time?
for ( var i = 0; i < entries.length; i++ ) {
startedBy = entries[ i ].metadata.jsonPayload.fields.actor.structValue.fields.user.stringValue;
startTime = entries[ i ].metadata.jsonPayload.fields.event_timestamp_us.stringValue / 1000; // This is nano seconds since epoch
}
if ( cb )
cb( null, { "startedBy": startedBy, "startTime": moment( startTime ).format() } );
} );
}
Espero que eso ayude a alguien porque fue un buen trabajo armarlo.