• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

supabase / storage / 22971456993

11 Mar 2026 07:49PM UTC coverage: 76.296% (+0.09%) from 76.204%
22971456993

push

github

web-flow
fix: improve performance, upgraded otel fastify (#902)

* fix: improve performance, upgraded otel fastify

* fix: Update monitoring/grafana/dashboards/storage-otel.json

4029 of 5727 branches covered (70.35%)

Branch coverage included in aggregate %.

280 of 368 new or added lines in 7 files covered. (76.09%)

5 existing lines in 4 files now uncovered.

26790 of 34667 relevant lines covered (77.28%)

185.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.88
/src/http/plugins/log-request.ts
1
import { logger, logSchema, redactQueryParamFromRequest } from '@internal/monitoring'
2✔
2
import { FastifyReply } from 'fastify/types/reply'
2✔
3
import { FastifyRequest } from 'fastify/types/request'
2✔
4
import fastifyPlugin from 'fastify-plugin'
2✔
5

2✔
6
interface RequestLoggerOptions {
2✔
7
  excludeUrls?: string[]
2✔
8
}
2✔
9

2✔
10
declare module 'fastify' {
2✔
11
  interface FastifyRequest {
2✔
12
    executionError?: Error
2✔
13
    operation?: { type: string }
2✔
14
    resources?: string[]
2✔
15
    startTime: number
2✔
16
    executionTime?: number
2✔
17
  }
2✔
18

2✔
19
  interface FastifyContextConfig {
2✔
20
    operation?: { type: string }
2✔
21
    resources?: (req: FastifyRequest<any>) => string[]
2✔
22
    logMetadata?: (req: FastifyRequest<any>) => Record<string, unknown>
2✔
23
  }
2✔
24
}
2✔
25

2✔
26
/**
2✔
27
 * Request logger plugin
2✔
28
 * @param options
2✔
29
 */
2✔
30
export const logRequest = (options: RequestLoggerOptions) =>
2✔
31
  fastifyPlugin(
266✔
32
    async (fastify) => {
266✔
33
      fastify.addHook('onRequest', async (req, res) => {
264✔
34
        req.startTime = Date.now()
284✔
35

284✔
36
        res.raw.once('close', () => {
284✔
37
          if (req.raw.aborted) {
284!
38
            doRequestLog(req, {
×
39
              excludeUrls: options.excludeUrls,
×
40
              statusCode: 'ABORTED REQ',
×
41
              responseTime: (Date.now() - req.startTime) / 1000,
×
42
            })
×
43
            return
×
44
          }
×
45

284✔
46
          if (!res.raw.writableFinished) {
284✔
47
            doRequestLog(req, {
284✔
48
              excludeUrls: options.excludeUrls,
284✔
49
              statusCode: 'ABORTED RES',
284✔
50
              responseTime: (Date.now() - req.startTime) / 1000,
284✔
51
            })
284✔
52
          }
284✔
53
        })
284✔
54
      })
264✔
55

264✔
56
      /**
264✔
57
       * Adds req.resources and req.operation to the request object
264✔
58
       */
264✔
59
      fastify.addHook('preHandler', async (req) => {
264✔
60
        const resourceFromParams = Object.values(req.params || {}).join('/')
250!
61
        const resources = getFirstDefined<string[]>(
250✔
62
          req.resources,
250✔
63
          req.routeOptions.config.resources?.(req),
250✔
64
          (req.raw as any).resources,
250✔
65
          resourceFromParams ? [resourceFromParams] : ([] as string[])
250✔
66
        )
250✔
67

250✔
68
        if (resources && resources.length > 0) {
250✔
69
          resources.map((resource, index) => {
250✔
70
            if (!resource.startsWith('/')) {
80,292✔
71
              resources[index] = `/${resource}`
80,292✔
72
            }
80,292✔
73
          })
250✔
74
        }
250✔
75

250✔
76
        req.resources = resources
250✔
77
        req.operation = req.routeOptions.config.operation
250✔
78

250✔
79
        if (req.operation && typeof req.opentelemetry === 'function') {
250!
NEW
80
          req.opentelemetry()?.span?.setAttribute('http.operation', req.operation.type)
×
UNCOV
81
        }
×
82
      })
264✔
83

264✔
84
      fastify.addHook('onSend', async (req, _, payload) => {
264✔
85
        req.executionTime = Date.now() - req.startTime
284✔
86
        return payload
284✔
87
      })
264✔
88

264✔
89
      fastify.addHook('onResponse', async (req, reply) => {
264✔
90
        doRequestLog(req, {
284✔
91
          reply,
284✔
92
          excludeUrls: options.excludeUrls,
284✔
93
          statusCode: reply.statusCode,
284✔
94
          responseTime: reply.elapsedTime,
284✔
95
          executionTime: req.executionTime,
284✔
96
        })
284✔
97
      })
264✔
98
    },
264✔
99
    { name: 'log-request' }
266✔
100
  )
2✔
101

2✔
102
interface LogRequestOptions {
2✔
103
  reply?: FastifyReply
2✔
104
  excludeUrls?: string[]
2✔
105
  statusCode: number | 'ABORTED REQ' | 'ABORTED RES'
2✔
106
  responseTime: number
2✔
107
  executionTime?: number
2✔
108
}
2✔
109

2✔
110
function doRequestLog(req: FastifyRequest, options: LogRequestOptions) {
568✔
111
  if (options.excludeUrls?.includes(req.url)) {
568!
112
    return
×
113
  }
×
114

568✔
115
  const rMeth = req.method
568✔
116
  const rUrl = redactQueryParamFromRequest(req, [
568✔
117
    'token',
568✔
118
    'X-Amz-Credential',
568✔
119
    'X-Amz-Signature',
568✔
120
    'X-Amz-Security-Token',
568✔
121
  ])
568✔
122
  const uAgent = req.headers['user-agent']
568✔
123
  const rId = req.id
568✔
124
  const cIP = req.ip
568✔
125
  const statusCode = options.statusCode
568✔
126
  const error = (req.raw as any).executionError || req.executionError
568✔
127
  const tenantId = req.tenantId
568✔
128

568✔
129
  let reqMetadata: Record<string, unknown> = {}
568✔
130

568✔
131
  if (req.routeOptions.config.logMetadata) {
568✔
132
    try {
44✔
133
      reqMetadata = req.routeOptions.config.logMetadata(req)
44✔
134

44✔
135
      if (reqMetadata) {
44✔
136
        try {
44✔
137
          if (typeof req.opentelemetry === 'function') {
44!
NEW
138
            req.opentelemetry()?.span?.setAttribute('http.metadata', JSON.stringify(reqMetadata))
×
NEW
139
          }
×
140
        } catch (e) {
44!
141
          // do nothing
×
142
          logSchema.warning(logger, 'Failed to serialize log metadata', {
×
143
            type: 'otel',
×
144
            error: e,
×
145
          })
×
146
        }
×
147
      }
44✔
148
    } catch (e) {
44!
149
      logSchema.error(logger, 'Failed to get log metadata', {
×
150
        type: 'request',
×
151
        error: e,
×
152
      })
×
153
    }
×
154
  }
44✔
155

568✔
156
  const buildLogMessage = `${tenantId} | ${rMeth} | ${statusCode} | ${cIP} | ${rId} | ${rUrl} | ${uAgent}`
568✔
157

568✔
158
  logSchema.request(req.log, buildLogMessage, {
568✔
159
    type: 'request',
568✔
160
    req,
568✔
161
    reqMetadata,
568✔
162
    res: options.reply,
568✔
163
    responseTime: options.responseTime,
568✔
164
    executionTime: options.executionTime,
568✔
165
    error,
568✔
166
    owner: req.owner,
568✔
167
    role: req.jwtPayload?.role,
568✔
168
    resources: req.resources,
568✔
169
    operation: req.operation?.type ?? req.routeOptions.config.operation?.type,
568✔
170
    serverTimes: req.serverTimings,
568✔
171
  })
568✔
172
}
568✔
173

2✔
174
function getFirstDefined<T>(...values: any[]): T | undefined {
250✔
175
  for (const value of values) {
250✔
176
    if (value !== undefined) {
892✔
177
      return value
250✔
178
    }
250✔
179
  }
892✔
180
  return undefined
×
181
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc