You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by GitBox <gi...@apache.org> on 2022/07/01 08:59:41 UTC

[GitHub] [incubator-shenyu] yu199195 commented on a diff in pull request #3636: [type:feature] add logging-aliyun-sls plugin

yu199195 commented on code in PR #3636:
URL: https://github.com/apache/incubator-shenyu/pull/3636#discussion_r911766280


##########
shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-aliyun-sls/src/main/java/org/apache/shenyu/plugin/aliyun/sls/body/LoggingServerHttpResponse.java:
##########
@@ -0,0 +1,254 @@
+/*
+ * 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 org.apache.shenyu.plugin.aliyun.sls.body;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.common.enums.RpcTypeEnum;
+import org.apache.shenyu.common.utils.DateUtils;
+import org.apache.shenyu.plugin.aliyun.sls.DefaultLogCollector;
+import org.apache.shenyu.plugin.aliyun.sls.LogCollector;
+import org.apache.shenyu.plugin.aliyun.sls.constant.LoggingConstant;
+import org.apache.shenyu.plugin.aliyun.sls.entity.ShenyuRequestLog;
+import org.apache.shenyu.plugin.aliyun.sls.utils.LogCollectConfigUtils;
+import org.apache.shenyu.plugin.aliyun.sls.utils.LogCollectUtils;
+import org.apache.shenyu.plugin.api.context.ShenyuContext;
+import org.apache.shenyu.plugin.api.result.ShenyuResult;
+import org.apache.shenyu.plugin.api.result.ShenyuResultWrap;
+import org.reactivestreams.Publisher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.io.buffer.DataBuffer;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.server.reactive.ServerHttpResponse;
+import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
+import org.springframework.web.server.ResponseStatusException;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+import reactor.util.annotation.NonNull;
+
+import java.net.URI;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+
+/**
+ * decorate ServerHttpResponse for read body.
+ */
+public class LoggingServerHttpResponse extends ServerHttpResponseDecorator {
+
+    private static final Logger LOG = LoggerFactory.getLogger(LoggingServerHttpResponse.class);
+
+    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
+
+    private final ShenyuRequestLog logInfo;
+
+    private ServerWebExchange exchange;
+
+    private final LogCollector logCollector;
+
+    /**
+     * Constructor LoggingServerHttpResponse.
+     *
+     * @param delegate     delegate ServerHttpResponse
+     * @param logInfo      access log
+     * @param logCollector LogCollector  instance
+     */
+    public LoggingServerHttpResponse(final ServerHttpResponse delegate, final ShenyuRequestLog logInfo,
+                                     final LogCollector logCollector) {
+        super(delegate);
+        this.logInfo = logInfo;
+        this.logCollector = logCollector;
+    }
+
+    /**
+     * set relevant ServerWebExchange.
+     *
+     * @param exchange ServerWebExchange
+     */
+    public void setExchange(final ServerWebExchange exchange) {
+        this.exchange = exchange;
+    }
+
+    /**
+     * write with a publisher.
+     *
+     * @param body response body
+     * @return Mono
+     */
+    @Override
+    @NonNull
+    public Mono<Void> writeWith(@NonNull final Publisher<? extends DataBuffer> body) {
+        return super.writeWith(appendResponse(body));
+    }
+
+    /**
+     * append response.
+     *
+     * @param body publisher
+     * @return wrap Flux
+     */
+    @NonNull
+    private Flux<? extends DataBuffer> appendResponse(final Publisher<? extends DataBuffer> body) {
+        ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT);
+        assert shenyuContext != null;
+        if (getStatusCode() != null) {
+            logInfo.setStatus(getStatusCode().value());
+        }
+        logInfo.setResponseHeader(LogCollectUtils.getHeaders(getHeaders()));
+        BodyWriter writer = new BodyWriter();
+        logInfo.setTraceId(getTraceId());
+        return Flux.from(body).doOnNext(buffer -> {
+            if (LogCollectUtils.isNotBinaryType(getHeaders())) {
+                writer.write(buffer.asByteBuffer().asReadOnlyBuffer());
+            }
+        }).doFinally(signal -> logResponse(shenyuContext, writer));
+    }
+
+    /**
+     * record response log.
+     *
+     * @param shenyuContext request context
+     * @param writer        bodyWriter
+     */
+    private void logResponse(final ShenyuContext shenyuContext, final BodyWriter writer) {
+        if (StringUtils.isNotBlank(getHeaders().getFirst(HttpHeaders.CONTENT_LENGTH))) {
+            String size = StringUtils.defaultIfEmpty(getHeaders().getFirst(HttpHeaders.CONTENT_LENGTH), "0");
+            logInfo.setResponseContentLength(Integer.parseInt(size));
+        } else {
+            logInfo.setResponseContentLength(writer.size());
+        }
+        logInfo.setTimeLocal(shenyuContext.getStartDateTime().format(DATE_TIME_FORMATTER));
+        logInfo.setModule(shenyuContext.getModule());
+        long costTime = DateUtils.acquireMillisBetween(shenyuContext.getStartDateTime(), LocalDateTime.now());
+        logInfo.setUpstreamResponseTime(costTime);
+        logInfo.setMethod(shenyuContext.getMethod());
+        logInfo.setRpcType(shenyuContext.getRpcType());
+        if (StringUtils.isNotBlank(shenyuContext.getRpcType())) {
+            logInfo.setUpstreamIp(getUpstreamIp());
+        }
+        int size = writer.size();
+        String body = writer.output();
+        if (size > 0 && !LogCollectConfigUtils.isResponseBodyTooLarge(size)) {
+            logInfo.setResponseBody(body);
+        }
+        // collect log
+        if (logCollector != null) {

Review Comment:
   used Objects.nonNull, and checked all code



##########
shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-aliyun-sls/pom.xml:
##########
@@ -0,0 +1,82 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <groupId>org.apache.shenyu</groupId>
+        <artifactId>shenyu-plugin-logging</artifactId>
+        <version>2.5.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+    <artifactId>shenyu-plugin-logging-aliyun-sls</artifactId>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <aliyun-log.version>0.6.70</aliyun-log.version>
+        <protobuf-java.version>3.19.2</protobuf-java.version>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.shenyu</groupId>
+            <artifactId>shenyu-plugin-base</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>com.aliyun.openservices</groupId>
+            <artifactId>aliyun-log</artifactId>

Review Comment:
   whats license? pls add in  shenyu-dist/license file



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shenyu.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org