forked from bluesky-social/atproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
116 lines (105 loc) · 3.2 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* eslint-env node */
'use strict'
const dd = require('dd-trace')
dd.tracer
.init()
.use('http2', {
client: true, // calls into dataplane
server: false,
})
.use('express', {
hooks: {
request: (span, req) => {
maintainXrpcResource(span, req)
},
},
})
// modify tracer in order to track calls to dataplane as a service with proper resource names
const DATAPLANE_PREFIX = '/bsky.Service/'
const origStartSpan = dd.tracer._tracer.startSpan
dd.tracer._tracer.startSpan = function (name, options) {
if (
name !== 'http.request' ||
options?.tags?.component !== 'http2' ||
!options?.tags?.['http.url']
) {
return origStartSpan.call(this, name, options)
}
const uri = new URL(options.tags['http.url'])
if (!uri.pathname.startsWith(DATAPLANE_PREFIX)) {
return origStartSpan.call(this, name, options)
}
options.tags['service.name'] = 'dataplane-bsky'
options.tags['resource.name'] = uri.pathname.slice(DATAPLANE_PREFIX.length)
return origStartSpan.call(this, name, options)
}
// Tracer code above must come before anything else
const path = require('node:path')
const assert = require('node:assert')
const cluster = require('cluster')
const { Secp256k1Keypair } = require('@atproto/crypto')
const { ServerConfig, BskyAppView } = require('@atproto/bsky')
const main = async () => {
const env = getEnv()
const config = ServerConfig.readEnv()
assert(env.serviceSigningKey, 'must set BSKY_SERVICE_SIGNING_KEY')
const signingKey = await Secp256k1Keypair.import(env.serviceSigningKey)
const bsky = BskyAppView.create({ config, signingKey })
await bsky.start()
// Graceful shutdown (see also https://aws.amazon.com/blogs/containers/graceful-shutdowns-with-ecs/)
const shutdown = async () => {
await bsky.destroy()
}
process.on('SIGTERM', shutdown)
process.on('disconnect', shutdown) // when clustering
}
const getEnv = () => ({
serviceSigningKey: process.env.BSKY_SERVICE_SIGNING_KEY || undefined,
})
const maybeParseInt = (str) => {
if (!str) return
const int = parseInt(str, 10)
if (isNaN(int)) return
return int
}
const maintainXrpcResource = (span, req) => {
// Show actual xrpc method as resource rather than the route pattern
if (span && req.originalUrl?.startsWith('/xrpc/')) {
span.setTag(
'resource.name',
[
req.method,
path.posix.join(req.baseUrl || '', req.path || '', '/').slice(0, -1), // Ensures no trailing slash
]
.filter(Boolean)
.join(' '),
)
}
}
const workerCount = maybeParseInt(process.env.CLUSTER_WORKER_COUNT)
if (workerCount) {
if (cluster.isPrimary) {
console.log(`primary ${process.pid} is running`)
const workers = new Set()
for (let i = 0; i < workerCount; ++i) {
workers.add(cluster.fork())
}
let teardown = false
cluster.on('exit', (worker) => {
workers.delete(worker)
if (!teardown) {
workers.add(cluster.fork()) // restart on crash
}
})
process.on('SIGTERM', () => {
teardown = true
console.log('disconnecting workers')
workers.forEach((w) => w.disconnect())
})
} else {
console.log(`worker ${process.pid} is running`)
main()
}
} else {
main() // non-clustering
}