Not sure if you sorted this out, but I had to do something similar to get who started the instance so I can badger them into stopping the instance if they are not using it. I put together a Logging query:
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
And here is the nodejs function I put together to get it:
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() } );
} );
}
Hopefully that helps someone because it was a good bit of work to put together.