Feature/fga/rust sdk tracing (#1036)
* Align TracingConfiguration with iOS * Create TracingTree from rust sdk * tracing: create a working configuration with RustTracingTree * Tracing: WIP implementation of new api * Tracing: clean up * Tracing: use the latest api * Tracing: some more clean up * Remove generated logcat file after compressing it --------- Co-authored-by: ganfra <francoisg@element.io> Co-authored-by: Jorge Martín <jorgem@element.io>
This commit is contained in:
parent
f15817447c
commit
3f1d241b48
19 changed files with 455 additions and 162 deletions
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2022 New Vector Ltd
|
||||
* Copyright (c) 2023 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -17,64 +17,7 @@
|
|||
package io.element.android.libraries.matrix.api.tracing
|
||||
|
||||
data class TracingConfiguration(
|
||||
val overrides: Map<Target, LogLevel> = emptyMap()
|
||||
) {
|
||||
|
||||
// Order should matters
|
||||
private val targets: MutableMap<Target, LogLevel> = mutableMapOf(
|
||||
Target.Common to LogLevel.Warn,
|
||||
Target.Hyper to LogLevel.Warn,
|
||||
Target.Sled to LogLevel.Warn,
|
||||
Target.MatrixSdk.Root to LogLevel.Warn,
|
||||
Target.MatrixSdk.Sled to LogLevel.Warn,
|
||||
Target.MatrixSdk.Crypto to LogLevel.Debug,
|
||||
Target.MatrixSdk.HttpClient to LogLevel.Debug,
|
||||
Target.MatrixSdk.SlidingSync to LogLevel.Trace,
|
||||
Target.MatrixSdk.BaseSlidingSync to LogLevel.Trace,
|
||||
)
|
||||
|
||||
val filter: String
|
||||
get() {
|
||||
overrides.forEach { (target, logLevel) ->
|
||||
targets[target] = logLevel
|
||||
}
|
||||
return targets.map {
|
||||
if (it.key.filter.isEmpty()) {
|
||||
it.value.filter
|
||||
} else {
|
||||
"${it.key.filter}=${it.value.filter}"
|
||||
}
|
||||
}.joinToString(separator = ",")
|
||||
}
|
||||
}
|
||||
|
||||
sealed class Target(open val filter: String) {
|
||||
object Common : Target("")
|
||||
object Hyper : Target("hyper")
|
||||
object Sled : Target("sled")
|
||||
sealed class MatrixSdk(override val filter: String) : Target(filter) {
|
||||
object Root : MatrixSdk("matrix_sdk")
|
||||
object Sled : MatrixSdk("matrix_sdk_sled")
|
||||
object Crypto: MatrixSdk("matrix_sdk_crypto")
|
||||
object FFI : MatrixSdk("matrix_sdk_ffi")
|
||||
object HttpClient : MatrixSdk("matrix_sdk::http_client")
|
||||
object UniffiAPI : MatrixSdk("matrix_sdk_ffi::uniffi_api")
|
||||
object SlidingSync : MatrixSdk("matrix_sdk::sliding_sync")
|
||||
object BaseSlidingSync : MatrixSdk("matrix_sdk_base::sliding_sync")
|
||||
}
|
||||
}
|
||||
|
||||
sealed class LogLevel(val filter: String) {
|
||||
object Warn : LogLevel("warn")
|
||||
object Trace : LogLevel("trace")
|
||||
object Info : LogLevel("info")
|
||||
object Debug : LogLevel("debug")
|
||||
object Error : LogLevel("error")
|
||||
}
|
||||
|
||||
object TracingConfigurations {
|
||||
val release = TracingConfiguration(overrides = mapOf(Target.Common to LogLevel.Info))
|
||||
val debug = TracingConfiguration(overrides = mapOf(Target.Common to LogLevel.Info))
|
||||
|
||||
fun custom(overrides: Map<Target, LogLevel>) = TracingConfiguration(overrides)
|
||||
}
|
||||
val filterConfiguration: TracingFilterConfiguration,
|
||||
val writesToLogcat: Boolean,
|
||||
val writesToFilesConfiguration: WriteToFilesConfiguration,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,93 @@
|
|||
/*
|
||||
* Copyright (c) 2022 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.matrix.api.tracing
|
||||
|
||||
data class TracingFilterConfiguration(
|
||||
val overrides: Map<Target, LogLevel> = emptyMap(),
|
||||
) {
|
||||
|
||||
// Order should matters
|
||||
private val targetsToLogLevel: MutableMap<Target, LogLevel> = mutableMapOf(
|
||||
Target.COMMON to LogLevel.Info,
|
||||
Target.HYPER to LogLevel.Warn,
|
||||
Target.MATRIX_SDK_CRYPTO to LogLevel.Debug,
|
||||
Target.MATRIX_SDK_HTTP_CLIENT to LogLevel.Debug,
|
||||
Target.MATRIX_SDK_SLIDING_SYNC to LogLevel.Trace,
|
||||
Target.MATRIX_SDK_BASE_SLIDING_SYNC to LogLevel.Trace,
|
||||
Target.MATRIX_SDK_UI_TIMELINE to LogLevel.Info,
|
||||
)
|
||||
|
||||
val filter: String
|
||||
get() {
|
||||
overrides.forEach { (target, logLevel) ->
|
||||
targetsToLogLevel[target] = logLevel
|
||||
}
|
||||
return targetsToLogLevel.map {
|
||||
if (it.key.filter.isEmpty()) {
|
||||
it.value.filter
|
||||
} else {
|
||||
"${it.key.filter}=${it.value.filter}"
|
||||
}
|
||||
}.joinToString(separator = ",")
|
||||
}
|
||||
}
|
||||
|
||||
enum class Target(open val filter: String) {
|
||||
COMMON(""),
|
||||
ELEMENT("elementx"),
|
||||
HYPER("hyper"),
|
||||
MATRIX_SDK_FFI("matrix_sdk_ffi"),
|
||||
MATRIX_SDK_UNIFFI_API("matrix_sdk_ffi::uniffi_api"),
|
||||
MATRIX_SDK_CRYPTO("matrix_sdk_crypto"),
|
||||
MATRIX_SDK_HTTP_CLIENT("matrix_sdk::http_client"),
|
||||
MATRIX_SDK_SLIDING_SYNC("matrix_sdk::sliding_sync"),
|
||||
MATRIX_SDK_BASE_SLIDING_SYNC("matrix_sdk_base::sliding_sync"),
|
||||
MATRIX_SDK_UI_TIMELINE("matrix_sdk_ui::timeline"),
|
||||
}
|
||||
|
||||
sealed class LogLevel(val filter: String) {
|
||||
object Warn : LogLevel("warn")
|
||||
object Trace : LogLevel("trace")
|
||||
object Info : LogLevel("info")
|
||||
object Debug : LogLevel("debug")
|
||||
object Error : LogLevel("error")
|
||||
}
|
||||
|
||||
object TracingFilterConfigurations {
|
||||
val release = TracingFilterConfiguration(
|
||||
overrides = mapOf(
|
||||
Target.COMMON to LogLevel.Info,
|
||||
Target.ELEMENT to LogLevel.Debug
|
||||
),
|
||||
)
|
||||
val debug = TracingFilterConfiguration(
|
||||
overrides = mapOf(
|
||||
Target.COMMON to LogLevel.Info,
|
||||
Target.ELEMENT to LogLevel.Trace
|
||||
)
|
||||
)
|
||||
|
||||
/**
|
||||
* Use this method to create a custom configuration where all targets will have the same log level.
|
||||
*/
|
||||
fun custom(logLevel: LogLevel) = TracingFilterConfiguration(overrides = Target.values().associateWith { logLevel })
|
||||
|
||||
/**
|
||||
* Use this method to override the log level of specific targets.
|
||||
*/
|
||||
fun custom(overrides: Map<Target, LogLevel>) = TracingFilterConfiguration(overrides)
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2022 New Vector Ltd
|
||||
* Copyright (c) 2023 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -14,18 +14,11 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.matrix.impl.tracing
|
||||
package io.element.android.libraries.matrix.api.tracing
|
||||
|
||||
import io.element.android.libraries.matrix.api.tracing.TracingConfiguration
|
||||
import timber.log.Timber
|
||||
|
||||
fun setupTracing(tracingConfiguration: TracingConfiguration) {
|
||||
val filter = tracingConfiguration.filter
|
||||
Timber.v("Tracing config filter = $filter")
|
||||
val rustTracingConfiguration = org.matrix.rustcomponents.sdk.TracingConfiguration(
|
||||
filter = filter,
|
||||
writeToStdoutOrSystem = true,
|
||||
writeToFiles = null,
|
||||
)
|
||||
org.matrix.rustcomponents.sdk.setupTracing(rustTracingConfiguration)
|
||||
interface TracingService {
|
||||
fun setupTracing(tracingConfiguration: TracingConfiguration)
|
||||
fun createTimberTree(): Timber.Tree
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* Copyright (c) 2023 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.matrix.api.tracing
|
||||
|
||||
sealed class WriteToFilesConfiguration {
|
||||
object Disabled : WriteToFilesConfiguration()
|
||||
data class Enabled(val directory: String, val filenamePrefix: String) : WriteToFilesConfiguration()
|
||||
}
|
||||
|
|
@ -29,7 +29,7 @@ anvil {
|
|||
}
|
||||
|
||||
dependencies {
|
||||
// api(projects.libraries.rustsdk)
|
||||
// implementation(projects.libraries.rustsdk)
|
||||
implementation(libs.matrix.sdk)
|
||||
implementation(projects.libraries.di)
|
||||
implementation(projects.libraries.androidutils)
|
||||
|
|
|
|||
|
|
@ -37,12 +37,12 @@ class RustSyncService(
|
|||
) : SyncService {
|
||||
|
||||
override suspend fun startSync() = runCatching {
|
||||
Timber.v("Start sync")
|
||||
Timber.i("Start sync")
|
||||
innerSyncService.start()
|
||||
}
|
||||
|
||||
override fun stopSync() = runCatching {
|
||||
Timber.v("Stop sync")
|
||||
Timber.i("Stop sync")
|
||||
innerSyncService.pause()
|
||||
}
|
||||
|
||||
|
|
@ -50,7 +50,7 @@ class RustSyncService(
|
|||
roomListStateFlow
|
||||
.map(RoomListServiceState::toSyncState)
|
||||
.onEach { state ->
|
||||
Timber.v("Sync state=$state")
|
||||
Timber.i("Sync state=$state")
|
||||
}
|
||||
.distinctUntilChanged()
|
||||
.stateIn(sessionCoroutineScope, SharingStarted.Eagerly, SyncState.Idle)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Copyright (c) 2023 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.matrix.impl.tracing
|
||||
|
||||
/**
|
||||
* This class is used to provide file, line, column information to the Rust SDK [org.matrix.rustcomponents.sdk.logEvent] method.
|
||||
* The data is extracted from a [StackTraceElement] instance.
|
||||
*/
|
||||
data class LogEventLocation(
|
||||
val file: String,
|
||||
val line: UInt,
|
||||
val column: UInt,
|
||||
) {
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Create a [LogEventLocation] from a [StackTraceElement].
|
||||
*/
|
||||
fun from(stackTraceElement: StackTraceElement): LogEventLocation {
|
||||
return LogEventLocation(
|
||||
file = stackTraceElement.fileName,
|
||||
line = stackTraceElement.lineNumber.toUInt(),
|
||||
column = 0u,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Copyright (c) 2023 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.matrix.impl.tracing
|
||||
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.matrix.api.tracing.TracingConfiguration
|
||||
import io.element.android.libraries.matrix.api.tracing.TracingService
|
||||
import io.element.android.libraries.matrix.api.tracing.WriteToFilesConfiguration
|
||||
import org.matrix.rustcomponents.sdk.TracingFileConfiguration
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
@ContributesBinding(AppScope::class)
|
||||
class RustTracingService @Inject constructor() : TracingService {
|
||||
|
||||
override fun setupTracing(tracingConfiguration: TracingConfiguration) {
|
||||
val filter = tracingConfiguration.filterConfiguration
|
||||
val rustTracingConfiguration = org.matrix.rustcomponents.sdk.TracingConfiguration(
|
||||
filter = tracingConfiguration.filterConfiguration.filter,
|
||||
writeToStdoutOrSystem = tracingConfiguration.writesToLogcat,
|
||||
writeToFiles = when (val writeToFilesConfiguration = tracingConfiguration.writesToFilesConfiguration) {
|
||||
is WriteToFilesConfiguration.Disabled -> null
|
||||
is WriteToFilesConfiguration.Enabled -> TracingFileConfiguration(
|
||||
path = writeToFilesConfiguration.directory,
|
||||
filePrefix = writeToFilesConfiguration.filenamePrefix,
|
||||
)
|
||||
},
|
||||
)
|
||||
org.matrix.rustcomponents.sdk.setupTracing(rustTracingConfiguration)
|
||||
Timber.v("Tracing config filter = $filter")
|
||||
}
|
||||
|
||||
override fun createTimberTree(): Timber.Tree {
|
||||
return RustTracingTree()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* Copyright (c) 2023 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.matrix.impl.tracing
|
||||
|
||||
import android.util.Log
|
||||
import io.element.android.libraries.matrix.api.tracing.Target
|
||||
import org.matrix.rustcomponents.sdk.LogLevel
|
||||
import org.matrix.rustcomponents.sdk.logEvent
|
||||
import timber.log.Timber
|
||||
|
||||
/**
|
||||
* List of fully qualified class names to ignore when looking for the first stack trace element.
|
||||
*/
|
||||
private val fqcnIgnore = listOf(
|
||||
Timber::class.java.name,
|
||||
Timber.Forest::class.java.name,
|
||||
Timber.Tree::class.java.name,
|
||||
RustTracingTree::class.java.name,
|
||||
)
|
||||
|
||||
/**
|
||||
* A Timber tree that passes logs to the Rust SDK.
|
||||
*/
|
||||
internal class RustTracingTree : Timber.Tree() {
|
||||
|
||||
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
|
||||
val location = getLogEventLocationFromStackTrace()
|
||||
val logLevel = priority.toLogLevel()
|
||||
logEvent(
|
||||
file = location.file,
|
||||
line = location.line,
|
||||
column = location.column,
|
||||
level = logLevel,
|
||||
target = Target.ELEMENT.filter,
|
||||
message = message,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the [LogEventLocation] from the stack trace.
|
||||
*/
|
||||
private fun getLogEventLocationFromStackTrace(): LogEventLocation {
|
||||
return Throwable(null, null).stackTrace
|
||||
.first { it.className !in fqcnIgnore }
|
||||
.let(LogEventLocation::from)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a log priority to a Rust SDK log level.
|
||||
*/
|
||||
private fun Int.toLogLevel(): LogLevel {
|
||||
return when (this) {
|
||||
Log.VERBOSE -> LogLevel.TRACE
|
||||
Log.DEBUG -> LogLevel.DEBUG
|
||||
Log.INFO -> LogLevel.INFO
|
||||
Log.WARN -> LogLevel.WARN
|
||||
Log.ERROR -> LogLevel.ERROR
|
||||
else -> LogLevel.DEBUG
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue