-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
9c78c64
commit 7b8b79b
Showing
9 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...nance/src/main/java/com/huaweicloud/governance/adapters/RequestServiceInfoLoggerUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (C) 2020-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
* Licensed 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.huaweicloud.governance.adapters; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.cloud.client.ServiceInstance; | ||
|
||
import com.huaweicloud.common.context.InvocationContext; | ||
import com.huaweicloud.governance.GovernanceConst; | ||
|
||
public class RequestServiceInfoLoggerUtil { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(RequestServiceInfoLoggerUtil.class); | ||
|
||
public static void logServiceInfo(InvocationContext context, Throwable e) { | ||
if (context != null && context.getLocalContext(GovernanceConst.CONTEXT_CURRENT_INSTANCE) != null) { | ||
ServiceInstance instance = context.getLocalContext(GovernanceConst.CONTEXT_CURRENT_INSTANCE); | ||
LOGGER.error("request >>>>>>>>>>>>>> service {}[{}:{}] failed", instance.getServiceId(), instance.getHost(), | ||
instance.getPort(), e); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...om/huaweicloud/governance/adapters/webclient/ServiceInfoLoggerExchangeFilterFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (C) 2020-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
* Licensed 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.huaweicloud.governance.adapters.webclient; | ||
|
||
import java.util.Optional; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.web.reactive.function.client.ClientRequest; | ||
import org.springframework.web.reactive.function.client.ClientResponse; | ||
import org.springframework.web.reactive.function.client.ExchangeFilterFunction; | ||
import org.springframework.web.reactive.function.client.ExchangeFunction; | ||
|
||
import com.huaweicloud.governance.adapters.loadbalancer.RetryContext; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
public class ServiceInfoLoggerExchangeFilterFunction implements ExchangeFilterFunction, Ordered { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceInfoLoggerExchangeFilterFunction.class); | ||
|
||
@Override | ||
public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) { | ||
return next.exchange(request).map(response -> logServiceInfo(response, request)); | ||
} | ||
|
||
private ClientResponse logServiceInfo(ClientResponse response, ClientRequest request) { | ||
if (response.statusCode().value() != 200) { | ||
Optional<Object> invocationContext = request.attribute(RetryContext.RETRY_SERVICE_INSTANCE); | ||
if (invocationContext.isPresent() && invocationContext.get() instanceof ServiceInstance instance) { | ||
LOGGER.error("request >>>>>>>>>>>>>> service {}[{}:{}] failed", instance.getServiceId(), instance.getHost(), | ||
instance.getPort()); | ||
} | ||
} | ||
return response; | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return Integer.MAX_VALUE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...n/java/com/huaweicloud/governance/adapters/webflux/RequestServiceInfoLoggerWebFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (C) 2020-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
* Licensed 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.huaweicloud.governance.adapters.webflux; | ||
|
||
import org.springframework.boot.web.reactive.filter.OrderedWebFilter; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import org.springframework.web.server.WebFilterChain; | ||
|
||
import com.huaweicloud.common.configration.dynamic.GovernanceProperties; | ||
import com.huaweicloud.common.context.InvocationContext; | ||
import com.huaweicloud.common.context.InvocationContextHolder; | ||
import com.huaweicloud.governance.adapters.RequestServiceInfoLoggerUtil; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
public class RequestServiceInfoLoggerWebFilter implements OrderedWebFilter { | ||
@Override | ||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { | ||
return chain.filter(exchange).onErrorResume(Exception.class, (t) -> { | ||
InvocationContext context = exchange.getAttribute(InvocationContextHolder.ATTRIBUTE_KEY); | ||
RequestServiceInfoLoggerUtil.logServiceInfo(context, t); | ||
return Mono.error(t); | ||
}); | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return GovernanceProperties.WEB_FILTER_REQUEST_LOGGER_ORDER; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
.../main/java/com/huaweicloud/governance/adapters/webmvc/RequestServiceInfoLoggerFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (C) 2020-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
* Licensed 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.huaweicloud.governance.adapters.webmvc; | ||
|
||
import java.io.IOException; | ||
|
||
import com.huaweicloud.common.context.InvocationContextHolder; | ||
import com.huaweicloud.governance.adapters.RequestServiceInfoLoggerUtil; | ||
|
||
import jakarta.servlet.Filter; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.ServletRequest; | ||
import jakarta.servlet.ServletResponse; | ||
|
||
public class RequestServiceInfoLoggerFilter implements Filter { | ||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
throws IOException, ServletException { | ||
try { | ||
if (WebMvcUtils.isNotHttpServlet(request, response)) { | ||
chain.doFilter(request, response); | ||
return; | ||
} | ||
chain.doFilter(request, response); | ||
} catch (Throwable e) { | ||
RequestServiceInfoLoggerUtil.logServiceInfo(InvocationContextHolder.getOrCreateInvocationContext(), e); | ||
throw e; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters