-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #600 from embrace-io/leandro/composite_log
Switch between event and OTel log services based on configuration
- Loading branch information
Showing
9 changed files
with
356 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
...roid-sdk/src/main/java/io/embrace/android/embracesdk/internal/logs/CompositeLogService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package io.embrace.android.embracesdk.internal.logs | ||
|
||
import io.embrace.android.embracesdk.Embrace | ||
import io.embrace.android.embracesdk.EventType | ||
import io.embrace.android.embracesdk.LogExceptionType | ||
import io.embrace.android.embracesdk.Severity | ||
import io.embrace.android.embracesdk.config.ConfigService | ||
import io.embrace.android.embracesdk.event.LogMessageService | ||
import io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger | ||
import io.embrace.android.embracesdk.payload.NetworkCapturedCall | ||
|
||
/** | ||
* Allows to switch between the current service that sends Embrace logs as Events and the new one | ||
* that sends them as OTel logs, based on a remote configuration. Once the SDK is expected to send | ||
* only OTel logs, this class can be removed. | ||
*/ | ||
internal class CompositeLogService( | ||
private val v1LogService: LogMessageService, | ||
private val v2LogService: LogService, | ||
private val configService: ConfigService | ||
) : LogMessageService { | ||
|
||
private val useV2LogService: Boolean | ||
get() = configService.sessionBehavior.useV2Payload() | ||
|
||
private val baseLogService: BaseLogService | ||
get() = if (useV2LogService) v2LogService else v1LogService | ||
|
||
override fun logNetwork(networkCaptureCall: NetworkCapturedCall?) { | ||
// Network logs are still always handled by the v1 LogMessageService | ||
v1LogService.logNetwork(networkCaptureCall) | ||
} | ||
|
||
override fun log( | ||
message: String, | ||
type: EventType, | ||
logExceptionType: LogExceptionType, | ||
properties: Map<String, Any>?, | ||
stackTraceElements: Array<StackTraceElement>?, | ||
customStackTrace: String?, | ||
framework: Embrace.AppFramework, | ||
context: String?, | ||
library: String?, | ||
exceptionName: String?, | ||
exceptionMessage: String? | ||
) { | ||
// When LogMessageService is removed, the cascade calling of event-based logMessage() | ||
// in EmbraceImpl can be replaced with different calls to LogService | ||
if (useV2LogService) { | ||
// Currently, any call to this log method can only have an event type of INFO_LOG, | ||
// WARNING_LOG, or ERROR_LOG, since it is taken from the fromSeverity() method | ||
// in EventType. | ||
if (type.getSeverity() == null) { | ||
InternalStaticEmbraceLogger.logError("Invalid event type for log: $type") | ||
return | ||
} | ||
val severity = type.getSeverity() ?: Severity.INFO | ||
if (logExceptionType == LogExceptionType.NONE) { | ||
v2LogService.log( | ||
message, | ||
severity, | ||
properties | ||
) | ||
} else { | ||
v2LogService.logException( | ||
message = message, | ||
severity = severity, | ||
logExceptionType = logExceptionType, | ||
properties = properties, | ||
stackTraceElements = stackTraceElements, | ||
customStackTrace = customStackTrace, | ||
framework = framework, | ||
context = context, | ||
library = library, | ||
exceptionName = exceptionName, | ||
exceptionMessage = exceptionMessage | ||
) | ||
} | ||
} else { | ||
v1LogService.log( | ||
message = message, | ||
type = type, | ||
logExceptionType = logExceptionType, | ||
properties = properties, | ||
stackTraceElements = stackTraceElements, | ||
customStackTrace = customStackTrace, | ||
framework = framework, | ||
context = context, | ||
library = library, | ||
exceptionName = exceptionName, | ||
exceptionMessage = exceptionMessage | ||
) | ||
} | ||
} | ||
|
||
override fun findInfoLogIds(startTime: Long, endTime: Long): List<String> { | ||
return baseLogService.findInfoLogIds(startTime, endTime) | ||
} | ||
|
||
override fun findWarningLogIds(startTime: Long, endTime: Long): List<String> { | ||
return baseLogService.findWarningLogIds(startTime, endTime) | ||
} | ||
|
||
override fun findErrorLogIds(startTime: Long, endTime: Long): List<String> { | ||
return baseLogService.findErrorLogIds(startTime, endTime) | ||
} | ||
|
||
override fun findNetworkLogIds(startTime: Long, endTime: Long): List<String> { | ||
// Network logs are still always handled by the v1 LogMessageService | ||
return v1LogService.findNetworkLogIds(startTime, endTime) | ||
} | ||
|
||
override fun getInfoLogsAttemptedToSend(): Int { | ||
return baseLogService.getInfoLogsAttemptedToSend() | ||
} | ||
|
||
override fun getWarnLogsAttemptedToSend(): Int { | ||
return baseLogService.getWarnLogsAttemptedToSend() | ||
} | ||
|
||
override fun getErrorLogsAttemptedToSend(): Int { | ||
return baseLogService.getErrorLogsAttemptedToSend() | ||
} | ||
|
||
override fun getUnhandledExceptionsSent(): Int { | ||
return baseLogService.getUnhandledExceptionsSent() | ||
} | ||
|
||
override fun cleanCollections() { | ||
return baseLogService.cleanCollections() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/fakes/FakeLogService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package io.embrace.android.embracesdk.fakes | ||
|
||
import io.embrace.android.embracesdk.Embrace | ||
import io.embrace.android.embracesdk.LogExceptionType | ||
import io.embrace.android.embracesdk.Severity | ||
import io.embrace.android.embracesdk.internal.logs.LogService | ||
|
||
internal class FakeLogService : LogService { | ||
|
||
val loggedMessages = mutableListOf<String>() | ||
val loggedExceptions = mutableListOf<String>() | ||
|
||
override fun log(message: String, severity: Severity, properties: Map<String, Any>?) { | ||
loggedMessages.add(message) | ||
} | ||
|
||
override fun logException( | ||
message: String, | ||
severity: Severity, | ||
logExceptionType: LogExceptionType, | ||
properties: Map<String, Any>?, | ||
stackTraceElements: Array<StackTraceElement>?, | ||
customStackTrace: String?, | ||
framework: Embrace.AppFramework, | ||
context: String?, | ||
library: String?, | ||
exceptionName: String?, | ||
exceptionMessage: String? | ||
) { | ||
loggedExceptions.add(message) | ||
} | ||
|
||
override fun findInfoLogIds(startTime: Long, endTime: Long): List<String> { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun findWarningLogIds(startTime: Long, endTime: Long): List<String> { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun findErrorLogIds(startTime: Long, endTime: Long): List<String> { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun getInfoLogsAttemptedToSend(): Int { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun getWarnLogsAttemptedToSend(): Int { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun getErrorLogsAttemptedToSend(): Int { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun getUnhandledExceptionsSent(): Int { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun cleanCollections() { | ||
TODO("Not yet implemented") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.