Skip to content

Commit 433f09a

Browse files
authored
[feature]A preliminary logging module implementation (apache#3218)
1 parent 706baff commit 433f09a

File tree

9 files changed

+314
-6
lines changed

9 files changed

+314
-6
lines changed

hertzbeat-common/src/main/java/org/apache/hertzbeat/common/constants/ConfigConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ interface FunctionModuleConstants {
6565
String INFO = "info";
6666

6767
String GRAFANA = "grafana";
68+
69+
String LOG = "log";
6870
}
6971

7072
}

hertzbeat-log/pom.xml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to You under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<parent>
22+
<groupId>org.apache.hertzbeat</groupId>
23+
<artifactId>hertzbeat</artifactId>
24+
<version>2.0-SNAPSHOT</version>
25+
</parent>
26+
27+
<artifactId>hertzbeat-log</artifactId>
28+
<name>${project.artifactId}</name>
29+
<properties>
30+
<maven-jar-plugin.version>3.2.0</maven-jar-plugin.version>
31+
<maven-assembly-plugin.version>3.3.0</maven-assembly-plugin.version>
32+
</properties>
33+
34+
<dependencies>
35+
<!-- hertzbeat common -->
36+
<dependency>
37+
<groupId>org.apache.hertzbeat</groupId>
38+
<artifactId>hertzbeat-common</artifactId>
39+
</dependency>
40+
<!-- hertzbeat warehouse -->
41+
<dependency>
42+
<groupId>org.apache.hertzbeat</groupId>
43+
<artifactId>hertzbeat-warehouse</artifactId>
44+
</dependency>
45+
<!-- OpenTelemetry -->
46+
<dependency>
47+
<groupId>io.opentelemetry</groupId>
48+
<artifactId>opentelemetry-api</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>io.opentelemetry</groupId>
52+
<artifactId>opentelemetry-sdk</artifactId>
53+
</dependency>
54+
<dependency>
55+
<groupId>io.opentelemetry</groupId>
56+
<artifactId>opentelemetry-sdk-logs</artifactId>
57+
</dependency>
58+
<dependency>
59+
<groupId>io.opentelemetry</groupId>
60+
<artifactId>opentelemetry-exporter-otlp</artifactId>
61+
</dependency>
62+
<dependency>
63+
<groupId>io.opentelemetry.instrumentation</groupId>
64+
<artifactId>opentelemetry-logback-appender-1.0</artifactId>
65+
</dependency>
66+
</dependencies>
67+
</project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.hertzbeat.log.config;
19+
20+
import org.apache.hertzbeat.common.constants.ConfigConstants;
21+
import org.apache.hertzbeat.common.constants.SignConstants;
22+
import org.apache.hertzbeat.warehouse.store.history.greptime.GreptimeProperties;
23+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
24+
import org.springframework.context.annotation.ComponentScan;
25+
26+
/**
27+
* Log auto configuration.
28+
*/
29+
@ComponentScan(basePackages = ConfigConstants.PkgConstant.PKG
30+
+ SignConstants.DOT
31+
+ ConfigConstants.FunctionModuleConstants.LOG
32+
)
33+
@EnableConfigurationProperties(GreptimeProperties.class)
34+
public class LogAutoConfiguration {
35+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
19+
package org.apache.hertzbeat.log.config;
20+
21+
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME;
22+
import io.opentelemetry.api.OpenTelemetry;
23+
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter;
24+
import io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender;
25+
import io.opentelemetry.sdk.OpenTelemetrySdk;
26+
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
27+
import io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor;
28+
import io.opentelemetry.sdk.resources.Resource;
29+
30+
import jakarta.annotation.PostConstruct;
31+
import java.util.Base64;
32+
import java.util.HashMap;
33+
import java.util.Map;
34+
import java.util.concurrent.TimeUnit;
35+
36+
import lombok.extern.slf4j.Slf4j;
37+
import org.apache.commons.lang3.StringUtils;
38+
import org.apache.hertzbeat.warehouse.store.history.greptime.GreptimeProperties;
39+
import org.springframework.beans.factory.annotation.Autowired;
40+
import org.springframework.context.annotation.Configuration;
41+
42+
/**
43+
* OpenTelemetryConfig is responsible for initializing OpenTelemetry with the specified service name and GrepTimeDB endpoint.
44+
* It ensures that the initialization is done in a thread-safe manner and includes authentication for GrepTimeDB.
45+
*/
46+
@Configuration
47+
@Slf4j
48+
public class OpenTelemetryConfig {
49+
50+
@Autowired
51+
private GreptimeProperties greptimeProperties;
52+
53+
/**
54+
* Initializes OpenTelemetry with the given service name and GrepTimeDB endpoint.
55+
* Includes authentication if configured in GreptimeProperties.
56+
*/
57+
@PostConstruct
58+
public void initializeOpenTelemetry() {
59+
if (greptimeProperties == null || !greptimeProperties.enabled()) {
60+
log.info("GrepTimeDB logging is disabled, skipping OpenTelemetry configuration.");
61+
return;
62+
}
63+
64+
try {
65+
Resource resource = Resource.getDefault()
66+
.merge(Resource.builder()
67+
.put(SERVICE_NAME, "HertzBeat")
68+
.build());
69+
70+
Map<String, String> headers = new HashMap<>();
71+
72+
headers.put("X-Greptime-DB-Name", "public");
73+
headers.put("X-Greptime-Log-Table-Name", "hzb_log");
74+
75+
addAuthenticationHeaders(headers);
76+
77+
OtlpHttpLogRecordExporter logExporter = OtlpHttpLogRecordExporter.builder()
78+
.setEndpoint(greptimeProperties.httpEndpoint() + "/v1/otlp/v1/logs")
79+
.setHeaders(()-> headers)
80+
.setTimeout(10, TimeUnit.SECONDS)
81+
.build();
82+
83+
SdkLoggerProvider loggerProvider = SdkLoggerProvider.builder()
84+
.setResource(resource)
85+
.addLogRecordProcessor(
86+
BatchLogRecordProcessor.builder(logExporter)
87+
.setScheduleDelay(1000, TimeUnit.MILLISECONDS)
88+
.setMaxExportBatchSize(512)
89+
.build())
90+
.build();
91+
92+
OpenTelemetry openTelemetry = OpenTelemetrySdk.builder()
93+
.setLoggerProvider(loggerProvider)
94+
.build();
95+
96+
OpenTelemetryAppender.install(openTelemetry);
97+
log.info("OpenTelemetry successfully configured with GrepTimeDB exporter.");
98+
} catch (Exception e) {
99+
log.error("Failed to initialize OpenTelemetry with GrepTimeDB", e);
100+
}
101+
}
102+
103+
/**
104+
* Adds authentication headers to the provided map if username and password are configured.
105+
*
106+
* @param headers the map to which authentication headers will be added
107+
*/
108+
private void addAuthenticationHeaders(Map<String, String> headers) {
109+
if (StringUtils.isNotBlank(greptimeProperties.username())
110+
&& StringUtils.isNotBlank(greptimeProperties.password())) {
111+
String credentials = greptimeProperties.username() + ":" + greptimeProperties.password();
112+
String encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes());
113+
headers.put("Authorization", "Basic " + encodedCredentials);
114+
}
115+
116+
}
117+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one or more
2+
# contributor license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright ownership.
4+
# The ASF licenses this file to You under the Apache License, Version 2.0
5+
# (the "License"); you may not use this file except in compliance with
6+
# the License. You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
17+
org.apache.hertzbeat.log.config.LogAutoConfiguration

hertzbeat-manager/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@
8989
<groupId>org.apache.hertzbeat</groupId>
9090
<artifactId>hertzbeat-grafana</artifactId>
9191
</dependency>
92+
<!-- log -->
93+
<dependency>
94+
<groupId>org.apache.hertzbeat</groupId>
95+
<artifactId>hertzbeat-log</artifactId>
96+
</dependency>
9297
<!-- spring -->
9398
<dependency>
9499
<groupId>org.springframework.boot</groupId>

hertzbeat-manager/src/main/resources/logback-spring.xml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@
7676
</filter>
7777
</appender>
7878

79+
<!-- OpenTelemetry Appender for shipping logs to GrepTimeDB -->
80+
<appender name="OpenTelemetryAppender" class="io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender">
81+
<!-- Capture source code information (class name, method name, line number) -->
82+
<captureCodeAttributes>true</captureCodeAttributes>
83+
84+
<!-- Capture MDC context values -->
85+
<captureMdcAttributes>
86+
<pattern>.*</pattern>
87+
</captureMdcAttributes>
88+
89+
<!-- Capture experimental attributes like exception details -->
90+
<captureExperimentalAttributes>true</captureExperimentalAttributes>
91+
92+
<!-- Capture marker attributes -->
93+
<captureMarkerAttribute>true</captureMarkerAttribute>
94+
95+
<!-- Capture logger context attributes -->
96+
<captureLoggerContext>true</captureLoggerContext>
97+
</appender>
98+
7999
<!-- Settings for this logger: for example, all output logs under the org.springframework package must be at level info or above to be output! -->
80100
<!-- This can avoid outputting many common debug information of the spring framework! -->
81101
<logger name="org.springframework" level="info"/>
@@ -90,12 +110,14 @@
90110
<logger name="org.mongodb" level="warn"/>
91111
<logger name="io.greptime" level="warn"/>
92112
<logger name="org.apache.kafka" level="warn"/>
113+
<logger name="io.opentelemetry" level="info"/>
93114

94115
<!-- Production environment configuration -->
95116
<springProfile name="prod">
96117
<root level="INFO">
97118
<appender-ref ref="SystemOutFileAppender"/>
98119
<appender-ref ref="ErrOutFileAppender"/>
120+
<appender-ref ref="OpenTelemetryAppender"/>
99121
</root>
100122
<!-- North log -->
101123
<Logger name="com.obs.services.AbstractClient" level="OFF"
@@ -118,6 +140,7 @@
118140
<appender-ref ref="ConsoleAppender"/>
119141
<appender-ref ref="SystemOutFileAppender"/>
120142
<appender-ref ref="ErrOutFileAppender"/>
143+
<appender-ref ref="OpenTelemetryAppender"/>
121144
</root>
122145
</springProfile>
123146

@@ -127,6 +150,7 @@
127150
<appender-ref ref="ConsoleAppender"/>
128151
<appender-ref ref="SystemOutFileAppender"/>
129152
<appender-ref ref="ErrOutFileAppender"/>
153+
<appender-ref ref="OpenTelemetryAppender"/>
130154
</root>
131155
</springProfile>
132156

@@ -136,7 +160,8 @@
136160
<appender-ref ref="ConsoleAppender"/>
137161
<appender-ref ref="SystemOutFileAppender"/>
138162
<appender-ref ref="ErrOutFileAppender"/>
163+
<appender-ref ref="OpenTelemetryAppender"/>
139164
</root>
140165
</springProfile>
141166

142-
</configuration>
167+
</configuration>

material/licenses/LICENSE

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,14 @@ The text of each license is the standard Apache 2.0 license.
431431
https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml/4.1.1 Apache-2.0
432432
https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas/4.1.1 Apache-2.0
433433
https://mvnrepository.com/artifact/xml-apis/xml-apis/1.4.01 Apache-2.0
434-
https://mvnrepository.com/artifact/org.webjars/swagger-ui/5.10.3
435-
https://mvnrepository.com/artifact/com.google.flatbuffers/flatbuffers-java/1.12.0
436-
https://mvnrepository.com/artifact/com.vesoft/client/3.6.0
437-
434+
https://mvnrepository.com/artifact/org.webjars/swagger-ui/5.10.3 Apache-2.0
435+
https://mvnrepository.com/artifact/com.google.flatbuffers/flatbuffers-java/1.12.0 Apache-2.0
436+
https://mvnrepository.com/artifact/com.vesoft/client/3.6.0 Apache-2.0
437+
https://mvnrepository.com/artifact/io.opentelemetry/opentelemetry-api/1.49.0 Apache-2.0
438+
https://mvnrepository.com/artifact/io.opentelemetry/opentelemetry-sdk/1.49.0 Apache-2.0
439+
https://mvnrepository.com/artifact/io.opentelemetry/opentelemetry-sdk-logs/1.49.0 Apache-2.0
440+
https://mvnrepository.com/artifact/io.opentelemetry/opentelemetry-exporter-otlp/1.49.0 Apache-2.0
441+
https://mvnrepository.com/artifact/io.opentelemetry.instrumentation/opentelemetry-logback-appender-1.0 Apache-2.0
438442

439443
========================================================================
440444
BSD-2-Clause licenses

0 commit comments

Comments
 (0)