Skip to content

Commit

Permalink
Handle the encode and non encode logName filter scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakhi Mundhada authored and Rakhi Mundhada committed Oct 17, 2024
1 parent 8448ab8 commit 37d5f19
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
40 changes: 23 additions & 17 deletions collectors/googlestackdriver/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ class GooglestackdriverCollector extends PawsCollector {

AlLogger.info(`GSTA000001 Collecting data from ${state.since} till ${state.until} for ${state.stream}`);

// TODO: figure out a better way to format this. I'm pretty sure that it needs the newlines in it.
const filter = collector.generateFilter(state);

let pagesRetireved = 0;
Expand Down Expand Up @@ -184,30 +183,37 @@ class GooglestackdriverCollector extends PawsCollector {
}

generateFilter(state) {
const logTypes = process.env.paws_collector_param_string_2 ? JSON.parse(process.env.paws_collector_param_string_2) : [];
let filterParts = [];
let logNameFilter;

if (logTypes && logTypes.length > 0) {
logTypes.forEach(logType => {
if (state.stream && logType && logType.trim() !== "") { // Check that logType is not empty
filterParts.push(`logName="${state.stream}/logs/${logType}"`);
} else if (!logType || logType.trim() === "") {
AlLogger.warn("Skipping empty log type.");
const logIds = process.env.paws_collector_param_string_2 ? JSON.parse(process.env.paws_collector_param_string_2) : [];
const filterConditions = [];
let logFilterCondition;

if (logIds.length > 0) {
logIds.forEach(logId => {
const trimmedLogId = logId?.trim();

if (trimmedLogId) {
const encodeLogId = this.isUriEncoded(logId) ? logId : encodeURIComponent(logId);
filterConditions.push(`logName:"${encodeLogId}"`);
} else {
AlLogger.warn("Skipping empty log ID.");
}
});
if (filterParts.length > 0) {
logNameFilter = filterParts.join(" OR ");
if (filterConditions.length > 0) {
logFilterCondition = filterConditions.join(" OR ");
}
}
// Construct the basic timestamp filter
let filter = `timestamp >= "${state.since}" AND timestamp < "${state.until}"`;
let filterQuery = `timestamp >= "${state.since}" AND timestamp < "${state.until}"`;

if (logNameFilter) {
if (logFilterCondition) {
// Combine the LogName and timesamp filter
filter = `${filter} AND (${logNameFilter})`;
filterQuery = `${filterQuery} AND (${logFilterCondition})`;
}
return filter;
return filterQuery;
}

isUriEncoded(logId) {
return decodeURIComponent(logId) !== logId;
}


Expand Down
2 changes: 1 addition & 1 deletion collectors/googlestackdriver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"dependencies": {
"@alertlogic/al-collector-js": "3.0.14",
"@alertlogic/paws-collector": "2.2.5",
"@alertlogic/paws-collector": "2.2.6",
"async": "^3.2.6",
"debug": "^4.3.7",
"google-auth-library": "^9.14.1",
Expand Down

0 comments on commit 37d5f19

Please sign in to comment.