Skip to content

Commit cd19358

Browse files
committed
refactor: typed live instrument events
1 parent ee3e60b commit cd19358

File tree

10 files changed

+80
-144
lines changed

10 files changed

+80
-144
lines changed

src/main/kotlin/spp/protocol/instrument/event/LiveBreakpointHit.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ data class LiveBreakpointHit(
3535
val serviceInstance: String,
3636
val service: String,
3737
val stackTrace: LiveStackTrace
38-
) : TrackedLiveEvent {
38+
) : LiveInstrumentEvent {
3939
override val eventType: LiveInstrumentEventType = LiveInstrumentEventType.BREAKPOINT_HIT
4040

4141
constructor(json: JsonObject) : this(
@@ -47,7 +47,7 @@ data class LiveBreakpointHit(
4747
LiveStackTrace(json.getJsonObject("stackTrace"))
4848
)
4949

50-
fun toJson(): JsonObject {
50+
override fun toJson(): JsonObject {
5151
return JsonObject.mapFrom(this)
5252
}
5353
}

src/main/kotlin/spp/protocol/instrument/event/LiveInstrumentAdded.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import java.time.Instant
3232
data class LiveInstrumentAdded(
3333
val liveInstrument: LiveInstrument,
3434
override val occurredAt: Instant = Instant.now()
35-
) : TrackedLiveEvent {
35+
) : LiveInstrumentEvent {
3636
override val eventType: LiveInstrumentEventType
3737
get() {
3838
return when (liveInstrument.type) {
@@ -48,7 +48,7 @@ data class LiveInstrumentAdded(
4848
occurredAt = Instant.parse(json.getString("occurredAt"))
4949
)
5050

51-
fun toJson(): JsonObject {
51+
override fun toJson(): JsonObject {
5252
return JsonObject.mapFrom(this)
5353
}
5454
}

src/main/kotlin/spp/protocol/instrument/event/LiveInstrumentApplied.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import java.time.Instant
3131
@DataObject
3232
data class LiveInstrumentApplied(
3333
val liveInstrument: LiveInstrument,
34-
override val occurredAt: Instant = Instant.now()
35-
) : TrackedLiveEvent {
34+
override val occurredAt: Instant
35+
) : LiveInstrumentEvent {
3636
override val eventType: LiveInstrumentEventType
3737
get() {
3838
return when (liveInstrument.type) {
@@ -48,7 +48,7 @@ data class LiveInstrumentApplied(
4848
occurredAt = Instant.parse(json.getString("occurredAt"))
4949
)
5050

51-
fun toJson(): JsonObject {
51+
override fun toJson(): JsonObject {
5252
return JsonObject.mapFrom(this)
5353
}
5454
}

src/main/kotlin/spp/protocol/instrument/event/LiveInstrumentEvent.kt

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,45 @@
1717
package spp.protocol.instrument.event
1818

1919
import io.vertx.core.json.JsonObject
20+
import spp.protocol.instrument.event.LiveInstrumentEventType.*
21+
import java.time.Instant
2022

2123
/**
2224
* todo: description.
2325
*
2426
* @since 0.3.0
2527
* @author [Brandon Fergerson](mailto:bfergerson@apache.org)
2628
*/
27-
data class LiveInstrumentEvent(
28-
val eventType: LiveInstrumentEventType,
29-
val data: String //todo: type out
30-
) {
31-
constructor(json: JsonObject) : this(
32-
LiveInstrumentEventType.valueOf(json.getString("eventType")),
33-
json.getString("data")
34-
)
35-
36-
fun toJson(): JsonObject {
37-
return JsonObject.mapFrom(this)
29+
interface LiveInstrumentEvent {
30+
31+
val occurredAt: Instant
32+
val eventType: LiveInstrumentEventType
33+
34+
fun toJson(): JsonObject
35+
36+
companion object {
37+
@JvmStatic
38+
fun fromJson(json: JsonObject): LiveInstrumentEvent {
39+
return when (valueOf(json.getString("eventType"))) {
40+
BREAKPOINT_ADDED -> LiveInstrumentAdded(json)
41+
BREAKPOINT_APPLIED -> LiveInstrumentApplied(json)
42+
BREAKPOINT_HIT -> LiveBreakpointHit(json)
43+
BREAKPOINT_REMOVED -> LiveInstrumentRemoved(json)
44+
45+
LOG_ADDED -> LiveInstrumentAdded(json)
46+
LOG_APPLIED -> LiveInstrumentApplied(json)
47+
LOG_HIT -> LiveLogHit(json)
48+
LOG_REMOVED -> LiveInstrumentRemoved(json)
49+
50+
METER_ADDED -> LiveInstrumentAdded(json)
51+
METER_APPLIED -> LiveInstrumentApplied(json)
52+
METER_REMOVED -> LiveInstrumentRemoved(json)
53+
METER_UPDATED -> TODO()
54+
55+
SPAN_ADDED -> LiveInstrumentAdded(json)
56+
SPAN_APPLIED -> LiveInstrumentApplied(json)
57+
SPAN_REMOVED -> LiveInstrumentRemoved(json)
58+
}
59+
}
3860
}
3961
}

src/main/kotlin/spp/protocol/instrument/event/LiveInstrumentRemoved.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ data class LiveInstrumentRemoved(
3434
val liveInstrument: LiveInstrument,
3535
override val occurredAt: Instant,
3636
val cause: LiveStackTrace? = null
37-
) : TrackedLiveEvent {
37+
) : LiveInstrumentEvent {
3838
override val eventType: LiveInstrumentEventType
3939
get() {
4040
return when (liveInstrument.type) {
@@ -51,7 +51,7 @@ data class LiveInstrumentRemoved(
5151
cause = json.getJsonObject("cause")?.let { LiveStackTrace(it) }
5252
)
5353

54-
fun toJson(): JsonObject {
54+
override fun toJson(): JsonObject {
5555
return JsonObject.mapFrom(this)
5656
}
5757
}

src/main/kotlin/spp/protocol/instrument/event/LiveLogHit.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ data class LiveLogHit(
3434
val serviceInstance: String,
3535
val service: String,
3636
val logResult: LogResult
37-
) : TrackedLiveEvent {
37+
) : LiveInstrumentEvent {
3838
override val eventType: LiveInstrumentEventType = LiveInstrumentEventType.LOG_HIT
3939

4040
constructor(json: JsonObject) : this(
@@ -45,7 +45,7 @@ data class LiveLogHit(
4545
LogResult(json.getJsonObject("logResult"))
4646
)
4747

48-
fun toJson(): JsonObject {
48+
override fun toJson(): JsonObject {
4949
return JsonObject.mapFrom(this)
5050
}
5151
}

src/main/kotlin/spp/protocol/instrument/event/TrackedLiveEvent.kt

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/main/kotlin/spp/protocol/service/listen/LiveInstrumentListener.kt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616
*/
1717
package spp.protocol.service.listen
1818

19-
import spp.protocol.instrument.*
20-
import spp.protocol.instrument.event.LiveBreakpointHit
21-
import spp.protocol.instrument.event.LiveInstrumentEvent
22-
import spp.protocol.instrument.event.LiveInstrumentRemoved
23-
import spp.protocol.instrument.event.LiveLogHit
19+
import spp.protocol.instrument.event.*
2420

2521
interface LiveInstrumentListener {
2622

@@ -36,24 +32,24 @@ interface LiveInstrumentListener {
3632
fun onBreakpointHitEvent(event: LiveBreakpointHit) {
3733
}
3834

39-
fun onBreakpointAddedEvent(event: LiveBreakpoint) {
35+
fun onBreakpointAddedEvent(event: LiveInstrumentAdded) {
4036
}
4137

4238
fun onInstrumentRemovedEvent(event: LiveInstrumentRemoved) {
4339
}
4440

45-
fun onLogAddedEvent(event: LiveLog) {
41+
fun onLogAddedEvent(event: LiveInstrumentAdded) {
4642
}
4743

48-
fun onInstrumentAppliedEvent(event: LiveInstrument) {
44+
fun onInstrumentAppliedEvent(event: LiveInstrumentApplied) {
4945
}
5046

51-
fun onMeterAddedEvent(event: LiveMeter) {
47+
fun onMeterAddedEvent(event: LiveInstrumentAdded) {
5248
}
5349

5450
// fun onMeterUpdatedEvent(event: LiveMeterUpdated) {
5551
// }
5652

57-
fun onSpanAddedEvent(event: LiveSpan) {
53+
fun onSpanAddedEvent(event: LiveInstrumentAdded) {
5854
}
5955
}

src/main/kotlin/spp/protocol/service/listen/LiveInstrumentListenerImpl.kt

Lines changed: 22 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ package spp.protocol.service.listen
1919
import io.vertx.core.Future
2020
import io.vertx.core.Vertx
2121
import io.vertx.core.eventbus.MessageConsumer
22-
import io.vertx.core.json.JsonArray
2322
import io.vertx.core.json.JsonObject
24-
import spp.protocol.instrument.*
23+
import spp.protocol.instrument.LiveInstrument
2524
import spp.protocol.instrument.event.*
25+
import spp.protocol.instrument.event.LiveInstrumentEventType.*
2626
import spp.protocol.service.SourceServices.Subscribe.toLiveInstrumentSubscriberAddress
2727

2828
/**
@@ -42,85 +42,32 @@ class LiveInstrumentListenerImpl(
4242

4343
init {
4444
consumer = vertx.eventBus().consumer(toLiveInstrumentSubscriberAddress(subscriptionId)) {
45-
val liveEvent = LiveInstrumentEvent(it.body())
46-
listener.onInstrumentEvent(liveEvent)
47-
48-
when (liveEvent.eventType) {
49-
LiveInstrumentEventType.LOG_HIT -> onLogHitEvent(liveEvent)
50-
LiveInstrumentEventType.BREAKPOINT_HIT -> onBreakpointHitEvent(liveEvent)
51-
LiveInstrumentEventType.BREAKPOINT_ADDED -> onBreakpointAddedEvent(liveEvent)
52-
LiveInstrumentEventType.BREAKPOINT_REMOVED -> onInstrumentRemovedEvent(liveEvent)
53-
LiveInstrumentEventType.LOG_ADDED -> onLogAddedEvent(liveEvent)
54-
LiveInstrumentEventType.LOG_REMOVED -> onInstrumentRemovedEvent(liveEvent)
55-
LiveInstrumentEventType.BREAKPOINT_APPLIED -> onInstrumentAppliedEvent(liveEvent)
56-
LiveInstrumentEventType.LOG_APPLIED -> onInstrumentAppliedEvent(liveEvent)
57-
LiveInstrumentEventType.METER_ADDED -> onMeterAddedEvent(liveEvent)
58-
LiveInstrumentEventType.METER_APPLIED -> onInstrumentAppliedEvent(liveEvent)
59-
LiveInstrumentEventType.METER_UPDATED -> onMeterUpdatedEvent(liveEvent)
60-
LiveInstrumentEventType.METER_REMOVED -> onInstrumentRemovedEvent(liveEvent)
61-
LiveInstrumentEventType.SPAN_ADDED -> onSpanAddedEvent(liveEvent)
62-
LiveInstrumentEventType.SPAN_APPLIED -> onInstrumentAppliedEvent(liveEvent)
63-
LiveInstrumentEventType.SPAN_REMOVED -> onInstrumentRemovedEvent(liveEvent)
45+
val event = LiveInstrumentEvent.fromJson(it.body())
46+
listener.onInstrumentEvent(event)
47+
48+
when (event.eventType) {
49+
LOG_HIT -> listener.onLogHitEvent(event as LiveLogHit)
50+
BREAKPOINT_HIT -> listener.onBreakpointHitEvent(event as LiveBreakpointHit)
51+
BREAKPOINT_ADDED -> listener.onBreakpointAddedEvent(event as LiveInstrumentAdded)
52+
BREAKPOINT_REMOVED -> listener.onInstrumentRemovedEvent(event as LiveInstrumentRemoved)
53+
LOG_ADDED -> listener.onLogAddedEvent(event as LiveInstrumentAdded)
54+
LOG_REMOVED -> listener.onInstrumentRemovedEvent(event as LiveInstrumentRemoved)
55+
BREAKPOINT_APPLIED -> listener.onInstrumentAppliedEvent(event as LiveInstrumentApplied)
56+
LOG_APPLIED -> listener.onInstrumentAppliedEvent(event as LiveInstrumentApplied)
57+
METER_ADDED -> listener.onMeterAddedEvent(event as LiveInstrumentAdded)
58+
METER_APPLIED -> listener.onInstrumentAppliedEvent(event as LiveInstrumentApplied)
59+
METER_UPDATED -> Unit // TODO
60+
METER_REMOVED -> listener.onInstrumentRemovedEvent(event as LiveInstrumentRemoved)
61+
SPAN_ADDED -> listener.onSpanAddedEvent(event as LiveInstrumentAdded)
62+
SPAN_APPLIED -> listener.onInstrumentAppliedEvent(event as LiveInstrumentApplied)
63+
SPAN_REMOVED -> listener.onInstrumentRemovedEvent(event as LiveInstrumentRemoved)
6464
}
6565

66-
listener.afterInstrumentEvent(liveEvent)
66+
listener.afterInstrumentEvent(event)
6767
}
6868
}
6969

7070
fun unregister(): Future<Void> {
7171
return consumer.unregister()
7272
}
73-
74-
private fun onLogHitEvent(liveEvent: LiveInstrumentEvent) {
75-
val logHit = LiveLogHit(JsonObject(liveEvent.data))
76-
listener.onLogHitEvent(logHit)
77-
}
78-
79-
private fun onBreakpointHitEvent(liveEvent: LiveInstrumentEvent) {
80-
val breakpointHit = LiveBreakpointHit(JsonObject(liveEvent.data))
81-
listener.onBreakpointHitEvent(breakpointHit)
82-
}
83-
84-
private fun onBreakpointAddedEvent(liveEvent: LiveInstrumentEvent) {
85-
val breakpointAdded = LiveBreakpoint(JsonObject(liveEvent.data))
86-
listener.onBreakpointAddedEvent(breakpointAdded)
87-
}
88-
89-
private fun onInstrumentRemovedEvent(liveEvent: LiveInstrumentEvent) {
90-
if (liveEvent.data.startsWith("[")) {
91-
val instrumentsRemoved = JsonArray(liveEvent.data)
92-
for (i in 0 until instrumentsRemoved.size()) {
93-
val instrumentRemoved = LiveInstrumentRemoved(instrumentsRemoved.getJsonObject(i))
94-
listener.onInstrumentRemovedEvent(instrumentRemoved)
95-
}
96-
} else {
97-
val instrumentRemoved = LiveInstrumentRemoved(JsonObject(liveEvent.data))
98-
listener.onInstrumentRemovedEvent(instrumentRemoved)
99-
}
100-
}
101-
102-
private fun onLogAddedEvent(liveEvent: LiveInstrumentEvent) {
103-
val logAdded = LiveLog(JsonObject(liveEvent.data))
104-
listener.onLogAddedEvent(logAdded)
105-
}
106-
107-
private fun onInstrumentAppliedEvent(liveEvent: LiveInstrumentEvent) {
108-
val instrumentApplied = LiveInstrument.fromJson(JsonObject(liveEvent.data))
109-
listener.onInstrumentAppliedEvent(instrumentApplied)
110-
}
111-
112-
private fun onMeterAddedEvent(liveEvent: LiveInstrumentEvent) {
113-
val meterAdded = LiveMeter(JsonObject(liveEvent.data))
114-
listener.onMeterAddedEvent(meterAdded)
115-
}
116-
117-
private fun onMeterUpdatedEvent(liveEvent: LiveInstrumentEvent) {
118-
// val meterUpdated = LiveMeterUpdated(JsonObject(liveEvent.data))
119-
// handleMeterUpdatedEvent(meterUpdated)
120-
}
121-
122-
private fun onSpanAddedEvent(liveEvent: LiveInstrumentEvent) {
123-
val spanAdded = LiveSpan(JsonObject(liveEvent.data))
124-
listener.onSpanAddedEvent(spanAdded)
125-
}
12673
}

src/test/kotlin/spp/protocol/service/listen/LiveInstrumentListenerTest.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class LiveInstrumentListenerTest {
6363
}
6464
})
6565
//todo: DataObjectMessageCodec
66-
val event = LiveInstrumentEvent(LiveInstrumentEventType.LOG_HIT, logHit.toJson().toString())
66+
val event = LiveInstrumentEvent.fromJson(logHit.toJson())
6767
vertx.eventBus().publish(toLiveInstrumentSubscriberAddress("system"), JsonObject.mapFrom(event))
6868

6969
if (testContext.awaitCompletion(5, TimeUnit.SECONDS)) {
@@ -100,7 +100,7 @@ class LiveInstrumentListenerTest {
100100
}
101101
})
102102
//todo: DataObjectMessageCodec
103-
val event = LiveInstrumentEvent(LiveInstrumentEventType.BREAKPOINT_HIT, bpHit.toJson().toString())
103+
val event = LiveInstrumentEvent.fromJson(bpHit.toJson())
104104
vertx.eventBus().publish(toLiveInstrumentSubscriberAddress("system"), JsonObject.mapFrom(event))
105105

106106
if (testContext.awaitCompletion(5, TimeUnit.SECONDS)) {
@@ -128,9 +128,6 @@ class LiveInstrumentListenerTest {
128128
),
129129
Instant.now()
130130
)
131-
val bpsRemoved = JsonArray()
132-
.add(bpRemoved1.toJson())
133-
.add(bpRemoved2.toJson())
134131

135132
vertx.addLiveInstrumentListener("system", object : LiveInstrumentListener {
136133
override fun onInstrumentRemovedEvent(event: LiveInstrumentRemoved) {
@@ -145,8 +142,12 @@ class LiveInstrumentListenerTest {
145142
}
146143
})
147144
//todo: DataObjectMessageCodec
148-
val event = LiveInstrumentEvent(LiveInstrumentEventType.BREAKPOINT_REMOVED, bpsRemoved.toString())
149-
vertx.eventBus().publish(toLiveInstrumentSubscriberAddress("system"), JsonObject.mapFrom(event))
145+
vertx.eventBus().publish(toLiveInstrumentSubscriberAddress("system"),
146+
JsonObject.mapFrom(LiveInstrumentEvent.fromJson(bpRemoved1.toJson()))
147+
)
148+
vertx.eventBus().publish(toLiveInstrumentSubscriberAddress("system"),
149+
JsonObject.mapFrom(LiveInstrumentEvent.fromJson(bpRemoved2.toJson()))
150+
)
150151

151152
if (testContext.awaitCompletion(5, TimeUnit.SECONDS)) {
152153
if (testContext.failed()) {

0 commit comments

Comments
 (0)