Skip to content

Commit

Permalink
addLogRecordExporter api (#708)
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsitoPuglisi authored Apr 22, 2024
1 parent 6ea2748 commit c8d2f62
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions embrace-android-sdk/api/embrace-android-sdk.api
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public final class io/embrace/android/embracesdk/BuildConfig {

public final class io/embrace/android/embracesdk/Embrace : io/embrace/android/embracesdk/EmbraceAndroidApi {
public fun addBreadcrumb (Ljava/lang/String;)V
public fun addLogRecordExporter (Lio/opentelemetry/sdk/logs/export/LogRecordExporter;)V
public fun addSessionProperty (Ljava/lang/String;Ljava/lang/String;Z)Z
public fun addSpanExporter (Lio/opentelemetry/sdk/trace/export/SpanExporter;)V
public fun addUserPersona (Ljava/lang/String;)V
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.embrace.android.embracesdk.testcases

import android.os.Build
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.embrace.android.embracesdk.IntegrationTestRule
import io.embrace.android.embracesdk.Severity
import io.embrace.android.embracesdk.fakes.FakeLogRecordExporter
import io.embrace.android.embracesdk.fakes.FakeLoggerAction
import io.embrace.android.embracesdk.logging.InternalEmbraceLogger
import io.embrace.android.embracesdk.recordSession
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.annotation.Config
import java.lang.Thread.sleep

@Config(sdk = [Build.VERSION_CODES.TIRAMISU])
@RunWith(AndroidJUnit4::class)
internal class LogRecordExporterTest {

@Rule
@JvmField
val testRule: IntegrationTestRule = IntegrationTestRule(
harnessSupplier = {
IntegrationTestRule.newHarness(startImmediately = false)
}
)

@Test
fun `SDK can receive a LogRecordExporter`() {
with(testRule) {

val fakeLogRecordExporter = FakeLogRecordExporter()
embrace.addLogRecordExporter(fakeLogRecordExporter)
embrace.start(harness.overriddenCoreModule.context)

harness.recordSession {
embrace.logMessage("test message", Severity.INFO)

sleep(3000)
}
Assert.assertTrue((fakeLogRecordExporter.exportedLogs?.size ?: 0) > 0)
Assert.assertEquals("test message", fakeLogRecordExporter.exportedLogs?.first()?.body?.asString())
}
}

@Test
fun `a LogRecordExporter added after initialization won't be used`() {
with(testRule) {

val fake = FakeLoggerAction()
harness.overriddenInitModule.logger.apply { addLoggerAction(fake) }

val fakeLogRecordExporter = FakeLogRecordExporter()
embrace.start(harness.overriddenCoreModule.context)
embrace.addLogRecordExporter(fakeLogRecordExporter)

harness.recordSession {
embrace.logMessage("test message", Severity.INFO)

sleep(3000)
}
Assert.assertTrue((fakeLogRecordExporter.exportedLogs?.size ?: 0) == 0)
Assert.assertTrue(fake.msgQueue.any {
it.msg == "A LogRecordExporter can only be added before the SDK is started."
})
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package io.embrace.android.embracesdk.testcases
import android.os.Build
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.embrace.android.embracesdk.IntegrationTestRule
import io.embrace.android.embracesdk.Severity
import io.embrace.android.embracesdk.fakes.FakeLogRecordExporter
import io.embrace.android.embracesdk.fakes.FakeLoggerAction
import io.embrace.android.embracesdk.fakes.FakeSpanExporter
import io.embrace.android.embracesdk.opentelemetry.assertExpectedAttributes
import io.embrace.android.embracesdk.opentelemetry.assertHasEmbraceAttribute
import io.embrace.android.embracesdk.opentelemetry.embProcessIdentifier
import io.embrace.android.embracesdk.opentelemetry.embSequenceId
import io.embrace.android.embracesdk.recordSession
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Rule
Expand Down Expand Up @@ -63,4 +67,29 @@ internal class SpanTest {
}
}
}



@Test
fun `a SpanExporter added after initialization won't be used`() {
with(testRule) {

val fake = FakeLoggerAction()
harness.overriddenInitModule.logger.apply { addLoggerAction(fake) }

val fakeSpanExporter = FakeSpanExporter()
embrace.start(harness.overriddenCoreModule.context)
embrace.addSpanExporter(fakeSpanExporter)

harness.recordSession {
embrace.startSpan("test")?.stop()

Thread.sleep(3000)
}
assertTrue(fakeSpanExporter.exportedSpans.size == 0)
assertTrue(fake.msgQueue.any {
it.msg == "A SpanExporter can only be added before the SDK is started."
})
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.embrace.android.embracesdk.spans.EmbraceSpan;
import io.embrace.android.embracesdk.spans.EmbraceSpanEvent;
import io.embrace.android.embracesdk.spans.ErrorCode;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import kotlin.jvm.functions.Function0;

Expand Down Expand Up @@ -542,6 +543,18 @@ public void addSpanExporter(@NonNull SpanExporter spanExporter) {
}
}

/**
* Adds a [LogRecordExporter] to the open telemetry logger.
*
* @param logRecordExporter the LogRecord exporter to add
*/
@Override
public void addLogRecordExporter(@NonNull LogRecordExporter logRecordExporter) {
if (verifyNonNullParameters("addLogRecordExporter", logRecordExporter)) {
impl.addLogRecordExporter(logRecordExporter);
}
}

@Override
public void logPushNotification(@Nullable String title,
@Nullable String body,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import androidx.annotation.Nullable;

import io.embrace.android.embracesdk.spans.TracingApi;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.trace.export.SpanExporter;

/**
Expand Down Expand Up @@ -95,4 +96,11 @@ interface EmbraceApi extends LogsApi, MomentsApi, NetworkRequestApi, SessionApi,
* @param spanExporter the span exporter to add
*/
void addSpanExporter(@NonNull SpanExporter spanExporter);

/**
* Adds a [LogRecordExporter] to the open telemetry logger.
*
* @param logRecordExporter the LogRecord exporter to add
*/
void addLogRecordExporter(@NonNull LogRecordExporter logRecordExporter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import io.embrace.android.embracesdk.utils.PropertyUtils;
import io.embrace.android.embracesdk.worker.WorkerName;
import io.embrace.android.embracesdk.worker.WorkerThreadModule;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import kotlin.Lazy;
import kotlin.LazyKt;
Expand Down Expand Up @@ -1203,6 +1204,18 @@ private boolean checkSdkStarted(@NonNull String action, boolean logPublicApiUsag
}

public void addSpanExporter(@NonNull SpanExporter spanExporter) {
if (isStarted()) {
internalEmbraceLogger.logError("A SpanExporter can only be added before the SDK is started.");
return;
}
moduleInitBootstrapper.getOpenTelemetryModule().getOpenTelemetryConfiguration().addSpanExporter(spanExporter);
}

public void addLogRecordExporter(@NonNull LogRecordExporter logRecordExporter) {
if (isStarted()) {
internalEmbraceLogger.logError("A LogRecordExporter can only be added before the SDK is started.");
return;
}
moduleInitBootstrapper.getOpenTelemetryModule().getOpenTelemetryConfiguration().addLogExporter(logRecordExporter);
}
}

0 comments on commit c8d2f62

Please sign in to comment.