Skip to content

Commit 3560bb2

Browse files
authored
feat: add unified observability endpoint with SLS/DB routing (higress-group#193)
- Add ObservabilityConfig to switch log source via OBSERVABILITY_LOG_SOURCE env var (default: SLS) - Add ObservabilityController as unified entry at /observability/statistics - Route requests to SlsController or DBCollectorController based on config - Update frontend slsApi.ts to call /observability/statistics - Log data source details on startup (SLS config or DB URL + table)
1 parent 39b3a25 commit 3560bb2

File tree

4 files changed

+128
-1
lines changed

4 files changed

+128
-1
lines changed

himarket-bootstrap/src/main/resources/application.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ acp:
8585
supports-mcp: true
8686
supports-skill: true
8787

88+
observability:
89+
log-source: ${OBSERVABILITY_LOG_SOURCE:SLS}
90+
8891
sls:
8992
# SLS服务端点(必填)
9093
# cn-chengdu.log.aliyuncs.com
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with 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,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.alibaba.himarket.config;
21+
22+
import jakarta.annotation.PostConstruct;
23+
import javax.sql.DataSource;
24+
import lombok.Data;
25+
import lombok.RequiredArgsConstructor;
26+
import lombok.extern.slf4j.Slf4j;
27+
import org.springframework.boot.context.properties.ConfigurationProperties;
28+
import org.springframework.context.annotation.Configuration;
29+
30+
@Data
31+
@Slf4j
32+
@Configuration
33+
@RequiredArgsConstructor
34+
@ConfigurationProperties(prefix = "observability")
35+
public class ObservabilityConfig {
36+
37+
private final SlsConfig slsConfig;
38+
private final DataSource dataSource;
39+
40+
@PostConstruct
41+
public void init() {
42+
log.info("Observability log source: {}", logSource);
43+
if (logSource == LogSource.SLS) {
44+
log.info(
45+
"SLS endpoint: {}, project: {}, logstore: {}, authType: {}",
46+
slsConfig.getEndpoint(),
47+
slsConfig.getDefaultProject(),
48+
slsConfig.getDefaultLogstore(),
49+
slsConfig.getAuthType());
50+
if (!slsConfig.isConfigured()) {
51+
log.warn("SLS endpoint is not configured! Queries will return empty results.");
52+
}
53+
} else {
54+
try {
55+
String url = dataSource.getConnection().getMetaData().getURL();
56+
log.info("DB datasource URL: {}, table: access_logs", url);
57+
} catch (Exception e) {
58+
log.info("DB datasource: unable to retrieve URL ({})", e.getMessage());
59+
}
60+
}
61+
}
62+
63+
public enum LogSource {
64+
DB,
65+
SLS
66+
}
67+
68+
/**
69+
* 日志数据源:SLS(默认,查询阿里云日志服务)或 DB(查询本地 access_logs 表)
70+
*/
71+
private LogSource logSource = LogSource.SLS;
72+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with 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,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.alibaba.himarket.controller;
21+
22+
import com.alibaba.himarket.config.ObservabilityConfig;
23+
import com.alibaba.himarket.dto.params.sls.GenericSlsQueryRequest;
24+
import com.alibaba.himarket.dto.params.sls.ScenarioQueryResponse;
25+
import io.swagger.v3.oas.annotations.Operation;
26+
import io.swagger.v3.oas.annotations.tags.Tag;
27+
import lombok.RequiredArgsConstructor;
28+
import org.springframework.validation.annotation.Validated;
29+
import org.springframework.web.bind.annotation.PostMapping;
30+
import org.springframework.web.bind.annotation.RequestBody;
31+
import org.springframework.web.bind.annotation.RequestMapping;
32+
import org.springframework.web.bind.annotation.RestController;
33+
34+
@Tag(name = "可观测统一入口", description = "根据配置自动路由到 SLS 或 DB 数据源")
35+
@RestController
36+
@RequestMapping("/observability")
37+
@RequiredArgsConstructor
38+
public class ObservabilityController {
39+
40+
private final ObservabilityConfig observabilityConfig;
41+
private final SlsController slsController;
42+
private final DBCollectorController dbCollectorController;
43+
44+
@PostMapping("/statistics")
45+
@Operation(summary = "统一日志指标聚合查询(自动路由 SLS / DB)")
46+
public ScenarioQueryResponse query(@RequestBody @Validated GenericSlsQueryRequest request) {
47+
if (observabilityConfig.getLogSource() == ObservabilityConfig.LogSource.SLS) {
48+
return slsController.slsScenarioQuery(request);
49+
}
50+
return dbCollectorController.DBCollectorScenarioQuery(request);
51+
}
52+
}

himarket-web/himarket-admin/src/lib/slsApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export async function querySlsStatistics(
2626
): Promise<ScenarioQueryResponse> {
2727
const { data } = await api.post<
2828
ScenarioQueryResponse | ApiResponse<ScenarioQueryResponse>
29-
>("/db-collector/statistics", request);
29+
>("/observability/statistics", request);
3030

3131
// 解包后端响应,从 { code, message, data } 中提取 data
3232
if (isWrappedResponse<ScenarioQueryResponse>(data)) {

0 commit comments

Comments
 (0)