axios-prometheus-adapter 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.createAxiosPrometheusMiddleware = void 0;
7
7
  const prom_client_1 = __importDefault(require("prom-client"));
8
+ // Path normalizer to prevent unbounded cardinality in metrics
9
+ // Replaces dynamic segments (numbers, UUIDs, etc.) with placeholders
10
+ const normalizePath = (path) => {
11
+ return path
12
+ // Replace UUIDs (8-4-4-4-12 format)
13
+ .replace(/\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi, '/:uuid')
14
+ // Replace any numeric IDs in paths
15
+ .replace(/\/[0-9]+(?=\/|$)/g, '/:id');
16
+ };
8
17
  const createAxiosPrometheusMiddleware = (axiosInstance, registry, config) => {
9
18
  const clientRequestDuration = new prom_client_1.default.Histogram({
10
19
  name: 'http_client_requests_seconds',
@@ -20,13 +29,15 @@ const createAxiosPrometheusMiddleware = (axiosInstance, registry, config) => {
20
29
  };
21
30
  return config;
22
31
  });
32
+ // eslint-disable-next-line complexity
23
33
  axiosInstance.interceptors.response.use((response) => {
34
+ var _a, _b;
24
35
  const labels = {
25
- status_code: response.status,
26
- method: response.request.method,
27
- protocol: response.request.protocol,
28
- host: response.request.host,
29
- path: response.request.path.split('?')[0],
36
+ status_code: response.status.toString(),
37
+ method: response.request.method || ((_a = response.config.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || 'UNKNOWN',
38
+ protocol: response.request.protocol || 'https:',
39
+ host: response.request.host || new URL(response.config.url || '').hostname,
40
+ path: normalizePath((_b = response.request.path) === null || _b === void 0 ? void 0 : _b.split('?')[0]),
30
41
  };
31
42
  response.config.metadata.endTimer(labels);
32
43
  return response;
@@ -34,29 +45,29 @@ const createAxiosPrometheusMiddleware = (axiosInstance, registry, config) => {
34
45
  let labels;
35
46
  if (error.response) {
36
47
  labels = {
37
- status_code: error.response.status,
48
+ status_code: error.response.status.toString(),
38
49
  method: error.response.request.method,
39
50
  protocol: error.response.request.protocol,
40
51
  host: error.response.request.host,
41
- path: error.response.request.path.split('?')[0],
52
+ path: normalizePath(error.response.request.path.split('?')[0]),
42
53
  };
43
54
  }
44
55
  else if (error.request._currentRequest) {
45
56
  labels = {
46
- status_code: error.status,
57
+ status_code: error.status.toString(),
47
58
  method: error.request._currentRequest.method,
48
59
  protocol: error.request._currentRequest.protocol,
49
60
  host: error.request._currentRequest.host,
50
- path: error.request._currentRequest.path.split('?')[0],
61
+ path: normalizePath(error.request._currentRequest.path.split('?')[0]),
51
62
  };
52
63
  }
53
64
  else {
54
65
  labels = {
55
66
  status_code: 500,
56
- method: "UNKNOWN",
57
- protocol: "UNKNOWN",
58
- host: "UNKNOWN",
59
- path: "UNKNOWN",
67
+ method: 'UNKNOWN',
68
+ protocol: 'UNKNOWN',
69
+ host: 'UNKNOWN',
70
+ path: 'UNKNOWN',
60
71
  };
61
72
  }
62
73
  error.config.metadata.endTimer(labels);
@@ -1 +1 @@
1
- {"version":3,"file":"AxiosPrometheusAdapter.js","sourceRoot":"","sources":["../../lib/AxiosPrometheusAdapter.ts"],"names":[],"mappings":";;;;;;AAEA,8DAAiC;AAQ1B,MAAM,+BAA+B,GAAG,CAC7C,aAA4B,EAC5B,QAAkB,EAClB,MAAqC,EAC/B,EAAE;IACR,MAAM,qBAAqB,GAAG,IAAI,qBAAM,CAAC,SAAS,CAAC;QACjD,IAAI,EAAE,8BAA8B;QACpC,IAAI,EAAE,2BAA2B;QACjC,UAAU,EAAE,CAAC,aAAa,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,CAAC;QACjE,GAAG,MAAM;KACV,CAAC,CAAC;IAEH,QAAQ,CAAC,cAAc,CAAC,qBAAqB,CAAC,CAAC;IAE/C,aAAa,CAA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C,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAhEW,QAAA,+BAA+B,mCAgE1C"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "axios-prometheus-adapter",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "Provides prometheus metrics for outgoing axios requests",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -11,7 +11,7 @@
11
11
  ],
12
12
  "scripts": {
13
13
  "build": "tsc -p tsconfig.prod.json",
14
- "start": "ts-node-dev examples/index.ts",
14
+ "start": "ts-node-dev examples/axiosRequest.ts",
15
15
  "test": "jest"
16
16
  },
17
17
  "repository": {
package/readme.md CHANGED
@@ -63,34 +63,20 @@ Output:
63
63
  ...
64
64
  # HELP http_client_requests_seconds_count http_client_requests_seconds_count
65
65
  # TYPE http_client_requests_seconds_count histogram
66
- http_client_requests_seconds_bucket{le="0.005",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 0
67
- http_client_requests_seconds_bucket{le="0.01",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 0
68
- http_client_requests_seconds_bucket{le="0.025",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 0
69
- http_client_requests_seconds_bucket{le="0.05",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 0
70
- http_client_requests_seconds_bucket{le="0.1",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 0
71
- http_client_requests_seconds_bucket{le="0.25",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 0
72
- http_client_requests_seconds_bucket{le="0.5",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 1
73
- http_client_requests_seconds_bucket{le="1",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 1
74
- http_client_requests_seconds_bucket{le="2.5",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 1
75
- http_client_requests_seconds_bucket{le="5",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 1
76
- http_client_requests_seconds_bucket{le="10",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 1
77
- http_client_requests_seconds_bucket{le="+Inf",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 1
78
- http_client_requests_seconds_sum{status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 0.257366432
79
- http_client_requests_seconds_count{status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 1
80
- http_client_requests_seconds_bucket{le="0.005",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 0
81
- http_client_requests_seconds_bucket{le="0.01",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 0
82
- http_client_requests_seconds_bucket{le="0.025",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 0
83
- http_client_requests_seconds_bucket{le="0.05",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 0
84
- http_client_requests_seconds_bucket{le="0.1",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 0
85
- http_client_requests_seconds_bucket{le="0.25",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 1
86
- http_client_requests_seconds_bucket{le="0.5",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 1
87
- http_client_requests_seconds_bucket{le="1",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 1
88
- http_client_requests_seconds_bucket{le="2.5",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 1
89
- http_client_requests_seconds_bucket{le="5",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 1
90
- http_client_requests_seconds_bucket{le="10",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 1
91
- http_client_requests_seconds_bucket{le="+Inf",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 1
92
- http_client_requests_seconds_sum{status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 0.107517055
93
- http_client_requests_seconds_count{status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 1
66
+ http_client_requests_seconds_bucket{le="0.005",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/:id"} 0
67
+ http_client_requests_seconds_bucket{le="0.01",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/:id"} 0
68
+ http_client_requests_seconds_bucket{le="0.025",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/:id"} 0
69
+ http_client_requests_seconds_bucket{le="0.05",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/:id"} 0
70
+ http_client_requests_seconds_bucket{le="0.1",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/:id"} 0
71
+ http_client_requests_seconds_bucket{le="0.25",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/:id"} 0
72
+ http_client_requests_seconds_bucket{le="0.5",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/:id"} 1
73
+ http_client_requests_seconds_bucket{le="1",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/:id"} 1
74
+ http_client_requests_seconds_bucket{le="2.5",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/:id"} 2
75
+ http_client_requests_seconds_bucket{le="5",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/:id"} 2
76
+ http_client_requests_seconds_bucket{le="10",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/:id"} 2
77
+ http_client_requests_seconds_bucket{le="+Inf",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/:id"} 2
78
+ http_client_requests_seconds_sum{status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/:id"} 1.372366292
79
+ http_client_requests_seconds_count{status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/:id"} 2
94
80
  ```
95
81
 
96
82
  ### Custom config