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.
Add SpanReader wrapper for creating additional spans (emeraldpay#155)
- Loading branch information
1 parent
076bbbe
commit 01ea756
Showing
18 changed files
with
283 additions
and
91 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/main/kotlin/io/emeraldpay/dshackle/commons/constatnts.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,20 @@ | ||
package io.emeraldpay.dshackle.commons | ||
|
||
const val SPAN_READER_NAME = "reader.name" | ||
const val SPAN_REQUEST_INFO = "request.info" | ||
const val SPAN_ERROR = "error" | ||
const val SPAN_STATUS_MESSAGE = "status.message" | ||
const val SPAN_READER_RESULT = "reader.result" | ||
const val SPAN_REQUEST_API_TYPE = "request.api.type" | ||
const val SPAN_REQUEST_UPSTREAM_ID = "request.upstreamId" | ||
const val SPAN_REQUEST_ID = "request.id" | ||
|
||
const val LOCAL_READER = "localReader" | ||
const val REMOTE_QUORUM_RPC_READER = "remoteQuorumRpcReader" | ||
const val API_READER = "apiReader" | ||
const val CACHE_BLOCK_BY_HASH_READER = "cacheBlockByHashReader" | ||
const val DIRECT_QUORUM_RPC_READER = "directQuorumRpcReader" | ||
const val CACHE_HEIGHT_BY_HASH_READER = "cacheHeightByHashReader" | ||
const val CACHE_BLOCK_BY_HEIGHT_READER = "cacheBlockByHeightReader" | ||
const val CACHE_TX_BY_HASH_READER = "cacheTxByHashReader" | ||
const val CACHE_RECEIPTS_READER = "cacheReceiptsReader" |
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
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
57 changes: 57 additions & 0 deletions
57
src/main/kotlin/io/emeraldpay/dshackle/reader/SpannedReader.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,57 @@ | ||
package io.emeraldpay.dshackle.reader | ||
|
||
import io.emeraldpay.dshackle.commons.SPAN_ERROR | ||
import io.emeraldpay.dshackle.commons.SPAN_READER_NAME | ||
import io.emeraldpay.dshackle.commons.SPAN_READER_RESULT | ||
import io.emeraldpay.dshackle.commons.SPAN_REQUEST_INFO | ||
import io.emeraldpay.dshackle.commons.SPAN_STATUS_MESSAGE | ||
import io.emeraldpay.dshackle.data.HashId | ||
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcRequest | ||
import org.springframework.cloud.sleuth.Tracer | ||
import org.springframework.cloud.sleuth.instrument.reactor.ReactorSleuth | ||
import reactor.core.publisher.Mono | ||
import reactor.kotlin.core.publisher.switchIfEmpty | ||
|
||
class SpannedReader<K, D>( | ||
private val reader: Reader<K, D>, | ||
private val tracer: Tracer, | ||
private val name: String, | ||
private val additionalParams: Map<String, String> = emptyMap() | ||
) : Reader<K, D> { | ||
|
||
override fun read(key: K): Mono<D> { | ||
val newSpan = tracer.nextSpan(tracer.currentSpan()) | ||
.name(reader.javaClass.name) | ||
.tag(SPAN_READER_NAME, name) | ||
.start() | ||
|
||
extractInfoFromKey(key)?.let { | ||
newSpan.tag(SPAN_REQUEST_INFO, it) | ||
} | ||
additionalParams.forEach { newSpan.tag(it.key, it.value) } | ||
|
||
return reader.read(key) | ||
.contextWrite { ReactorSleuth.putSpanInScope(tracer, it, newSpan) } | ||
.doOnError { | ||
newSpan.apply { | ||
tag(SPAN_ERROR, "true") | ||
tag(SPAN_STATUS_MESSAGE, it.message) | ||
end() | ||
} | ||
} | ||
.doOnNext { newSpan.end() } | ||
.switchIfEmpty { | ||
newSpan.tag(SPAN_READER_RESULT, "empty result") | ||
newSpan.end() | ||
Mono.empty() | ||
} | ||
} | ||
|
||
private fun extractInfoFromKey(key: K): String? { | ||
return when (key) { | ||
is JsonRpcRequest -> "method: ${key.method}" | ||
is HashId, Long -> "params: $key" | ||
else -> null | ||
} | ||
} | ||
} |
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.