Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implementations of benchmark profiling api #2220

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion dd-sdk-android-internal/api/apiSurface
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ interface com.datadog.android.internal.profiler.BenchmarkProfiler
fun getTracer(String): BenchmarkTracer
interface com.datadog.android.internal.profiler.BenchmarkSpan
fun stop()
interface com.datadog.android.internal.profiler.BenchmarkSpanBuilder
fun startSpan(): BenchmarkSpan
fun <T: Any?> withinBenchmarkSpan(String, BenchmarkSpan.() -> T): T
interface com.datadog.android.internal.profiler.BenchmarkTracer
fun startSpan(String): BenchmarkSpan
fun spanBuilder(String): BenchmarkSpanBuilder
object com.datadog.android.internal.profiler.GlobalBenchmark
fun register(BenchmarkProfiler)
fun get(): BenchmarkProfiler
Expand Down
10 changes: 9 additions & 1 deletion dd-sdk-android-internal/api/dd-sdk-android-internal.api
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ public abstract interface class com/datadog/android/internal/profiler/BenchmarkS
public abstract fun stop ()V
}

public abstract interface class com/datadog/android/internal/profiler/BenchmarkSpanBuilder {
public abstract fun startSpan ()Lcom/datadog/android/internal/profiler/BenchmarkSpan;
}

public final class com/datadog/android/internal/profiler/BenchmarkSpanExtKt {
public static final fun withinBenchmarkSpan (Ljava/lang/String;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
}

public abstract interface class com/datadog/android/internal/profiler/BenchmarkTracer {
public abstract fun startSpan (Ljava/lang/String;)Lcom/datadog/android/internal/profiler/BenchmarkSpan;
public abstract fun spanBuilder (Ljava/lang/String;)Lcom/datadog/android/internal/profiler/BenchmarkSpanBuilder;
}

public final class com/datadog/android/internal/profiler/GlobalBenchmark {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/

package com.datadog.android.internal.profiler

import com.datadog.tools.annotation.NoOpImplementation

/**
* Interface of benchmark span builder. This should only used by internal benchmarking.
*/
@NoOpImplementation
interface BenchmarkSpanBuilder {

/**
* Returns a new [BenchmarkSpan] and start the span.
*/
fun startSpan(): BenchmarkSpan
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/

package com.datadog.android.internal.profiler

/**
* Wraps the provided lambda within a [BenchmarkSpan].
* @param T the type returned by the lambda
* @param operationName the name of the [BenchmarkSpan] created around the lambda
* (default is `true`)
* @param block the lambda function traced by this newly created [BenchmarkSpan]
*
*/
inline fun <T : Any?> withinBenchmarkSpan(
operationName: String,
block: BenchmarkSpan.() -> T
): T {
val tracer = GlobalBenchmark.get().getTracer("dd-sdk-android")

val spanBuilder = tracer.spanBuilder(operationName)

val span = spanBuilder.startSpan()

return try {
span.block()
} finally {
span.stop()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import com.datadog.tools.annotation.NoOpImplementation
interface BenchmarkTracer {

/**
* Returns a new [BenchmarkSpan].
* Returns a new [BenchmarkSpanBuilder].
*
* @param spanName The name of the returned span.
* @return a new [BenchmarkSpan].
* @return a new [BenchmarkSpanBuilder].
*/
fun startSpan(spanName: String): BenchmarkSpan
fun spanBuilder(spanName: String): BenchmarkSpanBuilder
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

package com.datadog.benchmark

import com.datadog.android.internal.profiler.GlobalBenchmark
import com.datadog.benchmark.exporter.DatadogMetricExporter
import com.datadog.benchmark.exporter.DatadogSpanExporter
import com.datadog.benchmark.internal.reader.CPUVitalReader
import com.datadog.benchmark.internal.reader.FpsVitalReader
import com.datadog.benchmark.internal.reader.MemoryVitalReader
import com.datadog.benchmark.internal.reader.VitalReader
import com.datadog.benchmark.noop.NoOpObservableDoubleGauge
import com.datadog.benchmark.profiler.DDBenchmarkProfiler
import io.opentelemetry.api.GlobalOpenTelemetry
import io.opentelemetry.api.OpenTelemetry
import io.opentelemetry.api.metrics.Meter
Expand Down Expand Up @@ -105,6 +107,7 @@ class DatadogMeter private constructor(private val meter: Meter) {
.setMeterProvider(sdkMeterProvider)
.build()
GlobalOpenTelemetry.set(openTelemetry)
GlobalBenchmark.register(DDBenchmarkProfiler())
val meter = openTelemetry.getMeter(METER_INSTRUMENTATION_SCOPE_NAME)
return DatadogMeter(meter)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/

package com.datadog.benchmark.profiler

import com.datadog.android.internal.profiler.BenchmarkProfiler
import com.datadog.android.internal.profiler.BenchmarkTracer
import io.opentelemetry.api.GlobalOpenTelemetry

/**
* Implementation of [BenchmarkProfiler].
*/
class DDBenchmarkProfiler : BenchmarkProfiler {

override fun getTracer(operation: String): BenchmarkTracer {
return DDBenchmarkTracer(GlobalOpenTelemetry.get().getTracer(operation))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/

package com.datadog.benchmark.profiler

import com.datadog.android.internal.profiler.BenchmarkSpan
import io.opentelemetry.api.trace.Span
import io.opentelemetry.context.Scope

/**
* Implementation of [BenchmarkSpan].
*/
class DDBenchmarkSpan(private val span: Span, private val scope: Scope) : BenchmarkSpan {
override fun stop() {
scope.close()
span.end()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/

package com.datadog.benchmark.profiler

import com.datadog.android.internal.profiler.BenchmarkSpan
import com.datadog.android.internal.profiler.BenchmarkSpanBuilder
import io.opentelemetry.api.trace.SpanBuilder

/**
* Implementation of [BenchmarkSpanBuilder].
*/
class DDBenchmarkSpanBuilder(
private val spanBuilder: SpanBuilder
) : BenchmarkSpanBuilder {

override fun startSpan(): BenchmarkSpan {
val span = spanBuilder.startSpan()
return DDBenchmarkSpan(span, span.makeCurrent())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/

package com.datadog.benchmark.profiler

import com.datadog.android.internal.profiler.BenchmarkSpanBuilder
import com.datadog.android.internal.profiler.BenchmarkTracer
import io.opentelemetry.api.trace.Tracer
import io.opentelemetry.context.Context

/**
* Implementation of [BenchmarkTracer].
*/
class DDBenchmarkTracer(private val tracer: Tracer) : BenchmarkTracer {

override fun spanBuilder(spanName: String): BenchmarkSpanBuilder {
return DDBenchmarkSpanBuilder(
tracer
.spanBuilder(spanName)
.setParent(Context.current())
)
}
}