This is an automated email from the ASF dual-hosted git repository.
zhaoqingran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
The following commit(s) were added to refs/heads/master by this push:
new 433f09a4e3 [feature]A preliminary logging module implementation (#3218)
433f09a4e3 is described below
commit 433f09a4e3de795051e33fc556f837f81c4d468e
Author: Logic <[email protected]>
AuthorDate: Thu Apr 10 15:21:48 2025 +0800
[feature]A preliminary logging module implementation (#3218)
---
.../common/constants/ConfigConstants.java | 2 +
hertzbeat-log/pom.xml | 67 ++++++++++++
.../hertzbeat/log/config/LogAutoConfiguration.java | 35 ++++++
.../hertzbeat/log/config/OpenTelemetryConfig.java | 117 +++++++++++++++++++++
.../src/main/resources/META-INF/spring.factories | 17 +++
hertzbeat-manager/pom.xml | 5 +
.../src/main/resources/logback-spring.xml | 27 ++++-
material/licenses/LICENSE | 12 ++-
pom.xml | 38 ++++++-
9 files changed, 314 insertions(+), 6 deletions(-)
diff --git
a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/constants/ConfigConstants.java
b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/constants/ConfigConstants.java
index 727d9d7d12..d71e7dc2e6 100644
---
a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/constants/ConfigConstants.java
+++
b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/constants/ConfigConstants.java
@@ -65,6 +65,8 @@ public interface ConfigConstants {
String INFO = "info";
String GRAFANA = "grafana";
+
+ String LOG = "log";
}
}
diff --git a/hertzbeat-log/pom.xml b/hertzbeat-log/pom.xml
new file mode 100644
index 0000000000..b76d5311b2
--- /dev/null
+++ b/hertzbeat-log/pom.xml
@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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.
+ -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.hertzbeat</groupId>
+ <artifactId>hertzbeat</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>hertzbeat-log</artifactId>
+ <name>${project.artifactId}</name>
+ <properties>
+ <maven-jar-plugin.version>3.2.0</maven-jar-plugin.version>
+ <maven-assembly-plugin.version>3.3.0</maven-assembly-plugin.version>
+ </properties>
+
+ <dependencies>
+ <!-- hertzbeat common -->
+ <dependency>
+ <groupId>org.apache.hertzbeat</groupId>
+ <artifactId>hertzbeat-common</artifactId>
+ </dependency>
+ <!-- hertzbeat warehouse -->
+ <dependency>
+ <groupId>org.apache.hertzbeat</groupId>
+ <artifactId>hertzbeat-warehouse</artifactId>
+ </dependency>
+ <!-- OpenTelemetry -->
+ <dependency>
+ <groupId>io.opentelemetry</groupId>
+ <artifactId>opentelemetry-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>io.opentelemetry</groupId>
+ <artifactId>opentelemetry-sdk</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>io.opentelemetry</groupId>
+ <artifactId>opentelemetry-sdk-logs</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>io.opentelemetry</groupId>
+ <artifactId>opentelemetry-exporter-otlp</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>io.opentelemetry.instrumentation</groupId>
+ <artifactId>opentelemetry-logback-appender-1.0</artifactId>
+ </dependency>
+ </dependencies>
+</project>
diff --git
a/hertzbeat-log/src/main/java/org/apache/hertzbeat/log/config/LogAutoConfiguration.java
b/hertzbeat-log/src/main/java/org/apache/hertzbeat/log/config/LogAutoConfiguration.java
new file mode 100644
index 0000000000..0d2daa7909
--- /dev/null
+++
b/hertzbeat-log/src/main/java/org/apache/hertzbeat/log/config/LogAutoConfiguration.java
@@ -0,0 +1,35 @@
+/*
+ * 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.log.config;
+
+import org.apache.hertzbeat.common.constants.ConfigConstants;
+import org.apache.hertzbeat.common.constants.SignConstants;
+import
org.apache.hertzbeat.warehouse.store.history.greptime.GreptimeProperties;
+import
org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.ComponentScan;
+
+/**
+ * Log auto configuration.
+ */
+@ComponentScan(basePackages = ConfigConstants.PkgConstant.PKG
+ + SignConstants.DOT
+ + ConfigConstants.FunctionModuleConstants.LOG
+)
+@EnableConfigurationProperties(GreptimeProperties.class)
+public class LogAutoConfiguration {
+}
diff --git
a/hertzbeat-log/src/main/java/org/apache/hertzbeat/log/config/OpenTelemetryConfig.java
b/hertzbeat-log/src/main/java/org/apache/hertzbeat/log/config/OpenTelemetryConfig.java
new file mode 100644
index 0000000000..adb71b7fd0
--- /dev/null
+++
b/hertzbeat-log/src/main/java/org/apache/hertzbeat/log/config/OpenTelemetryConfig.java
@@ -0,0 +1,117 @@
+/*
+ * 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.log.config;
+
+import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME;
+import io.opentelemetry.api.OpenTelemetry;
+import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter;
+import
io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender;
+import io.opentelemetry.sdk.OpenTelemetrySdk;
+import io.opentelemetry.sdk.logs.SdkLoggerProvider;
+import io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor;
+import io.opentelemetry.sdk.resources.Resource;
+
+import jakarta.annotation.PostConstruct;
+import java.util.Base64;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import
org.apache.hertzbeat.warehouse.store.history.greptime.GreptimeProperties;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * OpenTelemetryConfig is responsible for initializing OpenTelemetry with the
specified service name and GrepTimeDB endpoint.
+ * It ensures that the initialization is done in a thread-safe manner and
includes authentication for GrepTimeDB.
+ */
+@Configuration
+@Slf4j
+public class OpenTelemetryConfig {
+
+ @Autowired
+ private GreptimeProperties greptimeProperties;
+
+ /**
+ * Initializes OpenTelemetry with the given service name and GrepTimeDB
endpoint.
+ * Includes authentication if configured in GreptimeProperties.
+ */
+ @PostConstruct
+ public void initializeOpenTelemetry() {
+ if (greptimeProperties == null || !greptimeProperties.enabled()) {
+ log.info("GrepTimeDB logging is disabled, skipping OpenTelemetry
configuration.");
+ return;
+ }
+
+ try {
+ Resource resource = Resource.getDefault()
+ .merge(Resource.builder()
+ .put(SERVICE_NAME, "HertzBeat")
+ .build());
+
+ Map<String, String> headers = new HashMap<>();
+
+ headers.put("X-Greptime-DB-Name", "public");
+ headers.put("X-Greptime-Log-Table-Name", "hzb_log");
+
+ addAuthenticationHeaders(headers);
+
+ OtlpHttpLogRecordExporter logExporter =
OtlpHttpLogRecordExporter.builder()
+ .setEndpoint(greptimeProperties.httpEndpoint() +
"/v1/otlp/v1/logs")
+ .setHeaders(()-> headers)
+ .setTimeout(10, TimeUnit.SECONDS)
+ .build();
+
+ SdkLoggerProvider loggerProvider = SdkLoggerProvider.builder()
+ .setResource(resource)
+ .addLogRecordProcessor(
+ BatchLogRecordProcessor.builder(logExporter)
+ .setScheduleDelay(1000,
TimeUnit.MILLISECONDS)
+ .setMaxExportBatchSize(512)
+ .build())
+ .build();
+
+ OpenTelemetry openTelemetry = OpenTelemetrySdk.builder()
+ .setLoggerProvider(loggerProvider)
+ .build();
+
+ OpenTelemetryAppender.install(openTelemetry);
+ log.info("OpenTelemetry successfully configured with GrepTimeDB
exporter.");
+ } catch (Exception e) {
+ log.error("Failed to initialize OpenTelemetry with GrepTimeDB", e);
+ }
+ }
+
+ /**
+ * Adds authentication headers to the provided map if username and
password are configured.
+ *
+ * @param headers the map to which authentication headers will be added
+ */
+ private void addAuthenticationHeaders(Map<String, String> headers) {
+ if (StringUtils.isNotBlank(greptimeProperties.username())
+ && StringUtils.isNotBlank(greptimeProperties.password())) {
+ String credentials = greptimeProperties.username() + ":" +
greptimeProperties.password();
+ String encodedCredentials =
Base64.getEncoder().encodeToString(credentials.getBytes());
+ headers.put("Authorization", "Basic " + encodedCredentials);
+ }
+
+ }
+}
\ No newline at end of file
diff --git a/hertzbeat-log/src/main/resources/META-INF/spring.factories
b/hertzbeat-log/src/main/resources/META-INF/spring.factories
new file mode 100644
index 0000000000..07b683cf39
--- /dev/null
+++ b/hertzbeat-log/src/main/resources/META-INF/spring.factories
@@ -0,0 +1,17 @@
+# 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.
+
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+org.apache.hertzbeat.log.config.LogAutoConfiguration
\ No newline at end of file
diff --git a/hertzbeat-manager/pom.xml b/hertzbeat-manager/pom.xml
index 3d20aef1e9..d5351ad9cf 100644
--- a/hertzbeat-manager/pom.xml
+++ b/hertzbeat-manager/pom.xml
@@ -89,6 +89,11 @@
<groupId>org.apache.hertzbeat</groupId>
<artifactId>hertzbeat-grafana</artifactId>
</dependency>
+ <!-- log -->
+ <dependency>
+ <groupId>org.apache.hertzbeat</groupId>
+ <artifactId>hertzbeat-log</artifactId>
+ </dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
diff --git a/hertzbeat-manager/src/main/resources/logback-spring.xml
b/hertzbeat-manager/src/main/resources/logback-spring.xml
index 950db70962..787964f255 100644
--- a/hertzbeat-manager/src/main/resources/logback-spring.xml
+++ b/hertzbeat-manager/src/main/resources/logback-spring.xml
@@ -76,6 +76,26 @@
</filter>
</appender>
+ <!-- OpenTelemetry Appender for shipping logs to GrepTimeDB -->
+ <appender name="OpenTelemetryAppender"
class="io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender">
+ <!-- Capture source code information (class name, method name, line
number) -->
+ <captureCodeAttributes>true</captureCodeAttributes>
+
+ <!-- Capture MDC context values -->
+ <captureMdcAttributes>
+ <pattern>.*</pattern>
+ </captureMdcAttributes>
+
+ <!-- Capture experimental attributes like exception details -->
+ <captureExperimentalAttributes>true</captureExperimentalAttributes>
+
+ <!-- Capture marker attributes -->
+ <captureMarkerAttribute>true</captureMarkerAttribute>
+
+ <!-- Capture logger context attributes -->
+ <captureLoggerContext>true</captureLoggerContext>
+ </appender>
+
<!-- Settings for this logger: for example, all output logs under the
org.springframework package must be at level info or above to be output! -->
<!-- This can avoid outputting many common debug information of the spring
framework! -->
<logger name="org.springframework" level="info"/>
@@ -90,12 +110,14 @@
<logger name="org.mongodb" level="warn"/>
<logger name="io.greptime" level="warn"/>
<logger name="org.apache.kafka" level="warn"/>
+ <logger name="io.opentelemetry" level="info"/>
<!-- Production environment configuration -->
<springProfile name="prod">
<root level="INFO">
<appender-ref ref="SystemOutFileAppender"/>
<appender-ref ref="ErrOutFileAppender"/>
+ <appender-ref ref="OpenTelemetryAppender"/>
</root>
<!-- North log -->
<Logger name="com.obs.services.AbstractClient" level="OFF"
@@ -118,6 +140,7 @@
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="SystemOutFileAppender"/>
<appender-ref ref="ErrOutFileAppender"/>
+ <appender-ref ref="OpenTelemetryAppender"/>
</root>
</springProfile>
@@ -127,6 +150,7 @@
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="SystemOutFileAppender"/>
<appender-ref ref="ErrOutFileAppender"/>
+ <appender-ref ref="OpenTelemetryAppender"/>
</root>
</springProfile>
@@ -136,7 +160,8 @@
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="SystemOutFileAppender"/>
<appender-ref ref="ErrOutFileAppender"/>
+ <appender-ref ref="OpenTelemetryAppender"/>
</root>
</springProfile>
-</configuration>
+</configuration>
\ No newline at end of file
diff --git a/material/licenses/LICENSE b/material/licenses/LICENSE
index 6763f98a30..ed76a87333 100644
--- a/material/licenses/LICENSE
+++ b/material/licenses/LICENSE
@@ -431,10 +431,14 @@ The text of each license is the standard Apache 2.0
license.
https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml/4.1.1
Apache-2.0
https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas/4.1.1
Apache-2.0
https://mvnrepository.com/artifact/xml-apis/xml-apis/1.4.01 Apache-2.0
- https://mvnrepository.com/artifact/org.webjars/swagger-ui/5.10.3
-
https://mvnrepository.com/artifact/com.google.flatbuffers/flatbuffers-java/1.12.0
- https://mvnrepository.com/artifact/com.vesoft/client/3.6.0
-
+ https://mvnrepository.com/artifact/org.webjars/swagger-ui/5.10.3 Apache-2.0
+
https://mvnrepository.com/artifact/com.google.flatbuffers/flatbuffers-java/1.12.0
Apache-2.0
+ https://mvnrepository.com/artifact/com.vesoft/client/3.6.0 Apache-2.0
+
https://mvnrepository.com/artifact/io.opentelemetry/opentelemetry-api/1.49.0
Apache-2.0
+
https://mvnrepository.com/artifact/io.opentelemetry/opentelemetry-sdk/1.49.0
Apache-2.0
+
https://mvnrepository.com/artifact/io.opentelemetry/opentelemetry-sdk-logs/1.49.0
Apache-2.0
+
https://mvnrepository.com/artifact/io.opentelemetry/opentelemetry-exporter-otlp/1.49.0
Apache-2.0
+
https://mvnrepository.com/artifact/io.opentelemetry.instrumentation/opentelemetry-logback-appender-1.0
Apache-2.0
========================================================================
BSD-2-Clause licenses
diff --git a/pom.xml b/pom.xml
index bec9f62c07..395e0904a5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -88,6 +88,7 @@
<module>hertzbeat-push</module>
<module>hertzbeat-plugin</module>
<module>hertzbeat-grafana</module>
+ <module>hertzbeat-log</module>
<module>hertzbeat-e2e</module>
<module>hertzbeat-base</module>
</modules>
@@ -173,6 +174,8 @@
<arrow.version>18.1.0</arrow.version>
<snappy-java.version>1.1.10.7</snappy-java.version>
<sshd-sftp.version>2.13.1</sshd-sftp.version>
+ <opentelemetry-api.version>1.43.0</opentelemetry-api.version>
+
<opentelemetry-logback.version>2.14.0-alpha</opentelemetry-logback.version>
</properties>
<dependencyManagement>
@@ -231,6 +234,12 @@
<artifactId>hertzbeat-grafana</artifactId>
<version>${hertzbeat.version}</version>
</dependency>
+ <!-- log -->
+ <dependency>
+ <groupId>org.apache.hertzbeat</groupId>
+ <artifactId>hertzbeat-log</artifactId>
+ <version>${hertzbeat.version}</version>
+ </dependency>
<!-- collector-basic -->
<dependency>
<groupId>org.apache.hertzbeat</groupId>
@@ -241,7 +250,6 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
- <version>${spring-boot-dependencies.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
@@ -467,6 +475,34 @@
<artifactId>sshd-sftp</artifactId>
<version>${sshd-sftp.version}</version>
</dependency>
+ <!-- OpenTelemetry -->
+ <dependency>
+ <groupId>io.opentelemetry</groupId>
+ <artifactId>opentelemetry-api</artifactId>
+ <version>${opentelemetry-api.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>io.opentelemetry</groupId>
+ <artifactId>opentelemetry-sdk</artifactId>
+ <version>${opentelemetry-api.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>io.opentelemetry</groupId>
+ <artifactId>opentelemetry-sdk-logs</artifactId>
+ <version>${opentelemetry-api.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>io.opentelemetry</groupId>
+ <artifactId>opentelemetry-exporter-otlp</artifactId>
+ <version>${opentelemetry-api.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>io.opentelemetry.instrumentation</groupId>
+ <artifactId>opentelemetry-logback-appender-1.0</artifactId>
+ <version>${opentelemetry-logback.version}</version>
+ </dependency>
</dependencies>
</dependencyManagement>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]