Tracer
-
Cmn
abstract class Tracer
A Tracer is the entry point for all Tracing APIs.
To obtain an instance of Tracer use AbstractTraceDriver.tracer.
Summary
Public companion functions |
||
|---|---|---|
Tracer |
Cmn
|
|
Unit |
Resets the |
Cmn
|
Unit |
@<error>Registers the global |
Cmn
|
Public companion properties |
||
|---|---|---|
Tracer |
The Global tracer configured by the application. |
Cmn
|
Public constructors |
|
|---|---|
Tracer() |
Cmn
|
Public functions |
||
|---|---|---|
suspend inline EventMetadataCloseable |
beginCoroutineSection(Writes a trace message indicating that a given suspending section of code has begun. |
Cmn
|
abstract suspend EventMetadataCloseable |
@DelicateTracingApiWrites a trace message indicating that a given suspending section of code has begun. |
Cmn
|
inline AutoCloseable |
beginSection(Writes a trace message indicating that a given section of code has begun. |
Cmn
|
abstract EventMetadataCloseable |
@DelicateTracingApiWrites a trace message indicating that a given section of code has begun. |
Cmn
|
abstract Counter |
Cmn
|
|
inline Unit |
@<error>Emits a zero duration trace section. |
Cmn
|
abstract Boolean |
isCategoryEnabled(category: String) |
Cmn
|
abstract PropagationToken |
Creates a |
Cmn
|
abstract suspend PropagationToken |
This gives the ability to control how context propagation works for a |
Cmn
|
abstract PropagationToken |
This gives the ability to control how context propagation works for a |
Cmn
|
inline T |
@<error>Traces the |
Cmn
|
suspend inline T |
@<error>Traces the suspending |
Cmn
|
abstract EventMetadataCloseable |
@DelicateTracingApiWrites a zero duration section to the Trace. |
Cmn
|
Extension functions |
||
|---|---|---|
Nothing |
Tracer.recordExceptionAndThrow(Records an exceptional event in a trace. |
Cmn
|
Public companion functions
resetGlobalTracer
@VisibleForTesting
fun resetGlobalTracer(): Unit
Resets the global for JVM and Android tests.
Note: This API should only be used in tests.
setGlobalTracer
@<error>
@DelicateTracingApi
fun setGlobalTracer(tracer: Tracer): Unit
Registers the global Tracer instance.
This should only ever be done once per process lifecycle and by the application initializing the AbstractTraceDriver; typically during app startup.
This should never be called by libraries.
Public companion properties
global
@<error>
val global: Tracer
The Global tracer configured by the application.
This is the Tracer that should be used by both application and library developers. You should always use Tracer.global and not cache references this field, given the Tracer being used, is updated after the application is initialized.
The global tracer is typically bootstrapped during process startup. When using androidx.tracing:tracing-wire, androidx.tracing.profiler.ConnectedProfilerTracingInitializer discovers the AbstractTraceDriver.Factory, and constructs the instance. It then registers a global Tracer by calling Tracer.setGlobalTracer.
Otherwise, construct AbstractTraceDriver during startup and register it via Tracer.setGlobalTracer so other components can discover and use it.
Public constructors
Public functions
beginCoroutineSection
suspend inline fun beginCoroutineSection(
category: String,
name: String,
token: PropagationToken?,
isRoot: Boolean = false,
crossinline metadataBlock: EventMetadata.() -> Unit
): EventMetadataCloseable
Writes a trace message indicating that a given suspending section of code has begun.
Should be followed by a corresponding call to AutoCloseable.close returned by the call to beginCoroutineSectionWithMetadata. If the corresponding AutoCloseable.close is missing, the section will be present in the trace, but non-terminating (generally shown as fading out to the left).
| Parameters | |
|---|---|
category: String |
The |
name: String |
The name of the code section to appear in the trace. |
token: PropagationToken? |
An optional |
isRoot: Boolean = false |
A hint that tells the |
crossinline metadataBlock: EventMetadata.() -> Unit |
The lambda that can be used to decorate the trace event with additional debug annotations. |
| Returns | |
|---|---|
EventMetadataCloseable |
A |
beginCoroutineSectionWithMetadata
@DelicateTracingApi
abstract suspend fun beginCoroutineSectionWithMetadata(
category: String,
name: String,
token: PropagationToken?,
isRoot: Boolean
): EventMetadataCloseable
Writes a trace message indicating that a given suspending section of code has begun.
Should be followed by a corresponding call to AutoCloseable.close returned by the call to beginCoroutineSectionWithMetadata. If the corresponding AutoCloseable.close is missing, the section will be present in the trace, but non-terminating (generally shown as fading out to the left).
| Parameters | |
|---|---|
category: String |
The category that the trace section belongs to. Apps can potentially filter sections to the categories that they are interested in looking into. |
name: String |
The name of the code section to appear in the trace. |
token: PropagationToken? |
An optional |
isRoot: Boolean |
A hint that tells the |
| Returns | |
|---|---|
EventMetadataCloseable |
A |
beginSection
inline fun beginSection(
category: String,
name: String,
token: PropagationToken?,
isRoot: Boolean = false,
crossinline metadataBlock: EventMetadata.() -> Unit
): AutoCloseable
Writes a trace message indicating that a given section of code has begun.
Should be followed by a corresponding call to AutoCloseable.close returned by the call to beginSection. If the corresponding AutoCloseable.close is missing, the section will be present in the trace, but non-terminating (generally shown as fading out to the left).
| Parameters | |
|---|---|
category: String |
The category that the trace section belongs to. Apps can potentially filter sections to the categories that they are interested in looking into. |
name: String |
The name of the code section to appear in the trace. |
token: PropagationToken? |
An optional |
isRoot: Boolean = false |
A hint that tells the |
crossinline metadataBlock: EventMetadata.() -> Unit |
The lambda that can be used to decorate the trace event with additional debug annotations. |
| Returns | |
|---|---|
AutoCloseable |
A |
beginSectionWithMetadata
@DelicateTracingApi
abstract fun beginSectionWithMetadata(
category: String,
name: String,
token: PropagationToken?,
isRoot: Boolean
): EventMetadataCloseable
Writes a trace message indicating that a given section of code has begun.
Should be followed by a corresponding call to AutoCloseable.close returned by the call to beginSection. If the corresponding AutoCloseable.close is missing, the section will be present in the trace, but non-terminating (generally shown as fading out to the left).
| Parameters | |
|---|---|
category: String |
The category that the trace section belongs to. Apps can potentially filter sections to the categories that they are interested in looking into. |
name: String |
The name of the code section to appear in the trace. |
token: PropagationToken? |
An optional |
isRoot: Boolean |
A hint that tells the |
| Returns | |
|---|---|
EventMetadataCloseable |
A |
instant
@<error>
inline fun instant(
category: String,
name: String,
token: PropagationToken? = null,
crossinline metadataBlock: EventMetadata.() -> Unit = {}
): Unit
Emits a zero duration trace section.
| Parameters | |
|---|---|
category: String |
The category that the trace section belongs to. Apps can potentially filter sections to the categories that they are interested in looking into. |
name: String |
The name of the code section to appear in the trace. |
token: PropagationToken? = null |
The optional |
crossinline metadataBlock: EventMetadata.() -> Unit = {} |
The lambda that can be used to decorate the trace event with additional debug annotations. |
isCategoryEnabled
abstract fun isCategoryEnabled(category: String): Boolean
| Returns | |
|---|---|
Boolean |
If Note:This method should be extremely low overhead given it's called every time a |
tokenForManualPropagation
@ExperimentalContextPropagation
abstract fun tokenForManualPropagation(flowIds: List<Long> = listOf(monotonicId())): PropagationToken
Creates a PropagationToken that can be used for manual context propagation in androidx.tracing.Tracer.
tokenFromCoroutineContext
@DelicateTracingApi
abstract suspend fun tokenFromCoroutineContext(): PropagationToken
This gives the ability to control how context propagation works for a androidx.tracing.Tracer.
Alternative implementations can choose to override this method to do something different. Examples include writing your own kotlin.coroutines.CoroutineContext.Element that bridges the gap with non-coroutine code by using ThreadLocals under the hood.
tokenFromThreadContext
@DelicateTracingApi
abstract fun tokenFromThreadContext(): PropagationToken
This gives the ability to control how context propagation works for a androidx.tracing.Tracer.
The default implementation does not support context propagation in non-suspending contexts by returning a PropagationUnsupportedToken instance. Alternative implementations can choose to override this method to do something different. Examples include using a ThreadLocal like primitive track of PropagationTokens across threads.
trace
@<error>
inline fun <T : Any?> trace(
category: String,
name: String,
token: PropagationToken? = null,
isRoot: Boolean = false,
crossinline metadataBlock: EventMetadata.() -> Unit = {},
crossinline block: () -> T
): T
Traces the block as a named section of code in the trace with context propagation.
| Parameters | |
|---|---|
category: String |
The |
name: String |
The name of the trace section. |
token: PropagationToken? = null |
The optional |
isRoot: Boolean = false |
A hint that tells the |
crossinline metadataBlock: EventMetadata.() -> Unit = {} |
The lambda that can be used to decorate the trace event with additional debug annotations. |
crossinline block: () -> T |
The |
traceCoroutine
@<error>
suspend inline fun <T : Any?> traceCoroutine(
category: String,
name: String,
token: PropagationToken? = null,
isRoot: Boolean = false,
crossinline metadataBlock: EventMetadata.() -> Unit = {},
crossinline block: suspend () -> T
): T
Traces the suspending block as a named section of code in the trace with context propagation. The tokenFromCoroutineContext method is used to obtain the PropagationToken for context propagation.
| Parameters | |
|---|---|
category: String |
The |
name: String |
The name of the trace section. |
token: PropagationToken? = null |
An optional explicit |
isRoot: Boolean = false |
A hint that tells the |
crossinline metadataBlock: EventMetadata.() -> Unit = {} |
The lambda that can be used to decorate the trace event with additional debug annotations. |
crossinline block: suspend () -> T |
The suspending |
writeInstant
@DelicateTracingApi
abstract fun writeInstant(category: String, name: String, token: PropagationToken?): EventMetadataCloseable
Writes a zero duration section to the Trace.
| Parameters | |
|---|---|
category: String |
The category that the trace section belongs to. Apps can potentially filter sections to the categories that they are interested in looking into. |
name: String |
The name of the code section to appear in the trace. |
token: PropagationToken? |
The optional |
Extension functions
Tracer.recordExceptionAndThrow
fun Tracer.recordExceptionAndThrow(
category: String,
name: String,
throwable: Throwable
): Nothing
Records an exceptional event in a trace. This method rethrows the throwable after having extracting all metadata worth tracing.
| Parameters | |
|---|---|
category: String |
The |
name: String |
The name of the trace section. |
throwable: Throwable |
The exception thrown by a block of code. |