Skip to content

Commit

Permalink
fix: handle remote attack and manage 404 status
Browse files Browse the repository at this point in the history
  • Loading branch information
wallet77 committed May 27, 2024
1 parent 38c0b1b commit 41250c2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ It works with different HTTP servers:

By default you can access your metrics on this endpoint: http://localhost:9350/metrics

In order to handle remote attack and to avoid high cardinality, the default behavior of this lib is to
override the `path` label in case of HTTP code 404.

# Installation

```console
Expand Down
4 changes: 4 additions & 0 deletions src/http_hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ const init = function (client, config) {
status: res.statusCode
}

if (res.statusCode === 404) {
labels.route = '/404' // avoid high cardinality in case of remote attack
}

const spanContext = opentelemetry.trace.getSpanContext(opentelemetry.context.active())
const traceId = spanContext && spanContext.traceId
exemplarLabels.traceID = traceId
Expand Down
10 changes: 10 additions & 0 deletions tests/http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ describe('retry', () => {
expect(data.indexOf('http_request_duration_seconds_count{method="GET",route="/test/1234",status="200"} 20') > -1).toEqual(true)
})

it('should handle 404', async () => {
try {
await httpRequest('http://localhost:3000/unknown')
} catch (err) {
expect(err.message).toEqual('statusCode=404')
const data = await httpRequest('http://localhost:9350/metrics')
expect(data.indexOf('http_request_duration_seconds_count{method="GET",route="/404",status="404"}') > -1).toEqual(true)
}
})

it('should return 404', async () => {
try {
await httpRequest('http://localhost:9350/unknown')
Expand Down

0 comments on commit 41250c2

Please sign in to comment.