18
18
package org .apache .hertzbeat .collector .collect .http ;
19
19
20
20
import static org .apache .hertzbeat .common .constants .SignConstants .RIGHT_DASH ;
21
+
21
22
import com .google .gson .JsonArray ;
22
23
import com .google .gson .JsonElement ;
23
24
import com .google .gson .JsonObject ;
24
25
import com .google .gson .JsonParser ;
26
+
25
27
import java .io .IOException ;
28
+ import java .io .InputStream ;
26
29
import java .io .InterruptedIOException ;
27
30
import java .io .StringReader ;
28
31
import java .net .ConnectException ;
34
37
import java .util .Map ;
35
38
import java .util .Objects ;
36
39
import java .util .Set ;
37
- import java .util .concurrent .ConcurrentHashMap ;
38
40
import java .util .stream .Collectors ;
39
- import java .util .stream .Stream ;
40
41
import javax .net .ssl .SSLException ;
41
42
import javax .xml .parsers .DocumentBuilder ;
42
43
import javax .xml .parsers .DocumentBuilderFactory ;
43
44
import javax .xml .xpath .XPath ;
44
45
import javax .xml .xpath .XPathConstants ;
45
46
import javax .xml .xpath .XPathExpressionException ;
46
47
import javax .xml .xpath .XPathFactory ;
48
+
47
49
import lombok .extern .slf4j .Slf4j ;
48
50
import org .apache .hertzbeat .collector .collect .AbstractCollect ;
49
51
import org .apache .hertzbeat .collector .collect .common .http .CommonHttpClient ;
50
52
import org .apache .hertzbeat .collector .collect .http .promethus .AbstractPrometheusParse ;
51
53
import org .apache .hertzbeat .collector .collect .http .promethus .PrometheusParseCreator ;
52
- import org .apache .hertzbeat .collector .collect .http . promethus . exporter . ExporterParser ;
53
- import org .apache .hertzbeat .collector .collect .http . promethus . exporter . MetricFamily ;
54
+ import org .apache .hertzbeat .collector .collect .prometheus . parser . MetricFamily ;
55
+ import org .apache .hertzbeat .collector .collect .prometheus . parser . OnlineParser ;
54
56
import org .apache .hertzbeat .collector .constants .CollectorConstants ;
55
57
import org .apache .hertzbeat .collector .dispatch .DispatchConstants ;
56
58
import org .apache .hertzbeat .collector .util .CollectUtil ;
103
105
*/
104
106
@ Slf4j
105
107
public class HttpCollectImpl extends AbstractCollect {
106
- private static final Map <Long , ExporterParser > EXPORTER_PARSER_TABLE = new ConcurrentHashMap <>();
107
- private final Set <Integer > defaultSuccessStatusCodes = Stream .of (HttpStatus .SC_OK , HttpStatus .SC_CREATED ,
108
- HttpStatus .SC_ACCEPTED , HttpStatus .SC_MULTIPLE_CHOICES , HttpStatus .SC_MOVED_PERMANENTLY ,
109
- HttpStatus .SC_MOVED_TEMPORARILY ).collect (Collectors .toSet ());
108
+ private final Set <Integer > defaultSuccessStatusCodes = Set .of (
109
+ HttpStatus .SC_OK ,
110
+ HttpStatus .SC_CREATED ,
111
+ HttpStatus .SC_ACCEPTED ,
112
+ HttpStatus .SC_MULTIPLE_CHOICES ,
113
+ HttpStatus .SC_MOVED_PERMANENTLY ,
114
+ HttpStatus .SC_MOVED_TEMPORARILY );
110
115
111
116
@ Override
112
117
public void preCheck (Metrics metrics ) throws IllegalArgumentException {
@@ -127,7 +132,7 @@ public void collect(CollectRep.MetricsData.Builder builder, Metrics metrics) {
127
132
if (CollectionUtils .isEmpty (httpProtocol .getSuccessCodes ())) {
128
133
httpProtocol .setSuccessCodes (List .of (HttpStatus .SC_OK + "" ));
129
134
}
130
-
135
+
131
136
HttpContext httpContext = createHttpContext (metrics .getHttp ());
132
137
HttpUriRequest request = createHttpRequest (metrics .getHttp ());
133
138
try (CloseableHttpResponse response = CommonHttpClient .getHttpClient ().execute (request , httpContext )) {
@@ -139,10 +144,11 @@ public void collect(CollectRep.MetricsData.Builder builder, Metrics metrics) {
139
144
builder .setMsg (NetworkConstants .STATUS_CODE + SignConstants .BLANK + statusCode );
140
145
return ;
141
146
}
142
- // todo This code converts an InputStream directly to a String. For large data in Prometheus exporters,
143
- // this could create large objects, potentially impacting JVM memory space significantly.
144
- // Option 1: Parse using InputStream, but this requires significant code changes;
145
- // Option 2: Manually trigger garbage collection, similar to how it's done in Dubbo for large inputs.
147
+ /*
148
+ this could create large objects, potentially impacting JVM memory space significantly.
149
+ Option 1: Parse using InputStream, but this requires significant code changes;
150
+ Option 2: Manually trigger garbage collection, similar to how it's done in Dubbo for large inputs.
151
+ */
146
152
String resp = EntityUtils .toString (response .getEntity (), StandardCharsets .UTF_8 );
147
153
if (!StringUtils .hasText (resp )) {
148
154
log .info ("http response entity is empty, status: {}." , statusCode );
@@ -156,7 +162,7 @@ public void collect(CollectRep.MetricsData.Builder builder, Metrics metrics) {
156
162
case DispatchConstants .PARSE_PROM_QL ->
157
163
parseResponseByPromQl (resp , metrics .getAliasFields (), metrics .getHttp (), builder );
158
164
case DispatchConstants .PARSE_PROMETHEUS ->
159
- parseResponseByPrometheusExporter (resp , metrics .getAliasFields (), builder );
165
+ parseResponseByPrometheusExporter (response . getEntity (). getContent () , metrics .getAliasFields (), builder );
160
166
case DispatchConstants .PARSE_XML_PATH ->
161
167
parseResponseByXmlPath (resp , metrics , builder , responseTime );
162
168
case DispatchConstants .PARSE_WEBSITE ->
@@ -594,36 +600,22 @@ private void parseResponseByPromQl(String resp, List<String> aliasFields, HttpPr
594
600
prometheusParser .handle (resp , aliasFields , http , builder );
595
601
}
596
602
597
- private void parseResponseByPrometheusExporter (String resp , List <String > aliasFields ,
598
- CollectRep . MetricsData . Builder builder ) {
599
- if (! EXPORTER_PARSER_TABLE . containsKey ( builder . getId () )) {
600
- EXPORTER_PARSER_TABLE . put ( builder . getId (), new ExporterParser ()) ;
603
+ private void parseResponseByPrometheusExporter (InputStream content , List <String > aliasFields , CollectRep . MetricsData . Builder builder ) throws IOException {
604
+ Map < String , MetricFamily > metricFamilyMap = OnlineParser . parseMetrics ( content );
605
+ if (metricFamilyMap == null || metricFamilyMap . isEmpty ( )) {
606
+ return ;
601
607
}
602
- ExporterParser parser = EXPORTER_PARSER_TABLE .get (builder .getId ());
603
- Map <String , MetricFamily > metricFamilyMap = parser .textToMetric (resp );
604
608
String metrics = builder .getMetrics ();
605
609
if (metricFamilyMap .containsKey (metrics )) {
606
610
MetricFamily metricFamily = metricFamilyMap .get (metrics );
607
611
for (MetricFamily .Metric metric : metricFamily .getMetricList ()) {
608
- Map <String , String > labelMap = metric .getLabelPair ()
612
+ Map <String , String > labelMap = metric .getLabels ()
609
613
.stream ()
610
614
.collect (Collectors .toMap (MetricFamily .Label ::getName , MetricFamily .Label ::getValue ));
611
615
CollectRep .ValueRow .Builder valueRowBuilder = CollectRep .ValueRow .newBuilder ();
612
616
for (String aliasField : aliasFields ) {
613
617
if ("value" .equals (aliasField )) {
614
- if (metric .getCounter () != null ) {
615
- valueRowBuilder .addColumn (String .valueOf (metric .getCounter ().getValue ()));
616
- } else if (metric .getGauge () != null ) {
617
- valueRowBuilder .addColumn (String .valueOf (metric .getGauge ().getValue ()));
618
- } else if (metric .getUntyped () != null ) {
619
- valueRowBuilder .addColumn (String .valueOf (metric .getUntyped ().getValue ()));
620
- } else if (metric .getInfo () != null ) {
621
- valueRowBuilder .addColumn (String .valueOf (metric .getInfo ().getValue ()));
622
- } else if (metric .getSummary () != null ) {
623
- valueRowBuilder .addColumn (String .valueOf (metric .getSummary ().getValue ()));
624
- } else if (metric .getHistogram () != null ) {
625
- valueRowBuilder .addColumn (String .valueOf (metric .getHistogram ().getValue ()));
626
- }
618
+ valueRowBuilder .addColumn (String .valueOf (metric .getValue ()));
627
619
} else {
628
620
String columnValue = labelMap .get (aliasField );
629
621
valueRowBuilder .addColumn (columnValue == null ? CommonConstants .NULL_VALUE : columnValue );
0 commit comments