Skip to content

Commit 29cacaf

Browse files
committed
add meters/spans
1 parent ecc4576 commit 29cacaf

File tree

5 files changed

+223
-0
lines changed

5 files changed

+223
-0
lines changed

src/main/kotlin/spp/cli/Main.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,13 @@ object Main {
5959
//instrument
6060
AddBreakpoint(),
6161
AddLog(),
62+
AddMeter(),
63+
AddSpan(),
6264
GetBreakpoints(),
6365
GetInstruments(),
6466
GetLogs(),
67+
GetMeters(),
68+
GetSpans(),
6569
RemoveInstrument(),
6670
RemoveInstruments(),
6771
ClearInstruments(),
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package spp.cli.commands.instrument
2+
3+
import com.apollographql.apollo3.api.CustomScalarAdapters
4+
import com.apollographql.apollo3.api.Optional
5+
import com.apollographql.apollo3.api.json.MapJsonWriter
6+
import com.github.ajalt.clikt.core.CliktCommand
7+
import com.github.ajalt.clikt.parameters.arguments.argument
8+
import com.github.ajalt.clikt.parameters.options.default
9+
import com.github.ajalt.clikt.parameters.options.option
10+
import com.github.ajalt.clikt.parameters.types.enum
11+
import com.github.ajalt.clikt.parameters.types.int
12+
import com.github.ajalt.clikt.parameters.types.long
13+
import kotlinx.coroutines.runBlocking
14+
import spp.cli.Main
15+
import spp.cli.PlatformCLI.apolloClient
16+
import spp.cli.PlatformCLI.echoError
17+
import spp.cli.protocol.instrument.AddLiveMeterMutation
18+
import spp.cli.protocol.instrument.adapter.AddLiveMeterMutation_ResponseAdapter.AddLiveMeter
19+
import spp.cli.protocol.type.*
20+
import spp.cli.util.JsonCleaner
21+
import kotlin.system.exitProcess
22+
23+
class AddMeter : CliktCommand() {
24+
25+
val source by argument(help = "Qualified class name")
26+
val line by argument(help = "Line number").int()
27+
val meterName by argument(help = "Meter name")
28+
val meterType by argument(help = "Meter type").enum<MeterType>()
29+
val valueType by argument(help = "Metric value type").enum<MetricValueType>()
30+
val value by option("-value", "-v", help = "Metric value")
31+
val condition by option("-condition", "-c", help = "Trigger condition")
32+
val expiresAt by option("-expiresAt", "-e", help = "Expiration time (epoch time [ms])").long()
33+
val hitLimit by option("-hitLimit", "-h", help = "Trigger hit limit").int()
34+
val throttleLimit by option("-throttleLimit", "-t", help = "Trigger throttle limit").int().default(1)
35+
val throttleStep by option("-throttleStep", "-s", help = "Trigger throttle step").enum<ThrottleStep>()
36+
.default(ThrottleStep.SECOND)
37+
38+
override fun run() = runBlocking {
39+
val input = LiveMeterInput(
40+
meterName = meterName,
41+
meterType = meterType,
42+
metricValue = MetricValueInput(
43+
valueType = valueType,
44+
value = Optional.presentIfNotNull(value)
45+
),
46+
location = LiveSourceLocationInput(source, line),
47+
condition = Optional.Present(condition),
48+
expiresAt = Optional.Present(expiresAt),
49+
hitLimit = Optional.Present(hitLimit),
50+
throttle = Optional.Present(InstrumentThrottleInput(throttleLimit, throttleStep))
51+
)
52+
val response = try {
53+
apolloClient.mutation(AddLiveMeterMutation(input)).execute()
54+
} catch (e: Exception) {
55+
echoError(e)
56+
if (Main.standalone) exitProcess(-1) else return@runBlocking
57+
}
58+
if (response.hasErrors()) {
59+
echo(response.errors?.get(0)?.message, err = true)
60+
if (Main.standalone) exitProcess(-1) else return@runBlocking
61+
}
62+
63+
echo(JsonCleaner.cleanJson(MapJsonWriter().let {
64+
it.beginObject()
65+
AddLiveMeter.toJson(it, CustomScalarAdapters.Empty, response.data!!.addLiveMeter)
66+
it.endObject()
67+
(it.root() as LinkedHashMap<*, *>)
68+
}).encodePrettily())
69+
if (Main.standalone) exitProcess(0)
70+
}
71+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package spp.cli.commands.instrument
2+
3+
import com.apollographql.apollo3.api.CustomScalarAdapters
4+
import com.apollographql.apollo3.api.Optional
5+
import com.apollographql.apollo3.api.json.MapJsonWriter
6+
import com.github.ajalt.clikt.core.CliktCommand
7+
import com.github.ajalt.clikt.parameters.arguments.argument
8+
import com.github.ajalt.clikt.parameters.options.default
9+
import com.github.ajalt.clikt.parameters.options.option
10+
import com.github.ajalt.clikt.parameters.types.enum
11+
import com.github.ajalt.clikt.parameters.types.int
12+
import com.github.ajalt.clikt.parameters.types.long
13+
import kotlinx.coroutines.runBlocking
14+
import spp.cli.Main
15+
import spp.cli.PlatformCLI.apolloClient
16+
import spp.cli.PlatformCLI.echoError
17+
import spp.cli.protocol.instrument.AddLiveSpanMutation
18+
import spp.cli.protocol.instrument.adapter.AddLiveSpanMutation_ResponseAdapter.AddLiveSpan
19+
import spp.cli.protocol.type.InstrumentThrottleInput
20+
import spp.cli.protocol.type.LiveSourceLocationInput
21+
import spp.cli.protocol.type.LiveSpanInput
22+
import spp.cli.protocol.type.ThrottleStep
23+
import spp.cli.util.JsonCleaner
24+
import kotlin.system.exitProcess
25+
26+
class AddSpan : CliktCommand() {
27+
28+
val source by argument(help = "Qualified class name")
29+
val line by argument(help = "Line number").int()
30+
val operationName by argument(help = "Operation name")
31+
val condition by option("-condition", "-c", help = "Trigger condition")
32+
val expiresAt by option("-expiresAt", "-e", help = "Expiration time (epoch time [ms])").long()
33+
val hitLimit by option("-hitLimit", "-h", help = "Trigger hit limit").int()
34+
val throttleLimit by option("-throttleLimit", "-t", help = "Trigger throttle limit").int().default(1)
35+
val throttleStep by option("-throttleStep", "-s", help = "Trigger throttle step").enum<ThrottleStep>()
36+
.default(ThrottleStep.SECOND)
37+
38+
override fun run() = runBlocking {
39+
val input = LiveSpanInput(
40+
operationName = operationName,
41+
location = LiveSourceLocationInput(source, line),
42+
condition = Optional.Present(condition),
43+
expiresAt = Optional.Present(expiresAt),
44+
hitLimit = Optional.Present(hitLimit),
45+
throttle = Optional.Present(InstrumentThrottleInput(throttleLimit, throttleStep))
46+
)
47+
val response = try {
48+
apolloClient.mutation(AddLiveSpanMutation(input)).execute()
49+
} catch (e: Exception) {
50+
echoError(e)
51+
if (Main.standalone) exitProcess(-1) else return@runBlocking
52+
}
53+
if (response.hasErrors()) {
54+
echo(response.errors?.get(0)?.message, err = true)
55+
if (Main.standalone) exitProcess(-1) else return@runBlocking
56+
}
57+
58+
echo(JsonCleaner.cleanJson(MapJsonWriter().let {
59+
it.beginObject()
60+
AddLiveSpan.toJson(it, CustomScalarAdapters.Empty, response.data!!.addLiveSpan)
61+
it.endObject()
62+
(it.root() as LinkedHashMap<*, *>)
63+
}).encodePrettily())
64+
if (Main.standalone) exitProcess(0)
65+
}
66+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package spp.cli.commands.instrument
2+
3+
import com.apollographql.apollo3.api.CustomScalarAdapters
4+
import com.apollographql.apollo3.api.json.MapJsonWriter
5+
import com.github.ajalt.clikt.core.CliktCommand
6+
import kotlinx.coroutines.runBlocking
7+
import spp.cli.Main
8+
import spp.cli.PlatformCLI.apolloClient
9+
import spp.cli.PlatformCLI.echoError
10+
import spp.cli.protocol.instrument.GetLiveMetersQuery
11+
import spp.cli.protocol.instrument.adapter.GetLiveMetersQuery_ResponseAdapter.GetLiveMeter
12+
import spp.cli.util.JsonCleaner
13+
import kotlin.system.exitProcess
14+
15+
class GetMeters : CliktCommand() {
16+
17+
override fun run() = runBlocking {
18+
val response = try {
19+
apolloClient.query(GetLiveMetersQuery()).execute()
20+
} catch (e: Exception) {
21+
echoError(e)
22+
if (Main.standalone) exitProcess(-1) else return@runBlocking
23+
}
24+
if (response.hasErrors()) {
25+
echo(response.errors?.get(0)?.message, err = true)
26+
if (Main.standalone) exitProcess(-1) else return@runBlocking
27+
}
28+
29+
echo(JsonCleaner.cleanJson(MapJsonWriter().let {
30+
it.beginArray()
31+
response.data!!.getLiveMeters.forEach { ob ->
32+
it.beginObject()
33+
GetLiveMeter.toJson(it, CustomScalarAdapters.Empty, ob)
34+
it.endObject()
35+
}
36+
it.endArray()
37+
(it.root() as ArrayList<*>)
38+
}).encodePrettily())
39+
if (Main.standalone) exitProcess(0)
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package spp.cli.commands.instrument
2+
3+
import com.apollographql.apollo3.api.CustomScalarAdapters
4+
import com.apollographql.apollo3.api.json.MapJsonWriter
5+
import com.github.ajalt.clikt.core.CliktCommand
6+
import kotlinx.coroutines.runBlocking
7+
import spp.cli.Main
8+
import spp.cli.PlatformCLI.apolloClient
9+
import spp.cli.PlatformCLI.echoError
10+
import spp.cli.protocol.instrument.GetLiveSpansQuery
11+
import spp.cli.protocol.instrument.adapter.GetLiveSpansQuery_ResponseAdapter.GetLiveSpan
12+
import spp.cli.util.JsonCleaner
13+
import kotlin.system.exitProcess
14+
15+
class GetSpans : CliktCommand() {
16+
17+
override fun run() = runBlocking {
18+
val response = try {
19+
apolloClient.query(GetLiveSpansQuery()).execute()
20+
} catch (e: Exception) {
21+
echoError(e)
22+
if (Main.standalone) exitProcess(-1) else return@runBlocking
23+
}
24+
if (response.hasErrors()) {
25+
echo(response.errors?.get(0)?.message, err = true)
26+
if (Main.standalone) exitProcess(-1) else return@runBlocking
27+
}
28+
29+
echo(JsonCleaner.cleanJson(MapJsonWriter().let {
30+
it.beginArray()
31+
response.data!!.getLiveSpans.forEach { ob ->
32+
it.beginObject()
33+
GetLiveSpan.toJson(it, CustomScalarAdapters.Empty, ob)
34+
it.endObject()
35+
}
36+
it.endArray()
37+
(it.root() as ArrayList<*>)
38+
}).encodePrettily())
39+
if (Main.standalone) exitProcess(0)
40+
}
41+
}

0 commit comments

Comments
 (0)