@@ -10,12 +10,16 @@ import {
1010import { RuntimeDataSource , sceneGraph } from '@grafana/scenes' ;
1111import { isPrivateLabel } from '@shared/components/QueryBuilder/domain/helpers/isPrivateLabel' ;
1212import { labelsRepository } from '@shared/infrastructure/labels/labelsRepository' ;
13+ import pLimit from 'p-limit' ;
1314
1415import { GroupByVariable } from '../../domain/variables/GroupByVariable/GroupByVariable' ;
1516import { computeRoundedTimeRange } from '../../helpers/computeRoundedTimeRange' ;
1617import { PYROSCOPE_LABELS_DATA_SOURCE } from '../pyroscope-data-sources' ;
1718import { LabelsApiClient } from './http/LabelsApiClient' ;
1819
20+ const MAX_CONCURRENT_LABEL_VALUES_REQUESTS = 20 ;
21+ const limit = pLimit ( MAX_CONCURRENT_LABEL_VALUES_REQUESTS ) ;
22+
1923export 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
0 commit comments