Skip to content

Commit 4828494

Browse files
committed
Add Query Logs action with pagination, error handling, and documentation updates
1 parent 0ca86c5 commit 4828494

File tree

1 file changed

+52
-20
lines changed

1 file changed

+52
-20
lines changed
Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { axios } from "@pipedream/platform";
22
import app from "../../new_relic.app.mjs";
33

4+
/**
5+
* Query logs from New Relic using NRQL, supporting pagination via nextCursor and robust error handling.
6+
*/
47
export default {
58
name: "Query Logs",
6-
description: "Query logs from New Relic using NRQL.",
9+
description: "Query logs from New Relic using NRQL. Supports pagination for large result sets and includes comprehensive error handling.",
710
key: "new_relic-query-logs",
8-
version: "0.0.1",
11+
version: "0.0.3",
912
type: "action",
1013
props: {
1114
app,
@@ -16,28 +19,57 @@ export default {
1619
nrql: {
1720
type: "string",
1821
label: "NRQL Query",
19-
description: "The NRQL query to run against logs. Example: `SELECT * FROM Log WHERE message LIKE '%error%' LIMIT 10`",
22+
description: "The NRQL query to run against logs. Example: `SELECT * FROM Log WHERE message LIKE '%error%' LIMIT 1000`",
2023
},
2124
},
2225
async run({ $ }) {
23-
const url = `https://api.newrelic.com/graphql`;
24-
const headers = this.app._getHeaders();
25-
const query = `{
26-
actor {
27-
account(id: ${this.accountId}) {
28-
nrql(query: \"${this.nrql}\") {
29-
results
26+
try {
27+
// Validate accountId is a numeric string
28+
if (!/^\d+$/.test(this.accountId)) {
29+
throw new Error("Account ID must be a numeric string");
30+
}
31+
32+
const url = `https://api.newrelic.com/graphql`;
33+
const headers = this.app._getHeaders();
34+
let allResults = [];
35+
let nextCursor = null;
36+
37+
do {
38+
const query = `{
39+
actor {
40+
account(id: ${this.accountId}) {
41+
nrql(query: \"${this.nrql}\"${nextCursor ? `, nextCursor: \"${nextCursor}\"` : ""}) {
42+
results
43+
nextCursor
44+
}
3045
}
3146
}
32-
}
33-
}`;
34-
const response = await axios(this, {
35-
method: "POST",
36-
url,
37-
headers,
38-
data: { query },
39-
});
40-
$.export("$summary", `Queried logs with NRQL: ${this.nrql}`);
41-
return response.data;
47+
}`;
48+
const response = await axios(this, {
49+
method: "POST",
50+
url,
51+
headers,
52+
data: { query },
53+
});
54+
55+
// Handle GraphQL errors
56+
if (response.data.errors) {
57+
throw new Error(`GraphQL errors: ${JSON.stringify(response.data.errors)}`);
58+
}
59+
60+
const nrqlData = response.data?.data?.actor?.account?.nrql;
61+
if (!nrqlData) {
62+
throw new Error("Invalid response structure from New Relic API");
63+
}
64+
65+
if (nrqlData.results) allResults.push(...nrqlData.results);
66+
nextCursor = nrqlData.nextCursor;
67+
} while (nextCursor);
68+
69+
$.export("$summary", `Queried logs with NRQL: ${this.nrql}. Total results: ${allResults.length}`);
70+
return allResults;
71+
} catch (error) {
72+
throw new Error(`Failed to query New Relic logs: ${error.message}`);
73+
}
4274
},
4375
};

0 commit comments

Comments
 (0)