Skip to content

Commit

Permalink
Add example of exposing prometheus metrics to the sync server
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjg committed Oct 3, 2024
1 parent ab7a34f commit 77451ec
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
44 changes: 42 additions & 2 deletions examples/sync-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,35 @@ import { WebSocketServer } from "ws"
import { Repo } from "@automerge/automerge-repo"
import { NodeWSServerAdapter } from "@automerge/automerge-repo-network-websocket"
import { NodeFSStorageAdapter } from "@automerge/automerge-repo-storage-nodefs"
import { default as Prometheus } from "prom-client"
import os from "os"

const registry = new Prometheus.Registry()
Prometheus.collectDefaultMetrics({ register: registry })

const buckets = Prometheus.linearBuckets(0, 1000, 60)

const metrics = {
docLoaded: new Prometheus.Histogram({
name: "automerge_repo_doc_loaded_duration_millis",
help: "Duration of loading a document",
buckets,
registers: [registry],
}),
receiveSyncMessage: new Prometheus.Histogram({
name: "automerge_repo_receive_sync_message_duration_millis",
help: "Duration of receiving a sync message",
buckets,
registers: [registry],
}),
numOps: new Prometheus.Histogram({
name: "automerge_repo_num_ops",
help: "Number of operations in a document",
buckets: Prometheus.exponentialBuckets(1, 2, 20),
registers: [registry],
}),
}

export class Server {
/** @type WebSocketServer */
#socket
Expand Down Expand Up @@ -45,12 +72,25 @@ export class Server {
}
const serverRepo = new Repo(config)

// Observe metrics for prometheus and also log the events so log aggregators like loki can pick them up
serverRepo.on("doc-metrics", (event) => {
console.log(JSON.stringify(event))
metrics.numOps.observe(event.numOps)
if (event.type === "doc-loaded") {
metrics.docLoaded.observe(event.durationMillis)
} else if (event.type === "receive-sync-message") {
metrics.receiveSyncMessage.observe(event.durationMillis)
}
})

app.get("/", (req, res) => {
res.send(`👍 @automerge/example-sync-server is running`)
})

app.get("/metrics", (req, res) => {
res.json(serverRepo.metrics())
// In a real server this endpoint would be authenticated or not event part of the same express app
app.get("/prometheus_metrics", async (req, res) => {
res.set("Content-Type", registry.contentType)
res.end(await registry.metrics())
})

this.#server = app.listen(PORT, () => {
Expand Down
1 change: 1 addition & 0 deletions examples/sync-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@automerge/automerge-repo-network-websocket": "workspace:*",
"@automerge/automerge-repo-storage-nodefs": "workspace:*",
"express": "^4.18.1",
"prom-client": "^15.1.3",
"ws": "^8.7.0"
},
"devDependencies": {
Expand Down
30 changes: 30 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 77451ec

Please sign in to comment.