package Ġbr . com . gui ab ol so . tracing . example Ċ Ċ import Ġbr . com . gui ab ol so . tracing . engine . datadog . Dat adog Tracer Ċ import Ġbr . com . gui ab ol so . tracing . factory . Tracer Factory Ċ import Ġbr . com . gui ab ol so . tracing . utils . Dat adog Utils Ċ import Ġdat adog . trace . api . Trace Ċ import Ġorg . slf 4 j . LoggerFactory Ċ import Ġjava . util . concurrent . Executors Ċ import Ġjava . util . concurrent . TimeUnit . SECONDS Ċ Ċ private Ġval Ġlogger Ġ= ĠLoggerFactory . getLogger ( Dat adog Tracer :: class . java ) Ċ Ċ /** Ċ Ġ* ĠSee Ġhow Ġto Ġconfigure Ġthe Ġagent Ġat Ġhttps :// docs . datadog hq . com / tracing / setup / java / Ċ Ġ* Ċ Ġ* ĠStart Ġthe ĠJVM Ġwith Ġ'- java agent :/ path / to / the / dd - java - agent . jar ' Ċ Ġ* ĠSet Ġenvironment Ġvariable Ġ' DD _ SERVICE _ NAME ' Ċ Ġ*/ Ċ fun Ġmain ( var arg Ġargs : ĠString ) Ġ{ ĊĠĠĠ Ġval Ġtracer Ġ= ĠTr acer Factory . create Tracer With Dat adog () ĊĠĠĠ Ġval Ġexecutor Ġ= ĠExec utors . new Fixed ThreadPool ( 2 ) ĊĊĠĠĠ Ġ// You Ġdon 't Ġneed Ġto Ġuse ĠDat adog Utils . trace As New Operation Ġwhen Ġusing Ġservlet , Ġits Ġautomatic . ĊĠĠĠ ĠDat adog Utils . trace As New Operation (" simple Operation ") Ġ{ ĊĠĠĠĠĠĠĠ Ġtracer . addProperty (" one Tag ", Ġ" some Value 1 ") ĊĠĠĠĠĠĠĠ Ġsome Work () ĊĠĠĠ Ġ} ĊĊĠĠĠ ĠDat adog Utils . trace As New Operation (" simple Operation WithError ") Ġ{ ĊĠĠĠĠĠĠĠ Ġtracer . addProperty (" one Tag ", Ġ" some Value 2 ") ĊĠĠĠĠĠĠĠ Ġsome Work WithError () ĊĠĠĠ Ġ} ĊĊĠĠĠ ĠDat adog Utils . trace As New Operation (" simple Operation WithError Handled ") Ġ{ ĊĠĠĠĠĠĠĠ Ġtracer . addProperty (" one Tag ", Ġ" some Value 3 ") ĊĠĠĠĠĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġsome Work WithError () ĊĠĠĠĠĠĠĠ Ġ} Ġcatch Ġ( e : ĠException ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ// Fallback ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ// In Ġthis Ġscenario Ġonly Ġthe Ġchild Ġspan Ġwill Ġbe Ġmarked Ġas Ġerror ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ ĠDat adog Utils . trace As New Operation (" some AsyncOperation ") Ġ{ ĊĠĠĠĠĠĠĠ Ġtracer . addProperty (" one Tag ", Ġ" some Value 4 ") ĊĠĠĠĠĠĠĠ Ġsome Work () ĊĊĠĠĠĠĠĠĠ Ġtracer . execute Async ( executor ) Ġ{ Ġsome Work () Ġ} ĊĠĠĠĠĠĠĠ ĠThread . sleep ( 1 0 ) ĊĠĠĠĠĠĠĠ Ġtracer . execute Async ( executor ) Ġ{ Ġsome Work () Ġ} ĊĠĠĠĠĠĠĠ ĠThread . sleep ( 1 0 ) ĊĠĠĠĠĠĠĠ Ġtracer . execute Async ( executor ) Ġ{ Ġsome Work WithError () Ġ} ĊĊĠĠĠĠĠĠĠ Ġsome Work () ĊĠĠĠ Ġ} ĊĊĠĠĠ ĠDat adog Utils . trace As New Operation (" some AsyncOperation WithError ") Ġ{ ĊĠĠĠĠĠĠĠ Ġtracer . addProperty (" one Tag ", Ġ" some Value 5 ") ĊĠĠĠĠĠĠĠ Ġsome Work () ĊĊĠĠĠĠĠĠĠ Ġtracer . execute Async ( executor ) Ġ{ Ġsome Work () Ġ} ĊĠĠĠĠĠĠĠ ĠThread . sleep ( 1 0 ) ĊĠĠĠĠĠĠĠ Ġtracer . execute Async ( executor ) Ġ{ Ġsome Work () Ġ} ĊĠĠĠĠĠĠĠ ĠThread . sleep ( 1 0 ) ĊĠĠĠĠĠĠĠ Ġtracer . execute Async ( executor ) Ġ{ Ġsome Work () Ġ} ĊĠĠĠĠĠĠĠ ĠThread . sleep ( 1 0 ) ĊĊĠĠĠĠĠĠĠ Ġsome Work WithError () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġexecutor . shutdown () ĊĠĠĠ Ġexecutor . await Termination ( 1 0 , ĠSE CON DS ) Ċ } Ċ Ċ // If Ġyou Ġwant Ġto Ġchange Ġthe Ġname Ġof Ġthis Ġmethod Ġspan Ġset Ġ' operation Name '. Ċ // ĠRemember Ġthat Ġun like ĠNew Rel ic 's Ġmetrics Ġ'/' Ġshould Ġnot Ġbe Ġused . Ċ @ Trace Ċ private Ġfun Ġsome Work () Ġ{ ĊĠĠĠ Ġlogger . info (" Starting Ġsome Ġwork ") ĊĠĠĠ ĠThread . sleep ( random ( 1 0 0 , Ġ 2 0 0 )) ĊĠĠĠ Ġlogger . info (" Finished Ġsome Ġwork ") Ċ } Ċ Ċ @ Trace Ċ private Ġfun Ġsome Work WithError () Ġ{ ĊĠĠĠ Ġlogger . info (" Starting Ġsome Ġwork ") ĊĠĠĠ ĠThread . sleep ( random ( 1 0 0 , Ġ 2 0 0 )) ĊĠĠĠ Ġthrow ĠRuntimeException (" This Ġis Ġan Ġerror .") Ċ } Ċ Ċ private Ġfun Ġrandom ( min : ĠInt , Ġmax : ĠInt ): ĠLong Ġ= Ġmin . to Long () Ġ+ Ġ( max Ġ* ĠMath . random ()). to Long ()