Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: 处理Esb请求时,请求结束后加上出口日志 #3378 #3383

Merged
merged 1 commit into from
Jan 16, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,50 @@
package com.tencent.bk.job.common.web.config;

import com.tencent.bk.job.common.web.filter.RepeatableReadWriteServletRequestResponseFilter;
import com.tencent.bk.job.common.web.filter.WebRepeatableReadServletRequestFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfig {

/**
* 给/esb/api/*, /service/* 用的过滤器,包装request和response
*
*/
@Bean
public FilterRegistrationBean repeatableRSRRFilterRegister() {
FilterRegistrationBean<RepeatableReadWriteServletRequestResponseFilter> registration =
new FilterRegistrationBean<>();
registration.setFilter(repeatableRRRFilter());
registration.addUrlPatterns("/esb/api/*", "/service/*", "/web/*");
registration.addUrlPatterns("/esb/api/*", "/service/*");
registration.setName("repeatableReadRequestResponseFilter");
registration.setOrder(0);
return registration;
}

/**
* 给/web/* 用的过滤器,仅包装request
*
*/
@Bean
public FilterRegistrationBean webRepeatableRRFilterRegister() {
FilterRegistrationBean<WebRepeatableReadServletRequestFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(webRepeatableReadRequestFilter());
registration.addUrlPatterns("/web/*");
registration.setName("webRepeatableReadRequestFilter");
registration.setOrder(1);
return registration;
}

@Bean(name = "repeatableReadRequestResponseFilter")
public RepeatableReadWriteServletRequestResponseFilter repeatableRRRFilter() {
return new RepeatableReadWriteServletRequestResponseFilter();
}

@Bean(name = "webRepeatableReadRequestFilter")
public WebRepeatableReadServletRequestFilter webRepeatableReadRequestFilter() {
return new WebRepeatableReadServletRequestFilter();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

package com.tencent.bk.job.common.web.filter;

import com.tencent.bk.job.common.web.utils.ServletUtil;
import com.tencent.bk.job.common.web.model.RepeatableReadHttpServletResponse;
import com.tencent.bk.job.common.web.model.RepeatableReadWriteHttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
Expand All @@ -49,35 +49,15 @@ public void init(FilterConfig filterConfig) {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
ServletRequest servletRequest = request;
ServletResponse servletResponse = response;
if (isJsonRequest(request)) {
// 仅处理 ContentType: application/json 请求
servletRequest = new RepeatableReadWriteHttpServletRequest((HttpServletRequest) request);
}
if (isJsonResponse(response)) {
// 仅处理 ContentType: application/json 响应
servletResponse = new RepeatableReadHttpServletResponse((HttpServletResponse) response);
if (!ServletUtil.isJsonRequest(request)) {
chain.doFilter(request, response);
return;
}
ServletRequest servletRequest = new RepeatableReadWriteHttpServletRequest((HttpServletRequest) request);
ServletResponse servletResponse = new RepeatableReadHttpServletResponse((HttpServletResponse) response);
chain.doFilter(servletRequest, servletResponse);
}

private boolean isJsonRequest(ServletRequest request) {
return isJsonContentType(request.getContentType());
}

private boolean isJsonResponse(ServletResponse response) {
return isJsonContentType(response.getContentType());
}

private boolean isJsonContentType(String contentType) {
if (StringUtils.isBlank(contentType)) {
return false;
}
contentType = contentType.trim().toLowerCase();
return contentType.startsWith("application/json");
}

@Override
public void destroy() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-JOB蓝鲸智云作业平台 is licensed under the MIT License.
*
* License for BK-JOB蓝鲸智云作业平台:
* --------------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.common.web.filter;

import com.tencent.bk.job.common.web.utils.ServletUtil;
import com.tencent.bk.job.common.web.model.RepeatableReadWriteHttpServletRequest;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Slf4j
public class WebRepeatableReadServletRequestFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// do nothing
}

/**
* 仅包装ServletRequest,给/web使用
*
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
ServletRequest servletRequest = request;

if (ServletUtil.isJsonRequest(request)) {
// 仅处理 ContentType: application/json 请求
servletRequest = new RepeatableReadWriteHttpServletRequest((HttpServletRequest) request);
}

chain.doFilter(servletRequest, response);
}

@Override
public void destroy() {
// do nothing
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-JOB蓝鲸智云作业平台 is licensed under the MIT License.
*
* License for BK-JOB蓝鲸智云作业平台:
* --------------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.common.web.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletRequest;


/**
* 处理servlet请求和响应的工具类
*/
public class ServletUtil {

/**
* 判断 servlet 请求的 Content-Type 是否是 application/json
* @param request 请求
* @return 请求的 Content-Type 是否是 application/json
*/
public static boolean isJsonRequest(ServletRequest request) {
return isJsonContentType(request.getContentType());
}

private static boolean isJsonContentType(String contentType) {
if (StringUtils.isBlank(contentType)) {
return false;
}
contentType = contentType.trim().toLowerCase();
return contentType.startsWith("application/json");
}
}
Loading