Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions himarket-bootstrap/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ acp:
supports-mcp: true
supports-skill: true

observability:
log-source: ${OBSERVABILITY_LOG_SOURCE:SLS}

sls:
# SLS服务端点(必填)
# cn-chengdu.log.aliyuncs.com
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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 com.alibaba.himarket.config;

import jakarta.annotation.PostConstruct;
import javax.sql.DataSource;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Data
@Slf4j
@Configuration
@RequiredArgsConstructor
@ConfigurationProperties(prefix = "observability")
public class ObservabilityConfig {

private final SlsConfig slsConfig;
private final DataSource dataSource;

@PostConstruct
public void init() {
log.info("Observability log source: {}", logSource);
if (logSource == LogSource.SLS) {
log.info(
"SLS endpoint: {}, project: {}, logstore: {}, authType: {}",
slsConfig.getEndpoint(),
slsConfig.getDefaultProject(),
slsConfig.getDefaultLogstore(),
slsConfig.getAuthType());
if (!slsConfig.isConfigured()) {
log.warn("SLS endpoint is not configured! Queries will return empty results.");
}
} else {
try {
String url = dataSource.getConnection().getMetaData().getURL();
log.info("DB datasource URL: {}, table: access_logs", url);
} catch (Exception e) {
log.info("DB datasource: unable to retrieve URL ({})", e.getMessage());
}
}
}

public enum LogSource {
DB,
SLS
}

/**
* 日志数据源:SLS(默认,查询阿里云日志服务)或 DB(查询本地 access_logs 表)
*/
private LogSource logSource = LogSource.SLS;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 com.alibaba.himarket.controller;

import com.alibaba.himarket.config.ObservabilityConfig;
import com.alibaba.himarket.dto.params.sls.GenericSlsQueryRequest;
import com.alibaba.himarket.dto.params.sls.ScenarioQueryResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
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.RestController;

@Tag(name = "可观测统一入口", description = "根据配置自动路由到 SLS 或 DB 数据源")
@RestController
@RequestMapping("/observability")
@RequiredArgsConstructor
public class ObservabilityController {

private final ObservabilityConfig observabilityConfig;
private final SlsController slsController;
private final DBCollectorController dbCollectorController;

@PostMapping("/statistics")
@Operation(summary = "统一日志指标聚合查询(自动路由 SLS / DB)")
public ScenarioQueryResponse query(@RequestBody @Validated GenericSlsQueryRequest request) {
if (observabilityConfig.getLogSource() == ObservabilityConfig.LogSource.SLS) {
return slsController.slsScenarioQuery(request);
}
return dbCollectorController.DBCollectorScenarioQuery(request);
}
}
2 changes: 1 addition & 1 deletion himarket-web/himarket-admin/src/lib/slsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function querySlsStatistics(
): Promise<ScenarioQueryResponse> {
const { data } = await api.post<
ScenarioQueryResponse | ApiResponse<ScenarioQueryResponse>
>("/db-collector/statistics", request);
>("/observability/statistics", request);

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