Skip to content

Commit

Permalink
LogsMetrics improved
Browse files Browse the repository at this point in the history
  • Loading branch information
javipacheco committed Oct 18, 2023
1 parent cf6a149 commit 60f940e
Showing 1 changed file with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ package com.xebia.functional.xef.metrics

import com.xebia.functional.xef.conversation.Conversation
import com.xebia.functional.xef.prompt.Prompt
import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KotlinLogging
import io.ktor.util.date.*

class LogsMetric : Metric {

private val logger: KLogger = KotlinLogging.logger {}
private val identSize = 4

override suspend fun <A> promptSpan(
conversation: Conversation,
prompt: Prompt,
block: suspend Metric.() -> A
): A {
val milis = getTimeMillis()
val name = "Prompt: ${prompt.messages.lastOrNull()?.content ?: "empty"}"
logger.info { "Start span: $name" }
val name = prompt.messages.lastOrNull()?.content ?: "empty"
println("Prompt-Span: $name")
val output = block()
logger.info { "End span (${getTimeMillis()-milis} ms): $name" }
println("${writeIdent()}|-- Finished in ${getTimeMillis()-milis} ms")
return output
}

override fun log(conversation: Conversation, message: String) {
logger.info { message }
println("${writeIdent()}|-- $message".padStart(identSize, ' '))
}

private fun writeIdent(times: Int = 1) = (1 .. identSize * times).fold("") { a, b -> "$a " }
}

0 comments on commit 60f940e

Please sign in to comment.