Runnable example

packageoc.tutorials;importjava.util.Collection;importio.opencensus.common.Scope;importio.opencensus.trace.Span;importio.opencensus.trace.SpanContext;importio.opencensus.trace.Tracer;importio.opencensus.trace.Tracing;importio.opencensus.trace.config.TraceConfig;importio.opencensus.trace.export.SpanData;importio.opencensus.trace.export.SpanExporter;importio.opencensus.trace.samplers.Samplers;publicclassCustomTraceExporterextendsSpanExporter.Handler{privatestaticTracertracer=Tracing.getTracer();publicstaticvoidmain(String...args){// Firstly create and register the exporter
CustomTraceExporter.createAndRegister();// For demo purposes, we'll always sample
TraceConfigtraceConfig=Tracing.getTraceConfig();traceConfig.updateActiveTraceParams(traceConfig.getActiveTraceParams().toBuilder().setSampler(Samplers.alwaysSample()).build());// Do some work
for(inti=0;i<5;i++){Stringname=String.format("sample-%d",i);Scopess=tracer.spanBuilder(name).startScopedSpan();tracer.getCurrentSpan().addAnnotation("This annotation is for "+name);sleep(200);// Sleep for 200 milliseconds
ss.close();}sleep(8000);// Sleep for 8 seconds to give exporting time to complete
System.exit(0);}@Overridepublicvoidexport(Collection<SpanData>spanDataList){for(SpanDatasd:spanDataList){SpanContextsc=sd.getContext();System.out.println(String.format("Name: %s\nTraceID: %s\nSpanID: %s\nParentSpanID: %s\nStartTime: %d\nEndTime: %d\nAnnotations: %s\n\n",sd.getName(),sc.getTraceId(),sc.getSpanId(),sd.getParentSpanId(),sd.getStartTimestamp().getSeconds(),sd.getEndTimestamp().getSeconds(),sd.getAnnotations()));}}publicstaticvoidcreateAndRegister(){// Please remember to register your exporter
// so that it can receive exportered spanData.
Tracing.getExportComponent().getSpanExporter().registerHandler(CustomTraceExporter.class.getName(),newCustomTraceExporter());}privatestaticvoidsleep(intms){// A helper to avoid try-catch when invoking Thread.sleep so that
// sleeps can be succinct and not permeated by exception handling.
try{Thread.sleep(ms);}catch(Exceptione){System.err.println(String.format("Failed to sleep for %dms. Exception: %s",ms,e));}}}

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>oc.tutorial</groupId><artifactId>tracetutorial</artifactId><packaging>jar</packaging><version>1.0-SNAPSHOT</version><name>quickstart</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><opencensus.version>0.15.0</opencensus.version><!-- The OpenCensus version to use --></properties><dependencies><dependency><groupId>io.opencensus</groupId><artifactId>opencensus-api</artifactId><version>${opencensus.version}</version></dependency><dependency><groupId>io.opencensus</groupId><artifactId>opencensus-impl</artifactId><version>${opencensus.version}</version></dependency></dependencies><build><extensions><extension><groupId>kr.motd.maven</groupId><artifactId>os-maven-plugin</artifactId><version>1.5.0.Final</version></extension></extensions><pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.7.0</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin><plugin><groupId>org.codehaus.mojo</groupId><artifactId>appassembler-maven-plugin</artifactId><version>1.10</version><configuration><programs><program><id>CustomTraceExporter</id><mainClass>oc.tutorials.CustomTraceExporter</mainClass></program></programs></configuration></plugin></plugins></pluginManagement></build></project>