Skip to content

Commit fbf7872

Browse files
authored
[Fix][Connector-V2] Fix prometheus check time can not parse double value (#9311)
1 parent 6fafe6f commit fbf7872

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

seatunnel-connectors-v2/connector-prometheus/src/main/java/org/apache/seatunnel/connectors/seatunnel/prometheus/config/PrometheusSourceParameter.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.seatunnel.connectors.seatunnel.http.config.HttpRequestMethod;
2424
import org.apache.seatunnel.connectors.seatunnel.prometheus.Exception.PrometheusConnectorException;
2525

26+
import java.time.Instant;
2627
import java.time.ZonedDateTime;
2728
import java.time.format.DateTimeFormatter;
2829
import java.time.format.DateTimeParseException;
@@ -76,14 +77,18 @@ private String checkTimeParam(String time) {
7677
if (isValidISO8601(time)) {
7778
return time;
7879
}
79-
throw new PrometheusConnectorException(
80-
CommonErrorCode.UNSUPPORTED_DATA_TYPE, "unsupported time type");
80+
try {
81+
Double.parseDouble(time);
82+
return time;
83+
} catch (NumberFormatException e) {
84+
throw new PrometheusConnectorException(
85+
CommonErrorCode.UNSUPPORTED_DATA_TYPE, "unsupported time type");
86+
}
8187
}
8288

8389
private boolean isValidISO8601(String dateTimeString) {
8490
try {
85-
DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT;
86-
ZonedDateTime.parse(dateTimeString, formatter);
91+
Instant.parse(dateTimeString);
8792
return true;
8893
} catch (DateTimeParseException e) {
8994
return false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.seatunnel.connectors.seatunnel.prometheus;
19+
20+
import org.apache.seatunnel.api.configuration.ReadonlyConfig;
21+
import org.apache.seatunnel.connectors.seatunnel.prometheus.config.PrometheusSourceParameter;
22+
23+
import org.junit.jupiter.api.Assertions;
24+
import org.junit.jupiter.api.Test;
25+
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
29+
public class PrometheusParamCheckTest {
30+
31+
@Test
32+
public void checkTime() {
33+
final PrometheusSourceParameter prometheusSourceParameter = new PrometheusSourceParameter();
34+
Map<String, Object> map1 = new HashMap<>();
35+
map1.put("url", "http://localhost:9090");
36+
map1.put("query", "node_cpu_seconds_total");
37+
map1.put("query_type", "Range");
38+
map1.put("start", "2025-05-13T02:25:23Z");
39+
map1.put("end", "2025-05-13T02:25:23.001Z");
40+
prometheusSourceParameter.buildWithConfig(ReadonlyConfig.fromMap(map1));
41+
42+
Map<String, Object> map2 = new HashMap<>();
43+
map2.put("url", "http://localhost:9090");
44+
map2.put("query", "node_cpu_seconds_total");
45+
map2.put("query_type", "Range");
46+
map2.put("start", "2025-05-13T02:25:23Z");
47+
map2.put("end", "2025-05-13T02:25:23.001");
48+
Assertions.assertThrows(
49+
Exception.class,
50+
() -> prometheusSourceParameter.buildWithConfig(ReadonlyConfig.fromMap(map2)));
51+
52+
Map<String, Object> map3 = new HashMap<>();
53+
map3.put("url", "http://localhost:9090");
54+
map3.put("query", "node_cpu_seconds_total");
55+
map3.put("query_type", "Range");
56+
map3.put("start", "1747103123.083");
57+
map3.put("end", "1747106723");
58+
prometheusSourceParameter.buildWithConfig(ReadonlyConfig.fromMap(map3));
59+
60+
Map<String, Object> map4 = new HashMap<>();
61+
map4.put("url", "http://localhost:9090");
62+
map4.put("query", "node_cpu_seconds_total");
63+
map4.put("query_type", "Range");
64+
map4.put("start", "CURRENT_TIMESTAMP");
65+
map4.put("end", "CURRENT_TIMESTAMP");
66+
prometheusSourceParameter.buildWithConfig(ReadonlyConfig.fromMap(map4));
67+
}
68+
}

0 commit comments

Comments
 (0)