Skip to content

Commit 22bc28e

Browse files
committed
jb: push backend memory metrics to prometheus
1 parent aa75ee7 commit 22bc28e

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

components/ide/jetbrains/backend-plugin/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ dependencies {
3939
type = "jar"
4040
}
4141
}
42+
implementation("io.prometheus:simpleclient_pushgateway:0.15.0")
4243
compileOnly("javax.websocket:javax.websocket-api:1.1")
4344
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.18.1")
4445
testImplementation(kotlin("test"))

components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodManager.kt

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import git4idea.config.GitVcsApplicationSettings
1919
import io.gitpod.gitpodprotocol.api.GitpodClient
2020
import io.gitpod.gitpodprotocol.api.GitpodServerLauncher
2121
import io.gitpod.jetbrains.remote.services.HeartbeatService
22-
import io.gitpod.jetbrains.remote.utils.LatencyRecord
2322
import io.gitpod.jetbrains.remote.utils.Retrier.retry
2423
import io.gitpod.supervisor.api.*
2524
import io.gitpod.supervisor.api.Info.WorkspaceInfoResponse
@@ -28,6 +27,9 @@ import io.grpc.ManagedChannel
2827
import io.grpc.ManagedChannelBuilder
2928
import io.grpc.stub.ClientCallStreamObserver
3029
import io.grpc.stub.ClientResponseObserver
30+
import io.prometheus.client.CollectorRegistry
31+
import io.prometheus.client.Histogram
32+
import io.prometheus.client.exporter.PushGateway
3133
import kotlinx.coroutines.GlobalScope
3234
import kotlinx.coroutines.delay
3335
import kotlinx.coroutines.future.await
@@ -39,8 +41,6 @@ import java.net.URI
3941
import java.net.http.HttpClient
4042
import java.net.http.HttpRequest
4143
import java.net.http.HttpResponse
42-
import java.nio.file.Files
43-
import java.nio.file.Paths
4444
import java.time.Duration
4545
import java.util.concurrent.CancellationException
4646
import java.util.concurrent.CompletableFuture
@@ -56,40 +56,46 @@ class GitpodManager : Disposable {
5656
}
5757

5858
val devMode = System.getenv("JB_DEV").toBoolean()
59+
private val backendKind = System.getenv("JETBRAINS_GITPOD_BACKEND_KIND") ?: "unknown"
5960

6061
private val lifetime = Lifetime.Eternal.createNested()
6162

6263
override fun dispose() {
6364
lifetime.terminate()
6465
}
6566

66-
private val memProfileMode = System.getenv("JB_MEM_PROFILE").toBoolean()
67-
6867
init {
69-
val memProfileJob = GlobalScope.launch {
70-
if (application.isHeadlessEnvironment || !memProfileMode) {
68+
val monitoringJob = GlobalScope.launch {
69+
if (application.isHeadlessEnvironment) {
7170
return@launch
7271
}
73-
Files.newBufferedWriter(Paths.get("/tmp/jb_mem_profile.log")).use { writer ->
74-
writer.write("")
75-
writer.flush()
76-
val allocatedRecord = LatencyRecord()
77-
val usedRecord = LatencyRecord()
78-
while (isActive) {
79-
val totalMemory = Runtime.getRuntime().totalMemory() / (1024 * 1024)
80-
val usedMemory = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (1024 * 1024)
81-
allocatedRecord.update(totalMemory)
82-
usedRecord.update(usedMemory)
83-
writer.write("allocated - current ${totalMemory}M, avg ${allocatedRecord.averageLatency}M, max ${allocatedRecord.maxLatency}M, 90% percentile ${allocatedRecord.percentile(
84-
90)}M\n")
85-
writer.write("used - current ${usedMemory}M,avg ${usedRecord.averageLatency}M, max ${usedRecord.maxLatency}M, 90% percentile ${usedRecord.percentile(
86-
90)}M\n")
87-
writer.flush()
88-
delay(1000)
72+
val pg = PushGateway("localhost:22999")
73+
val registry = CollectorRegistry()
74+
val labels = mapOf("kind" to backendKind)
75+
val allocatedHistogram = Histogram.build()
76+
.name("jb_backend_allocated_memory_megabytes")
77+
.help("Allocated memory of JB backend in megabytes.")
78+
.buckets(512.0, 1024.0, 2048.0, 4096.0, 8192.0)
79+
.register(registry)
80+
val usedHistogram = Histogram.build()
81+
.name("jb_backend_used_memory_megabytes")
82+
.help("Used memory of JB backend in megabytes.")
83+
.buckets(512.0, 1024.0, 2048.0, 4096.0, 8192.0)
84+
.register(registry)
85+
while(isActive) {
86+
val totalMemory = Runtime.getRuntime().totalMemory() / (1024 * 1024)
87+
val usedMemory = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (1024 * 1024)
88+
allocatedHistogram.observeWithExemplar(totalMemory.toDouble(), labels)
89+
usedHistogram.observeWithExemplar(usedMemory.toDouble(), labels)
90+
try {
91+
pg.push(registry, "jb_backend")
92+
} catch (t: Throwable) {
93+
thisLogger().error("gitpod: failed to push monitoring metrics:", t)
8994
}
95+
delay(5000)
9096
}
9197
}
92-
lifetime.onTerminationOrNow { memProfileJob.cancel() }
98+
lifetime.onTerminationOrNow { monitoringJob.cancel() }
9399
}
94100

95101
init {

components/ide/jetbrains/image/status/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func main() {
8888
if err != nil {
8989
log.WithError(err).Error("failed to configure backend Xmx")
9090
}
91-
go run(wsInfo)
91+
go run(wsInfo, alias)
9292

9393
http.HandleFunc("/joinLink", func(w http.ResponseWriter, r *http.Request) {
9494
backendPort := r.URL.Query().Get("backendPort")
@@ -224,11 +224,12 @@ func resolveWorkspaceInfo(ctx context.Context) (*supervisor.ContentStatusRespons
224224
return nil, nil, errors.New("failed with attempt 10 times")
225225
}
226226

227-
func run(wsInfo *supervisor.WorkspaceInfoResponse) {
227+
func run(wsInfo *supervisor.WorkspaceInfoResponse, alias string) {
228228
var args []string
229229
args = append(args, "run")
230230
args = append(args, wsInfo.GetCheckoutLocation())
231231
cmd := remoteDevServerCmd(args)
232+
cmd.Env = append(cmd.Env, "JETBRAINS_GITPOD_BACKEND_KIND="+alias)
232233
workspaceUrl, err := url.Parse(wsInfo.WorkspaceUrl)
233234
if err == nil {
234235
cmd.Env = append(cmd.Env, "JETBRAINS_GITPOD_WORKSPACE_HOST="+workspaceUrl.Hostname())

0 commit comments

Comments
 (0)