-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Add “Query Logs” Action to New Relic Component #16968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
edward-rosado
wants to merge
3
commits into
PipedreamHQ:master
Choose a base branch
from
BookerSoftwareInc:feature/query-logs-action
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
0ca86c5
Add Query Logs action and update documentation for New Relic componen…
edward-rosado 4828494
Add Query Logs action with pagination, error handling, and documentat…
edward-rosado 22a11b1
feat: add NRQL query validation, sanitization, and update docs for Qu…
edward-rosado File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { axios } from "@pipedream/platform"; | ||
import app from "../../new_relic.app.mjs"; | ||
|
||
/** | ||
* Query logs from New Relic using NRQL, supporting pagination via nextCursor and robust error handling. | ||
*/ | ||
function sanitizeNrql(nrql) { | ||
// Remove dangerous characters and trim whitespace | ||
return nrql.replace(/[;`$\\]/g, "").trim(); | ||
} | ||
|
||
function isValidNrql(nrql) { | ||
// Basic validation: must start with SELECT, not empty, and not contain forbidden patterns | ||
const forbidden = /[;`$\\]/; | ||
return ( | ||
typeof nrql === "string" && | ||
nrql.trim().length > 0 && | ||
/^SELECT\s+/i.test(nrql.trim()) && | ||
!forbidden.test(nrql) | ||
); | ||
} | ||
|
||
export default { | ||
name: "Query Logs", | ||
description: "Query logs from New Relic using NRQL. Supports pagination for large result sets and includes comprehensive error handling.", | ||
key: "new_relic-query-logs", | ||
version: "0.0.5", | ||
type: "action", | ||
props: { | ||
app, | ||
accountId: { | ||
type: "string", | ||
label: "Account ID", | ||
}, | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
nrql: { | ||
type: "string", | ||
label: "NRQL Query", | ||
description: "The NRQL query to run against logs. Example: `SELECT * FROM Log WHERE message LIKE '%error%' LIMIT 1000`", | ||
}, | ||
}, | ||
async run({ $ }) { | ||
try { | ||
// Validate accountId is a numeric string | ||
if (!/^\d+$/.test(this.accountId)) { | ||
throw new Error("Account ID must be a numeric string"); | ||
} | ||
// Validate and sanitize NRQL | ||
if (!isValidNrql(this.nrql)) { | ||
throw new Error("Invalid NRQL query. Must start with SELECT and not contain forbidden characters."); | ||
} | ||
const safeNrql = sanitizeNrql(this.nrql); | ||
|
||
const url = `https://api.newrelic.com/graphql`; | ||
const headers = this.app._getHeaders(); | ||
let allResults = []; | ||
let nextCursor = null; | ||
|
||
do { | ||
const query = ` | ||
query($accountId: Int!, $nrql: String!, $nextCursor: String) { | ||
actor { | ||
account(id: $accountId) { | ||
nrql(query: $nrql, nextCursor: $nextCursor) { | ||
results | ||
nextCursor | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
const variables = { | ||
accountId: parseInt(this.accountId, 10), | ||
nrql: safeNrql, | ||
nextCursor, | ||
}; | ||
const response = await axios(this, { | ||
method: "POST", | ||
url, | ||
headers, | ||
data: { query, variables }, | ||
}); | ||
|
||
// Handle GraphQL errors | ||
if (response.data.errors) { | ||
throw new Error(`GraphQL errors: ${JSON.stringify(response.data.errors)}`); | ||
} | ||
|
||
const nrqlData = response.data?.data?.actor?.account?.nrql; | ||
if (!nrqlData) { | ||
throw new Error("Invalid response structure from New Relic API"); | ||
} | ||
|
||
if (nrqlData.results) allResults.push(...nrqlData.results); | ||
nextCursor = nrqlData.nextCursor; | ||
} while (nextCursor); | ||
|
||
$.export("$summary", `Queried logs with NRQL: ${safeNrql}. Total results: ${allResults.length}`); | ||
return allResults; | ||
} catch (error) { | ||
throw new Error(`Failed to query New Relic logs: ${error.message}`); | ||
} | ||
}, | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.