Collect error spans from providers (#198)
This commit is contained in:
@@ -30,7 +30,8 @@ class ConfiguredUpstreamsSpec extends Specification {
|
||||
Executors.newFixedThreadPool(1),
|
||||
ChainsConfig.default(),
|
||||
GrpcTracing.create(Tracing.newBuilder().build()),
|
||||
Schedulers.boundedElastic()
|
||||
Schedulers.boundedElastic(),
|
||||
null
|
||||
)
|
||||
def methods = new UpstreamsConfig.Methods(
|
||||
[
|
||||
@@ -60,7 +61,8 @@ class ConfiguredUpstreamsSpec extends Specification {
|
||||
Executors.newFixedThreadPool(1),
|
||||
ChainsConfig.default(),
|
||||
GrpcTracing.create(Tracing.newBuilder().build()),
|
||||
Schedulers.boundedElastic()
|
||||
Schedulers.boundedElastic(),
|
||||
null
|
||||
)
|
||||
def methods = new UpstreamsConfig.Methods(
|
||||
[
|
||||
@@ -89,7 +91,8 @@ class ConfiguredUpstreamsSpec extends Specification {
|
||||
Executors.newFixedThreadPool(1),
|
||||
ChainsConfig.default(),
|
||||
GrpcTracing.create(Tracing.newBuilder().build()),
|
||||
Schedulers.boundedElastic()
|
||||
Schedulers.boundedElastic(),
|
||||
null
|
||||
)
|
||||
expect:
|
||||
configurer.getHash(node, src) == expected
|
||||
@@ -113,7 +116,8 @@ class ConfiguredUpstreamsSpec extends Specification {
|
||||
Executors.newFixedThreadPool(1),
|
||||
ChainsConfig.default(),
|
||||
GrpcTracing.create(Tracing.newBuilder().build()),
|
||||
Schedulers.boundedElastic()
|
||||
Schedulers.boundedElastic(),
|
||||
null
|
||||
)
|
||||
when:
|
||||
def h1 = configurer.getHash(null, "hohoho")
|
||||
@@ -142,7 +146,8 @@ class ConfiguredUpstreamsSpec extends Specification {
|
||||
Executors.newFixedThreadPool(1),
|
||||
ChainsConfig.default(),
|
||||
GrpcTracing.create(Tracing.newBuilder().build()),
|
||||
Schedulers.boundedElastic()
|
||||
Schedulers.boundedElastic(),
|
||||
null
|
||||
)
|
||||
def methodsGroup = new UpstreamsConfig.MethodGroups(
|
||||
["filter"] as Set,
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
package io.emeraldpay.dshackle.config.spans
|
||||
|
||||
import brave.handler.SpanHandler
|
||||
import org.junit.jupiter.api.Assertions.assertThrows
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.assertDoesNotThrow
|
||||
import org.junit.jupiter.api.extension.ExtendWith
|
||||
import org.mockito.Mockito.mock
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.context.TestConfiguration
|
||||
import org.springframework.cloud.sleuth.Tracer
|
||||
import org.springframework.context.ApplicationContext
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.test.context.ContextConfiguration
|
||||
import org.springframework.test.context.TestPropertySource
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension
|
||||
|
||||
class CollectSpanConfigTest {
|
||||
|
||||
@ContextConfiguration(
|
||||
classes = [SpanConfig::class],
|
||||
)
|
||||
@ExtendWith(SpringExtension::class)
|
||||
@TestPropertySource(
|
||||
properties = [
|
||||
"spans.collect.enabled=false"
|
||||
]
|
||||
)
|
||||
class SpanConfigTest {
|
||||
@Autowired
|
||||
private lateinit var appCtx: ApplicationContext
|
||||
|
||||
@Test
|
||||
fun testSpanConfig() {
|
||||
assertThrows(NoSuchBeanDefinitionException::class.java) {
|
||||
appCtx.getBean(SpanConfig::class.java)
|
||||
}
|
||||
assertThrows(NoSuchBeanDefinitionException::class.java) {
|
||||
appCtx.getBean(ErrorSpanHandler::class.java)
|
||||
}
|
||||
assertThrows(NoSuchBeanDefinitionException::class.java) {
|
||||
appCtx.getBean(ServerSpansInterceptor::class.java)
|
||||
}
|
||||
assertThrows(NoSuchBeanDefinitionException::class.java) {
|
||||
appCtx.getBean(ClientSpansInterceptor::class.java)
|
||||
}
|
||||
assertThrows(NoSuchBeanDefinitionException::class.java) {
|
||||
appCtx.getBean("spanMapper")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ContextConfiguration(
|
||||
classes = [SpanConfig::class, SpanProviderConfigTest.Config::class],
|
||||
)
|
||||
@ExtendWith(SpringExtension::class)
|
||||
@TestPropertySource(
|
||||
properties = [
|
||||
"spans.collect.enabled=true",
|
||||
"spans.collect.provider.enabled=true",
|
||||
"spans.collect.main.enabled=false"
|
||||
]
|
||||
)
|
||||
class SpanProviderConfigTest {
|
||||
@Autowired
|
||||
private lateinit var appCtx: ApplicationContext
|
||||
|
||||
@Test
|
||||
fun testSpanProviderConfig() {
|
||||
assertDoesNotThrow {
|
||||
appCtx.getBean(SpanConfig::class.java)
|
||||
}
|
||||
assertDoesNotThrow {
|
||||
appCtx.getBean(ErrorSpanHandler::class.java)
|
||||
}
|
||||
assertDoesNotThrow {
|
||||
appCtx.getBean(ServerSpansInterceptor::class.java)
|
||||
}
|
||||
assertThrows(NoSuchBeanDefinitionException::class.java) {
|
||||
appCtx.getBean(ClientSpansInterceptor::class.java)
|
||||
}
|
||||
assertDoesNotThrow {
|
||||
appCtx.getBean("spanMapper")
|
||||
}
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
open class Config {
|
||||
@Bean
|
||||
open fun tracer(): Tracer = mock(Tracer::class.java)
|
||||
}
|
||||
}
|
||||
|
||||
@ContextConfiguration(
|
||||
classes = [SpanConfig::class, SpanMainConfigTest.Config::class],
|
||||
)
|
||||
@ExtendWith(SpringExtension::class)
|
||||
@TestPropertySource(
|
||||
properties = [
|
||||
"spans.collect.enabled=true",
|
||||
"spans.collect.provider.enabled=false",
|
||||
"spans.collect.main.enabled=true"
|
||||
]
|
||||
)
|
||||
class SpanMainConfigTest {
|
||||
@Autowired
|
||||
private lateinit var appCtx: ApplicationContext
|
||||
|
||||
@Test
|
||||
fun testSpanMainConfig() {
|
||||
assertDoesNotThrow {
|
||||
appCtx.getBean(SpanConfig::class.java)
|
||||
}
|
||||
assertThrows(NoSuchBeanDefinitionException::class.java) {
|
||||
appCtx.getBean(ErrorSpanHandler::class.java)
|
||||
}
|
||||
assertThrows(NoSuchBeanDefinitionException::class.java) {
|
||||
appCtx.getBean(ServerSpansInterceptor::class.java)
|
||||
}
|
||||
assertDoesNotThrow {
|
||||
appCtx.getBean(ClientSpansInterceptor::class.java)
|
||||
}
|
||||
assertDoesNotThrow {
|
||||
appCtx.getBean("spanMapper")
|
||||
}
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
open class Config {
|
||||
@Bean
|
||||
open fun tracer(): brave.Tracer = mock(brave.Tracer::class.java)
|
||||
|
||||
@Bean
|
||||
open fun zipkinSpanHandler(): SpanHandler = mock(SpanHandler::class.java)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package io.emeraldpay.dshackle.config.spans
|
||||
|
||||
import brave.handler.MutableSpan
|
||||
import brave.handler.SpanHandler
|
||||
import brave.propagation.TraceContext
|
||||
import com.fasterxml.jackson.module.kotlin.readValue
|
||||
import io.emeraldpay.dshackle.commons.SPAN_ERROR
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.Mockito.`when`
|
||||
import org.springframework.cloud.sleuth.Span
|
||||
import org.springframework.cloud.sleuth.brave.bridge.BraveTraceContext
|
||||
|
||||
class ErrorSpanHandlerTest {
|
||||
private val mapper = SpanConfig().spanMapper()
|
||||
private val ctx = TraceContext.newBuilder()
|
||||
.traceId(1223324)
|
||||
.spanId(234235)
|
||||
.build()
|
||||
|
||||
@Test
|
||||
fun `span with length of traceId less than 20 is not collected`() {
|
||||
val spanId = "f7e83f2b69ec684d"
|
||||
val currentSpan = Mockito.mock(Span::class.java)
|
||||
val handler = spanHandler()
|
||||
|
||||
`when`(currentSpan.context()).thenReturn(BraveTraceContext(ctx))
|
||||
|
||||
handler.end(ctx, span("f7e83f2b69ec684d", spanId), SpanHandler.Cause.FINISHED)
|
||||
|
||||
val result = handler.getErrorSpans(spanId, currentSpan)
|
||||
assertEquals("", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `span without parenId is not collected`() {
|
||||
val spanId = "f7e83f2b69ec684d"
|
||||
val currentSpan = Mockito.mock(Span::class.java)
|
||||
val handler = spanHandler()
|
||||
|
||||
`when`(currentSpan.context()).thenReturn(BraveTraceContext(ctx))
|
||||
|
||||
handler.end(ctx, span("6666632728347823749827349723985", spanId), SpanHandler.Cause.FINISHED)
|
||||
|
||||
val result = handler.getErrorSpans(spanId, currentSpan)
|
||||
assertEquals("", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `span with length of traceId greater than 20 and with parentId is collected`() {
|
||||
val spanId = "f7e83f2b69ec684d"
|
||||
val currentSpan = Mockito.mock(Span::class.java)
|
||||
val handler = spanHandler()
|
||||
val span = span("6666632728347823749827349723985", spanId)
|
||||
.apply { parentId("f7e83f2b69ec682d") }
|
||||
|
||||
`when`(currentSpan.context()).thenReturn(BraveTraceContext(ctx))
|
||||
|
||||
handler.end(ctx, span, SpanHandler.Cause.FINISHED)
|
||||
|
||||
val result = handler.getErrorSpans("f7e83f2b69ec682d", currentSpan)
|
||||
val collectedSpans = mapper.readValue<List<MutableSpan>>(result)
|
||||
assertTrue(collectedSpans.size == 1)
|
||||
assertEquals(span, collectedSpans[0])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `span without error tag is not collected`() {
|
||||
val spanId = "f7e83f2b69ec684d"
|
||||
val currentSpan = Mockito.mock(Span::class.java)
|
||||
val handler = spanHandler()
|
||||
val span = span("6666632728347823749827349723985", spanId)
|
||||
.apply {
|
||||
parentId("f7e83f2b69ec682d")
|
||||
removeTag(SPAN_ERROR)
|
||||
}
|
||||
|
||||
`when`(currentSpan.context()).thenReturn(BraveTraceContext(ctx))
|
||||
|
||||
handler.end(ctx, span, SpanHandler.Cause.FINISHED)
|
||||
|
||||
val result = handler.getErrorSpans("f7e83f2b69ec682d", currentSpan)
|
||||
assertEquals("", result)
|
||||
}
|
||||
|
||||
private fun span(traceId: String, spanId: String) = MutableSpan()
|
||||
.apply {
|
||||
traceId(traceId)
|
||||
id(spanId)
|
||||
tag(SPAN_ERROR, "true")
|
||||
}
|
||||
|
||||
private fun spanHandler() = ErrorSpanHandler(mapper)
|
||||
}
|
||||
Reference in New Issue
Block a user