Node.js SDK

How to add nodejs sdk into your application

1. Installing node-SDK package

HyperTest's npm packages are hosted on a github private NPM registry To Install and use HyperTest's node-sdk package in your application, we will do the following steps:

  1. Create NPM_TOKEN - The Hypertest Team share this with you.

  2. set NPM_TOKEN in .npmrc file

  3. Install HyperTest's node-sdk package

Add the following content in your .npmrc

1st line in this file has authToken for pulling packages from NPM registry.

2nd line indicates to use https://npm.pkg.github.com registry to pull packages scoped with @hypertestco

.npmrc
//npm.pkg.github.com/:_authToken=<NPM_TOKEN>
@hypertestco:registry=https://npm.pkg.github.com/ 

npm install @hypertestco/node-sdk --save-exact @opentelemetry/sdk-node @opentelemetry/exporter-trace-otlp-grpc 

2. Initializing sdk

2.1 Adding SDK in code

Add these lines at the top of your app.

This needs to happen as early in your app as possible.

// import * as htSdk from '@hypertestco/node-sdk'; // for esm/ts
const htSdk = require('@hypertestco/node-sdk'); // for commonJS
htSdk.initialize({
    apiKey: '<your-api-key>',
    serviceId: '<your-service-identifier-from-dashboard>'
});

2.2 Initialize open telemetry sdk with hypetest

  • Initialize opentelemtry sdk with HtTraceExporter. Set the trace exporter url to hypertest logger as shown below.

  • Set tracer provider generated by opentelemetry's SDK for proper tracing.

  • Call the markAppAsReady method when the app is ready to receive traffic, it indicates that tests can be started (ONLY FOR REPLAY mode).

// import * as opentelemetry from '@opentelemetry/sdk-node'; // for esm/ts
// import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc'

const opentelemetry = require('@opentelemetry/sdk-node');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc');

const HtTraceExporter = htSdk.getHtTraceExporter(OTLPTraceExporter);

const sdk = new opentelemetry.NodeSDK({
  traceExporter: new HtTraceExporter({
    url: '<hypertest-logger-url>',
  }),
});

sdk.start();
htSdk.setHtTracerProvider(sdk._tracerProvider);
// Rest Application code...
// add htSdk.markAppAsReady(); when your app is ready to accept requests
app.listen(3000, () => {
  console.log(`Listening for requests on http://localhost:3000`);
  // Indicates the App has booted up successfully for REPLAY And Tests can be started
  htSdk.markAppAsReady();
});

To enable hypertest, set the HT_MODE env variable to RECORD and start your app

HT_MODE=RECORD node my-app.js

OR

export HT_MODE=RECORD
node my-app.js

OR

// Set this on top inside your JS app
process.env.HT_MODE = process.env.HT_MODE || 'RECORD'

4. Verifying traffic is captured

Start sending http requests on your app running with hypertest sdk.

You should start seeing requests under All requests section on the dashboard

Last updated