Trace exporter

Table of contents

Introduction

A trace exporter must implement Exporter. Which must contain a method onEndSpan, that is called whenever a new span is ended. Usualy, this method writes the span to an Exporter Buffer, allowing the Exporter to store spans and send batched data to a client. A trace exporter must also contain a publish method that will translate and export root spans and its child spans to the service, if an Exporter Buffer is used, it will be responsible for calling the publish method when it has reached its time or size capacity.

A trace exporter should also have a onStartSpan method, that will be called whenever a span is started. This method is useful for close to real time exporters, like zPages.

Implementation

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

/** Format and sends span data to the console. */exportclassMyConsoleTraceExporterimplementstypes.Exporter{/** Buffer object to store the spans. */privatebuffer: ExporterBuffer;/**
* Constructs a new ConsoleLogExporter instance.
* @param config Exporter configuration object to create a console log
* exporter.
*/constructor(config: types.ExporterConfig){this.buffer=newExporterBuffer(this,config);}/**
* Our onStartSpan will do nothing. The exporting logic will be concentraded
* at the onEndSpan event.
*/onStartSpan(root: modelTypes.RootSpan){}/**
* Called whenever a span is ended.
* @param root Ended span.
*/onEndSpan(root: modelTypes.RootSpan){// We will just add the ended span to the buffer and wait for it to call
// the exporter back giving all the stored spans. This allow us to print
// spans in batch.
this.buffer.addToBuffer(root);}/**
* Sends the spans information to the console.
* @param rootSpans The stored spans
*/publish(rootSpans: modelTypes.RootSpan[]){// Let's iterate over all root spans formating the data the way we want
rootSpans.map((root)=>{constROOT_STR=`RootSpan: {traceId: ${root.traceId}, spanId: ${root.id}, name: ${root.name} }`;constSPANS_STR: string[]=root.spans.map((span)=>[`\t\t{spanId: ${span.id}, name: ${span.name}}`].join('\n'));constresult: string[]=[];result.push(ROOT_STR+'\n\tChildSpans:\n'+`${SPANS_STR.join('\n')}`);console.log(`${result}`);});returnPromise.resolve();}}

Runnable example

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

vartracing=require('@opencensus/opencensus-nodejs');// Let's create an instance of our just created exporter
varexporter=newMyConsoleTraceExporter();// And start tracing with it
tracing.registerExporter(exporter).start();// Now, lets create a simple HTTP 2 server
varhttp2=require('http2')varserver2=http2.createServer();// On every call to http://localhost:8080 we will return a Hello World message
server2.on('stream',(stream,requestHeaders)=>{stream.end("Hello World");});server2.listen(8080);

Now, run it with node example.js and go to http://localhost:8080 on your browser. In the begining, you may see nothing in the console, but a few seconds latter, the exporter buffer kicks in and calls the publish method on your console log exporter, this will print the span info we wanted.