Comments (1)
Please also provide withTrace
and withSpan
kotlin extensions which make it easy to instrument code with an concise and kotin idomatic syntax. These extensions should also support kotlin coroutines and propagate context in coroutines correctly. I started an implementation my self, here is what I did sofar:
data class SpanContext(val span: Span) : AbstractCoroutineContextElement(SpanContext) {
companion object Key : CoroutineContext.Key<SpanContext>
}
fun CoroutineContext.currentSpan(): Span {
return get(SpanContext)?.span ?: throw IllegalStateException("No active Span in context")
}
suspend fun <T> withTrace(
name: String,
spanName: String,
block: suspend CoroutineScope.() -> T
): T {
val trace = startTrace(name, spanName)
return withSpan(trace, block)
}
suspend fun <T> withSpan(name: String, block: suspend CoroutineScope.() -> T): T {
val span = currentCoroutineContext().currentSpan()
val childSpan = span.startChild(name)
return withSpan(childSpan, block)
}
suspend fun <T> withSpan(span: Span, block: suspend CoroutineScope.() -> T): T {
return try {
withContext(SpanContext(span), block)
} catch (e: Throwable) {
if (e is CancellationException) {
span.setStatus("cancelled")
} else {
span.setThrowable(e)
span.setStatus("error")
}
throw e
} finally {
span.finish()
}
}
This allows to add the context to the coroutine context, which ensures it is propagates in suspend code with structured concurrency. This means the child span must no be propagated manually. The extension functions allow to instrument code as follows:
suspend fun getData(): String {
return withSpan("getData") {
delay(1000)
"foo"
}
}
from sentry-kotlin-multiplatform.
Related Issues (20)
- Test Apple targets don't find Sentry framework when using SPM HOT 22
- isCrashedLastRun method
- The Ability to Adjust App Hang Preferences HOT 4
- Cococa deps updater should also update dependencies in samples
- Docs: clarify difference between native android init docs and kmp android init
- Support Compose Multiplatform
- Crashes on App Boot HOT 4
- [Gradle Plugin] Better support for tests not working on cocoa targets
- Implement & Document `lastEventId` with User Feedback
- Improve the KMP SDK
- Make `setSentryUnhandledExceptionHook` public HOT 2
- Build failed for iOS: Undefined symbols for architecture arm64: "_OBJC_CLASS_$_SentryId" HOT 9
- Problem when used together with Phoenix library HOT 9
- Expose platform specific Configuration via expect actual HOT 18
- ci: build iOS app and upload to TestFlight
- Support Spotlight
- Add internal logging mechanism
- Compilation failed: Linking globals named 'kniprot_cocoapods_Sentry3_SentryMetricsAPIDelegate': symbol multiply defined! HOT 9
- WASM support HOT 1
- Send trace origin HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sentry-kotlin-multiplatform.