Skip to content

Commit

Permalink
Merge branch 'master' into release-1.6.0-rc2-doc-update
Browse files Browse the repository at this point in the history
  • Loading branch information
zqr10159 authored May 20, 2024
2 parents 43b3e21 + 7c9b398 commit 8b95a89
Show file tree
Hide file tree
Showing 39 changed files with 1,020 additions and 78 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ Add WeChat account `tan-cloud` to pull you into the WeChat group.

6. 待本地后端启动后,在web-app目录下启动本地前端 `ng serve --open`

7. 浏览器访问 localhost:4200 即可开始,默认账号密码 admin/hertzbeat**
7. 浏览器访问 localhost:4200 即可开始,默认账号密码 *admin/hertzbeat*

### 寻找任务

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -53,6 +51,7 @@
import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
import org.apache.hertzbeat.collector.util.CollectUtil;
import org.apache.hertzbeat.collector.util.JsonPathParser;
import org.apache.hertzbeat.collector.util.TimeExpressionUtil;
import org.apache.hertzbeat.common.constants.CollectorConstants;
import org.apache.hertzbeat.common.constants.CommonConstants;
import org.apache.hertzbeat.common.entity.job.Metrics;
Expand Down Expand Up @@ -92,11 +91,6 @@
*/
@Slf4j
public class HttpCollectImpl extends AbstractCollect {

public static final String OpenAIHost = "api.openai.com";
public static final String OpenAIUsageAPI = "/dashboard/billing/usage";
public static final String startDate = "start_date";
public static final String endDate = "end_date";
private static final Map<Long, ExporterParser> EXPORTER_PARSER_TABLE = new ConcurrentHashMap<>();
private final Set<Integer> defaultSuccessStatusCodes = Stream.of(HttpStatus.SC_OK, HttpStatus.SC_CREATED,
HttpStatus.SC_ACCEPTED, HttpStatus.SC_MULTIPLE_CHOICES, HttpStatus.SC_MOVED_PERMANENTLY,
Expand Down Expand Up @@ -499,18 +493,10 @@ public HttpUriRequest createHttpRequest(HttpProtocol httpProtocol) {
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> param : params.entrySet()) {
if (StringUtils.hasText(param.getValue())) {
requestBuilder.addParameter(param.getKey(), param.getValue());
requestBuilder.addParameter(param.getKey(), TimeExpressionUtil.calculate(param.getValue()));
}
}
}
// OpenAI /dashboard/billing/usage
if (OpenAIHost.equalsIgnoreCase(httpProtocol.getHost()) && OpenAIUsageAPI.equalsIgnoreCase(httpProtocol.getUrl())) {
LocalDate today = LocalDate.now();
LocalDate tomorrow = LocalDate.now().plusDays(1);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
requestBuilder.addParameter(startDate, today.format(formatter));
requestBuilder.addParameter(endDate, tomorrow.format(formatter));
}
// The default request header can be overridden if customized
// keep-alive
requestBuilder.addHeader(HttpHeaders.CONNECTION, "keep-alive");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* 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.collector.util;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;

/**
* time expression deal util
*/
public class TimeExpressionUtil {

private TimeExpressionUtil() {
}


/**
* localDatetime formatter
*/
private static final Map<String, Function<LocalDateTime, String>> FORMATTER = new LinkedHashMap<>();
/**
* unit string mapping to TemporalUnit
*/
private static final Map<Character, TemporalUnit> UNIT_MAP = new HashMap<>();

private static final String FORMATTER_NAMES;
private static final String UNIT_NAMES;


static {
FORMATTER.put("now", date -> DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(date));
FORMATTER.put("date", date -> DateTimeFormatter.ofPattern("yyyy-MM-dd").format(date));
FORMATTER.put("timestamp10", date -> String.valueOf(date.toEpochSecond(ZoneOffset.UTC)));
FORMATTER.put("timestamp", date -> String.valueOf(date.toInstant(ZoneOffset.UTC).toEpochMilli()));
FORMATTER.put("time", date -> DateTimeFormatter.ofPattern("HH:mm:ss").format(date));
FORMATTER.put("year", date -> DateTimeFormatter.ofPattern("yyyy").format(date));
FORMATTER.put("month", date -> DateTimeFormatter.ofPattern("MM").format(date));
FORMATTER.put("day", date -> DateTimeFormatter.ofPattern("dd").format(date));
FORMATTER.put("hour", date -> DateTimeFormatter.ofPattern("HH").format(date));
FORMATTER.put("minute", date -> DateTimeFormatter.ofPattern("mm").format(date));
FORMATTER.put("millisecond", date -> DateTimeFormatter.ofPattern("SSS").format(date));
FORMATTER.put("second", date -> DateTimeFormatter.ofPattern("ss").format(date));

UNIT_MAP.put('y', ChronoUnit.YEARS);
UNIT_MAP.put('M', ChronoUnit.MONTHS);
UNIT_MAP.put('d', ChronoUnit.DAYS);
UNIT_MAP.put('H', ChronoUnit.HOURS);
UNIT_MAP.put('m', ChronoUnit.MINUTES);
UNIT_MAP.put('s', ChronoUnit.SECONDS);
UNIT_MAP.put('w', ChronoUnit.WEEKS);

FORMATTER_NAMES = String.join("|", FORMATTER.keySet());
UNIT_NAMES = UNIT_MAP.keySet().stream().map(String::valueOf).collect(Collectors.joining(""));
}


public static String calculate(String template) {
return calculate(template, LocalDateTime.now());
}

public static String calculate(String template, LocalDateTime now) {
if (StringUtils.isBlank(template)) {
return template;
}
final String regex = "\\$\\{(@(" + FORMATTER_NAMES + ")(\\s*[-+]\\s*\\d+[" + UNIT_NAMES + "])*)}";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(template);

while (matcher.find()) {
String part = matcher.group(0);
String expression = matcher.group(1);
template = template.replace(part, calculateExpression(expression, now));

}
return template;
}


private static String calculateExpression(String expression, LocalDateTime now) {
expression = expression.replaceAll("\\s", "");
final String regex = "(@(" + FORMATTER_NAMES + "))|([-+]\\d+[" + UNIT_NAMES + "])";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(expression);
List<String> elements = new ArrayList<>();
while (matcher.find()) {
elements.add(matcher.group(0));
}
LocalDateTime result = now;
for (int i = 1; i < elements.size(); i++) {
String element = elements.get(i);
int operator = element.charAt(0) == '-' ? -1 : 1;
int duration = Integer.parseInt(element.substring(1, element.length() - 1));
char unit = element.charAt(element.length() - 1);
result = result.plus(operator * duration, UNIT_MAP.get(unit));

}
return FORMATTER.get(elements.get(0).substring(1)).apply(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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.collector.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.time.LocalDateTime;
import org.junit.jupiter.api.Test;

/**
* test for TimeExpressionUtil
*/
class TimeExpressionUtilTest {


LocalDateTime now = LocalDateTime.of(2022, 4, 24, 2, 40, 0);

@Test
void testBlankTemplate() {
assertEquals("", TimeExpressionUtil.calculate("", LocalDateTime.now()));
assertNull(TimeExpressionUtil.calculate(null, LocalDateTime.now()));
}

@Test
void testErrorTemplate() {
assertEquals("hello", TimeExpressionUtil.calculate("hello", LocalDateTime.now()));
assertEquals("${}", TimeExpressionUtil.calculate("${}", LocalDateTime.now()));
assertEquals("${@current-24m}", TimeExpressionUtil.calculate("${@current-24m}", LocalDateTime.now()));
}

@Test
void testWithSpace() {
assertEquals("2022-04-24 02:40:01", TimeExpressionUtil.calculate("${@now + 1s}", now));
}

@Test
void testCalculate() {
assertEquals("2022-04-24 02:40:00", TimeExpressionUtil.calculate("${@now}", now));
assertEquals("2022-04-24", TimeExpressionUtil.calculate("${@date}", now));
assertEquals("02:40:00", TimeExpressionUtil.calculate("${@time}", now));
assertEquals("1650768000000", TimeExpressionUtil.calculate("${@timestamp}", now));
assertEquals("1650768000", TimeExpressionUtil.calculate("${@timestamp10}", now));
assertEquals("2022", TimeExpressionUtil.calculate("${@year}", now));
assertEquals("04", TimeExpressionUtil.calculate("${@month}", now));
assertEquals("24", TimeExpressionUtil.calculate("${@day}", now));
assertEquals("02", TimeExpressionUtil.calculate("${@hour}", now));
assertEquals("40", TimeExpressionUtil.calculate("${@minute}", now));
assertEquals("00", TimeExpressionUtil.calculate("${@second}", now));
assertEquals("000", TimeExpressionUtil.calculate("${@millisecond}", now));

assertEquals("2022-04-24 02:25:00", TimeExpressionUtil.calculate("${@now-15m}", now));
assertEquals("2022-05-01 02:40:00", TimeExpressionUtil.calculate("${@now+1w}", now));
assertEquals("2022-05-01 02:25:00", TimeExpressionUtil.calculate("${@now+1w-15m}", now));
assertEquals("2022-04-25 02:40:00", TimeExpressionUtil.calculate("${@now+1d}", now));
assertEquals("02:40:00", TimeExpressionUtil.calculate("${@time+1d}", now));
assertEquals("2023-04-24 02:40:00", TimeExpressionUtil.calculate("${@now+1y}", now));
}

@Test
void testComplexTemplate() {
assertEquals("Three days after 2022-04-24 , is 2022-04-27", TimeExpressionUtil.calculate("Three days after ${@date} , is ${@date+3d}", now));
assertEquals("2022年04月24日 02:40:00", TimeExpressionUtil.calculate("${@year}年${@month}月${@day}日 ${@time}", now));
}
}
2 changes: 1 addition & 1 deletion hip/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ It is advised to create a master GitHub issue to formulate the execution plan an
- Merged PR means the HIP was accepted.
- Closed PR means the HIP was rejected.
- Open PR means the HIP was submitted and is in the process of discussion.
2. You can also take a look at the file in the `hip` folder. Each one is an approved HIP.
2. You can also take a look at the file in the `hip` folder. Each one is an approved HIP.
2 changes: 1 addition & 1 deletion hip/TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,4 @@ If there are alternatives that were already considered by the authors or, after
Updated afterwards
-->
* Mailing List discussion thread:
* Mailing List voting thread:
* Mailing List voting thread:
2 changes: 1 addition & 1 deletion hip/hip-01.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ Supplement the relevant unit tests.
Updated afterwards
-->
* Mailing List discussion thread: https://lists.apache.org/thread/cvvo7xg35fxq7kml5ggdrcdygrx6yvyj
* Mailing List voting thread: https://lists.apache.org/thread/1s7dhrb27qfdx1gsh29dvmo8frjbt619
* Mailing List voting thread: https://lists.apache.org/thread/1s7dhrb27qfdx1gsh29dvmo8frjbt619
1 change: 1 addition & 0 deletions home/docs/advanced/extend-http.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ metrics:
method: GET
headers:
apiVersion: v1
# query params,support time expression
params:
param1: param1
param2: param2
Expand Down
2 changes: 2 additions & 0 deletions home/docs/help/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ keywords: [open source monitoring tool, monitoring http api]
| Enable HTTPS | Whether to access the website through HTTPS. Note⚠️When HTTPS is enabled, the default corresponding port needs to be changed to 443 |
| Username | User name used for interface Basic authentication or Digest authentication |
| Password | Password used for interface Basic authentication or Digest authentication |
| Headers | HTTP request headers |
| Params | HTTP query params, support [time expression](time_expression) |
| Content-Type | Set the resource type when carrying the BODY request body data request |
| Request BODY | Set the carry BODY request body data, which is valid when PUT or POST request method is used |
| Collection interval | Interval time of monitor periodic data collection, unit: second, and the minimum interval that can be set is 30 seconds |
Expand Down
76 changes: 76 additions & 0 deletions home/docs/help/pulsar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
id: pulsar
title: Monitoring Pulsar Monitoring
sidebar_label: Apache Pulsar
keywords: [open-source monitoring system, open-source database monitoring, HbaseMaster monitoring]
---
> Collecting and monitoring general performance metrics of Pulsar
**Protocol Used: HTTP**

## Configuration Parameters


| Parameter Name | Description |
| ------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| Target Host | The monitored endpoint's IPV4, IPV6, or domain name. Note⚠️: Do not include the protocol header (e.g., https://, http://). |
| Port | The webServicePort value of Pulsar, default is 8080. |
| Task Name | The name identifying this monitoring task, must be unique. |
| Query Timeout | Set the connection timeout in milliseconds, default is 3000 milliseconds. |
| Monitoring Interval | Interval time for periodic data collection, in seconds, minimum interval is 30 seconds. |
| Binding Tags | Used for categorizing monitoring resources. |
| Description/Remarks | Additional notes and descriptions for this monitoring task. Users can add more information here. |

### Collected Metrics

#### Metric Set: Version Information


| Metric Name | Unit | Description |
| ------------ | ---- | ------------------- |
| Version Info | NONE | Version Information |

#### Metric Set: process_start_time_seconds


| Metric Name | Unit | Description |
| ------------------ | ---- | ------------------ |
| Process Start Time | NONE | Process Start Time |

#### Metric Set: process_open_fds


| Metric Name | Unit | Description |
| --------------------- | ---- | ------------------------------- |
| Open File Descriptors | NONE | Number of Open File Descriptors |

#### Metric Set: process_max_fds


| Metric Name | Unit | Description |
| -------------------- | ---- | ---------------------------------- |
| Max File Descriptors | NONE | Maximum Number of File Descriptors |

#### Metric Set: jvm_memory_pool_allocated_bytes

Number of bytes of memory allocated in a specific memory pool in the Java Virtual Machine (JVM). In Pulsar, this typically refers to the amount of memory allocated for various purposes in the JVM (such as heap memory, non-heap memory, etc.).

#### Metric Set: jvm_memory_pool_used_bytes

Unlike allocated_bytes, this metric shows the actual used memory, not just the allocated memory.

#### Metric Set: jvm_memory_pool_committed_bytes

Number of bytes of memory committed in a specific memory pool in the JVM. In the JVM, committed memory is the amount of memory guaranteed to be available for the application to use. This portion of memory is typically locked by the operating system to reduce swapping or garbage collection.

#### Metric Set: jvm_memory_pool_max_bytes

Maximum number of bytes of memory that can be allocated in a specific memory pool in the JVM. This is the upper limit on memory usage for that memory pool and helps in setting the memory usage cap.

#### Metric Set: pulsar_broker_publish_latency

Message publishing latency on the broker side.

#### Metric Set: pulsar_metadata_store_ops_latency_ms

Latency of metadata store operations on the broker side.
Loading

0 comments on commit 8b95a89

Please sign in to comment.