Skip to content

Commit fbdc9d3

Browse files
authored
fix: add metrics for agentkeepalive (#551)
1 parent ddd1558 commit fbdc9d3

File tree

19 files changed

+622
-200
lines changed

19 files changed

+622
-200
lines changed

package-lock.json

Lines changed: 422 additions & 153 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@
5555
"conventional-changelog-conventionalcommits": "^5.0.0",
5656
"crypto-js": "^4.2.0",
5757
"dotenv": "^16.0.0",
58-
"fastify": "^4.8.1",
58+
"fastify": "^4.28.1",
5959
"fastify-metrics": "^10.2.0",
60-
"fastify-plugin": "^4.0.0",
60+
"fastify-plugin": "^4.5.1",
6161
"fastify-xml-body-parser": "^2.2.0",
6262
"fs-extra": "^10.0.1",
6363
"fs-xattr": "0.3.1",
@@ -113,7 +113,7 @@
113113
"ts-node-dev": "^1.1.8",
114114
"tsx": "^4.16.0",
115115
"tus-js-client": "^3.1.0",
116-
"typescript": "^4.5.5"
116+
"typescript": "^5.6.2"
117117
},
118118
"bin": "./dist/server.js"
119119
}

src/http/plugins/storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const storageBackend = createStorageBackend(storageBackendType)
1717

1818
export const storage = fastifyPlugin(
1919
async function storagePlugin(fastify) {
20-
fastify.decorateRequest('storage', undefined)
20+
fastify.decorateRequest('storage', null)
2121
fastify.addHook('preHandler', async (request) => {
2222
const database = new StorageKnexDB(request.db, {
2323
tenantId: request.tenantId,

src/http/plugins/tracing.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ export const tracing = fastifyPlugin(
4242
const span = trace.getSpan(context.active())
4343

4444
if (span) {
45-
// We collect logs only in full and logs mode
45+
// We collect logs only in full,logs,debug mode
4646
if (
4747
tracingEnabled &&
4848
request.tracingMode &&
49-
!['full', 'logs'].includes(request.tracingMode)
49+
!['full', 'logs', 'debug'].includes(request.tracingMode)
5050
) {
5151
traceCollector.clearTrace(span.spanContext().traceId)
5252
}
@@ -68,7 +68,7 @@ export const traceServerTime = fastifyPlugin(
6868
const spans = traceCollector.getSpansForTrace(traceId)
6969
if (spans) {
7070
try {
71-
const serverTimingHeaders = spansToServerTimings(spans)
71+
const serverTimingHeaders = spansToServerTimings(spans, reply.statusCode >= 500)
7272

7373
request.serverTimings = serverTimingHeaders
7474

@@ -94,12 +94,15 @@ export const traceServerTime = fastifyPlugin(
9494
})
9595

9696
fastify.addHook('onRequestAbort', async (req) => {
97-
const traceId = trace.getSpan(context.active())?.spanContext().traceId
97+
const span = trace.getSpan(context.active())
98+
const traceId = span?.spanContext().traceId
99+
100+
span?.setAttribute('req_aborted', true)
98101

99102
if (traceId) {
100103
const spans = traceCollector.getSpansForTrace(traceId)
101104
if (spans) {
102-
req.serverTimings = spansToServerTimings(spans)
105+
req.serverTimings = spansToServerTimings(spans, true)
103106
}
104107
traceCollector.clearTrace(traceId)
105108
}
@@ -155,6 +158,8 @@ function spansToServerTimings(
155158
spanName,
156159
duration,
157160
action: span.item.attributes['db.statement'],
161+
error: span.item.attributes.error,
162+
status: span.item.status,
158163
host: hostName
159164
? isIP(hostName)
160165
? hostName

src/http/routes/object/deleteObject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FastifyInstance } from 'fastify'
2-
import { FromSchema, JSONSchema } from 'json-schema-to-ts'
2+
import { FromSchema } from 'json-schema-to-ts'
33
import { createDefaultSchema, createResponse } from '../../routes-helper'
44
import { AuthenticatedRequest } from '../../types'
55
import { ROUTE_OPERATIONS } from '../operations'

src/http/routes/object/updateObject.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export default async function routes(fastify: FastifyInstance) {
7777
.uploadOverridingObject(request, {
7878
owner,
7979
objectName: objectName,
80+
signal: request.signals.body.signal,
8081
})
8182

8283
return response.status(objectMetadata?.httpStatusCode ?? 200).send({

src/http/routes/object/uploadSignedObject.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export default async function routes(fastify: FastifyInstance) {
9494
owner,
9595
objectName,
9696
isUpsert: upsert,
97+
signal: request.signals.body.signal,
9798
})
9899

99100
return response.status(objectMetadata?.httpStatusCode ?? 200).send({

src/http/routes/s3/commands/upload-part.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,19 @@ export default function UploadPart(s3Router: S3Router) {
9797

9898
const metadata = s3Protocol.parseMetadataHeaders(req.Headers)
9999

100-
return s3Protocol.putObject({
101-
Body: ctx.req as any,
102-
Bucket: req.Params.Bucket,
103-
Key: req.Params['*'],
104-
CacheControl: req.Headers?.['cache-control'],
105-
ContentType: req.Headers?.['content-type'],
106-
Expires: req.Headers?.['expires'] ? new Date(req.Headers?.['expires']) : undefined,
107-
ContentEncoding: req.Headers?.['content-encoding'],
108-
Metadata: metadata,
109-
})
100+
return s3Protocol.putObject(
101+
{
102+
Body: ctx.req as any,
103+
Bucket: req.Params.Bucket,
104+
Key: req.Params['*'],
105+
CacheControl: req.Headers?.['cache-control'],
106+
ContentType: req.Headers?.['content-type'],
107+
Expires: req.Headers?.['expires'] ? new Date(req.Headers?.['expires']) : undefined,
108+
ContentEncoding: req.Headers?.['content-encoding'],
109+
Metadata: metadata,
110+
},
111+
ctx.signals.body
112+
)
110113
}
111114
)
112115
}

src/http/routes/tus/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type MultiPartRequest = http.IncomingMessage & {
5959

6060
function createTusStore() {
6161
if (storageBackendType === 's3') {
62-
const agent = createAgent(storageS3Endpoint?.includes('http://') ? 'http' : 'https')
62+
const agent = createAgent('s3_tus')
6363
return new S3Store({
6464
partSize: tusPartSize * 1024 * 1024, // Each uploaded part will have ${tusPartSize}MB,
6565
expirationPeriodInMilliseconds: tusUrlExpiryMs,

src/internal/monitoring/metrics.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,28 @@ export const DbActiveConnection = new client.Gauge({
6868
help: 'Number of database connections',
6969
labelNames: ['region', 'is_external'],
7070
})
71+
72+
// Create Prometheus metrics
73+
export const HttpPoolSocketsGauge = new client.Gauge({
74+
name: 'storage_api_http_pool_busy_sockets',
75+
help: 'Number of busy sockets currently in use',
76+
labelNames: ['name', 'region', 'protocol'],
77+
})
78+
79+
export const HttpPoolFreeSocketsGauge = new client.Gauge({
80+
name: 'storage_api_http_pool_free_sockets',
81+
help: 'Number of free sockets available for reuse',
82+
labelNames: ['name', 'region', 'protocol'],
83+
})
84+
85+
export const HttpPoolPendingRequestsGauge = new client.Gauge({
86+
name: 'storage_api_http_pool_requests',
87+
help: 'Number of pending requests waiting for a socket',
88+
labelNames: ['name', 'region', 'protocol'],
89+
})
90+
91+
export const HttpPoolErrorGauge = new client.Gauge({
92+
name: 'storage_api_http_pool_errors',
93+
help: 'Number of pending requests waiting for a socket',
94+
labelNames: ['name', 'region', 'type', 'protocol'],
95+
})

0 commit comments

Comments
 (0)