From c972e27d430fe8b76ba8c44a44f59cfc0008c91a Mon Sep 17 00:00:00 2001 From: KirillPamPam Date: Wed, 7 Jun 2023 17:15:26 +0400 Subject: [PATCH] Send spans if they are too long (#225) --- .../io/emeraldpay/dshackle/config/spans/export.kt | 14 ++++++++++++++ src/main/resources/application.yml | 1 + .../config/spans/ProviderSpanHandlerTest.kt | 12 +++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/io/emeraldpay/dshackle/config/spans/export.kt b/src/main/kotlin/io/emeraldpay/dshackle/config/spans/export.kt index fc60f2c5..b270aed9 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/config/spans/export.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/config/spans/export.kt @@ -3,7 +3,9 @@ package io.emeraldpay.dshackle.config.spans import brave.handler.MutableSpan import io.emeraldpay.dshackle.commons.SPAN_ERROR import io.emeraldpay.dshackle.commons.SPAN_NO_RESPONSE_MESSAGE +import org.springframework.beans.factory.annotation.Value import org.springframework.stereotype.Component +import java.util.concurrent.TimeUnit interface SpanExportable { fun isExportable(span: MutableSpan): Boolean @@ -18,3 +20,15 @@ class ErrorSpanExportable : SpanExportable { class NoResponseSpanExportable : SpanExportable { override fun isExportable(span: MutableSpan): Boolean = span.tags().containsKey(SPAN_NO_RESPONSE_MESSAGE) } + +@Component +class LongResponseSpanExportable( + @Value("\${spans.collect.provider.long-span-threshold}") + private val longSpanThreshold: Long? = null +) : SpanExportable { + + override fun isExportable(span: MutableSpan): Boolean { + val duration = span.finishTimestamp() - span.startTimestamp() + return TimeUnit.SECONDS.convert(duration, TimeUnit.MICROSECONDS) >= longSpanThreshold!! + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index ecf92177..21a811a2 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -22,5 +22,6 @@ spans: enabled: ${ENABLE_COLLECT_SPANS:false} provider: enabled: ${ENABLE_PROVIDER_COLLECT_SPANS:true} + long-span-threshold: ${LONG_SPAN_THRESHOLD:1} main: enabled: ${ENABLE_MAIN_COLLECT_SPANS:false} \ No newline at end of file diff --git a/src/test/kotlin/io/emeraldpay/dshackle/config/spans/ProviderSpanHandlerTest.kt b/src/test/kotlin/io/emeraldpay/dshackle/config/spans/ProviderSpanHandlerTest.kt index 3b9451aa..e521b53e 100644 --- a/src/test/kotlin/io/emeraldpay/dshackle/config/spans/ProviderSpanHandlerTest.kt +++ b/src/test/kotlin/io/emeraldpay/dshackle/config/spans/ProviderSpanHandlerTest.kt @@ -18,7 +18,9 @@ import org.springframework.cloud.sleuth.brave.bridge.BraveTraceContext class ProviderSpanHandlerTest { private val mapper = SpanConfig().spanMapper() - private val spanExportableList = listOf(ErrorSpanExportable(), NoResponseSpanExportable()) + private val spanExportableList = listOf( + ErrorSpanExportable(), NoResponseSpanExportable(), LongResponseSpanExportable(10) + ) private val ctx = TraceContext.newBuilder() .traceId(1223324) .spanId(234235) @@ -110,6 +112,14 @@ class ProviderSpanHandlerTest { id("f7e83f2b69ec111d") parentId("f7e83f2b69ec682d") tag(SPAN_NO_RESPONSE_MESSAGE, "noResp") + }, + MutableSpan() + .apply { + traceId("6666632728347823749827349723985") + id("f7e83f2b69ec111d") + parentId("f7e83f2b69ec682d") + startTimestamp(3034272) + finishTimestamp(15034272) } ) }