OpenCensus is being archived! Read the blog post to learn more

Stats exporter

Table of contents

Introduction

A stats exporter must implement Stats Event Listener. Which must contain two methods, onRegisterView and onRecord, that is called whenever a new view is registered and whenever a new measurement is registered, respectively.

Implemented in Typescript, a stats exporter implementation should look something like this:

Placed inside a file statsExporter.ts

/** Defines a trace exporter interface. */
class MyExporter implements StatsEventListener {
  /**
   * Is called whenever a new view is registered
   * @param view The registered view
   */
  onRegisterView(view: View): void {
    // Your on record code
  };

  /**
   * Is called whenever a new measurement is recorded.
   * @param views The views related to the measurement
   * @param measurement The recorded measurement
   */
  onRecord(views: View[], measurement: Measurement): void {
    // Your on record code
  };
}

Implementation

For example, let’s make a custom span exporter that will print span data to standard output.

/** Exporter that receives stats data and shows in the log console. */
export class MyConsoleStatsExporter implements StatsEventListener {
  /**
   * Called whenever a new view is registered
   * @param view The registered view
   */
  onRegisterView(view: View) {
    console.log(`View registered: ${view.name}, Measure registered: ${
        view.measure.name}`);
  }

  /**
   * Called whenever a measurement is recorded
   * @param views The updated views
   * @param measurement The recorded measurement
   */
  onRecord(views: View[], measurement: Measurement) {
    console.log(`Measurement recorded: ${measurement.measure.name}`);
  }
}

Runnable example

And now to test it out as we would in a typically linked program, let’s create a expample.js file:

const { globalStats, AggregationType, TagMap } = require('@opencensus/core');

// Let's create an instance of our just created exporter
const exporter = new MyConsoleStatsExporter();
// And register it
globalStats.registerExporter(exporter);

// Let's create a measure
const measure = globalStats.createMeasureInt64('my/measure', "1");
// our tags
const myTagKey = { name: "myTagKey" };
const tags = new TagMap();
tags.set(myTagKey, { value: "myTagValue" });

// Create and Register the view
const view = globalStats.createView(
  /* name */ 'my/view',
  measure, 
  AggregationType.LAST_VALUE, 
  [myTagKey], 
  /* description */ 'my view'
);
globalStats.registerView(view);
// and our measurement
const measurement = {measure, value: 10};

// finally, let's record it
globalStats.record([measurement], tags);

Now, run it with node example.js and you should see logs for our view being created and our measurement being recorded.