Skip to content

Commit 0c7f73b

Browse files
committed
feat: metric tracking for servers/groups
1 parent 905a98e commit 0c7f73b

File tree

6 files changed

+379
-20
lines changed

6 files changed

+379
-20
lines changed

controller-runtime/src/main/kotlin/app/simplecloud/controller/runtime/ControllerRuntime.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ class ControllerRuntime(
3333
private val database = DatabaseFactory.createDatabase(controllerStartCommand.databaseUrl)
3434
private val authCallCredentials = AuthCallCredentials(controllerStartCommand.authSecret)
3535

36-
private val groupRepository = GroupRepository(controllerStartCommand.groupPath)
36+
private val pubSubClient = PubSubClient(
37+
controllerStartCommand.grpcHost,
38+
controllerStartCommand.pubSubGrpcPort,
39+
authCallCredentials
40+
)
41+
private val groupRepository = GroupRepository(controllerStartCommand.groupPath, pubSubClient)
3742
private val numericalIdRepository = ServerNumericalIdRepository()
3843
private val serverRepository = ServerRepository(database, numericalIdRepository)
3944
private val hostRepository = ServerHostRepository()
@@ -160,11 +165,7 @@ class ControllerRuntime(
160165
hostRepository,
161166
groupRepository,
162167
authCallCredentials,
163-
PubSubClient(
164-
controllerStartCommand.grpcHost,
165-
controllerStartCommand.pubSubGrpcPort,
166-
authCallCredentials
167-
),
168+
pubSubClient,
168169
serverHostAttacher
169170
)
170171
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package app.simplecloud.controller.runtime
2+
3+
object MetricsEventNames {
4+
5+
private const val PREFIX = "metrics-droplet:"
6+
const val RECORD_METRIC = "${PREFIX}record-metric"
7+
8+
}

controller-runtime/src/main/kotlin/app/simplecloud/controller/runtime/YamlDirectoryRepository.kt

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import java.nio.file.*
1414
abstract class YamlDirectoryRepository<E, I>(
1515
private val directory: Path,
1616
private val clazz: Class<E>,
17+
private val watcherEvents: WatcherEvents<E> = WatcherEvents.empty()
1718
) : LoadableRepository<E, I> {
1819

1920
private val logger = LogManager.getLogger(this::class.java)
@@ -113,13 +114,24 @@ abstract class YamlDirectoryRepository<E, I>(
113114
val kind = event.kind()
114115
logger.info("Detected change in $resolvedPath (${getChangeStatus(kind)})")
115116
when (kind) {
116-
StandardWatchEventKinds.ENTRY_CREATE,
117-
StandardWatchEventKinds.ENTRY_MODIFY
118-
-> {
119-
load(resolvedPath.toFile())
117+
StandardWatchEventKinds.ENTRY_CREATE -> {
118+
val entity = load(resolvedPath.toFile())
119+
if (entity != null) {
120+
watcherEvents.onCreate(entity)
121+
}
122+
}
123+
StandardWatchEventKinds.ENTRY_MODIFY -> {
124+
val entity = load(resolvedPath.toFile())
125+
if (entity != null) {
126+
watcherEvents.onModify(entity)
127+
}
120128
}
121129

122130
StandardWatchEventKinds.ENTRY_DELETE -> {
131+
val entity = entities[resolvedPath.toFile()]
132+
if (entity != null) {
133+
watcherEvents.onDelete(entity)
134+
}
123135
deleteFile(resolvedPath.toFile())
124136
}
125137
}
@@ -138,4 +150,18 @@ abstract class YamlDirectoryRepository<E, I>(
138150
}
139151
}
140152

153+
interface WatcherEvents<E> {
154+
fun onCreate(entity: E)
155+
fun onDelete(entity: E)
156+
fun onModify(entity: E)
157+
158+
companion object {
159+
fun <E> empty(): WatcherEvents<E> = object : WatcherEvents<E> {
160+
override fun onCreate(entity: E) {}
161+
override fun onDelete(entity: E) {}
162+
override fun onModify(entity: E) {}
163+
}
164+
}
165+
}
166+
141167
}

controller-runtime/src/main/kotlin/app/simplecloud/controller/runtime/group/GroupRepository.kt

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package app.simplecloud.controller.runtime.group
22

3+
import app.simplecloud.controller.runtime.MetricsEventNames
34
import app.simplecloud.controller.runtime.YamlDirectoryRepository
45
import app.simplecloud.controller.shared.group.Group
6+
import app.simplecloud.droplet.api.time.ProtobufTimestamp
7+
import app.simplecloud.pubsub.PubSubClient
8+
import build.buf.gen.simplecloud.metrics.v1.metric
9+
import build.buf.gen.simplecloud.metrics.v1.metricMeta
510
import java.nio.file.Path
6-
import java.util.concurrent.CompletableFuture
11+
import java.time.LocalDateTime
712

813
class GroupRepository(
9-
path: Path
10-
) : YamlDirectoryRepository<Group, String>(path, Group::class.java) {
14+
path: Path,
15+
private val pubSubClient: PubSubClient
16+
) : YamlDirectoryRepository<Group, String>(path, Group::class.java, WatcherEvents(pubSubClient)) {
1117
override fun getFileName(identifier: String): String {
1218
return "$identifier.yml"
1319
}
@@ -23,4 +29,106 @@ class GroupRepository(
2329
override suspend fun getAll(): List<Group> {
2430
return entities.values.toList()
2531
}
32+
33+
private class WatcherEvents(
34+
private val pubsubClient: PubSubClient
35+
) : YamlDirectoryRepository.WatcherEvents<Group> {
36+
37+
override fun onCreate(entity: Group) {
38+
pubsubClient.publish(MetricsEventNames.RECORD_METRIC, metric {
39+
metricType = "ACTIVITY_LOG"
40+
metricValue = 1L
41+
time = ProtobufTimestamp.fromLocalDateTime(LocalDateTime.now())
42+
meta.addAll(
43+
listOf(
44+
metricMeta {
45+
dataName = "displayName"
46+
dataValue = entity.name
47+
},
48+
metricMeta {
49+
dataName = "status"
50+
dataValue = "CREATED"
51+
},
52+
metricMeta {
53+
dataName = "resourceType"
54+
dataValue = "GROUP"
55+
},
56+
metricMeta {
57+
dataName = "groupName"
58+
dataValue = entity.name
59+
},
60+
metricMeta {
61+
dataName = "by"
62+
dataValue = "FILE_WATCHER"
63+
}
64+
)
65+
)
66+
})
67+
}
68+
69+
override fun onDelete(entity: Group) {
70+
pubsubClient.publish(MetricsEventNames.RECORD_METRIC, metric {
71+
metricType = "ACTIVITY_LOG"
72+
metricValue = 1L
73+
time = ProtobufTimestamp.fromLocalDateTime(LocalDateTime.now())
74+
meta.addAll(
75+
listOf(
76+
metricMeta {
77+
dataName = "displayName"
78+
dataValue = entity.name
79+
},
80+
metricMeta {
81+
dataName = "status"
82+
dataValue = "DELETED"
83+
},
84+
metricMeta {
85+
dataName = "resourceType"
86+
dataValue = "GROUP"
87+
},
88+
metricMeta {
89+
dataName = "groupName"
90+
dataValue = entity.name
91+
},
92+
metricMeta {
93+
dataName = "by"
94+
dataValue = "FILE_WATCHER"
95+
}
96+
)
97+
)
98+
})
99+
}
100+
101+
override fun onModify(entity: Group) {
102+
pubsubClient.publish(MetricsEventNames.RECORD_METRIC, metric {
103+
metricType = "ACTIVITY_LOG"
104+
metricValue = 1L
105+
time = ProtobufTimestamp.fromLocalDateTime(LocalDateTime.now())
106+
meta.addAll(
107+
listOf(
108+
metricMeta {
109+
dataName = "displayName"
110+
dataValue = entity.name
111+
},
112+
metricMeta {
113+
dataName = "status"
114+
dataValue = "EDITED"
115+
},
116+
metricMeta {
117+
dataName = "resourceType"
118+
dataValue = "GROUP"
119+
},
120+
metricMeta {
121+
dataName = "groupName"
122+
dataValue = entity.name
123+
},
124+
metricMeta {
125+
dataName = "by"
126+
dataValue = "FILE_WATCHER"
127+
}
128+
)
129+
)
130+
})
131+
}
132+
133+
}
26134
}

controller-runtime/src/main/kotlin/app/simplecloud/controller/runtime/server/ServerRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import java.time.LocalDateTime
1818

1919
class ServerRepository(
2020
private val database: Database,
21-
private val numericalIdRepository: ServerNumericalIdRepository
21+
private val numericalIdRepository: ServerNumericalIdRepository,
2222
) : LoadableRepository<Server, String> {
2323

2424
override suspend fun find(identifier: String): Server? {

0 commit comments

Comments
 (0)