Skip to content

Commit

Permalink
poc: continue refactoring log implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Jan 13, 2025
1 parent 94f7f51 commit 0d782b2
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package io.embrace.android.embracesdk.internal.logs
import io.embrace.android.embracesdk.LogExceptionType
import io.embrace.android.embracesdk.Severity
import io.embrace.android.embracesdk.internal.arch.destination.LogWriter
import io.embrace.android.embracesdk.internal.arch.schema.EmbType.System.FlutterException.embFlutterExceptionContext
import io.embrace.android.embracesdk.internal.arch.schema.EmbType.System.FlutterException.embFlutterExceptionLibrary
import io.embrace.android.embracesdk.internal.arch.schema.SchemaType
import io.embrace.android.embracesdk.internal.arch.schema.SchemaType.Exception
import io.embrace.android.embracesdk.internal.arch.schema.SchemaType.FlutterException
Expand All @@ -18,7 +16,9 @@ import io.embrace.android.embracesdk.internal.payload.AppFramework
import io.embrace.android.embracesdk.internal.serialization.PlatformSerializer
import io.embrace.android.embracesdk.internal.serialization.truncatedStacktrace
import io.embrace.android.embracesdk.internal.spans.toOtelSeverity
import io.embrace.android.embracesdk.internal.utils.PropertyUtils.normalizeProperties
import io.embrace.android.embracesdk.internal.utils.Uuid
import io.opentelemetry.api.common.AttributeKey
import io.opentelemetry.semconv.ExceptionAttributes
import io.opentelemetry.semconv.incubating.LogIncubatingAttributes

Expand Down Expand Up @@ -46,13 +46,12 @@ class EmbraceLogService(
properties: Map<String, Any>?,
stackTraceElements: Array<StackTraceElement>?,
customStackTrace: String?,
context: String?,
library: String?,
exceptionName: String?,
exceptionMessage: String?,
customLogAttrs: Map<AttributeKey<String>, String> // TODO: lift up & reduce callers
) {
val redactedProperties = redactSensitiveProperties(properties)
val attrs = createTelemetryAttributes(redactedProperties)
val redactedProperties = redactSensitiveProperties(normalizeProperties(properties))
val attrs = createTelemetryAttributes(redactedProperties, customLogAttrs)

val schemaProvider: (TelemetryAttributes) -> SchemaType = when {
logExceptionType == LogExceptionType.NONE -> ::Log
Expand All @@ -69,13 +68,6 @@ class EmbraceLogService(
type = exceptionName,
message = exceptionMessage,
)
if (configService.appFramework == AppFramework.FLUTTER) {
populateFlutterExceptionAttrs(
context = context,
library = library,
attrs = attrs
)
}
}
addLogEventData(
message = message,
Expand All @@ -85,15 +77,6 @@ class EmbraceLogService(
)
}

private fun populateFlutterExceptionAttrs(
context: String?,
library: String?,
attrs: TelemetryAttributes,
) {
context?.let { attrs.setAttribute(embFlutterExceptionContext, it) }
library?.let { attrs.setAttribute(embFlutterExceptionLibrary, it) }
}

override fun getErrorLogsCount(): Int {
return logCounters.getValue(Severity.ERROR).getCount()
}
Expand All @@ -105,13 +88,19 @@ class EmbraceLogService(
/**
* Create [TelemetryAttributes] with the standard log properties
*/
private fun createTelemetryAttributes(customProperties: Map<String, Any>?): TelemetryAttributes {
private fun createTelemetryAttributes(
customProperties: Map<String, Any>?,
logAttrs: Map<AttributeKey<String>, String>
): TelemetryAttributes {
val attributes = TelemetryAttributes(
configService = configService,
sessionPropertiesProvider = sessionPropertiesService::getProperties,
customAttributes = customProperties?.mapValues { it.value.toString() } ?: emptyMap()
)
attributes.setAttribute(LogIncubatingAttributes.LOG_RECORD_UID, Uuid.getEmbUuid())
logAttrs.forEach {
attributes.setAttribute(it.key, it.value)
}
return attributes
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.embrace.android.embracesdk.internal.logs
import io.embrace.android.embracesdk.LogExceptionType
import io.embrace.android.embracesdk.Severity
import io.embrace.android.embracesdk.internal.session.MemoryCleanerListener
import io.opentelemetry.api.common.AttributeKey

/**
* Creates log records to be sent using the Open Telemetry Logs data model.
Expand All @@ -13,13 +14,11 @@ interface LogService : MemoryCleanerListener {
* Creates a remote log.
*
* @param message the message to log
* @param type the type of message to log, which must be INFO_LOG, WARNING_LOG, or ERROR_LOG
* @param severity the log severity
* @param logExceptionType whether the log is a handled exception, unhandled, or non an exception
* @param properties custom properties to send as part of the event
* @param stackTraceElements the stacktrace elements of a throwable
* @param customStackTrace stacktrace string for non-JVM exceptions
* @param context the context of the exception
* @param library the library of the exception
* @param exceptionName the exception name of a Throwable is it is present
* @param exceptionMessage the exception message of a Throwable is it is present
*/
Expand All @@ -31,10 +30,9 @@ interface LogService : MemoryCleanerListener {
properties: Map<String, Any>? = null,
stackTraceElements: Array<StackTraceElement>? = null,
customStackTrace: String? = null,
context: String? = null,
library: String? = null,
exceptionName: String? = null,
exceptionMessage: String? = null,
customLogAttrs: Map<AttributeKey<String>, String> = emptyMap()
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import io.embrace.android.embracesdk.fakes.config.FakeInstrumentedConfig
import io.embrace.android.embracesdk.fakes.config.FakeRedactionConfig
import io.embrace.android.embracesdk.fakes.createSessionBehavior
import io.embrace.android.embracesdk.internal.arch.schema.EmbType
import io.embrace.android.embracesdk.internal.arch.schema.EmbType.System.FlutterException.embFlutterExceptionContext
import io.embrace.android.embracesdk.internal.arch.schema.EmbType.System.FlutterException.embFlutterExceptionLibrary
import io.embrace.android.embracesdk.internal.arch.schema.toSessionPropertyAttributeName
import io.embrace.android.embracesdk.internal.config.behavior.REDACTED_LABEL
import io.embrace.android.embracesdk.internal.config.behavior.SensitiveKeysBehaviorImpl
Expand Down Expand Up @@ -310,8 +312,10 @@ internal class EmbraceLogServiceTest {
stackTraceElements = flutterException.stackTrace,
exceptionName = flutterException.javaClass.simpleName,
exceptionMessage = flutterException.message,
context = flutterContext,
library = flutterLibrary
customLogAttrs = mapOf(
embFlutterExceptionContext.attributeKey to flutterContext,
embFlutterExceptionLibrary.attributeKey to flutterLibrary,
),
)

// then the exception is correctly logged
Expand All @@ -323,8 +327,8 @@ internal class EmbraceLogServiceTest {
assertEquals(LogExceptionType.HANDLED.value, attributes[embExceptionHandling.name])
assertEquals(flutterException.javaClass.simpleName, attributes[ExceptionAttributes.EXCEPTION_TYPE.key])
assertEquals(flutterException.message, attributes[ExceptionAttributes.EXCEPTION_MESSAGE.key])
assertEquals(flutterContext, attributes[EmbType.System.FlutterException.embFlutterExceptionContext.name])
assertEquals(flutterLibrary, attributes[EmbType.System.FlutterException.embFlutterExceptionLibrary.name])
assertEquals(flutterContext, attributes[embFlutterExceptionContext.name])
assertEquals(flutterLibrary, attributes[embFlutterExceptionLibrary.name])
log.assertIsType(EmbType.System.FlutterException)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import io.embrace.android.embracesdk.internal.payload.AppFramework
import io.embrace.android.embracesdk.internal.worker.TaskPriority
import io.embrace.android.embracesdk.internal.worker.Worker
import io.embrace.android.embracesdk.spans.TracingApi
import io.opentelemetry.api.common.AttributeKey
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicBoolean

Expand Down Expand Up @@ -300,10 +301,9 @@ internal class EmbraceImpl @JvmOverloads constructor(
stackTraceElements: Array<StackTraceElement>?,
customStackTrace: String?,
logExceptionType: LogExceptionType,
context: String?,
library: String?,
exceptionName: String? = null,
exceptionMessage: String? = null,
customLogAttrs: Map<AttributeKey<String>, String> = emptyMap(),
) {
logsApiDelegate.logMessage(
severity,
Expand All @@ -312,10 +312,9 @@ internal class EmbraceImpl @JvmOverloads constructor(
stackTraceElements,
customStackTrace,
logExceptionType,
context,
library,
exceptionName,
exceptionMessage
exceptionMessage,
customLogAttrs
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ internal class EmbraceInternalInterfaceImpl(
properties,
customStackTrace ?: throwable.stackTrace,
null,
LogExceptionType.NONE,
null,
null
LogExceptionType.NONE
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import io.embrace.android.embracesdk.LogExceptionType
import io.embrace.android.embracesdk.Severity
import io.embrace.android.embracesdk.internal.EmbraceInternalInterface
import io.embrace.android.embracesdk.internal.FlutterInternalInterface
import io.embrace.android.embracesdk.internal.arch.schema.EmbType.System.FlutterException.embFlutterExceptionContext
import io.embrace.android.embracesdk.internal.arch.schema.EmbType.System.FlutterException.embFlutterExceptionLibrary
import io.embrace.android.embracesdk.internal.envelope.metadata.HostedSdkVersionInfo
import io.embrace.android.embracesdk.internal.logging.EmbLogger
import io.opentelemetry.api.common.AttributeKey

internal class FlutterInternalInterfaceImpl(
private val embrace: EmbraceImpl,
Expand Down Expand Up @@ -64,17 +67,20 @@ internal class FlutterInternalInterfaceImpl(
exceptionType: LogExceptionType,
) {
if (embrace.isStarted) {
val attrs = mutableMapOf<AttributeKey<String>, String>()
context?.let { attrs[embFlutterExceptionContext.attributeKey] = it }
library?.let { attrs[embFlutterExceptionLibrary.attributeKey] = it }

embrace.logMessage(
Severity.ERROR,
"Dart error",
null,
null,
stack,
exceptionType,
context,
library,
name,
message
message,
attrs
)
} else {
logger.logSdkNotInitialized("logDartError")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import io.embrace.android.embracesdk.internal.api.LogsApi
import io.embrace.android.embracesdk.internal.injection.ModuleInitBootstrapper
import io.embrace.android.embracesdk.internal.injection.embraceImplInject
import io.embrace.android.embracesdk.internal.payload.PushNotificationBreadcrumb
import io.embrace.android.embracesdk.internal.utils.PropertyUtils.normalizeProperties
import io.embrace.android.embracesdk.internal.utils.getSafeStackTrace
import io.opentelemetry.api.common.AttributeKey

internal class LogsApiDelegate(
bootstrapper: ModuleInitBootstrapper,
Expand All @@ -22,17 +22,9 @@ internal class LogsApiDelegate(
bootstrapper.featureModule.pushNotificationDataSource.dataSource
}

override fun logInfo(message: String) {
logMessage(message, Severity.INFO)
}

override fun logWarning(message: String) {
logMessage(message, Severity.WARNING)
}

override fun logError(message: String) {
logMessage(message, Severity.ERROR)
}
override fun logInfo(message: String) = logMessage(message, Severity.INFO)
override fun logWarning(message: String) = logMessage(message, Severity.WARNING)
override fun logError(message: String) = logMessage(message, Severity.ERROR)

override fun logMessage(message: String, severity: Severity) {
logMessage(message, severity, null)
Expand Down Expand Up @@ -81,9 +73,7 @@ internal class LogsApiDelegate(
properties,
null,
null,
LogExceptionType.NONE,
null,
null
LogExceptionType.NONE
)
}

Expand All @@ -104,8 +94,6 @@ internal class LogsApiDelegate(
throwable.getSafeStackTrace(),
null,
LogExceptionType.HANDLED,
null,
null,
throwable.javaClass.simpleName,
exceptionMessage
)
Expand All @@ -126,8 +114,6 @@ internal class LogsApiDelegate(
null,
LogExceptionType.HANDLED,
null,
null,
null,
message
)
}
Expand All @@ -140,24 +126,24 @@ internal class LogsApiDelegate(
stackTraceElements: Array<StackTraceElement>?,
customStackTrace: String?,
logExceptionType: LogExceptionType,
context: String?,
library: String?,
exceptionName: String? = null,
exceptionMessage: String? = null,
customLogAttrs: Map<AttributeKey<String>, String> = emptyMap(), // TODO: gradually lift up & remove most params
) {
if (sdkCallChecker.check("log_message")) {
runCatching {
val attrs = mutableMapOf<AttributeKey<String>, String>()

logService?.log(
message,
severity,
logExceptionType,
normalizeProperties(properties),
properties,
stackTraceElements,
customStackTrace,
context,
library,
exceptionName,
exceptionMessage
exceptionMessage,
attrs.plus(customLogAttrs)
)
sessionOrchestrator?.reportBackgroundActivityStateChange()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ internal class ReactNativeInternalInterfaceImpl(
properties,
null,
stacktrace,
LogExceptionType.HANDLED,
null,
null
LogExceptionType.HANDLED
)
} else {
logger.logSdkNotInitialized("log JS exception")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ internal class UnityInternalInterfaceImpl(
null,
stacktrace,
exceptionType,
null,
null,
name,
message
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ internal class EmbraceInternalInterfaceImplTest {
emptyMap<String, String>(),
exception.stackTrace,
null,
LogExceptionType.NONE,
null,
null
LogExceptionType.NONE
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ internal class FlutterInternalInterfaceImplTest {
null,
"stack",
LogExceptionType.UNHANDLED,
"ctx",
"lib",
"exception name",
"message"
)
Expand All @@ -87,8 +85,6 @@ internal class FlutterInternalInterfaceImplTest {
null,
"stack",
LogExceptionType.HANDLED,
"ctx",
"lib",
"exception name",
"message"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ internal class UnityInternalInterfaceImplTest {
null,
"stack",
LogExceptionType.UNHANDLED,
null,
null,
"name",
"msg"
)
Expand All @@ -86,8 +84,6 @@ internal class UnityInternalInterfaceImplTest {
null,
"stack",
LogExceptionType.HANDLED,
null,
null,
"name",
"msg"
)
Expand Down
Loading

0 comments on commit 0d782b2

Please sign in to comment.