Skip to content

Commit 28e01e9

Browse files
committed
Merge remote-tracking branch 'origin/master' into feat/disableMemoFlag
2 parents fb4268e + dcda99c commit 28e01e9

File tree

6 files changed

+81
-46
lines changed

6 files changed

+81
-46
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
### Added
1111

1212
- Allow disabling memoization for all requests of a client
13+
## [6.45.23] - 2023-10-04
14+
15+
### Fixed
16+
- Fix metrics being restricted by tracing restrictions
1317

1418
## [6.45.22] - 2023-09-20
1519

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vtex/api",
3-
"version": "6.45.22",
3+
"version": "6.45.23",
44
"description": "VTEX I/O API client",
55
"main": "lib/index.js",
66
"typings": "lib/index.d.ts",
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { finished as onStreamFinished } from 'stream'
2+
import { hrToMillisFloat } from '../../utils'
3+
import {
4+
createConcurrentRequestsInstrument,
5+
createRequestsResponseSizesInstrument,
6+
createRequestsTimingsInstrument,
7+
createTotalAbortedRequestsInstrument,
8+
createTotalRequestsInstrument,
9+
RequestsMetricLabels,
10+
} from '../tracing/metrics/instruments'
11+
import { ServiceContext } from '../worker/runtime/typings'
12+
13+
14+
export const addRequestMetricsMiddleware = () => {
15+
const concurrentRequests = createConcurrentRequestsInstrument()
16+
const requestTimings = createRequestsTimingsInstrument()
17+
const totalRequests = createTotalRequestsInstrument()
18+
const responseSizes = createRequestsResponseSizesInstrument()
19+
const abortedRequests = createTotalAbortedRequestsInstrument()
20+
21+
return async function addRequestMetrics(ctx: ServiceContext, next: () => Promise<void>) {
22+
const start = process.hrtime()
23+
concurrentRequests.inc(1)
24+
25+
ctx.req.once('aborted', () =>
26+
abortedRequests.inc({ [RequestsMetricLabels.REQUEST_HANDLER]: ctx.requestHandlerName }, 1)
27+
)
28+
29+
let responseClosed = false
30+
ctx.res.once('close', () => (responseClosed = true))
31+
32+
try {
33+
await next()
34+
} finally {
35+
const responseLength = ctx.response.length
36+
if (responseLength) {
37+
responseSizes.observe(
38+
{ [RequestsMetricLabels.REQUEST_HANDLER]: ctx.requestHandlerName },
39+
responseLength
40+
)
41+
}
42+
43+
totalRequests.inc(
44+
{
45+
[RequestsMetricLabels.REQUEST_HANDLER]: ctx.requestHandlerName,
46+
[RequestsMetricLabels.STATUS_CODE]: ctx.response.status,
47+
},
48+
1
49+
)
50+
51+
const onResFinished = () => {
52+
requestTimings.observe(
53+
{
54+
[RequestsMetricLabels.REQUEST_HANDLER]: ctx.requestHandlerName,
55+
},
56+
hrToMillisFloat(process.hrtime(start))
57+
)
58+
59+
concurrentRequests.dec(1)
60+
}
61+
62+
if (responseClosed) {
63+
onResFinished()
64+
} else {
65+
onStreamFinished(ctx.res, onResFinished)
66+
}
67+
}
68+
}
69+
}
70+

src/service/tracing/tracingMiddlewares.ts

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,19 @@ import { RuntimeLogFields } from '../../tracing/LogFields'
77
import { CustomHttpTags, OpentracingTags, VTEXIncomingRequestTags } from '../../tracing/Tags'
88
import { UserLandTracer } from '../../tracing/UserLandTracer'
99
import { cloneAndSanitizeHeaders } from '../../tracing/utils'
10-
import { hrToMillis, hrToMillisFloat } from '../../utils'
10+
import { hrToMillis } from '../../utils'
1111
import { ServiceContext } from '../worker/runtime/typings'
12-
import {
13-
createConcurrentRequestsInstrument,
14-
createRequestsResponseSizesInstrument,
15-
createRequestsTimingsInstrument,
16-
createTotalAbortedRequestsInstrument,
17-
createTotalRequestsInstrument,
18-
RequestsMetricLabels,
19-
} from './metrics/instruments'
2012

2113
const PATHS_BLACKLISTED_FOR_TRACING = ['/_status', '/healthcheck']
2214

2315
export const addTracingMiddleware = (tracer: Tracer) => {
24-
const concurrentRequests = createConcurrentRequestsInstrument()
25-
const requestTimings = createRequestsTimingsInstrument()
26-
const totalRequests = createTotalRequestsInstrument()
27-
const responseSizes = createRequestsResponseSizesInstrument()
28-
const abortedRequests = createTotalAbortedRequestsInstrument()
2916

3017
return async function addTracing(ctx: ServiceContext, next: () => Promise<void>) {
31-
const start = process.hrtime()
32-
concurrentRequests.inc(1)
3318
const rootSpan = tracer.extract(FORMAT_HTTP_HEADERS, ctx.request.headers) as undefined | SpanContext
3419
ctx.tracing = { tracer, currentSpan: undefined}
3520

3621
if (!shouldTrace(ctx, rootSpan)) {
3722
await next()
38-
concurrentRequests.dec(1)
3923
return
4024
}
4125

@@ -47,9 +31,6 @@ export const addTracingMiddleware = (tracer: Tracer) => {
4731
const initialSamplingDecision = getTraceInfo(currentSpan).isSampled
4832

4933
ctx.tracing = { currentSpan, tracer }
50-
ctx.req.once('aborted', () =>
51-
abortedRequests.inc({ [RequestsMetricLabels.REQUEST_HANDLER]: (currentSpan as any).operationName as string }, 1)
52-
)
5334

5435
let responseClosed = false
5536
ctx.res.once('close', () => (responseClosed = true))
@@ -60,22 +41,6 @@ export const addTracingMiddleware = (tracer: Tracer) => {
6041
ErrorReport.create({ originalError: err }).injectOnSpan(currentSpan, ctx.vtex?.logger)
6142
throw err
6243
} finally {
63-
const responseLength = ctx.response.length
64-
if (responseLength) {
65-
responseSizes.observe(
66-
{ [RequestsMetricLabels.REQUEST_HANDLER]: (currentSpan as any).operationName as string },
67-
responseLength
68-
)
69-
}
70-
71-
totalRequests.inc(
72-
{
73-
[RequestsMetricLabels.REQUEST_HANDLER]: (currentSpan as any).operationName as string,
74-
[RequestsMetricLabels.STATUS_CODE]: ctx.response.status,
75-
},
76-
1
77-
)
78-
7944
const traceInfo = getTraceInfo(currentSpan)
8045
if (traceInfo?.isSampled) {
8146
if (!initialSamplingDecision) {
@@ -98,14 +63,6 @@ export const addTracingMiddleware = (tracer: Tracer) => {
9863
}
9964

10065
const onResFinished = () => {
101-
requestTimings.observe(
102-
{
103-
[RequestsMetricLabels.REQUEST_HANDLER]: (currentSpan as any).operationName as string,
104-
},
105-
hrToMillisFloat(process.hrtime(start))
106-
)
107-
108-
concurrentRequests.dec(1)
10966
currentSpan?.finish()
11067
}
11168

@@ -120,7 +77,8 @@ export const addTracingMiddleware = (tracer: Tracer) => {
12077

12178
export const nameSpanOperationMiddleware = (operationType: string, operationName: string) => {
12279
return function nameSpanOperation(ctx: ServiceContext, next: () => Promise<void>) {
123-
ctx.tracing?.currentSpan?.setOperationName(`${operationType}:${operationName}`)
80+
ctx.requestHandlerName = `${operationType}:${operationName}`
81+
ctx.tracing?.currentSpan?.setOperationName(ctx.requestHandlerName)
12482
return next()
12583
}
12684
}

src/service/worker/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { MetricsAccumulator } from '../../metrics/MetricsAccumulator'
1212
import { getService } from '../loaders'
1313
import { logOnceToDevConsole } from '../logger/console'
1414
import { LogLevel } from '../logger/logger'
15+
import { addRequestMetricsMiddleware } from '../metrics/requestMetricsMiddleware'
1516
import { TracerSingleton } from '../tracing/TracerSingleton'
1617
import { addTracingMiddleware } from '../tracing/tracingMiddlewares'
1718
import { addProcessListeners, logger } from './listeners'
@@ -221,6 +222,7 @@ export const startWorker = (serviceJSON: ServiceJSON) => {
221222
.use(error)
222223
.use(prometheusLoggerMiddleware())
223224
.use(addTracingMiddleware(tracer))
225+
.use(addRequestMetricsMiddleware())
224226
.use(addMetricsLoggerMiddleware())
225227
.use(concurrentRateLimiter(serviceJSON?.rateLimitPerReplica?.concurrent))
226228
.use(compress())

src/service/worker/runtime/typings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface Context<T extends IOClients> {
3737
timings: Record<string, [number, number]>
3838
metrics: Record<string, [number, number]>
3939
previousTimerStart: [number, number]
40+
requestHandlerName?: string
4041
serverTiming?: ServerTiming
4142
tracing?: TracingContext
4243
}

0 commit comments

Comments
 (0)