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:
ganfra 2023-08-09 12:18:49 +02:00 committed by GitHub
parent 6800723722
commit e8d1f21a14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 455 additions and 162 deletions

View file

@ -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)

View file

@ -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,
)
}
}
}

View file

@ -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()
}
}

View file

@ -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
}
}

View file

@ -1,31 +0,0 @@
/*
* 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.impl.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)
}