Skip to content

Commit

Permalink
Remove unnecessary internal debugging traces (#93)
Browse files Browse the repository at this point in the history
## Goal

Remove unnecessary custom systrace events in spans workflow, leaving the SDK init one that customers and ourselves may potentially be interested in.
  • Loading branch information
bidetofevil authored Nov 20, 2023
1 parent 3c209e4 commit 52b6edc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package io.embrace.android.embracesdk.internal

import android.os.Trace
import io.embrace.android.embracesdk.annotation.InternalApi
import io.embrace.android.embracesdk.internal.spans.toEmbraceSpanName

/**
* Shim to add custom events to system traces if running in the applicable API versions. Basic alternative to using androidx.tracing.
*/
@InternalApi
internal class Systrace private constructor() {
companion object {

/**
* Start a trace section. The name of the section will be [sectionName] prefixed by "emb-"
*/
fun start(sectionName: String) {
Trace.beginSection("emb-$sectionName")
Trace.beginSection(sectionName.toEmbraceSpanName())
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.embrace.android.embracesdk.internal.spans

import io.embrace.android.embracesdk.BuildConfig
import io.embrace.android.embracesdk.annotation.InternalApi
import io.embrace.android.embracesdk.internal.Systrace
import io.embrace.android.embracesdk.spans.EmbraceSpan
import io.embrace.android.embracesdk.spans.EmbraceSpanEvent
import io.embrace.android.embracesdk.spans.ErrorCode
Expand All @@ -23,38 +21,30 @@ import java.util.concurrent.atomic.AtomicReference
/**
* Implementation of the core logic for [SpansService]
*/
@InternalApi
internal class SpansServiceImpl(
sdkInitStartTimeNanos: Long,
sdkInitEndTimeNanos: Long,
private val clock: Clock
) : SpansService {
private val sdkTracerProvider: SdkTracerProvider
by lazy {
Systrace.start("spans-service-init")
Systrace.trace("init-sdk-tracer-provider") {
SdkTracerProvider
.builder()
.addSpanProcessor(EmbraceSpanProcessor(EmbraceSpanExporter(this)))
.setClock(clock)
.build()
}
SdkTracerProvider
.builder()
.addSpanProcessor(EmbraceSpanProcessor(EmbraceSpanExporter(this)))
.setClock(clock)
.build()
}

private val openTelemetry: OpenTelemetry
by lazy {
Systrace.trace("init-otel-sdk") {
OpenTelemetrySdk.builder()
.setTracerProvider(sdkTracerProvider)
.build()
}
OpenTelemetrySdk.builder()
.setTracerProvider(sdkTracerProvider)
.build()
}

private val tracer: Tracer
by lazy {
Systrace.trace("init-tracer") {
openTelemetry.getTracer(BuildConfig.LIBRARY_PACKAGE_NAME, BuildConfig.VERSION_NAME)
}
openTelemetry.getTracer(BuildConfig.LIBRARY_PACKAGE_NAME, BuildConfig.VERSION_NAME)
}

/**
Expand All @@ -78,14 +68,11 @@ internal class SpansServiceImpl(
private var appAttributesRecorded = false

init {
Systrace.trace("log-sdk-init") {
recordCompletedSpan(
name = "sdk-init",
startTimeNanos = sdkInitStartTimeNanos,
endTimeNanos = sdkInitEndTimeNanos
)
}
Systrace.end()
recordCompletedSpan(
name = "sdk-init",
startTimeNanos = sdkInitStartTimeNanos,
endTimeNanos = sdkInitEndTimeNanos
)
}

override fun createSpan(name: String, parent: EmbraceSpan?, type: EmbraceAttributes.Type, internal: Boolean): EmbraceSpan? {
Expand All @@ -107,12 +94,7 @@ internal class SpansServiceImpl(
code: () -> T
): T {
return if (EmbraceSpanImpl.inputsValid(name) && validateAndUpdateContext(parent, internal)) {
Systrace.start("log-span-$name")
try {
createRootSpanBuilder(name = name, type = type, internal = internal).updateParent(parent).record(code)
} finally {
Systrace.end()
}
createRootSpanBuilder(name = name, type = type, internal = internal).updateParent(parent).record(code)
} else {
code()
}
Expand All @@ -134,26 +116,24 @@ internal class SpansServiceImpl(
}

return if (EmbraceSpanImpl.inputsValid(name, events, attributes) && validateAndUpdateContext(parent, internal)) {
Systrace.trace("log-completed-span-$name") {
val span = createRootSpanBuilder(name = name, type = type, internal = internal)
.updateParent(parent)
.setStartTimestamp(startTimeNanos, TimeUnit.NANOSECONDS)
.startSpan()
.setAllAttributes(Attributes.builder().fromMap(attributes).build())
val span = createRootSpanBuilder(name = name, type = type, internal = internal)
.updateParent(parent)
.setStartTimestamp(startTimeNanos, TimeUnit.NANOSECONDS)
.startSpan()
.setAllAttributes(Attributes.builder().fromMap(attributes).build())

events.forEach { event ->
if (EmbraceSpanEvent.inputsValid(event.name, event.attributes)) {
span.addEvent(
event.name,
Attributes.builder().fromMap(event.attributes).build(),
event.timestampNanos,
TimeUnit.NANOSECONDS
)
}
events.forEach { event ->
if (EmbraceSpanEvent.inputsValid(event.name, event.attributes)) {
span.addEvent(
event.name,
Attributes.builder().fromMap(event.attributes).build(),
event.timestampNanos,
TimeUnit.NANOSECONDS
)
}

span.endSpan(errorCode, endTimeNanos)
}

span.endSpan(errorCode, endTimeNanos)
true
} else {
false
Expand Down

0 comments on commit 52b6edc

Please sign in to comment.