44 * you may not use this file except in compliance with the Elastic License.
55 */
66
7- /* eslint-disable import/no-extraneous-dependencies */
87/* eslint-disable no-console */
98
9+ /* eslint-disable import/no-extraneous-dependencies */
10+
1011/**
1112 * This script is useful for ingesting previously generated APM data into Elasticsearch via APM Server
1213 *
1314 * You can either:
1415 * 1. Download a static test data file from: https://storage.googleapis.com/apm-ui-e2e-static-data/events.json
15- * 2. Or, generate the test data file yourself by following the steps in: https://github.com/elastic/kibana/blob/5207a0b68a66d4f513fe1b0cedb021b296641712/x-pack/legacy/plugins/apm/cypress/README.md#generate-static-data
16+ * 2. Or, generate the test data file yourself:
17+ * git clone https://github.com/elastic/apm-integration-testing.git
18+ * ./scripts/compose.py start master --no-kibana --with-opbeans-node --apm-server-record
19+ * docker cp localtesting_8.0.0_apm-server-2:/app/events.json . && cat events.json | wc -l
20+ *
21+ *
1622 *
1723 * Run the script:
1824 *
@@ -27,6 +33,7 @@ const axios = require('axios');
2733const readFile = promisify ( fs . readFile ) ;
2834const pLimit = require ( 'p-limit' ) ;
2935const { argv } = require ( 'yargs' ) ;
36+ const ora = require ( 'ora' ) ;
3037
3138const APM_SERVER_URL = argv . serverUrl ;
3239const SECRET_TOKEN = argv . secretToken ;
@@ -43,10 +50,27 @@ if (!EVENTS_PATH) {
4350}
4451
4552const delay = ms => new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
53+
54+ const requestProgress = {
55+ succeeded : 0 ,
56+ failed : 0 ,
57+ total : 0
58+ } ;
59+
60+ const spinner = ora ( { text : 'Warming up...' , stream : process . stdout } ) ;
61+
62+ function updateSpinnerText ( { success } ) {
63+ success ? requestProgress . succeeded ++ : requestProgress . failed ++ ;
64+ const remaining =
65+ requestProgress . total -
66+ ( requestProgress . succeeded + requestProgress . failed ) ;
67+
68+ spinner . text = `Remaining: ${ remaining } . Succeeded: ${ requestProgress . succeeded } . Failed: ${ requestProgress . failed } .` ;
69+ }
70+
4671async function insertItem ( item ) {
4772 try {
4873 const url = `${ APM_SERVER_URL } ${ item . url } ` ;
49- console . log ( Date . now ( ) , url ) ;
5074
5175 const headers = {
5276 'content-type' : 'application/x-ndjson'
@@ -63,31 +87,42 @@ async function insertItem(item) {
6387 data : item . body
6488 } ) ;
6589
90+ updateSpinnerText ( { success : true } ) ;
91+
6692 // add delay to avoid flooding the queue
6793 return delay ( 500 ) ;
6894 } catch ( e ) {
69- console . log ( 'an error occurred' ) ;
70- if ( e . response ) {
71- console . log ( e . response . data ) ;
72- } else {
73- console . log ( 'error' , e ) ;
74- }
95+ console . error (
96+ `${ e . response ? JSON . stringify ( e . response . data ) : e . message } `
97+ ) ;
98+ updateSpinnerText ( { success : false } ) ;
7599 }
76100}
77101
78102async function init ( ) {
79- const content = await readFile ( path . resolve ( __dirname , EVENTS_PATH ) ) ;
103+ const content = await readFile ( path . resolve ( EVENTS_PATH ) ) ;
80104 const items = content
81105 . toString ( )
82106 . split ( '\n' )
83107 . filter ( item => item )
84108 . map ( item => JSON . parse ( item ) )
85109 . filter ( item => item . url === '/intake/v2/events' ) ;
86110
111+ spinner . start ( ) ;
112+ requestProgress . total = items . length ;
113+
87114 const limit = pLimit ( 20 ) ; // number of concurrent requests
88115 await Promise . all ( items . map ( item => limit ( ( ) => insertItem ( item ) ) ) ) ;
89116}
90117
91- init ( ) . catch ( e => {
92- console . log ( 'An error occurred:' , e ) ;
93- } ) ;
118+ init ( )
119+ . catch ( e => {
120+ console . log ( 'An error occurred:' , e ) ;
121+ process . exit ( 1 ) ;
122+ } )
123+ . then ( ( ) => {
124+ spinner . succeed (
125+ `Successfully ingested ${ requestProgress . succeeded } of ${ requestProgress . total } events`
126+ ) ;
127+ process . exit ( 0 ) ;
128+ } ) ;
0 commit comments