forked from beltram/spring-test-sleuth-reproducer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TracingResource.kt
52 lines (39 loc) · 1.57 KB
/
TracingResource.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.spring.reproducer.spring.test.sleuth.reproducer
import org.springframework.cloud.sleuth.CurrentTraceContext
import org.springframework.cloud.sleuth.TraceContext
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Mono
import reactor.kotlin.core.publisher.toMono
import reactor.util.context.ContextView
@RestController
@RequestMapping("/api/tracing")
class TracingResource {
companion object {
val KEY = TraceContext::class.java
val CURRENT_KEY = CurrentTraceContext::class.java
}
@GetMapping("spanId")
fun spanId(): Mono<String> {
return Mono.deferContextual { view -> view.traceContext() }
.map { it.spanId() }
}
@GetMapping("parentId")
fun parentId(): Mono<String> {
return Mono.deferContextual { view -> view.traceContext() }
.map { it.parentId() }
}
private fun ContextView.traceContext() = get(KEY).toMono()
@GetMapping("currentSpanId")
fun spanIdForCurrentTraceContext(): Mono<String> {
return Mono.deferContextual { view -> view.currentTraceContext() }
.map { it.context()?.spanId() }
}
@GetMapping("currentParentId")
fun parentIdForCurrentTraceContext(): Mono<String> {
return Mono.deferContextual { view -> view.currentTraceContext() }
.map { it.context()?.parentId() }
}
private fun ContextView.currentTraceContext() = get(CURRENT_KEY).toMono()
}