Skip to content

Commit 36166a8

Browse files
committed
[#10] Fix: Trim URL for unnormalized case
1 parent 5fc56f3 commit 36166a8

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

packages/core/src/metrics/util.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,12 @@ export function getStatusCodeGroup(statusCode: number) {
4949
}
5050
}
5151
}
52+
53+
export const trimUrl = (url: string, maxDepth?: number) => {
54+
if (typeof maxDepth === 'number') {
55+
const withStartingSlashUrl = url.startsWith('/') ? url : '/' + url
56+
return withStartingSlashUrl.split('/', maxDepth + 1).join('/')
57+
} else {
58+
return url
59+
}
60+
}

packages/core/src/next/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import path from 'node:path'
33

44
import {glob} from 'glob'
55

6+
import {trimUrl} from '../metrics/util'
7+
68
interface RouteInfo {
79
page: string
810
regex: string
@@ -69,6 +71,6 @@ export function createNextRoutesUrlGroup(maxDepth: number) {
6971
}
7072
}
7173

72-
return typeof maxDepth === 'number' ? withoutBasePathUrl.split('/', maxDepth + 1).join('/') : withoutBasePathUrl
74+
return trimUrl(withoutBasePathUrl, maxDepth)
7375
}
7476
}

packages/hono/src/middleware/metrics.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
getStatusCodeGroup,
55
isBypassPath,
66
startTraceHistogram,
7+
trimUrl,
78
} from '@naverpay/prometheus-core'
89

910
import type {HonoPrometheusExporterOptions} from '../types'
@@ -29,7 +30,7 @@ export function getHonoMetricsMiddleware({
2930

3031
const extendedNormalizePath = (context: Context) => {
3132
const url = new URL(context.req.url)
32-
return normalizeNextRoutesPath?.(url.href) || normalizePath?.(context) || url.pathname
33+
return normalizeNextRoutesPath?.(url.href) || normalizePath?.(context) || trimUrl(url.pathname, maxDepth)
3334
}
3435

3536
return async (context, next) => {

packages/koa/src/middleware/metrics.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
getStatusCodeGroup,
55
isBypassPath,
66
startTraceHistogram,
7+
trimUrl,
78
} from '@naverpay/prometheus-core'
89
import onFinished from 'on-finished'
910

@@ -29,7 +30,7 @@ export function getKoaMetricsMiddleware({
2930
const normalizeNextRoutesPath = nextjs ? createNextRoutesUrlGroup(maxDepth) : undefined
3031

3132
const extendedNormalizePath = (context: Context) => {
32-
return normalizeNextRoutesPath?.(context.url) || normalizePath?.(context) || context.path
33+
return normalizeNextRoutesPath?.(context.url) || normalizePath?.(context) || trimUrl(context.path)
3334
}
3435

3536
return async (context, next) => {

0 commit comments

Comments
 (0)