Skip to content

Commit cb8149c

Browse files
authored
fix(LabelsDataSource): Limit the maximum number of concurrent requests to fetch label values (#165)
1 parent d3fe883 commit cb8149c

File tree

3 files changed

+48
-28
lines changed

3 files changed

+48
-28
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"file-saver": "^2.0.5",
5353
"markdown-to-jsx": "^7.3.2",
5454
"nanoid": "^4.0.2",
55+
"p-limit": "^6.1.0",
5556
"protobufjs": "^7.2.6",
5657
"react": "18.2.0",
5758
"react-dom": "18.2.0",

src/pages/ProfilesExplorerView/infrastructure/labels/LabelsDataSource.ts

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ import {
1010
import { RuntimeDataSource, sceneGraph } from '@grafana/scenes';
1111
import { isPrivateLabel } from '@shared/components/QueryBuilder/domain/helpers/isPrivateLabel';
1212
import { labelsRepository } from '@shared/infrastructure/labels/labelsRepository';
13+
import pLimit from 'p-limit';
1314

1415
import { GroupByVariable } from '../../domain/variables/GroupByVariable/GroupByVariable';
1516
import { computeRoundedTimeRange } from '../../helpers/computeRoundedTimeRange';
1617
import { PYROSCOPE_LABELS_DATA_SOURCE } from '../pyroscope-data-sources';
1718
import { LabelsApiClient } from './http/LabelsApiClient';
1819

20+
const MAX_CONCURRENT_LABEL_VALUES_REQUESTS = 20;
21+
const limit = pLimit(MAX_CONCURRENT_LABEL_VALUES_REQUESTS);
22+
1923
export class LabelsDataSource extends RuntimeDataSource {
2024
static MAX_TIMESERIES_LABEL_VALUES = 10;
2125

@@ -91,34 +95,32 @@ export class LabelsDataSource extends RuntimeDataSource {
9195

9296
labelsRepository.setApiClient(new LabelsApiClient({ dataSourceUid }));
9397

94-
const labels = await labelsRepository.listLabels({ query, from, to });
95-
96-
const sortedLabelsWithCounts = (
97-
await Promise.all(
98-
labels
99-
.filter(({ value }) => !isPrivateLabel(value))
100-
.sort((a, b) => a.label.localeCompare(b.label))
101-
.map(async ({ value }) => {
102-
const values = (await labelsRepository.listLabelValues({ query, from, to, label: value })).map(
103-
({ value }) => value
104-
);
105-
const count = values.length;
106-
107-
return {
108-
// TODO: check if there's a better way
109-
value: JSON.stringify({
110-
value,
111-
groupBy: {
112-
label: value,
113-
values,
114-
},
115-
}),
116-
text: `${value} (${count})`,
117-
count,
118-
};
119-
})
98+
const labels = (await labelsRepository.listLabels({ query, from, to })).filter(
99+
({ value }) => !isPrivateLabel(value)
100+
);
101+
102+
const labelsWithValuesAndCount = await Promise.all(
103+
labels.map(({ value }) =>
104+
limit(async () => {
105+
const values = await labelsRepository.listLabelValues({ query, from, to, label: value });
106+
const count = values.length;
107+
return {
108+
// TODO: check if there's a better way
109+
value: JSON.stringify({
110+
value,
111+
groupBy: {
112+
label: value,
113+
values,
114+
},
115+
}),
116+
text: `${value} (${count})`,
117+
count,
118+
};
119+
})
120120
)
121-
)
121+
);
122+
123+
const sortedLabels = labelsWithValuesAndCount
122124
.sort((a, b) => b.count - a.count)
123125
.map(({ value, text }) => ({ value, text }));
124126

@@ -128,7 +130,7 @@ export class LabelsDataSource extends RuntimeDataSource {
128130
value: 'all',
129131
text: 'All',
130132
},
131-
...sortedLabelsWithCounts,
133+
...sortedLabels,
132134
];
133135
}
134136

yarn.lock

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9998,6 +9998,15 @@ __metadata:
99989998
languageName: node
99999999
linkType: hard
1000010000

10001+
"p-limit@npm:^6.1.0":
10002+
version: 6.1.0
10003+
resolution: "p-limit@npm:6.1.0"
10004+
dependencies:
10005+
yocto-queue: "npm:^1.1.1"
10006+
checksum: 10/9670cb4426d77c20a477cad0e44e8207248381135650f5b9bfe039c9d93566cd4b05cfc557fe60f2db88c799991c7edc6be436255d287a87a605ee604a71cd91
10007+
languageName: node
10008+
linkType: hard
10009+
1000110010
"p-locate@npm:^4.1.0":
1000210011
version: 4.1.0
1000310012
resolution: "p-locate@npm:4.1.0"
@@ -10581,6 +10590,7 @@ __metadata:
1058110590
lint-staged: "npm:^15.0.2"
1058210591
markdown-to-jsx: "npm:^7.3.2"
1058310592
nanoid: "npm:^4.0.2"
10593+
p-limit: "npm:^6.1.0"
1058410594
prettier: "npm:^2.5.0"
1058510595
prettier-package-json: "npm:^2.8.0"
1058610596
protobufjs: "npm:^7.2.6"
@@ -14054,6 +14064,13 @@ __metadata:
1405414064
languageName: node
1405514065
linkType: hard
1405614066

14067+
"yocto-queue@npm:^1.1.1":
14068+
version: 1.1.1
14069+
resolution: "yocto-queue@npm:1.1.1"
14070+
checksum: 10/f2e05b767ed3141e6372a80af9caa4715d60969227f38b1a4370d60bffe153c9c5b33a862905609afc9b375ec57cd40999810d20e5e10229a204e8bde7ef255c
14071+
languageName: node
14072+
linkType: hard
14073+
1405714074
"zone.js@npm:^0.11.0":
1405814075
version: 0.11.8
1405914076
resolution: "zone.js@npm:0.11.8"

0 commit comments

Comments
 (0)