forked from emeraldpay/dshackle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collect error spans from providers (emeraldpay#198)
- Loading branch information
1 parent
fc488e1
commit 6b0e201
Showing
12 changed files
with
540 additions
and
10 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
62 changes: 62 additions & 0 deletions
62
src/main/kotlin/io/emeraldpay/dshackle/config/spans/ClientSpansInterceptor.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,62 @@ | ||
package io.emeraldpay.dshackle.config.spans | ||
|
||
import brave.Tracer | ||
import brave.handler.MutableSpan | ||
import brave.handler.SpanHandler | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import io.grpc.CallOptions | ||
import io.grpc.Channel | ||
import io.grpc.ClientCall | ||
import io.grpc.ClientInterceptor | ||
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall | ||
import io.grpc.ForwardingClientCallListener | ||
import io.grpc.Metadata | ||
import io.grpc.Metadata.ASCII_STRING_MARSHALLER | ||
import io.grpc.MethodDescriptor | ||
import org.springframework.beans.factory.annotation.Qualifier | ||
|
||
class ClientSpansInterceptor( | ||
private val zipkinSpanHandler: SpanHandler, | ||
private val tracer: Tracer, | ||
@Qualifier("spanMapper") | ||
private val spanMapper: ObjectMapper | ||
) : ClientInterceptor { | ||
|
||
override fun <ReqT : Any?, RespT : Any?> interceptCall( | ||
method: MethodDescriptor<ReqT, RespT>, | ||
callOptions: CallOptions, | ||
next: Channel | ||
): ClientCall<ReqT, RespT> { | ||
return if (method.fullMethodName == "emerald.Blockchain/NativeCall") { | ||
SpanClientCall(next.newCall(method, callOptions)) | ||
} else { | ||
next.newCall(method, callOptions) | ||
} | ||
} | ||
|
||
private inner class SpanClientCall<ReqT, RespT>( | ||
delegate: ClientCall<ReqT, RespT>? | ||
) : SimpleForwardingClientCall<ReqT, RespT>(delegate) { | ||
override fun start(responseListener: Listener<RespT>, headers: Metadata) { | ||
val spanResponseListener = SpanClientCallListener(responseListener) | ||
super.start(spanResponseListener, headers) | ||
} | ||
} | ||
|
||
private inner class SpanClientCallListener<RespT>( | ||
delegate: ClientCall.Listener<RespT>, | ||
) : ForwardingClientCallListener.SimpleForwardingClientCallListener<RespT>(delegate) { | ||
override fun onHeaders(headers: Metadata) { | ||
headers[Metadata.Key.of(SPAN_HEADER, ASCII_STRING_MARSHALLER)] | ||
?.takeIf { it.isNotBlank() } | ||
?.let { | ||
val spansFromProvider = spanMapper.readValue<List<MutableSpan>>(it) | ||
spansFromProvider.forEach { span -> | ||
zipkinSpanHandler.end(tracer.currentSpan().context(), span, SpanHandler.Cause.FINISHED) | ||
} | ||
} | ||
super.onHeaders(headers) | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/kotlin/io/emeraldpay/dshackle/config/spans/ErrorSpanHandler.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,76 @@ | ||
package io.emeraldpay.dshackle.config.spans | ||
|
||
import brave.handler.MutableSpan | ||
import brave.handler.SpanHandler | ||
import brave.propagation.TraceContext | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.github.benmanes.caffeine.cache.Caffeine | ||
import io.emeraldpay.dshackle.commons.SPAN_ERROR | ||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.cloud.sleuth.Span | ||
import java.time.Duration | ||
|
||
class ErrorSpanHandler( | ||
@Qualifier("spanMapper") | ||
private val spanMapper: ObjectMapper, | ||
) : SpanHandler() { | ||
private val spans = Caffeine | ||
.newBuilder() | ||
.expireAfterWrite(Duration.ofMinutes(5)) | ||
.build<String, MutableList<MutableSpan>>() | ||
|
||
override fun end(context: TraceContext, span: MutableSpan, cause: Cause): Boolean { | ||
if (span.traceId().length > 20 && span.parentId() != null) { | ||
val spanList = spans.asMap().computeIfAbsent(span.parentId()) { mutableListOf() } | ||
spanList.add(span) | ||
} | ||
return super.end(context, span, cause) | ||
} | ||
|
||
fun getErrorSpans(spanId: String, currentSpan: Span): String { | ||
val spansInfo = SpansInfo() | ||
|
||
enrichErrorSpans(spanId, spansInfo) | ||
currentSpan.end() | ||
currentSpan.context().parentId()?.let { | ||
spans.getIfPresent(it)?.let { mutableSpans -> | ||
if (mutableSpans.isNotEmpty()) { | ||
spansInfo.spans.add(mutableSpans[0]) | ||
} | ||
} | ||
} | ||
|
||
spansInfo.spans | ||
.map { it.parentId() } | ||
.forEach { | ||
if (it != null) { | ||
spans.invalidate(it) | ||
} | ||
} | ||
|
||
return if (spansInfo.hasError) { | ||
spanMapper.writeValueAsString(spansInfo.spans) | ||
} else { | ||
"" | ||
} | ||
} | ||
|
||
private fun enrichErrorSpans(spanId: String, spansInfo: SpansInfo) { | ||
val currentSpans: List<MutableSpan>? = spans.getIfPresent(spanId) | ||
|
||
currentSpans?.forEach { | ||
spansInfo.spans.add(it) | ||
if (it.tags().containsKey(SPAN_ERROR)) { | ||
spansInfo.hasError = true | ||
} | ||
if (spanId != it.id()) { | ||
enrichErrorSpans(it.id(), spansInfo) | ||
} | ||
} | ||
} | ||
|
||
private data class SpansInfo( | ||
var hasError: Boolean = false, | ||
val spans: MutableList<MutableSpan> = mutableListOf() | ||
) | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/kotlin/io/emeraldpay/dshackle/config/spans/ServerSpansInterceptor.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,44 @@ | ||
package io.emeraldpay.dshackle.config.spans | ||
|
||
import io.grpc.ForwardingServerCall.SimpleForwardingServerCall | ||
import io.grpc.Metadata | ||
import io.grpc.ServerCall | ||
import io.grpc.ServerCallHandler | ||
import io.grpc.ServerInterceptor | ||
import org.springframework.cloud.sleuth.Tracer | ||
|
||
class ServerSpansInterceptor( | ||
private val tracer: Tracer, | ||
private val errorSpanHandler: ErrorSpanHandler | ||
) : ServerInterceptor { | ||
override fun <ReqT : Any?, RespT : Any?> interceptCall( | ||
call: ServerCall<ReqT, RespT>, | ||
headers: Metadata, | ||
next: ServerCallHandler<ReqT, RespT> | ||
): ServerCall.Listener<ReqT> { | ||
val serverCall = if (call.methodDescriptor.fullMethodName == "emerald.Blockchain/NativeCall") { | ||
SpanServerCall(call) | ||
} else { | ||
call | ||
} | ||
|
||
return next.startCall(serverCall, headers) | ||
} | ||
|
||
private inner class SpanServerCall<ReqT, RespT>( | ||
private val call: ServerCall<ReqT, RespT> | ||
) : SimpleForwardingServerCall<ReqT, RespT>(call) { | ||
override fun sendHeaders(headers: Metadata) { | ||
tracer.currentSpan()?.let { | ||
val parentId = it.context().parentId() | ||
if (parentId != null) { | ||
val spans = errorSpanHandler.getErrorSpans(it.context().spanId(), it) | ||
if (spans.isNotBlank()) { | ||
headers.put(Metadata.Key.of(SPAN_HEADER, Metadata.ASCII_STRING_MARSHALLER), spans) | ||
} | ||
} | ||
call.sendHeaders(headers) | ||
} | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/kotlin/io/emeraldpay/dshackle/config/spans/SpanConfig.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,68 @@ | ||
package io.emeraldpay.dshackle.config.spans | ||
|
||
import brave.Tracer | ||
import brave.handler.SpanHandler | ||
import com.fasterxml.jackson.annotation.JsonAutoDetect | ||
import com.fasterxml.jackson.annotation.JsonInclude | ||
import com.fasterxml.jackson.databind.DeserializationFeature | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import io.grpc.ClientInterceptor | ||
import io.grpc.ServerInterceptor | ||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
|
||
const val SPAN_HEADER = "spans" | ||
|
||
@Configuration | ||
@ConditionalOnProperty(value = ["spans.collect.enabled"], havingValue = "true") | ||
open class SpanConfig { | ||
|
||
@Bean | ||
open fun spanMapper(): ObjectMapper = | ||
ObjectMapper() | ||
.apply { | ||
setSerializationInclusion(JsonInclude.Include.NON_DEFAULT) | ||
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) | ||
setVisibility( | ||
this.serializationConfig.defaultVisibilityChecker | ||
.withFieldVisibility(JsonAutoDetect.Visibility.ANY) | ||
.withGetterVisibility(JsonAutoDetect.Visibility.NONE) | ||
.withSetterVisibility(JsonAutoDetect.Visibility.NONE) | ||
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE) | ||
) | ||
} | ||
|
||
@Configuration | ||
@ConditionalOnBean(SpanConfig::class) | ||
@ConditionalOnProperty(value = ["spans.collect.main.enabled"], havingValue = "true") | ||
open class MainSpanConfig { | ||
@Bean | ||
open fun clientSpansInterceptor( | ||
zipkinSpanHandler: SpanHandler, | ||
tracer: Tracer, | ||
@Qualifier("spanMapper") | ||
spanMapper: ObjectMapper | ||
): ClientInterceptor = ClientSpansInterceptor(zipkinSpanHandler, tracer, spanMapper) | ||
} | ||
|
||
@Configuration | ||
@ConditionalOnBean(SpanConfig::class) | ||
@ConditionalOnProperty(value = ["spans.collect.provider.enabled"], havingValue = "true") | ||
open class ProviderSpanConfig { | ||
|
||
@Bean | ||
open fun errorSpanHandler( | ||
@Qualifier("spanMapper") | ||
spanMapper: ObjectMapper | ||
): ErrorSpanHandler = ErrorSpanHandler(spanMapper) | ||
|
||
@Bean | ||
open fun serverSpansInterceptor( | ||
tracer: org.springframework.cloud.sleuth.Tracer, | ||
errorSpanHandler: ErrorSpanHandler | ||
): ServerInterceptor = ServerSpansInterceptor(tracer, errorSpanHandler) | ||
} | ||
} |
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
Oops, something went wrong.