-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from GuiaBolso/backport-otel-support
Backport open telemetry support to v6.*.*
- Loading branch information
Showing
6 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
...cing/src/main/kotlin/br/com/guiabolso/tracing/engine/opentelemetry/OpenTelemetryTracer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package br.com.guiabolso.tracing.engine.opentelemetry | ||
|
||
import br.com.guiabolso.tracing.context.ThreadContextManager | ||
import br.com.guiabolso.tracing.engine.TracerEngine | ||
import br.com.guiabolso.tracing.utils.opentelemetry.OpenTelemetryUtils | ||
import io.opentelemetry.api.GlobalOpenTelemetry | ||
import io.opentelemetry.api.common.AttributeKey | ||
import io.opentelemetry.api.common.Attributes | ||
import io.opentelemetry.api.metrics.LongHistogram | ||
import io.opentelemetry.api.metrics.Meter | ||
import io.opentelemetry.api.trace.Span | ||
import io.opentelemetry.api.trace.Tracer | ||
import io.opentelemetry.context.Context | ||
import java.io.Closeable | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
@Suppress("TooManyFunctions") | ||
class OpenTelemetryTracer : TracerEngine, ThreadContextManager<Span> { | ||
|
||
override val type = Span::class.java | ||
|
||
private val tracer: Tracer by lazy { | ||
GlobalOpenTelemetry.getTracer(TRACER_NAME) | ||
} | ||
|
||
private val meter: Meter by lazy { | ||
GlobalOpenTelemetry.getMeter(TRACER_NAME) | ||
} | ||
|
||
override fun setOperationName(name: String) { | ||
val span = currentSpan() | ||
span?.updateName(name) | ||
} | ||
|
||
override fun addProperty(key: String, value: String?) { | ||
Span.current()?.addProperty(key, value) | ||
} | ||
|
||
override fun addRootProperty(key: String, value: String?) { | ||
currentSpan()?.addProperty(key, value) | ||
} | ||
|
||
override fun addProperty(key: String, value: Number?) { | ||
Span.current()?.addProperty(key, value) | ||
} | ||
|
||
override fun addRootProperty(key: String, value: Number?) { | ||
currentSpan()?.addProperty(key, value) | ||
} | ||
|
||
override fun addProperty(key: String, value: Boolean?) { | ||
Span.current()?.addProperty(key, value) | ||
} | ||
|
||
override fun addRootProperty(key: String, value: Boolean?) { | ||
currentSpan()?.addProperty(key, value) | ||
} | ||
|
||
override fun addProperty(key: String, value: List<*>) { | ||
val finalValue: String = value.joinToString(",") | ||
addProperty(key, finalValue) | ||
} | ||
|
||
override fun <T> recordExecutionTime(name: String, block: (MutableMap<String, String>) -> T): T { | ||
val start = System.currentTimeMillis() | ||
val context = mutableMapOf<String, String>() | ||
try { | ||
return block(context) | ||
} finally { | ||
val elapsedTime = System.currentTimeMillis() - start | ||
recordExecutionTime(name, elapsedTime, context) | ||
} | ||
} | ||
|
||
override fun recordExecutionTime(name: String, elapsedTime: Long, context: Map<String, String>) { | ||
val attributes = Attributes.builder() | ||
for ((k, v) in context) { | ||
attributes.put(k, v) | ||
} | ||
val lh = histogramCache.computeIfAbsent(name) { | ||
meter.histogramBuilder(name).setUnit("ms").ofLongs().build() | ||
} | ||
lh.record(elapsedTime, attributes.build()) | ||
} | ||
|
||
override fun notifyError(exception: Throwable, expected: Boolean) { | ||
Span.current()?.let { span -> | ||
OpenTelemetryUtils.notifyError(span, exception, expected) | ||
} | ||
} | ||
|
||
override fun notifyRootError(exception: Throwable, expected: Boolean) { | ||
currentSpan()?.let { span -> | ||
OpenTelemetryUtils.notifyError(span, exception, expected) | ||
} | ||
} | ||
|
||
override fun notifyError(message: String, params: Map<String, String?>, expected: Boolean) { | ||
Span.current()?.let { span -> | ||
OpenTelemetryUtils.notifyError(span, message, params, expected) | ||
} | ||
} | ||
|
||
override fun notifyRootError(message: String, params: Map<String, String?>, expected: Boolean) { | ||
currentSpan()?.let { span -> | ||
OpenTelemetryUtils.notifyError(span, message, params, expected) | ||
} | ||
} | ||
|
||
override fun clear() {} | ||
|
||
override fun extract(): Span { | ||
return Span.current() | ||
} | ||
|
||
override fun withContext(context: Span): Closeable { | ||
val span = tracer.spanBuilder("asyncTask").setParent(Context.current().with(context)).startSpan() | ||
val scope = span.makeCurrent() | ||
return Closeable { | ||
span.end() | ||
scope.close() | ||
} | ||
} | ||
|
||
private inline fun <reified T> Span.addProperty(key: String, value: T?) { | ||
val attrKey = getAttributeKey<T>(key) | ||
if (value != null) { | ||
this.setAttribute(attrKey, value) | ||
} | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
private inline fun <reified T> getAttributeKey(key: String): AttributeKey<T> { | ||
return keysMap.computeIfAbsent(key) { k: String -> | ||
val tClass = T::class.java | ||
when { | ||
String::class.java.isAssignableFrom(tClass) -> AttributeKey.stringKey(k) | ||
Double::class.java.isAssignableFrom(tClass) -> AttributeKey.doubleKey(k) | ||
Float::class.java.isAssignableFrom(tClass) -> AttributeKey.doubleKey(k) | ||
Int::class.java.isAssignableFrom(tClass) -> AttributeKey.longKey(k) | ||
Long::class.java.isAssignableFrom(tClass) -> AttributeKey.longKey(k) | ||
Number::class.java.isAssignableFrom(tClass) -> AttributeKey.doubleKey(k) | ||
Boolean::class.java.isAssignableFrom(tClass) -> AttributeKey.booleanKey(k) | ||
else -> error("Unsupported attribute type ${tClass.canonicalName}") | ||
} | ||
} as AttributeKey<T> | ||
} | ||
|
||
private fun currentSpan(): Span? = Span.current() | ||
|
||
companion object { | ||
const val TRACER_NAME = "events-tracing" | ||
private val keysMap = ConcurrentHashMap<String, AttributeKey<*>>() | ||
private val histogramCache = ConcurrentHashMap<String, LongHistogram>() | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...c/main/kotlin/br/com/guiabolso/tracing/utils/opentelemetry/DefaultUnspecifiedException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package br.com.guiabolso.tracing.utils.opentelemetry | ||
|
||
internal class DefaultUnspecifiedException(message: String) : RuntimeException(message) |
88 changes: 88 additions & 0 deletions
88
...racing/src/main/kotlin/br/com/guiabolso/tracing/utils/opentelemetry/OpenTelemetryUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package br.com.guiabolso.tracing.utils.opentelemetry | ||
|
||
import br.com.guiabolso.tracing.engine.opentelemetry.OpenTelemetryTracer.Companion.TRACER_NAME | ||
import io.opentelemetry.api.GlobalOpenTelemetry | ||
import io.opentelemetry.api.common.Attributes | ||
import io.opentelemetry.api.trace.Span | ||
import io.opentelemetry.api.trace.SpanKind | ||
import io.opentelemetry.api.trace.StatusCode | ||
import io.opentelemetry.api.trace.Tracer | ||
import kotlinx.coroutines.runBlocking | ||
|
||
object OpenTelemetryUtils { | ||
|
||
private val tracer: Tracer by lazy { | ||
GlobalOpenTelemetry.getTracer(TRACER_NAME) | ||
} | ||
|
||
@JvmStatic | ||
@JvmOverloads | ||
fun traceAsNewOperation( | ||
name: String, | ||
kind: SpanKind = SpanKind.SERVER, | ||
func: () -> Unit | ||
) = runBlocking { | ||
coTraceAsNewOperation(name, kind, func) | ||
} | ||
|
||
suspend fun coTraceAsNewOperation( | ||
name: String, | ||
kind: SpanKind = SpanKind.SERVER, | ||
func: suspend () -> Unit | ||
) { | ||
val span = tracer.spanBuilder(name) | ||
.setSpanKind(kind) | ||
.setNoParent() | ||
.startSpan()!! | ||
span.makeCurrent().use { | ||
try { | ||
func() | ||
} catch (e: Exception) { | ||
notifyError(span, e, false) | ||
throw e | ||
} finally { | ||
span.end() | ||
} | ||
} | ||
} | ||
|
||
@JvmStatic | ||
fun <T> traceBlock(name: String, func: () -> T): T = runBlocking { | ||
suspendingTraceBlock(name) { func() } | ||
} | ||
|
||
suspend fun <T> suspendingTraceBlock(name: String, func: suspend () -> T): T { | ||
val span = tracer.spanBuilder(name).startSpan()!! | ||
val scope = span.makeCurrent()!! | ||
return scope.use { | ||
try { | ||
func() | ||
} catch (e: Exception) { | ||
notifyError(span, e, false) | ||
throw e | ||
} finally { | ||
span.end() | ||
} | ||
} | ||
} | ||
|
||
@JvmStatic | ||
fun notifyError(span: Span, exception: Throwable, expected: Boolean) { | ||
val status = if (expected) StatusCode.OK else StatusCode.ERROR | ||
span.setStatus(status) | ||
span.recordException(exception) | ||
} | ||
|
||
@JvmStatic | ||
fun notifyError(span: Span, message: String, params: Map<String, String?>, expected: Boolean) { | ||
val status = if (expected) StatusCode.OK else StatusCode.ERROR | ||
span.setStatus(status) | ||
val builder = Attributes.builder() | ||
params.forEach { (key, value) -> | ||
if (value != null) { | ||
builder.put(key, value) | ||
} | ||
} | ||
span.recordException(DefaultUnspecifiedException(message), builder.build()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters