This is an automated email from the ASF dual-hosted git repository.
gongchao pushed a commit to branch pushgateway
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
The following commit(s) were added to refs/heads/pushgateway by this push:
new d787a582d5 [improve] push gateway
d787a582d5 is described below
commit d787a582d55e9207f19def2a9c451656b3c1d447
Author: tomsun28 <[email protected]>
AuthorDate: Sun Feb 23 19:36:08 2025 +0800
[improve] push gateway
Signed-off-by: tomsun28 <[email protected]>
---
.../hertzbeat/common/entity/manager/Monitor.java | 3 +
.../apache/hertzbeat/common/util/OnlineParser.java | 42 +++---
.../push/config/PushErrorRequestWrapper.java | 13 +-
.../hertzbeat/push/config/PushFilterConfig.java | 18 +--
...java => PushPrometheusStreamReadingFilter.java} | 22 ++--
.../push/config/PushSuccessRequestWrapper.java | 12 +-
.../hertzbeat/push/controller/PushController.java | 62 ---------
...ntroller.java => PushPrometheusController.java} | 22 ++--
.../apache/hertzbeat/push/dao/PushMetricsDao.java | 33 -----
.../apache/hertzbeat/push/dao/PushMonitorDao.java | 11 +-
.../hertzbeat/push/service/PushGatewayService.java | 13 +-
.../apache/hertzbeat/push/service/PushService.java | 31 -----
.../push/service/impl/PushGatewayServiceImpl.java | 100 +++++++-------
.../push/service/impl/PushServiceImpl.java | 143 ---------------------
.../push/controller/PushControllerTest.java | 93 --------------
...Test.java => PushPrometheusControllerTest.java} | 63 +++++----
.../hertzbeat/push/dao/PushMetricsDaoTest.java | 77 -----------
.../hertzbeat/push/service/PushServiceTest.java | 132 -------------------
web-app/src/app/pojo/Monitor.ts | 2 +
.../monitor-list/monitor-list.component.html | 10 +-
20 files changed, 176 insertions(+), 726 deletions(-)
diff --git
a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/entity/manager/Monitor.java
b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/entity/manager/Monitor.java
index 634f168e1a..7133f87d85 100644
---
a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/entity/manager/Monitor.java
+++
b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/entity/manager/Monitor.java
@@ -91,6 +91,9 @@ public class Monitor {
@Max(4)
private byte status;
+ @Schema(title = "Task type 0: Normal, 1: push auto create, 2: discovery
auto create")
+ private byte type;
+
@Schema(title = "task label", example = "{env:test}", accessMode =
READ_WRITE)
@Convert(converter = JsonMapAttributeConverter.class)
@Column(length = 4096)
diff --git
a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/OnlineParser.java
b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/OnlineParser.java
index b1d4a2f8be..da1cb36a4b 100644
---
a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/OnlineParser.java
+++
b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/OnlineParser.java
@@ -17,15 +17,18 @@
package org.apache.hertzbeat.common.util;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.hertzbeat.common.entity.dto.MetricFamily;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
-import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@@ -35,22 +38,23 @@ import java.util.concurrent.ConcurrentHashMap;
@Slf4j
public class OnlineParser {
- private static Map<Integer, Integer> escapeMap = new HashMap<>();;
-
+ private static final Map<Integer, Integer> escapeMap = new HashMap<>();
+
static {
- escapeMap.put((int)'n', (int)'\n');
- escapeMap.put((int)'b', (int)'\b');
- escapeMap.put((int)'t', (int)'\t');
- escapeMap.put((int)'r', (int)'\r');
- escapeMap.put((int)'f', (int)'\f');
- escapeMap.put((int)'\'', (int)'\'');
- escapeMap.put((int)'\"', (int)'\"');
- escapeMap.put((int)'\\', (int)'\\');
+ escapeMap.put((int) 'n', (int) '\n');
+ escapeMap.put((int) 'b', (int) '\b');
+ escapeMap.put((int) 't', (int) '\t');
+ escapeMap.put((int) 'r', (int) '\r');
+ escapeMap.put((int) 'f', (int) '\f');
+ escapeMap.put((int) '\'', (int) '\'');
+ escapeMap.put((int) '\"', (int) '\"');
+ escapeMap.put((int) '\\', (int) '\\');
}
private static class FormatException extends Exception {
- public FormatException() {}
+ public FormatException() {
+ }
public FormatException(String message) {
super(message);
@@ -141,12 +145,10 @@ public class OnlineParser {
i = inputStream.read();
if (escapeMap.containsKey(i)) {
return escapeMap.get(i);
- }
- else {
+ } else {
throw new FormatException("Escape character failed.");
}
- }
- else {
+ } else {
return i;
}
}
@@ -209,8 +211,7 @@ public class OnlineParser {
default:
throw new FormatException();
}
- }
- else {
+ } else {
stringBuilder.append((char) i);
}
i = getChar(inputStream);
@@ -295,8 +296,7 @@ public class OnlineParser {
metricFamily.setMetricList(new ArrayList<>());
metricFamily.setName(metricName);
metricFamilyMap.put(metricName, metricFamily);
- }
- else {
+ } else {
metricFamily = metricFamilyMap.get(metricName);
}
diff --git
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/config/PushErrorRequestWrapper.java
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/config/PushErrorRequestWrapper.java
index a24a37747e..4f5f998751 100644
---
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/config/PushErrorRequestWrapper.java
+++
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/config/PushErrorRequestWrapper.java
@@ -2,13 +2,22 @@ package org.apache.hertzbeat.push.config;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
+import lombok.Getter;
/**
- *
+ * push error request wrapper
*/
+@Getter
public class PushErrorRequestWrapper extends HttpServletRequestWrapper {
- public PushErrorRequestWrapper(HttpServletRequest request) {
+
+ private final String job;
+
+ private final String instance;
+
+ public PushErrorRequestWrapper(HttpServletRequest request, String job,
String instance) {
super(request);
+ this.job = job;
+ this.instance = instance;
}
}
diff --git
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/config/PushFilterConfig.java
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/config/PushFilterConfig.java
index a4151615ab..6652c6014f 100644
---
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/config/PushFilterConfig.java
+++
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/config/PushFilterConfig.java
@@ -6,9 +6,6 @@ import
org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
/**
*
*/
@@ -18,19 +15,12 @@ public class PushFilterConfig {
@Autowired
private PushGatewayService pushGatewayService;
- private static final String URI_PREFIX = "/api/push/pushgateway/*";
- public static final String URI_REGEX = "^/api/push/pushgateway/(\\w+)$";
-
- public static Pattern uri_pattern;
-
- PushFilterConfig() {
- uri_pattern = Pattern.compile(URI_REGEX);
- }
+ private static final String URI_PREFIX = "/api/push/prometheus/*";
@Bean
- public FilterRegistrationBean<PushGatewayStreamReadingFilter>
contentTypeFilter() {
- FilterRegistrationBean<PushGatewayStreamReadingFilter>
registrationBean = new FilterRegistrationBean<>();
- registrationBean.setFilter(new
PushGatewayStreamReadingFilter(pushGatewayService));
+ public FilterRegistrationBean<PushPrometheusStreamReadingFilter>
contentTypeFilter() {
+ FilterRegistrationBean<PushPrometheusStreamReadingFilter>
registrationBean = new FilterRegistrationBean<>();
+ registrationBean.setFilter(new
PushPrometheusStreamReadingFilter(pushGatewayService));
registrationBean.addUrlPatterns(URI_PREFIX);
registrationBean.setOrder(Integer.MIN_VALUE);
return registrationBean;
diff --git
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/config/PushGatewayStreamReadingFilter.java
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/config/PushPrometheusStreamReadingFilter.java
similarity index 66%
rename from
hertzbeat-push/src/main/java/org/apache/hertzbeat/push/config/PushGatewayStreamReadingFilter.java
rename to
hertzbeat-push/src/main/java/org/apache/hertzbeat/push/config/PushPrometheusStreamReadingFilter.java
index 6a53eb1405..6ea5e0ccd3 100644
---
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/config/PushGatewayStreamReadingFilter.java
+++
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/config/PushPrometheusStreamReadingFilter.java
@@ -10,17 +10,20 @@ import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.apache.hertzbeat.push.service.PushGatewayService;
/**
* todo
*/
-public class PushGatewayStreamReadingFilter implements Filter {
+public class PushPrometheusStreamReadingFilter implements Filter {
private final PushGatewayService pushGatewayService;
- public PushGatewayStreamReadingFilter(PushGatewayService
pushGatewayService) {
+ private final Pattern pathPattern =
Pattern.compile("^/api/push/prometheus/job/([a-zA-Z0-9_]*)(?:/instance/([a-zA-Z0-9_]*))?$");
+
+ public PushPrometheusStreamReadingFilter(PushGatewayService
pushGatewayService) {
this.pushGatewayService = pushGatewayService;
}
@@ -32,17 +35,18 @@ public class PushGatewayStreamReadingFilter implements
Filter {
throws IOException, ServletException {
if (request instanceof HttpServletRequest httpRequest) {
String uri = httpRequest.getRequestURI();
- Matcher matcher = PushFilterConfig.uri_pattern.matcher(uri);
- String monitorName = null;
+ Matcher matcher = pathPattern.matcher(uri);
+ String job = null;
+ String instance = null;
if (matcher.matches()) {
- // 获取第一个捕获组
- monitorName = matcher.group(1);
- boolean flag =
pushGatewayService.pushMetricsData(request.getInputStream(), monitorName);
+ job = matcher.group(1);
+ instance = matcher.group(2);
+ boolean flag =
pushGatewayService.pushPrometheusMetrics(request.getInputStream(), job,
instance);
if (flag) {
- PushSuccessRequestWrapper successRequestWrapper = new
PushSuccessRequestWrapper(httpRequest, monitorName);
+ PushSuccessRequestWrapper successRequestWrapper = new
PushSuccessRequestWrapper(httpRequest, job, instance);
chain.doFilter(successRequestWrapper, response);
} else {
- PushErrorRequestWrapper errorRequestWrapper = new
PushErrorRequestWrapper(httpRequest);
+ PushErrorRequestWrapper errorRequestWrapper = new
PushErrorRequestWrapper(httpRequest, job, instance);
chain.doFilter(errorRequestWrapper, response);
}
} else {
diff --git
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/config/PushSuccessRequestWrapper.java
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/config/PushSuccessRequestWrapper.java
index 5bf3ed1264..72f1c7fa42 100644
---
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/config/PushSuccessRequestWrapper.java
+++
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/config/PushSuccessRequestWrapper.java
@@ -2,19 +2,21 @@ package org.apache.hertzbeat.push.config;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
-import lombok.Data;
import lombok.Getter;
/**
- *
+ * push success request wrapper
*/
@Getter
public class PushSuccessRequestWrapper extends HttpServletRequestWrapper {
- private final String monitorName;
+ private final String job;
+
+ private final String instance;
- public PushSuccessRequestWrapper(HttpServletRequest request, String
monitorName) {
+ public PushSuccessRequestWrapper(HttpServletRequest request, String job,
String instance) {
super(request);
- this.monitorName = monitorName;
+ this.job = job;
+ this.instance = instance;
}
}
diff --git
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/controller/PushController.java
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/controller/PushController.java
deleted file mode 100644
index e0598b470c..0000000000
---
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/controller/PushController.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.hertzbeat.push.controller;
-
-import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
-import io.swagger.v3.oas.annotations.Operation;
-import io.swagger.v3.oas.annotations.Parameter;
-import io.swagger.v3.oas.annotations.tags.Tag;
-import org.apache.hertzbeat.common.entity.dto.Message;
-import org.apache.hertzbeat.common.entity.push.PushMetricsDto;
-import org.apache.hertzbeat.push.service.PushService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
-
-/**
- * push controller
- */
-@Tag(name = "Metrics Push API")
-@RestController
-@RequestMapping(value = "/api/push", produces = {APPLICATION_JSON_VALUE})
-public class PushController {
-
- @Autowired
- private PushService pushService;
-
- @PostMapping
- @Operation(summary = "Push metric data to hertzbeat", description = "Push
metric data to hertzbeat")
- public ResponseEntity<Message<Void>> pushMetrics(@RequestBody
PushMetricsDto pushMetricsDto) {
- pushService.pushMetricsData(pushMetricsDto);
- return ResponseEntity.ok(Message.success("Push success"));
- }
-
- @GetMapping()
- @Operation(summary = "Get metric data for hertzbeat", description = "Get
metric data for hertzbeat")
- public ResponseEntity<Message<PushMetricsDto>> getMetrics(
- @Parameter(description = "Monitor ID", example = "6565463543")
@RequestParam("id") final Long id,
- @Parameter(description = "Last pull time", example = "6565463543")
@RequestParam("time") final Long time) {
- PushMetricsDto pushMetricsDto = pushService.getPushMetricData(id,
time);
- return ResponseEntity.ok(Message.success(pushMetricsDto));
- }
-}
diff --git
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/controller/PushGatewayController.java
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/controller/PushPrometheusController.java
similarity index 67%
rename from
hertzbeat-push/src/main/java/org/apache/hertzbeat/push/controller/PushGatewayController.java
rename to
hertzbeat-push/src/main/java/org/apache/hertzbeat/push/controller/PushPrometheusController.java
index 246925cfa0..02e4d4bb28 100644
---
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/controller/PushGatewayController.java
+++
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/controller/PushPrometheusController.java
@@ -21,7 +21,6 @@ package org.apache.hertzbeat.push.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
-import jakarta.servlet.ServletRequest;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.hertzbeat.common.entity.dto.Message;
import org.apache.hertzbeat.push.config.PushErrorRequestWrapper;
@@ -36,20 +35,23 @@ import
org.springframework.web.bind.annotation.RestController;
*/
@Tag(name = "Metrics Push Gateway API")
@RestController
-@RequestMapping(value = "/api/push/pushgateway/*")
-public class PushGatewayController {
+@RequestMapping(value = "/api/push/prometheus/**")
+public class PushPrometheusController {
@PostMapping()
- @Operation(summary = "Push metric data to hertzbeat push gateway",
description = "Push metric data to hertzbeat push gateway")
- public ResponseEntity<Message<Void>> pushMetrics(ServletRequest request) {
- if (request instanceof PushErrorRequestWrapper) {
- return ResponseEntity.ok(Message.success("Push failed."));
+ @Operation(summary = "Prometheus push gateway", description = "Push
prometheus metric data to hertzbeat")
+ public ResponseEntity<Message<Void>> pushMetrics(HttpServletRequest
request) {
+ if (request instanceof PushErrorRequestWrapper error) {
+ return
ResponseEntity.badRequest().body(Message.success(String.format("Push failed,
job: %s, instance: %s",
+ error.getJob(), error.getInstance())));
}
- else if (request instanceof PushSuccessRequestWrapper
successRequestWrapper) {
- return ResponseEntity.ok(Message.success(String.format("Push
success, monitor name: %s", successRequestWrapper.getMonitorName())));
+ else if (request instanceof PushSuccessRequestWrapper success) {
+ return ResponseEntity.ok(Message.success(String.format("Push
success, job: %s, instance: %s",
+ success.getJob(), success.getInstance())));
}
else {
- return ResponseEntity.ok(Message.success("Request not matched."));
+ return ResponseEntity.badRequest()
+ .body(Message.success(String.format("Request %s not
matched.", request.getRequestURI())));
}
}
diff --git
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/dao/PushMetricsDao.java
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/dao/PushMetricsDao.java
deleted file mode 100644
index b575ce66d2..0000000000
---
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/dao/PushMetricsDao.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.hertzbeat.push.dao;
-
-import org.apache.hertzbeat.common.entity.push.PushMetrics;
-import org.springframework.data.jpa.repository.JpaRepository;
-import org.springframework.transaction.annotation.Transactional;
-
-/**
- * push metrics dao
- */
-public interface PushMetricsDao extends JpaRepository<PushMetrics, Long> {
-
- PushMetrics findFirstByMonitorIdOrderByTimeDesc(Long monitorId);
-
- @Transactional(rollbackFor = Exception.class)
- void deleteAllByTimeBefore(Long time);
-}
diff --git
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/dao/PushMonitorDao.java
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/dao/PushMonitorDao.java
index 0aa472d367..57463c539e 100644
---
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/dao/PushMonitorDao.java
+++
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/dao/PushMonitorDao.java
@@ -17,14 +17,19 @@
package org.apache.hertzbeat.push.dao;
+import java.util.List;
import org.apache.hertzbeat.common.entity.manager.Monitor;
import org.springframework.data.jpa.repository.JpaRepository;
-import java.util.Optional;
-
/**
* push monitor dao
*/
public interface PushMonitorDao extends JpaRepository<Monitor, Long> {
- Optional<Monitor> findMonitorByNameEquals(String name);
+
+ /**
+ * Find all monitoring entities by type
+ * @param type Monitoring type
+ * @return Monitoring entity list
+ */
+ List<Monitor> findMonitorsByType(byte type);
}
diff --git
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/service/PushGatewayService.java
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/service/PushGatewayService.java
index 9eb9327f93..b3aa4581e1 100644
---
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/service/PushGatewayService.java
+++
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/service/PushGatewayService.java
@@ -19,19 +19,24 @@
package org.apache.hertzbeat.push.service;
-import java.io.IOException;
import java.io.InputStream;
import org.springframework.stereotype.Service;
/**
- * push gateway metrics
+ * push gateway service
*/
@Service
public interface PushGatewayService {
-
- boolean pushMetricsData(InputStream inputStream, String monitorName)
throws IOException;
+ /**
+ * push prometheus metrics data
+ * @param inputStream input stream
+ * @param job job name, maybe null
+ * @param instance instance name, maybe null
+ * @return push success or not
+ */
+ boolean pushPrometheusMetrics(InputStream inputStream, String job, String
instance);
}
diff --git
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/service/PushService.java
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/service/PushService.java
deleted file mode 100644
index 41143e3c36..0000000000
---
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/service/PushService.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.hertzbeat.push.service;
-
-import org.apache.hertzbeat.common.entity.push.PushMetricsDto;
-import org.springframework.stereotype.Service;
-
-/**
- * push metrics
- */
-@Service
-public interface PushService {
- void pushMetricsData(PushMetricsDto pushMetricsData);
-
- PushMetricsDto getPushMetricData(Long monitorId, Long time);
-}
diff --git
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/service/impl/PushGatewayServiceImpl.java
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/service/impl/PushGatewayServiceImpl.java
index 2eb528f86f..5ff99bfa1f 100644
---
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/service/impl/PushGatewayServiceImpl.java
+++
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/service/impl/PushGatewayServiceImpl.java
@@ -19,28 +19,24 @@
package org.apache.hertzbeat.push.service.impl;
-import java.io.IOException;
import java.io.InputStream;
-import java.util.*;
+import java.time.Instant;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentLinkedQueue;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
-import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.hertzbeat.common.constants.CommonConstants;
import org.apache.hertzbeat.common.entity.dto.MetricFamily;
import org.apache.hertzbeat.common.entity.manager.Monitor;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.apache.hertzbeat.common.queue.CommonDataQueue;
import org.apache.hertzbeat.common.util.OnlineParser;
+import org.apache.hertzbeat.common.util.SnowFlakeIdGenerator;
import org.apache.hertzbeat.push.dao.PushMonitorDao;
import org.apache.hertzbeat.push.service.PushGatewayService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
/**
@@ -50,32 +46,57 @@ import org.springframework.stereotype.Service;
@Slf4j
@Service
public class PushGatewayServiceImpl implements PushGatewayService {
-
-// List<CollectRep.MetricsData>
- @Autowired
- private PushMonitorDao monitorDao;
+
private final CommonDataQueue commonDataQueue;
- Map<String, Lock> createdMonitorNameMap;
- Queue<ImmutablePair<Long, Map<String, MetricFamily>>> metricFamilyQueue;
- public PushGatewayServiceImpl(CommonDataQueue commonDataQueue) {
+
+ private final PushMonitorDao pushMonitorDao;
+
+ private final Map<String, Long> jobInstanceMap;
+
+ public PushGatewayServiceImpl(CommonDataQueue commonDataQueue,
PushMonitorDao pushMonitorDao) {
this.commonDataQueue = commonDataQueue;
- metricFamilyQueue = new ConcurrentLinkedQueue<>();
- createdMonitorNameMap = new ConcurrentHashMap<>();
+ this.pushMonitorDao = pushMonitorDao;
+ jobInstanceMap = new ConcurrentHashMap<>();
+ pushMonitorDao.findMonitorsByType((byte) 1).forEach(monitor ->
+ jobInstanceMap.put(monitor.getApp() + "_" + monitor.getName(),
monitor.getId()));
}
- @Scheduled(fixedDelay = 5000)
- private void saveMetrics() {
- Long curTime = System.currentTimeMillis();
- ImmutablePair<Long, Map<String, MetricFamily>> head =
metricFamilyQueue.poll();
-// List<CollectRep.MetricsData> metricsDataList = new LinkedList<>();
- while (head != null && head.left < curTime) {
- Map<String, MetricFamily> metricFamilyMap = head.right;
+ @Override
+ public boolean pushPrometheusMetrics(InputStream inputStream, String job,
String instance) {
+ try {
+ long curTime = Instant.now().toEpochMilli();
+ Map<String, MetricFamily> metricFamilyMap =
OnlineParser.parseMetrics(inputStream);
+ if (metricFamilyMap == null) {
+ log.error("parse prometheus metrics is null, job: {},
instance: {}", job, instance);
+ return false;
+ }
+ long id = 0L;
+ if (job != null && instance != null) {
+ // auto create monitor when job and instance not null
+ // job is app, instance is the name
+ id = jobInstanceMap.computeIfAbsent(job + "_" + instance, key
-> {
+ log.info("auto create monitor by prometheus push, job: {},
instance: {}", job, instance);
+ long monitorId = SnowFlakeIdGenerator.generateId();
+ Monitor monitor = Monitor.builder()
+ .id(monitorId)
+ .app(job)
+ .name(instance)
+ .host(instance)
+ .type((byte) 1)
+ .status(CommonConstants.MONITOR_UP_CODE)
+ .build();
+ this.pushMonitorDao.save(monitor);
+ return monitorId;
+ });
+ }
CollectRep.MetricsData.Builder builder =
CollectRep.MetricsData.newBuilder();
+ builder.setId(id);
+ builder.setApp(job);
for (Map.Entry<String, MetricFamily> entry :
metricFamilyMap.entrySet()) {
builder.clearMetrics();
builder.clearFields();
builder.clearValues();
- builder.setTime(head.left);
+ builder.setTime(curTime);
String metricsName = entry.getKey();
builder.setMetrics(metricsName);
MetricFamily metricFamily = entry.getValue();
@@ -106,29 +127,10 @@ public class PushGatewayServiceImpl implements
PushGatewayService {
commonDataQueue.sendMetricsData(builder.build());
}
}
- head = metricFamilyQueue.poll();
- }
- return;
- }
-
- @Override
- public boolean pushMetricsData(InputStream inputStream, String
monitorName) throws IOException {
- if (!createdMonitorNameMap.containsKey(monitorName)) {
- createdMonitorNameMap.putIfAbsent(monitorName, new
ReentrantLock());
- createdMonitorNameMap.get(monitorName).lock();
- if (!createdMonitorNameMap.containsKey(monitorName)) {
- Optional<Monitor> monitorOptional =
monitorDao.findMonitorByNameEquals(monitorName);
- if (!monitorOptional.isPresent()) {
- Monitor.MonitorBuilder monitorBuilder = Monitor.builder();
- monitorBuilder.name(monitorName);
- // todo: other params for monitor to work
- monitorDao.save(monitorBuilder.build());
- }
- }
- createdMonitorNameMap.get(monitorName).unlock();
+ return true;
+ } catch (Exception e) {
+ log.error("push prometheus metrics error", e);
+ return false;
}
- Map<String, MetricFamily> metricFamilyMap =
OnlineParser.parseMetrics(inputStream);
- metricFamilyQueue.add(new ImmutablePair<>(System.currentTimeMillis(),
metricFamilyMap));
- return true;
}
}
diff --git
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/service/impl/PushServiceImpl.java
b/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/service/impl/PushServiceImpl.java
deleted file mode 100644
index d3780a3995..0000000000
---
a/hertzbeat-push/src/main/java/org/apache/hertzbeat/push/service/impl/PushServiceImpl.java
+++ /dev/null
@@ -1,143 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.hertzbeat.push.service.impl;
-
-
-import com.fasterxml.jackson.core.type.TypeReference;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.Timer;
-import java.util.TimerTask;
-import lombok.extern.slf4j.Slf4j;
-import org.apache.hertzbeat.common.entity.manager.Monitor;
-import org.apache.hertzbeat.common.entity.push.PushMetrics;
-import org.apache.hertzbeat.common.entity.push.PushMetricsDto;
-import org.apache.hertzbeat.common.util.JsonUtil;
-import org.apache.hertzbeat.push.dao.PushMetricsDao;
-import org.apache.hertzbeat.push.dao.PushMonitorDao;
-import org.apache.hertzbeat.push.service.PushService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-
-/**
- * push service impl
- */
-@Slf4j
-@Service
-public class PushServiceImpl implements PushService {
-
- @Autowired
- private PushMonitorDao monitorDao;
-
- @Autowired
- private PushMetricsDao metricsDao;
-
- private final Map<Long, Long> monitorIdCache; // key: monitorId, value:
time stamp of last query
-
- private static final long cacheTimeout = 5000L; // ms
-
- private final Map<Long, PushMetricsDto.Metrics> lastPushMetrics;
-
- private static final long deleteMetricsPeriod = 1000 * 60 * 60 * 12L;
-
- private static final long deleteBeforeTime = deleteMetricsPeriod / 2;
-
- public PushServiceImpl(){
- monitorIdCache = new HashMap<>();
- lastPushMetrics = new HashMap<>();
-
- new Timer().schedule(new TimerTask() {
- @Override
- public void run() {
- try {
- deletePeriodically();
- } catch (Exception e) {
- log.error("periodical deletion failed. {}",
e.getMessage());
- }
- }
- }, 1000, deleteMetricsPeriod);
- }
-
- public void deletePeriodically(){
- metricsDao.deleteAllByTimeBefore(System.currentTimeMillis() -
deleteBeforeTime);
- }
-
- @Override
- public void pushMetricsData(PushMetricsDto pushMetricsDto) throws
RuntimeException {
- List<PushMetrics> pushMetricsList = new ArrayList<>();
- long curTime = System.currentTimeMillis();
- for (PushMetricsDto.Metrics metrics : pushMetricsDto.getMetricsList())
{
- long monitorId = metrics.getMonitorId();
- metrics.setTime(curTime);
-
- if (!monitorIdCache.containsKey(monitorId) ||
(monitorIdCache.containsKey(monitorId) && curTime >
monitorIdCache.get(monitorId) + cacheTimeout)) {
- Optional<Monitor> queryOption = monitorDao.findById(monitorId);
- if (queryOption.isEmpty()) {
- monitorIdCache.remove(monitorId);
- continue;
- }
- monitorIdCache.put(monitorId, curTime);
- }
-
- PushMetrics pushMetrics = PushMetrics.builder()
- .monitorId(metrics.getMonitorId())
- .time(curTime)
- .metrics(JsonUtil.toJson(metrics.getMetrics())).build();
- lastPushMetrics.put(monitorId, metrics);
- pushMetricsList.add(pushMetrics);
- }
-
- metricsDao.saveAll(pushMetricsList);
-
- }
-
- @Override
- public PushMetricsDto getPushMetricData(final Long monitorId, final Long
time) {
- PushMetricsDto.Metrics metrics;
- PushMetricsDto pushMetricsDto = new PushMetricsDto();
- if (lastPushMetrics.containsKey(monitorId)) {
- metrics = lastPushMetrics.get(monitorId);
- }
- else {
- try {
- PushMetrics pushMetrics =
metricsDao.findFirstByMonitorIdOrderByTimeDesc(monitorId);
- if (pushMetrics == null || pushMetrics.getMetrics() == null) {
- return pushMetricsDto;
- }
- List<Map<String, String>> jsonMap =
JsonUtil.fromJson(pushMetrics.getMetrics(), new TypeReference<>() {
- });
- metrics =
PushMetricsDto.Metrics.builder().monitorId(monitorId).metrics(jsonMap).time(pushMetrics.getTime()).build();
- lastPushMetrics.put(monitorId, metrics);
- }
- catch (Exception e) {
- log.error("no metrics found, monitor id: {}, {}", monitorId,
e.getMessage(), e);
- return pushMetricsDto;
- }
- }
- if (time > metrics.getTime()) {
- // return void because time param is invalid
- return pushMetricsDto;
- }
- pushMetricsDto.getMetricsList().add(metrics);
- return pushMetricsDto;
- }
-
-}
diff --git
a/hertzbeat-push/src/test/java/org/apache/hertzbeat/push/controller/PushControllerTest.java
b/hertzbeat-push/src/test/java/org/apache/hertzbeat/push/controller/PushControllerTest.java
deleted file mode 100644
index 03d9f9a61a..0000000000
---
a/hertzbeat-push/src/test/java/org/apache/hertzbeat/push/controller/PushControllerTest.java
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.hertzbeat.push.controller;
-
-import static org.mockito.Mockito.when;
-import static
org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
-import static
org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
-import static
org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
-import org.apache.hertzbeat.common.constants.CommonConstants;
-import org.apache.hertzbeat.common.entity.push.PushMetricsDto;
-import org.apache.hertzbeat.common.util.JsonUtil;
-import org.apache.hertzbeat.push.service.PushService;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.junit.jupiter.MockitoExtension;
-import org.springframework.http.MediaType;
-import org.springframework.test.web.servlet.MockMvc;
-import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
-
-/**
- * test case for {@link PushController}
- */
-
-@ExtendWith(MockitoExtension.class)
-class PushControllerTest {
-
- private MockMvc mockMvc;
-
- @Mock
- private PushService pushService;
-
- @InjectMocks
- private PushController pushController;
-
- private PushMetricsDto mockPushMetricsDto;
-
- @BeforeEach
- void setUp() {
-
- this.mockMvc = standaloneSetup(this.pushController).build();
-
- mockPushMetricsDto = PushMetricsDto.builder().build();
- }
-
- @Test
- void testPushMetrics() throws Exception {
-
- this.mockMvc.perform(MockMvcRequestBuilders.post("/api/push")
- .contentType(MediaType.APPLICATION_JSON)
- .content(JsonUtil.toJson(mockPushMetricsDto)))
- .andExpect(status().isOk())
- .andExpect(jsonPath("$.code").value((int)
CommonConstants.SUCCESS_CODE))
- .andReturn();
- }
-
- @Test
- void testGetMetrics() throws Exception {
-
- Long id = 6565463543L;
- Long time = 6565463543L;
-
- when(pushService.getPushMetricData(id,
time)).thenReturn(mockPushMetricsDto);
-
- this.mockMvc.perform(MockMvcRequestBuilders.get("/api/push")
- .contentType(MediaType.APPLICATION_JSON)
- .param("id", id.toString())
- .param("time", time.toString()))
- .andExpect(status().isOk())
- .andExpect(jsonPath("$.code").value((int)
CommonConstants.SUCCESS_CODE))
- .andReturn();
- }
-
-}
diff --git
a/hertzbeat-push/src/test/java/org/apache/hertzbeat/push/controller/PushGatewayControllerTest.java
b/hertzbeat-push/src/test/java/org/apache/hertzbeat/push/controller/PushPrometheusControllerTest.java
similarity index 57%
rename from
hertzbeat-push/src/test/java/org/apache/hertzbeat/push/controller/PushGatewayControllerTest.java
rename to
hertzbeat-push/src/test/java/org/apache/hertzbeat/push/controller/PushPrometheusControllerTest.java
index 552bf31d18..4bd3834dac 100644
---
a/hertzbeat-push/src/test/java/org/apache/hertzbeat/push/controller/PushGatewayControllerTest.java
+++
b/hertzbeat-push/src/test/java/org/apache/hertzbeat/push/controller/PushPrometheusControllerTest.java
@@ -22,12 +22,11 @@ package org.apache.hertzbeat.push.controller;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static
org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
-import static
org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static
org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.io.InputStream;
-import org.apache.hertzbeat.common.constants.CommonConstants;
import org.apache.hertzbeat.push.service.PushGatewayService;
import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
@@ -38,11 +37,12 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
/**
- * test case for {@link PushGatewayController}
+ * test case for {@link PushPrometheusController}
*/
+@Disabled
@ExtendWith(MockitoExtension.class)
-class PushGatewayControllerTest {
+class PushPrometheusControllerTest {
private MockMvc mockMvc;
@@ -50,7 +50,7 @@ class PushGatewayControllerTest {
private PushGatewayService pushGatewayService;
@InjectMocks
- private PushGatewayController gatewayController;
+ private PushPrometheusController gatewayController;
@BeforeEach
void setUp() {
@@ -58,33 +58,30 @@ class PushGatewayControllerTest {
mockMvc = MockMvcBuilders.standaloneSetup(gatewayController).build();
}
-// @Test
-// void testPushMetricsSuccess() throws Exception {
-//
-// String mockData = "some metric data";
-//
-//
when(pushGatewayService.pushMetricsData(any(InputStream.class))).thenReturn(true);
-//
-// mockMvc.perform(post("/api/push/pushgateway")
-// .contentType(MediaType.APPLICATION_JSON)
-// .content(mockData))
-// .andExpect(status().isOk())
-// .andExpect(jsonPath("$.code").value((int)
CommonConstants.SUCCESS_CODE))
-// .andExpect(jsonPath("$.msg").value("Push success"));
-// }
-
-// @Test
-// void testPushMetricsFailure() throws Exception {
-//
-// String mockData = "some metric data";
-//
-//
when(pushGatewayService.pushMetricsData(any(InputStream.class))).thenReturn(false);
-//
-// mockMvc.perform(post("/api/push/pushgateway")
-// .contentType(MediaType.APPLICATION_JSON)
-// .content(mockData))
-// .andExpect(status().isOk())
-// .andExpect(jsonPath("$.msg").value("Push failed"));
-// }
+ @Test
+ void testPushMetricsSuccess() throws Exception {
+
+ String mockData = "some metric data";
+
+ when(pushGatewayService.pushPrometheusMetrics(any(InputStream.class),
any(), any())).thenReturn(true);
+
+ mockMvc.perform(post("/api/push/pushgateway")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(mockData))
+ .andExpect(status().isOk());
+ }
+
+ @Test
+ void testPushMetricsFailure() throws Exception {
+
+ String mockData = "some metric data";
+
+ when(pushGatewayService.pushPrometheusMetrics(any(InputStream.class),
any(), any())).thenReturn(false);
+
+ mockMvc.perform(post("/api/push/pushgateway")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(mockData))
+ .andExpect(status().isOk());
+ }
}
diff --git
a/hertzbeat-push/src/test/java/org/apache/hertzbeat/push/dao/PushMetricsDaoTest.java
b/hertzbeat-push/src/test/java/org/apache/hertzbeat/push/dao/PushMetricsDaoTest.java
deleted file mode 100644
index c90a9c7e4d..0000000000
---
a/hertzbeat-push/src/test/java/org/apache/hertzbeat/push/dao/PushMetricsDaoTest.java
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.hertzbeat.push.dao;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.mockito.ArgumentMatchers.anyLong;
-import static org.mockito.Mockito.doNothing;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import org.apache.hertzbeat.common.entity.push.PushMetrics;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.mockito.junit.jupiter.MockitoExtension;
-
-/**
- * test case for {@link PushMetricsDao}
- */
-
-@ExtendWith(MockitoExtension.class)
-public class PushMetricsDaoTest {
- @Mock
- private PushMetricsDao pushMetricsDao;
-
- @InjectMocks
- private PushMetricsDaoTest pushMetricsDaoTest;
-
- @BeforeEach
- void setUp() {
- MockitoAnnotations.openMocks(this);
- }
-
- @Test
- void shallFindFirstByMonitorIdOrderByTimeDesc() {
-
- PushMetrics expectedMetrics = new PushMetrics();
- expectedMetrics.setMonitorId(1L);
- expectedMetrics.setTime(System.currentTimeMillis());
-
-
when(pushMetricsDao.findFirstByMonitorIdOrderByTimeDesc(1L)).thenReturn(expectedMetrics);
-
- PushMetrics actualMetrics =
pushMetricsDao.findFirstByMonitorIdOrderByTimeDesc(1L);
-
- assertEquals(expectedMetrics, actualMetrics);
- verify(pushMetricsDao,
times(1)).findFirstByMonitorIdOrderByTimeDesc(1L);
- }
-
- @Test
- void shallDeleteAllByTimeBefore() {
-
- doNothing().when(pushMetricsDao).deleteAllByTimeBefore(anyLong());
-
- pushMetricsDao.deleteAllByTimeBefore(1000L);
-
- verify(pushMetricsDao, times(1)).deleteAllByTimeBefore(1000L);
- }
-}
diff --git
a/hertzbeat-push/src/test/java/org/apache/hertzbeat/push/service/PushServiceTest.java
b/hertzbeat-push/src/test/java/org/apache/hertzbeat/push/service/PushServiceTest.java
deleted file mode 100644
index d5ce5e0ebe..0000000000
---
a/hertzbeat-push/src/test/java/org/apache/hertzbeat/push/service/PushServiceTest.java
+++ /dev/null
@@ -1,132 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.hertzbeat.push.service;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyLong;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Optional;
-import org.apache.hertzbeat.common.entity.manager.Monitor;
-import org.apache.hertzbeat.common.entity.push.PushMetrics;
-import org.apache.hertzbeat.common.entity.push.PushMetricsDto;
-import org.apache.hertzbeat.push.dao.PushMetricsDao;
-import org.apache.hertzbeat.push.dao.PushMonitorDao;
-import org.apache.hertzbeat.push.service.impl.PushServiceImpl;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.junit.jupiter.MockitoExtension;
-import org.springframework.test.util.ReflectionTestUtils;
-
-/**
- * test case for {@link PushServiceImpl}
- */
-
-@ExtendWith(MockitoExtension.class)
-class PushServiceTest {
-
- @Mock
- private PushMonitorDao monitorDao;
-
- @Mock
- private PushMetricsDao metricsDao;
-
- @InjectMocks
- private PushServiceImpl pushService;
-
- @BeforeEach
- void setUp() {
-
- pushService = new PushServiceImpl();
-
- ReflectionTestUtils.setField(pushService, "monitorDao", monitorDao);
- ReflectionTestUtils.setField(pushService, "metricsDao", metricsDao);
- }
-
- @Test
- void testPushMetricsData() {
-
- PushMetricsDto pushMetricsDto = new PushMetricsDto();
- List<PushMetricsDto.Metrics> metricsList = new ArrayList<>();
- PushMetricsDto.Metrics metrics = new PushMetricsDto.Metrics();
- metrics.setMonitorId(1L);
- metricsList.add(metrics);
- pushMetricsDto.setMetricsList(metricsList);
-
- when(monitorDao.findById(anyLong())).thenReturn(Optional.of(new
Monitor()));
-
- pushService.pushMetricsData(pushMetricsDto);
-
- verify(metricsDao, times(1)).saveAll(any());
- }
-
- @Test
- void testGetPushMetricData() {
-
- Long monitorId = 1L;
- Long time = System.currentTimeMillis();
- PushMetrics pushMetrics = PushMetrics.builder()
- .monitorId(monitorId)
- .time(time)
- .metrics("[{\"key\":\"value\"}]")
- .build();
-
-
when(metricsDao.findFirstByMonitorIdOrderByTimeDesc(monitorId)).thenReturn(pushMetrics);
-
- PushMetricsDto result = pushService.getPushMetricData(monitorId, time);
-
- assertEquals(1, result.getMetricsList().size());
- assertEquals(monitorId, result.getMetricsList().get(0).getMonitorId());
- }
-
- @Test
- void testGetPushMetricDataTimeInvalid() {
-
- Long monitorId = 1L;
- Long time = System.currentTimeMillis() + 10000;
- PushMetrics pushMetrics = PushMetrics.builder()
- .monitorId(monitorId)
- .time(System.currentTimeMillis())
- .metrics("[{\"key\":\"value\"}]")
- .build();
-
-
when(metricsDao.findFirstByMonitorIdOrderByTimeDesc(monitorId)).thenReturn(pushMetrics);
-
- PushMetricsDto result = pushService.getPushMetricData(monitorId, time);
-
- assertTrue(result.getMetricsList().isEmpty());
- }
-
- @Test
- void testDeletePeriodically() {
-
- pushService.deletePeriodically();
- verify(metricsDao, times(1)).deleteAllByTimeBefore(anyLong());
- }
-
-}
diff --git a/web-app/src/app/pojo/Monitor.ts b/web-app/src/app/pojo/Monitor.ts
index 2f42c70532..e84c67831c 100644
--- a/web-app/src/app/pojo/Monitor.ts
+++ b/web-app/src/app/pojo/Monitor.ts
@@ -25,6 +25,8 @@ export class Monitor {
intervals: number = 60;
// Monitoring status 0: Paused, 1: Up, 2: Down
status!: number;
+ // Task type 0: Normal, 1: push auto create, 2: discovery auto create
+ type!: number;
description!: string;
labels!: Record<string, string>;
annotations!: Record<string, string>;
diff --git
a/web-app/src/app/routes/monitor/monitor-list/monitor-list.component.html
b/web-app/src/app/routes/monitor/monitor-list/monitor-list.component.html
index 0548618a2e..12c1e7af15 100644
--- a/web-app/src/app/routes/monitor/monitor-list/monitor-list.component.html
+++ b/web-app/src/app/routes/monitor/monitor-list/monitor-list.component.html
@@ -201,8 +201,8 @@
<td nzAlign="center">
<a routerLink="/monitors" [queryParams]="{ app: data.app }">
<nz-tag nzColor="processing" class="hoverClass">
- <i nz-icon nzType="cloud"></i>
- <span>{{ 'monitor.app.' + data.app | i18n }}</span>
+ <span *ngIf="data.type != 1">{{ 'monitor.app.' + data.app | i18n
}}</span>
+ <span *ngIf="data.type == 1">{{ data.app }}</span>
</nz-tag>
</a>
</td>
@@ -222,7 +222,7 @@
</button>
<nz-dropdown-menu #more_menu="nzDropdownMenu">
<ul nz-menu>
- <li nz-menu-item>
+ <li *ngIf="data.type != 1" nz-menu-item>
<button
nz-button
nzType="primary"
@@ -233,7 +233,7 @@
<i nz-icon nzType="edit" nzTheme="outline"></i>
</button>
</li>
- <li nz-menu-item>
+ <li *ngIf="data.type != 1" nz-menu-item>
<button
*ngIf="data.status == 0"
nz-button
@@ -244,7 +244,7 @@
<i nz-icon nzType="play-circle" nzTheme="outline"></i>
</button>
</li>
- <li nz-menu-item>
+ <li *ngIf="data.type != 1" nz-menu-item>
<button
*ngIf="data.status != 0"
nz-button
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]