Skip to content

Commit ff6a1f0

Browse files
committed
chore: misc updated to ffm and otel
1 parent 7bbb747 commit ff6a1f0

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed

backend/jvm/src/main/kotlin/dev/suresh/lang/FFM.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ object FFM {
2323
}
2424

2525
private fun KLogger.strlen(str: String) {
26-
val strlenAddr = SYMBOL_LOOKUP.findOrNull("strlen")
26+
val strlenAddr = SYMBOL_LOOKUP.findOrThrow("strlen")
2727
val strlenDescriptor = FunctionDescriptor.of(ValueLayout.JAVA_INT, AddressLayout.ADDRESS)
2828
val strlen = LINKER.downcallHandle(strlenAddr, strlenDescriptor)
2929
Arena.ofConfined().use { arena ->
@@ -35,15 +35,15 @@ object FFM {
3535

3636
private fun KLogger.currTime() {
3737
// Print the current time.
38-
val timeAddr = SYMBOL_LOOKUP.findOrNull("time")
38+
val timeAddr = SYMBOL_LOOKUP.findOrThrow("time")
3939
val timeDesc = FunctionDescriptor.of(ValueLayout.JAVA_LONG)
4040
val time = LINKER.downcallHandle(timeAddr, timeDesc)
4141
val timeResult = time.invokeExact() as Long
4242
info { "time() = $timeResult epochSecond" }
4343
}
4444

4545
private fun KLogger.gmtime() {
46-
val gmtAddr = SYMBOL_LOOKUP.findOrNull("gmtime")
46+
val gmtAddr = SYMBOL_LOOKUP.findOrThrow("gmtime")
4747
val gmtDesc =
4848
FunctionDescriptor.of(
4949
AddressLayout.ADDRESS.withTargetLayout(TM.LAYOUT), AddressLayout.ADDRESS)
@@ -58,7 +58,7 @@ object FFM {
5858
}
5959

6060
private fun KLogger.getPid() {
61-
val getpidAddr = SYMBOL_LOOKUP.findOrNull("getpid")
61+
val getpidAddr = SYMBOL_LOOKUP.findOrThrow("getpid")
6262
val getpidDesc = FunctionDescriptor.of(ValueLayout.JAVA_INT)
6363
val getpid = LINKER.downcallHandle(getpidAddr, getpidDesc)
6464
val pid = getpid.invokeExact() as Int
Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,44 @@
11
package dev.suresh.plugins
22

3+
import BuildConfig
4+
import io.ktor.http.HttpMethod
35
import io.ktor.server.application.*
4-
import io.opentelemetry.api.OpenTelemetry
6+
import io.ktor.server.request.httpMethod
7+
import io.opentelemetry.api.trace.SpanKind
8+
import io.opentelemetry.instrumentation.ktor.v3_0.*
9+
import io.opentelemetry.sdk.OpenTelemetrySdk
510
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk
611
import io.opentelemetry.semconv.ServiceAttributes
12+
import kotlin.time.Clock
713

814
fun Application.configureOTel() {
9-
// install(OTelExtnPlugin) { enabled = true }
10-
//
11-
// install(KtorServerTracing) {
12-
// setOpenTelemetry(GlobalOpenTelemetry.get())
13-
// attributeExtractor {
14-
// onStart { attributes.put("start-time", Clock.System.now().toEpochMilliseconds()) }
15-
// onEnd { attributes.put("end-time", Clock.System.now().toEpochMilliseconds()) }
16-
// }
17-
// }
18-
}
15+
install(KtorServerTelemetry) {
16+
setOpenTelemetry(getOpenTelemetry(BuildConfig.name))
1917

20-
/**
21-
* [Manual
22-
* instrumentation](https://opentelemetry.io/docs/languages/java/instrumentation/#acquiring-a-tracer-in-java-agent)
23-
*/
24-
fun getOpenTelemetry(serviceName: String): OpenTelemetry {
25-
return AutoConfiguredOpenTelemetrySdk.builder()
26-
.addResourceCustomizer { oldResource, _ ->
27-
oldResource
28-
.toBuilder()
29-
.putAll(oldResource.attributes)
30-
.put(ServiceAttributes.SERVICE_NAME, serviceName)
31-
.build()
18+
spanKindExtractor {
19+
if (httpMethod == HttpMethod.Post) {
20+
SpanKind.PRODUCER
21+
} else {
22+
SpanKind.CLIENT
3223
}
33-
.build()
34-
.openTelemetrySdk
24+
}
25+
26+
attributesExtractor {
27+
onStart { attributes.put("start-time", Clock.System.now().toEpochMilliseconds()) }
28+
onEnd { attributes.put("end-time", Clock.System.now().toEpochMilliseconds()) }
29+
}
30+
}
31+
// install(OTelExtnPlugin) { enabled = true }
3532
}
33+
34+
/** See [Configure the SDK](https://opentelemetry.io/docs/languages/java/configuration/) */
35+
fun getOpenTelemetry(serviceName: String): OpenTelemetrySdk =
36+
AutoConfiguredOpenTelemetrySdk.builder()
37+
.addResourceCustomizer { res, _ ->
38+
res.toBuilder()
39+
.putAll(res.attributes)
40+
.put(ServiceAttributes.SERVICE_NAME, serviceName)
41+
.build()
42+
}
43+
.build()
44+
.openTelemetrySdk

backend/jvm/src/main/kotlin/dev/suresh/plugins/custom/Plugins.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dev.suresh.plugins.custom
22

33
import dev.suresh.plugins.isApi
44
import io.ktor.server.application.*
5-
import io.ktor.util.AttributeKey
5+
import io.ktor.util.*
66
import io.opentelemetry.api.trace.Span
77

88
/**

0 commit comments

Comments
 (0)