1
1
import { axios } from "@pipedream/platform" ;
2
2
import app from "../../new_relic.app.mjs" ;
3
3
4
+ /**
5
+ * Query logs from New Relic using NRQL, supporting pagination via nextCursor and robust error handling.
6
+ */
4
7
export default {
5
8
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. " ,
7
10
key : "new_relic-query-logs" ,
8
- version : "0.0.1 " ,
11
+ version : "0.0.3 " ,
9
12
type : "action" ,
10
13
props : {
11
14
app,
@@ -16,28 +19,57 @@ export default {
16
19
nrql : {
17
20
type : "string" ,
18
21
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 `" ,
20
23
} ,
21
24
} ,
22
25
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
+ }
30
45
}
31
46
}
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
+ }
42
74
} ,
43
75
} ;
0 commit comments