Skip to content

Commit 2546d44

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

File tree

8 files changed

+100
-44
lines changed

8 files changed

+100
-44
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package spp.protocol.instrument.event
1919
import io.vertx.codegen.annotations.DataObject
2020
import io.vertx.core.json.JsonObject
2121
import spp.protocol.artifact.exception.LiveStackTrace
22+
import spp.protocol.instrument.LiveInstrument
2223
import java.time.Instant
2324

2425
/**
@@ -29,22 +30,22 @@ import java.time.Instant
2930
*/
3031
@DataObject
3132
data class LiveBreakpointHit(
32-
val breakpointId: String,
33+
override val instrument: LiveInstrument,
3334
val traceId: String,
35+
val stackTrace: LiveStackTrace,
3436
override val occurredAt: Instant,
35-
val serviceInstance: String,
36-
val service: String,
37-
val stackTrace: LiveStackTrace
38-
) : LiveInstrumentEvent {
37+
override val serviceInstance: String,
38+
override val service: String
39+
) : LiveInstrumentHit {
3940
override val eventType: LiveInstrumentEventType = LiveInstrumentEventType.BREAKPOINT_HIT
4041

4142
constructor(json: JsonObject) : this(
42-
json.getString("breakpointId"),
43+
LiveInstrument.fromJson(json.getJsonObject("instrument")),
4344
json.getString("traceId"),
45+
LiveStackTrace(json.getJsonObject("stackTrace")),
4446
Instant.parse(json.getString("occurredAt")),
4547
json.getString("serviceInstance"),
46-
json.getString("service"),
47-
LiveStackTrace(json.getJsonObject("stackTrace"))
48+
json.getString("service")
4849
)
4950

5051
override fun toJson(): JsonObject {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ import java.time.Instant
3030
*/
3131
@DataObject
3232
data class LiveInstrumentAdded(
33-
val liveInstrument: LiveInstrument,
33+
val instrument: LiveInstrument,
3434
override val occurredAt: Instant = Instant.now()
3535
) : LiveInstrumentEvent {
3636
override val eventType: LiveInstrumentEventType
3737
get() {
38-
return when (liveInstrument.type) {
38+
return when (instrument.type) {
3939
LiveInstrumentType.BREAKPOINT -> LiveInstrumentEventType.BREAKPOINT_ADDED
4040
LiveInstrumentType.LOG -> LiveInstrumentEventType.LOG_ADDED
4141
LiveInstrumentType.METER -> LiveInstrumentEventType.METER_ADDED
@@ -44,7 +44,7 @@ data class LiveInstrumentAdded(
4444
}
4545

4646
constructor(json: JsonObject) : this(
47-
liveInstrument = LiveInstrument.fromJson(json.getJsonObject("liveInstrument")),
47+
instrument = LiveInstrument.fromJson(json.getJsonObject("instrument")),
4848
occurredAt = Instant.parse(json.getString("occurredAt"))
4949
)
5050

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ import java.time.Instant
3030
*/
3131
@DataObject
3232
data class LiveInstrumentApplied(
33-
val liveInstrument: LiveInstrument,
33+
val instrument: LiveInstrument,
3434
override val occurredAt: Instant
3535
) : LiveInstrumentEvent {
3636
override val eventType: LiveInstrumentEventType
3737
get() {
38-
return when (liveInstrument.type) {
38+
return when (instrument.type) {
3939
LiveInstrumentType.BREAKPOINT -> LiveInstrumentEventType.BREAKPOINT_APPLIED
4040
LiveInstrumentType.LOG -> LiveInstrumentEventType.LOG_APPLIED
4141
LiveInstrumentType.METER -> LiveInstrumentEventType.METER_APPLIED
@@ -44,7 +44,7 @@ data class LiveInstrumentApplied(
4444
}
4545

4646
constructor(json: JsonObject) : this(
47-
liveInstrument = LiveInstrument.fromJson(json.getJsonObject("liveInstrument")),
47+
instrument = LiveInstrument.fromJson(json.getJsonObject("instrument")),
4848
occurredAt = Instant.parse(json.getString("occurredAt"))
4949
)
5050

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Source++, the continuous feedback platform for developers.
3+
* Copyright (C) 2022-2023 CodeBrig, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package spp.protocol.instrument.event
18+
19+
import spp.protocol.instrument.LiveInstrument
20+
21+
/**
22+
* todo: description.
23+
*
24+
* @since 0.7.7
25+
* @author [Brandon Fergerson](mailto:bfergerson@apache.org)
26+
*/
27+
interface LiveInstrumentHit : LiveInstrumentEvent {
28+
val instrument: LiveInstrument
29+
val serviceInstance: String
30+
val service: String
31+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ import java.time.Instant
3131
*/
3232
@DataObject
3333
data class LiveInstrumentRemoved(
34-
val liveInstrument: LiveInstrument,
34+
val instrument: LiveInstrument,
3535
override val occurredAt: Instant,
3636
val cause: LiveStackTrace? = null
3737
) : LiveInstrumentEvent {
3838
override val eventType: LiveInstrumentEventType
3939
get() {
40-
return when (liveInstrument.type) {
40+
return when (instrument.type) {
4141
LiveInstrumentType.BREAKPOINT -> LiveInstrumentEventType.BREAKPOINT_REMOVED
4242
LiveInstrumentType.LOG -> LiveInstrumentEventType.LOG_REMOVED
4343
LiveInstrumentType.METER -> LiveInstrumentEventType.METER_REMOVED
@@ -46,7 +46,7 @@ data class LiveInstrumentRemoved(
4646
}
4747

4848
constructor(json: JsonObject) : this(
49-
liveInstrument = LiveInstrument.fromJson(json.getJsonObject("liveInstrument")),
49+
instrument = LiveInstrument.fromJson(json.getJsonObject("instrument")),
5050
occurredAt = Instant.parse(json.getString("occurredAt")),
5151
cause = json.getJsonObject("cause")?.let { LiveStackTrace(it) }
5252
)

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package spp.protocol.instrument.event
1919
import io.vertx.codegen.annotations.DataObject
2020
import io.vertx.core.json.JsonObject
2121
import spp.protocol.artifact.log.LogResult
22+
import spp.protocol.instrument.LiveInstrument
2223
import java.time.Instant
2324

2425
/**
@@ -29,20 +30,20 @@ import java.time.Instant
2930
*/
3031
@DataObject
3132
data class LiveLogHit(
32-
val logId: String,
33+
override val instrument: LiveInstrument,
34+
val logResult: LogResult,
3335
override val occurredAt: Instant,
34-
val serviceInstance: String,
35-
val service: String,
36-
val logResult: LogResult
37-
) : LiveInstrumentEvent {
36+
override val serviceInstance: String,
37+
override val service: String
38+
) : LiveInstrumentHit {
3839
override val eventType: LiveInstrumentEventType = LiveInstrumentEventType.LOG_HIT
3940

4041
constructor(json: JsonObject) : this(
41-
json.getString("logId"),
42+
LiveInstrument.fromJson(json.getJsonObject("instrument")),
43+
LogResult(json.getJsonObject("logResult")),
4244
Instant.parse(json.getString("occurredAt")),
4345
json.getString("serviceInstance"),
44-
json.getString("service"),
45-
LogResult(json.getJsonObject("logResult"))
46+
json.getString("service")
4647
)
4748

4849
override fun toJson(): JsonObject {

src/test/kotlin/spp/protocol/marshall/ProtocolMarshallerTest.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,14 @@ class ProtocolMarshallerTest {
124124
@Test
125125
fun testLiveBreakpointHit() {
126126
val liveBreakpointHit = LiveBreakpointHit(
127-
"breakpointId",
127+
LiveBreakpoint(
128+
location = LiveSourceLocation(
129+
"source",
130+
10,
131+
),
132+
condition = "1 == 1"
133+
),
128134
"traceId",
129-
Instant.now(),
130-
"serviceInstance",
131-
"service",
132135
LiveStackTrace(
133136
"exception",
134137
"message",
@@ -174,7 +177,10 @@ class ProtocolMarshallerTest {
174177
)
175178
)
176179
)
177-
)
180+
),
181+
Instant.now(),
182+
"serviceInstance",
183+
"service"
178184
)
179185

180186
val serialized = liveBreakpointHit.toJson()

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

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package spp.protocol.service.listen
1818

1919
import io.vertx.core.Vertx
20-
import io.vertx.core.json.JsonArray
2120
import io.vertx.core.json.JsonObject
2221
import io.vertx.junit5.VertxExtension
2322
import io.vertx.junit5.VertxTestContext
@@ -30,6 +29,7 @@ import spp.protocol.artifact.exception.LiveStackTrace
3029
import spp.protocol.artifact.log.LogOrderType
3130
import spp.protocol.artifact.log.LogResult
3231
import spp.protocol.instrument.LiveBreakpoint
32+
import spp.protocol.instrument.LiveLog
3333
import spp.protocol.instrument.event.*
3434
import spp.protocol.instrument.location.LiveSourceLocation
3535
import spp.protocol.service.SourceServices.Subscribe.toLiveInstrumentSubscriberAddress
@@ -44,15 +44,24 @@ class LiveInstrumentListenerTest {
4444
val testContext = VertxTestContext()
4545

4646
val logHit = LiveLogHit(
47-
"logId",
48-
Instant.now(),
49-
"serviceInstance",
50-
"service",
47+
LiveLog(
48+
"test {}",
49+
listOf("b"),
50+
location = LiveSourceLocation(
51+
"source",
52+
10,
53+
),
54+
applyImmediately = true,
55+
id = "id"
56+
),
5157
LogResult(
5258
ArtifactQualifiedName("identifier", type = ArtifactType.EXPRESSION),
5359
LogOrderType.NEWEST_LOGS,
5460
Instant.now(),
55-
)
61+
),
62+
Instant.now(),
63+
"serviceInstance",
64+
"service"
5665
)
5766
vertx.addLiveInstrumentListener("system", object : LiveInstrumentListener {
5867
override fun onLogHitEvent(event: LiveLogHit) {
@@ -80,16 +89,22 @@ class LiveInstrumentListenerTest {
8089
val testContext = VertxTestContext()
8190

8291
val bpHit = LiveBreakpointHit(
83-
"breakpointId",
92+
LiveBreakpoint(
93+
location = LiveSourceLocation(
94+
"source",
95+
10,
96+
),
97+
condition = "1 == 1"
98+
),
8499
"traceId",
85-
Instant.now(),
86-
"serviceInstance",
87-
"service",
88100
LiveStackTrace(
89101
"exceptionType",
90102
"message",
91103
mutableListOf()
92-
)
104+
),
105+
Instant.now(),
106+
"serviceInstance",
107+
"service",
93108
)
94109
vertx.addLiveInstrumentListener("system", object : LiveInstrumentListener {
95110
override fun onBreakpointHitEvent(event: LiveBreakpointHit) {
@@ -132,7 +147,7 @@ class LiveInstrumentListenerTest {
132147
vertx.addLiveInstrumentListener("system", object : LiveInstrumentListener {
133148
override fun onInstrumentRemovedEvent(event: LiveInstrumentRemoved) {
134149
testContext.verify {
135-
if (event.liveInstrument.location.source.endsWith("1")) {
150+
if (event.instrument.location.source.endsWith("1")) {
136151
assertEquals(bpRemoved1, event)
137152
} else {
138153
assertEquals(bpRemoved2, event)
@@ -142,10 +157,12 @@ class LiveInstrumentListenerTest {
142157
}
143158
})
144159
//todo: DataObjectMessageCodec
145-
vertx.eventBus().publish(toLiveInstrumentSubscriberAddress("system"),
160+
vertx.eventBus().publish(
161+
toLiveInstrumentSubscriberAddress("system"),
146162
JsonObject.mapFrom(LiveInstrumentEvent.fromJson(bpRemoved1.toJson()))
147163
)
148-
vertx.eventBus().publish(toLiveInstrumentSubscriberAddress("system"),
164+
vertx.eventBus().publish(
165+
toLiveInstrumentSubscriberAddress("system"),
149166
JsonObject.mapFrom(LiveInstrumentEvent.fromJson(bpRemoved2.toJson()))
150167
)
151168

0 commit comments

Comments
 (0)